authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-08 19:09:37-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-08 19:09:37-07:00
log803178353976fe4ef343aef25dfa6d8e418ef88f
tree22b79769fc5e0038332d3f2c39333311894cc7e2
parent38b2d6209239f0dad7cb38e656d9d38506f126ca

stage1: fix regression of shift by negative value error

The previous commit (38b2d6209239f0dad7cb38e656d9d38506f126ca) regressed the compile error test case for when doing saturating shift left of a comptime-known negative RHS. This commit additionally fixes the error for regular shifts in addition to saturating shifts.

2 files changed, 27 insertions(+), 11 deletions(-)

src/stage1/ir.cpp+24-8
...@@ -10121,6 +10121,30 @@ static Stage1AirInst *ir_analyze_bit_shift(IrAnalyze *ira, Stage1ZirInstBinOp *b...@@ -10121,6 +10121,30 @@ static Stage1AirInst *ir_analyze_bit_shift(IrAnalyze *ira, Stage1ZirInstBinOp *b
10121 if (op2_val == nullptr)10121 if (op2_val == nullptr)
10122 return ira->codegen->invalid_inst_gen;10122 return ira->codegen->invalid_inst_gen;
1012310123
10124 if (op2_val->type->id == ZigTypeIdVector) {
10125 expand_undef_array(ira->codegen, op2_val);
10126 size_t len = op2_val->type->data.vector.len;
10127 for (size_t i = 0; i < len; i += 1) {
10128 ZigValue *scalar_val = &op2_val->data.x_array.data.s_none.elements[i];
10129 if (scalar_val->data.x_bigint.is_negative) {
10130 Buf *val_buf = buf_alloc();
10131 bigint_append_buf(val_buf, &scalar_val->data.x_bigint, 10);
10132 ir_add_error(ira, casted_op2,
10133 buf_sprintf("shift by negative value %s at vector index %zu",
10134 buf_ptr(val_buf), i));
10135 return ira->codegen->invalid_inst_gen;
10136 }
10137 }
10138 } else {
10139 if (op2_val->data.x_bigint.is_negative) {
10140 Buf *val_buf = buf_alloc();
10141 bigint_append_buf(val_buf, &op2_val->data.x_bigint, 10);
10142 ir_add_error(ira, casted_op2,
10143 buf_sprintf("shift by negative value %s", buf_ptr(val_buf)));
10144 return ira->codegen->invalid_inst_gen;
10145 }
10146 }
10147
10124 if (value_cmp_numeric_val_all(op2_val, CmpEQ, nullptr))10148 if (value_cmp_numeric_val_all(op2_val, CmpEQ, nullptr))
10125 return ir_analyze_cast(ira, bin_op_instruction->base.scope, bin_op_instruction->base.source_node, op1->value->type, op1);10149 return ir_analyze_cast(ira, bin_op_instruction->base.scope, bin_op_instruction->base.source_node, op1->value->type, op1);
10126 }10150 }
...@@ -10515,14 +10539,6 @@ static Stage1AirInst *ir_analyze_bin_op_math(IrAnalyze *ira, Stage1ZirInstBinOp...@@ -10515,14 +10539,6 @@ static Stage1AirInst *ir_analyze_bin_op_math(IrAnalyze *ira, Stage1ZirInstBinOp
10515 return ira->codegen->invalid_inst_gen;10539 return ira->codegen->invalid_inst_gen;
10516 }10540 }
10517 }10541 }
10518 } else if (op_id == IrBinOpShlSat) {
10519 if (op2_val->data.x_bigint.is_negative) {
10520 Buf *val_buf = buf_alloc();
10521 bigint_append_buf(val_buf, &op2_val->data.x_bigint, 10);
10522 ir_add_error(ira, casted_op2,
10523 buf_sprintf("shift by negative value %s", buf_ptr(val_buf)));
10524 return ira->codegen->invalid_inst_gen;
10525 }
10526 }10542 }
1052710543
10528 return ir_analyze_math_op(ira, instruction->base.scope, instruction->base.source_node, resolved_type, op1_val, op_id, op2_val);10544 return ir_analyze_math_op(ira, instruction->base.scope, instruction->base.source_node, resolved_type, op1_val, op_id, op2_val);
test/compile_errors.zig+3-3
...@@ -8913,7 +8913,7 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -8913,7 +8913,7 @@ pub fn addCases(ctx: *TestContext) !void {
8913 });8913 });
89148914
8915 ctx.objErrStage1("saturating arithmetic does not allow floats",8915 ctx.objErrStage1("saturating arithmetic does not allow floats",
8916 \\pub fn main() !void {8916 \\export fn a() void {
8917 \\ _ = @as(f32, 1.0) +| @as(f32, 1.0);8917 \\ _ = @as(f32, 1.0) +| @as(f32, 1.0);
8918 \\}8918 \\}
8919 , &[_][]const u8{8919 , &[_][]const u8{
...@@ -8921,7 +8921,7 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -8921,7 +8921,7 @@ pub fn addCases(ctx: *TestContext) !void {
8921 });8921 });
89228922
8923 ctx.objErrStage1("saturating shl does not allow negative rhs at comptime",8923 ctx.objErrStage1("saturating shl does not allow negative rhs at comptime",
8924 \\pub fn main() !void {8924 \\export fn a() void {
8925 \\ _ = @as(i32, 1) <<| @as(i32, -2);8925 \\ _ = @as(i32, 1) <<| @as(i32, -2);
8926 \\}8926 \\}
8927 , &[_][]const u8{8927 , &[_][]const u8{
...@@ -8929,7 +8929,7 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -8929,7 +8929,7 @@ pub fn addCases(ctx: *TestContext) !void {
8929 });8929 });
89308930
8931 ctx.objErrStage1("saturating shl assign does not allow negative rhs at comptime",8931 ctx.objErrStage1("saturating shl assign does not allow negative rhs at comptime",
8932 \\pub fn main() !void {8932 \\export fn a() void {
8933 \\ comptime {8933 \\ comptime {
8934 \\ var x = @as(i32, 1);8934 \\ var x = @as(i32, 1);
8935 \\ x <<|= @as(i32, -2);8935 \\ x <<|= @as(i32, -2);