From 805e51389ba89ad1460efafcf2e0a373008d72bc Mon Sep 17 00:00:00 2001 From: David Rubin Date: Tue, 9 Jun 2026 18:37:12 -0700 Subject: [PATCH] Sema: catch missing @intCast case --- src/Sema.zig | 2 +- test/cases/safety/@intCast negative to u0.zig | 22 +++++++++++++++++++ ... to u0.zig => @intCast positive to u0.zig} | 0 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/cases/safety/@intCast negative to u0.zig rename test/cases/safety/{@intCast to u0.zig => @intCast positive to u0.zig} (100%) 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 to u0.zig b/test/cases/safety/@intCast positive to u0.zig similarity index 100% rename from test/cases/safety/@intCast to u0.zig rename to test/cases/safety/@intCast positive to u0.zig -- 2.54.0