** and ++ operators force comptime on operands

closes #1707
master
Andrew Kelley 2018-11-14 12:23:40 -05:00
parent 14308db923
commit 3666fbd9f9
No known key found for this signature in database
GPG Key ID: 4E7CD66038A4D47C
2 changed files with 25 additions and 2 deletions

View File

@ -3182,8 +3182,13 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
}
static IrInstruction *ir_gen_bin_op_id(IrBuilder *irb, Scope *scope, AstNode *node, IrBinOp op_id) {
IrInstruction *op1 = ir_gen_node(irb, node->data.bin_op_expr.op1, scope);
IrInstruction *op2 = ir_gen_node(irb, node->data.bin_op_expr.op2, scope);
Scope *inner_scope = scope;
if (op_id == IrBinOpArrayCat || op_id == IrBinOpArrayMult) {
inner_scope = create_comptime_scope(irb->codegen, node, scope);
}
IrInstruction *op1 = ir_gen_node(irb, node->data.bin_op_expr.op1, inner_scope);
IrInstruction *op2 = ir_gen_node(irb, node->data.bin_op_expr.op2, inner_scope);
if (op1 == irb->codegen->invalid_instruction || op2 == irb->codegen->invalid_instruction)
return irb->codegen->invalid_instruction;

View File

@ -762,3 +762,21 @@ test "*align(1) u16 is the same as *align(1:0:2) u16" {
//assert(*align(:0:2) u16 == *u16);
}
}
test "array concatenation forces comptime" {
var a = oneItem(3) ++ oneItem(4);
assert(std.mem.eql(i32, a, []i32{3, 4}));
}
test "array multiplication forces comptime" {
var a = oneItem(3) ** scalar(2);
assert(std.mem.eql(i32, a, []i32{3, 3}));
}
fn oneItem(x: i32) [1]i32 {
return []i32{x};
}
fn scalar(x: u32) u32 {
return x;
}