| author | |
| committer | |
| log | 16ac034a32e8c8feced3ee67f84ff14767404b76 |
| tree | f3d11a635720422f30a31b21cb735a7cb04c8c64 |
| parent | 411e9ca4ade243344f358b14151e87e2334e76a0 |
| signature | Signed by PGP key 4AEE18F83AFDEB23 |
* saturating shl - check for negative rhs at comptime
- adds expected compile_errors case for negative rhs
* add expected compile error for sat shl assign2 files changed, 27 insertions(+), 0 deletions(-)
src/stage1/ir.cpp+8| ... | @@ -10407,6 +10407,14 @@ static Stage1AirInst *ir_analyze_bin_op_math(IrAnalyze *ira, Stage1ZirInstBinOp | ... | @@ -10407,6 +10407,14 @@ static Stage1AirInst *ir_analyze_bin_op_math(IrAnalyze *ira, Stage1ZirInstBinOp |
| 10407 | return ira->codegen->invalid_inst_gen; | 10407 | return ira->codegen->invalid_inst_gen; |
| 10408 | } | 10408 | } |
| 10409 | } | 10409 | } |
| 10410 | } else if (op_id == IrBinOpShlSat) { | ||
| 10411 | if (op2_val->data.x_bigint.is_negative) { | ||
| 10412 | Buf *val_buf = buf_alloc(); | ||
| 10413 | bigint_append_buf(val_buf, &op2_val->data.x_bigint, 10); | ||
| 10414 | ir_add_error(ira, casted_op2, | ||
| 10415 | buf_sprintf("shift by negative value %s", buf_ptr(val_buf))); | ||
| 10416 | return ira->codegen->invalid_inst_gen; | ||
| 10417 | } | ||
| 10410 | } | 10418 | } |
| 10411 | 10419 | ||
| 10412 | return ir_analyze_math_op(ira, instruction->base.scope, instruction->base.source_node, resolved_type, op1_val, op_id, op2_val); | 10420 | 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+19| ... | @@ -8869,6 +8869,25 @@ pub fn addCases(ctx: *TestContext) !void { | ... | @@ -8869,6 +8869,25 @@ pub fn addCases(ctx: *TestContext) !void { |
| 8869 | "error: invalid operands to binary expression: 'f32' and 'f32'", | 8869 | "error: invalid operands to binary expression: 'f32' and 'f32'", |
| 8870 | }); | 8870 | }); |
| 8871 | 8871 | ||
| 8872 | ctx.objErrStage1("saturating shl does not allow negative rhs at comptime", | ||
| 8873 | \\pub fn main() !void { | ||
| 8874 | \\ _ = @as(i32, 1) <<| @as(i32, -2); | ||
| 8875 | \\} | ||
| 8876 | , &[_][]const u8{ | ||
| 8877 | "error: shift by negative value -2", | ||
| 8878 | }); | ||
| 8879 | |||
| 8880 | ctx.objErrStage1("saturating shl assign does not allow negative rhs at comptime", | ||
| 8881 | \\pub fn main() !void { | ||
| 8882 | \\ comptime { | ||
| 8883 | \\ var x = @as(i32, 1); | ||
| 8884 | \\ x <<|= @as(i32, -2); | ||
| 8885 | \\ } | ||
| 8886 | \\} | ||
| 8887 | , &[_][]const u8{ | ||
| 8888 | "error: shift by negative value -2", | ||
| 8889 | }); | ||
| 8890 | |||
| 8872 | ctx.objErrStage1("undeclared identifier in unanalyzed branch", | 8891 | ctx.objErrStage1("undeclared identifier in unanalyzed branch", |
| 8873 | \\export fn a() void { | 8892 | \\export fn a() void { |
| 8874 | \\ if (false) { | 8893 | \\ if (false) { |