diff --git a/src/Sema.zig b/src/Sema.zig index 431508292988b782c03b2d12beca356b829374ac..d5ca3362eff259d909c3cbd8236923ceab9291c6 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -9293,7 +9293,7 @@ fn intCast( break :ok all_in_range; } else ok: { const zero_inst = Air.internedToRef((try pt.intValue(operand_ty, 0)).toIntern()); - const is_in_range = try block.addBinOp(.cmp_lte, operand, zero_inst); + const is_in_range = try block.addBinOp(.cmp_eq, operand, zero_inst); break :ok is_in_range; }; try sema.addSafetyCheck(block, src, ok, .integer_out_of_bounds); diff --git a/test/cases/safety/@intCast negative to u0.zig b/test/cases/safety/@intCast negative to u0.zig new file mode 100644 index 0000000000000000000000000000000000000000..a445f755b7b8edb1e2a8f85375d92be95a8875aa --- /dev/null +++ b/test/cases/safety/@intCast negative to u0.zig @@ -0,0 +1,22 @@ +const std = @import("std"); + +pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn { + _ = stack_trace; + if (std.mem.eql(u8, message, "integer does not fit in destination type")) { + std.process.exit(0); + } + std.process.exit(1); +} + +pub fn main() !void { + bar(1, -1); + return error.TestFailed; +} + +fn bar(one: u1, not_zero: i32) void { + const x = one << @intCast(not_zero); + _ = x; +} +// run +// backend=selfhosted,llvm +// target=x86_64-linux,aarch64-linux diff --git a/test/cases/safety/@intCast positive to u0.zig b/test/cases/safety/@intCast positive to u0.zig new file mode 100644 index 0000000000000000000000000000000000000000..180ee78512fccc28ba1b33ef2b78e0570f0044cb --- /dev/null +++ b/test/cases/safety/@intCast positive to u0.zig @@ -0,0 +1,22 @@ +const std = @import("std"); + +pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn { + _ = stack_trace; + if (std.mem.eql(u8, message, "integer does not fit in destination type")) { + std.process.exit(0); + } + std.process.exit(1); +} + +pub fn main() !void { + bar(1, 1); + return error.TestFailed; +} + +fn bar(one: u1, not_zero: i32) void { + const x = one << @intCast(not_zero); + _ = x; +} +// run +// backend=selfhosted,llvm +// target=x86_64-linux,aarch64-linux diff --git a/test/cases/safety/@intCast to u0.zig b/test/cases/safety/@intCast to u0.zig deleted file mode 100644 index 180ee78512fccc28ba1b33ef2b78e0570f0044cb..0000000000000000000000000000000000000000 --- a/test/cases/safety/@intCast to u0.zig +++ /dev/null @@ -1,22 +0,0 @@ -const std = @import("std"); - -pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn { - _ = stack_trace; - if (std.mem.eql(u8, message, "integer does not fit in destination type")) { - std.process.exit(0); - } - std.process.exit(1); -} - -pub fn main() !void { - bar(1, 1); - return error.TestFailed; -} - -fn bar(one: u1, not_zero: i32) void { - const x = one << @intCast(not_zero); - _ = x; -} -// run -// backend=selfhosted,llvm -// target=x86_64-linux,aarch64-linux