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 *...@@ -181,7 +181,6 @@ ScopeFnDef *create_fndef_scope(CodeGen *g, AstNode *node, Scope *parent, ZigFn *
181}181}
182182
183Scope *create_comptime_scope(CodeGen *g, AstNode *node, Scope *parent) {183Scope *create_comptime_scope(CodeGen *g, AstNode *node, Scope *parent) {
184 assert(node->type == NodeTypeCompTime || node->type == NodeTypeSwitchExpr);
185 ScopeCompTime *scope = allocate<ScopeCompTime>(1);184 ScopeCompTime *scope = allocate<ScopeCompTime>(1);
186 init_scope(g, &scope->base, ScopeIdCompTime, node, parent);185 init_scope(g, &scope->base, ScopeIdCompTime, node, parent);
187 return &scope->base;186 return &scope->base;
src/ir.cpp+5-4
...@@ -5433,6 +5433,7 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n...@@ -5433,6 +5433,7 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n
5433 bool is_volatile = node->data.array_type.is_volatile;5433 bool is_volatile = node->data.array_type.is_volatile;
5434 AstNode *align_expr = node->data.array_type.align_expr;5434 AstNode *align_expr = node->data.array_type.align_expr;
54355435
5436 Scope *comptime_scope = create_comptime_scope(irb->codegen, node, scope);
5436 if (size_node) {5437 if (size_node) {
5437 if (is_const) {5438 if (is_const) {
5438 add_node_error(irb->codegen, node, buf_create_from_str("const qualifier invalid on array type"));5439 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...@@ -5447,11 +5448,11 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n
5447 return irb->codegen->invalid_instruction;5448 return irb->codegen->invalid_instruction;
5448 }5449 }
54495450
5450 IrInstruction *size_value = ir_gen_node(irb, size_node, scope);5451 IrInstruction *size_value = ir_gen_node(irb, size_node, comptime_scope);
5451 if (size_value == irb->codegen->invalid_instruction)5452 if (size_value == irb->codegen->invalid_instruction)
5452 return size_value;5453 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);
5455 if (child_type == irb->codegen->invalid_instruction)5456 if (child_type == irb->codegen->invalid_instruction)
5456 return child_type;5457 return child_type;
54575458
...@@ -5459,14 +5460,14 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n...@@ -5459,14 +5460,14 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n
5459 } else {5460 } else {
5460 IrInstruction *align_value;5461 IrInstruction *align_value;
5461 if (align_expr != nullptr) {5462 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);
5463 if (align_value == irb->codegen->invalid_instruction)5464 if (align_value == irb->codegen->invalid_instruction)
5464 return align_value;5465 return align_value;
5465 } else {5466 } else {
5466 align_value = nullptr;5467 align_value = nullptr;
5467 }5468 }
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);
5470 if (child_type == irb->codegen->invalid_instruction)5471 if (child_type == irb->codegen->invalid_instruction)
5471 return child_type;5472 return child_type;
54725473
test/cases/array.zig+9
...@@ -162,3 +162,12 @@ test "comptime evalutating function that takes array by value" {...@@ -162,3 +162,12 @@ test "comptime evalutating function that takes array by value" {
162 _ = comptime testArrayByValAtComptime(arr);162 _ = comptime testArrayByValAtComptime(arr);
163 _ = comptime testArrayByValAtComptime(arr);163 _ = comptime testArrayByValAtComptime(arr);
164}164}
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}