authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-11-14 12:23:40-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-11-14 12:23:40-05:00
log3666fbd9f9465ba2ae70b178106e5d9e3cea68ca
treef4d999b0d91df263c30b2bf6dff6f6a57fe3ea9b
parent14308db923d361d857bd768ca7d6cb1f512081a1
signature Commit is signed but in an unrecognized format.

** and ++ operators force comptime on operands

closes #1707

2 files changed, 25 insertions(+), 2 deletions(-)

src/ir.cpp+7-2
...@@ -3182,8 +3182,13 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode...@@ -3182,8 +3182,13 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
3182}3182}
31833183
3184static IrInstruction *ir_gen_bin_op_id(IrBuilder *irb, Scope *scope, AstNode *node, IrBinOp op_id) {3184static IrInstruction *ir_gen_bin_op_id(IrBuilder *irb, Scope *scope, AstNode *node, IrBinOp op_id) {
3185 IrInstruction *op1 = ir_gen_node(irb, node->data.bin_op_expr.op1, scope);3185 Scope *inner_scope = scope;
3186 IrInstruction *op2 = ir_gen_node(irb, node->data.bin_op_expr.op2, scope);3186 if (op_id == IrBinOpArrayCat || op_id == IrBinOpArrayMult) {
3187 inner_scope = create_comptime_scope(irb->codegen, node, scope);
3188 }
3189
3190 IrInstruction *op1 = ir_gen_node(irb, node->data.bin_op_expr.op1, inner_scope);
3191 IrInstruction *op2 = ir_gen_node(irb, node->data.bin_op_expr.op2, inner_scope);
31873192
3188 if (op1 == irb->codegen->invalid_instruction || op2 == irb->codegen->invalid_instruction)3193 if (op1 == irb->codegen->invalid_instruction || op2 == irb->codegen->invalid_instruction)
3189 return irb->codegen->invalid_instruction;3194 return irb->codegen->invalid_instruction;
test/cases/eval.zig+18
...@@ -762,3 +762,21 @@ test "*align(1) u16 is the same as *align(1:0:2) u16" {...@@ -762,3 +762,21 @@ test "*align(1) u16 is the same as *align(1:0:2) u16" {
762 //assert(*align(:0:2) u16 == *u16);762 //assert(*align(:0:2) u16 == *u16);
763 }763 }
764}764}
765
766test "array concatenation forces comptime" {
767 var a = oneItem(3) ++ oneItem(4);
768 assert(std.mem.eql(i32, a, []i32{3, 4}));
769}
770
771test "array multiplication forces comptime" {
772 var a = oneItem(3) ** scalar(2);
773 assert(std.mem.eql(i32, a, []i32{3, 3}));
774}
775
776fn oneItem(x: i32) [1]i32 {
777 return []i32{x};
778}
779
780fn scalar(x: u32) u32 {
781 return x;
782}