zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 16ac034a32e8c8feced3ee67f84ff14767404b76 (tree)
parent 411e9ca4ade243344f358b14151e87e2334e76a0
Author: travisstaloch <twostepted@gmail.com>
Date:   Fri, 15 Oct 2021 10:56:27 -0700

Sat shl neg rhs (#9949)

* saturating shl - check for negative rhs at comptime

- adds expected compile_errors case for negative rhs

* add expected compile error for sat shl assign
Diffstat:
Msrc/stage1/ir.cpp | 8++++++++
Mtest/compile_errors.zig | 19+++++++++++++++++++
2 files changed, 27 insertions(+), 0 deletions(-)

diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp @@ -10407,6 +10407,14 @@ static Stage1AirInst *ir_analyze_bin_op_math(IrAnalyze *ira, Stage1ZirInstBinOp return ira->codegen->invalid_inst_gen; } } + } else if (op_id == IrBinOpShlSat) { + if (op2_val->data.x_bigint.is_negative) { + Buf *val_buf = buf_alloc(); + bigint_append_buf(val_buf, &op2_val->data.x_bigint, 10); + ir_add_error(ira, casted_op2, + buf_sprintf("shift by negative value %s", buf_ptr(val_buf))); + return ira->codegen->invalid_inst_gen; + } } return ir_analyze_math_op(ira, instruction->base.scope, instruction->base.source_node, resolved_type, op1_val, op_id, op2_val); diff --git a/test/compile_errors.zig b/test/compile_errors.zig @@ -8869,6 +8869,25 @@ pub fn addCases(ctx: *TestContext) !void { "error: invalid operands to binary expression: 'f32' and 'f32'", }); + ctx.objErrStage1("saturating shl does not allow negative rhs at comptime", + \\pub fn main() !void { + \\ _ = @as(i32, 1) <<| @as(i32, -2); + \\} + , &[_][]const u8{ + "error: shift by negative value -2", + }); + + ctx.objErrStage1("saturating shl assign does not allow negative rhs at comptime", + \\pub fn main() !void { + \\ comptime { + \\ var x = @as(i32, 1); + \\ x <<|= @as(i32, -2); + \\ } + \\} + , &[_][]const u8{ + "error: shift by negative value -2", + }); + ctx.objErrStage1("undeclared identifier in unanalyzed branch", \\export fn a() void { \\ if (false) {