authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-11-09 19:26:54-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-11-09 19:26:54-05:00
logef5d7ce46382fb2735e83958fe1bbe416624906a
tree09a6ed7ea6e0ad622f0eb44b49ad01afd7147caa
parent8e69a18d8c01aaf2bf4f0c0f8495e47cb36c284f
signaturelock-open Commit is signed but in an unrecognized format.

array type syntax implies comptime


3 files changed, 14 insertions(+), 5 deletions(-)

src/analyze.cpp-1
......@@ -181,7 +181,6 @@ ScopeFnDef *create_fndef_scope(CodeGen *g, AstNode *node, Scope *parent, ZigFn *
181181}
182182
183183Scope *create_comptime_scope(CodeGen *g, AstNode *node, Scope *parent) {
184 assert(node->type == NodeTypeCompTime || node->type == NodeTypeSwitchExpr);
185184 ScopeCompTime *scope = allocate<ScopeCompTime>(1);
186185 init_scope(g, &scope->base, ScopeIdCompTime, node, parent);
187186 return &scope->base;
src/ir.cpp+5-4
......@@ -5433,6 +5433,7 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n
54335433 bool is_volatile = node->data.array_type.is_volatile;
54345434 AstNode *align_expr = node->data.array_type.align_expr;
54355435
5436 Scope *comptime_scope = create_comptime_scope(irb->codegen, node, scope);
54365437 if (size_node) {
54375438 if (is_const) {
54385439 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
54475448 return irb->codegen->invalid_instruction;
54485449 }
54495450
5450 IrInstruction *size_value = ir_gen_node(irb, size_node, scope);
5451 IrInstruction *size_value = ir_gen_node(irb, size_node, comptime_scope);
54515452 if (size_value == irb->codegen->invalid_instruction)
54525453 return size_value;
54535454
5454 IrInstruction *child_type = ir_gen_node(irb, child_type_node, scope);
5455 IrInstruction *child_type = ir_gen_node(irb, child_type_node, comptime_scope);
54555456 if (child_type == irb->codegen->invalid_instruction)
54565457 return child_type;
54575458
......@@ -5459,14 +5460,14 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n
54595460 } else {
54605461 IrInstruction *align_value;
54615462 if (align_expr != nullptr) {
5462 align_value = ir_gen_node(irb, align_expr, scope);
5463 align_value = ir_gen_node(irb, align_expr, comptime_scope);
54635464 if (align_value == irb->codegen->invalid_instruction)
54645465 return align_value;
54655466 } else {
54665467 align_value = nullptr;
54675468 }
54685469
5469 IrInstruction *child_type = ir_gen_node(irb, child_type_node, scope);
5470 IrInstruction *child_type = ir_gen_node(irb, child_type_node, comptime_scope);
54705471 if (child_type == irb->codegen->invalid_instruction)
54715472 return child_type;
54725473
test/cases/array.zig+9
......@@ -162,3 +162,12 @@ test "comptime evalutating function that takes array by value" {
162162 _ = comptime testArrayByValAtComptime(arr);
163163 _ = comptime testArrayByValAtComptime(arr);
164164}
165
166test "implicit comptime in array type size" {
167 var arr: [plusOne(10)]bool = undefined;
168 assert(arr.len == 11);
169}
170
171fn plusOne(x: u32) u32 {
172 return x + 1;
173}