From ef5d7ce46382fb2735e83958fe1bbe416624906a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 9 Nov 2018 19:26:54 -0500 Subject: [PATCH] array type syntax implies comptime --- src/analyze.cpp | 1 - src/ir.cpp | 9 +++++---- test/cases/array.zig | 9 +++++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index 33958c0902..f872a4f6fd 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -181,7 +181,6 @@ ScopeFnDef *create_fndef_scope(CodeGen *g, AstNode *node, Scope *parent, ZigFn * } Scope *create_comptime_scope(CodeGen *g, AstNode *node, Scope *parent) { - assert(node->type == NodeTypeCompTime || node->type == NodeTypeSwitchExpr); ScopeCompTime *scope = allocate(1); init_scope(g, &scope->base, ScopeIdCompTime, node, parent); return &scope->base; diff --git a/src/ir.cpp b/src/ir.cpp index 360ff00b2c..3835e78434 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -5433,6 +5433,7 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n bool is_volatile = node->data.array_type.is_volatile; AstNode *align_expr = node->data.array_type.align_expr; + Scope *comptime_scope = create_comptime_scope(irb->codegen, node, scope); if (size_node) { if (is_const) { add_node_error(irb->codegen, node, buf_create_from_str("const qualifier invalid on array type")); @@ -5447,11 +5448,11 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n return irb->codegen->invalid_instruction; } - IrInstruction *size_value = ir_gen_node(irb, size_node, scope); + IrInstruction *size_value = ir_gen_node(irb, size_node, comptime_scope); if (size_value == irb->codegen->invalid_instruction) return size_value; - IrInstruction *child_type = ir_gen_node(irb, child_type_node, scope); + IrInstruction *child_type = ir_gen_node(irb, child_type_node, comptime_scope); if (child_type == irb->codegen->invalid_instruction) return child_type; @@ -5459,14 +5460,14 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n } else { IrInstruction *align_value; if (align_expr != nullptr) { - align_value = ir_gen_node(irb, align_expr, scope); + align_value = ir_gen_node(irb, align_expr, comptime_scope); if (align_value == irb->codegen->invalid_instruction) return align_value; } else { align_value = nullptr; } - IrInstruction *child_type = ir_gen_node(irb, child_type_node, scope); + IrInstruction *child_type = ir_gen_node(irb, child_type_node, comptime_scope); if (child_type == irb->codegen->invalid_instruction) return child_type; diff --git a/test/cases/array.zig b/test/cases/array.zig index f7000f6e9d..bc35a152bb 100644 --- a/test/cases/array.zig +++ b/test/cases/array.zig @@ -162,3 +162,12 @@ test "comptime evalutating function that takes array by value" { _ = comptime testArrayByValAtComptime(arr); _ = comptime testArrayByValAtComptime(arr); } + +test "implicit comptime in array type size" { + var arr: [plusOne(10)]bool = undefined; + assert(arr.len == 11); +} + +fn plusOne(x: u32) u32 { + return x + 1; +}