| author | |
| committer | |
| log | 805e51389ba89ad1460efafcf2e0a373008d72bc |
| tree | d2112d473cd0510c9576e6f244dba754536893fe |
| parent | 5d3be4fbe8372e357a25a7e24e4825425de086fe |
4 files changed, 45 insertions(+), 23 deletions(-)
src/Sema.zig+1-1| ... | ... | @@ -9293,7 +9293,7 @@ fn intCast( |
| 9293 | 9293 | break :ok all_in_range; |
| 9294 | 9294 | } else ok: { |
| 9295 | 9295 | const zero_inst = Air.internedToRef((try pt.intValue(operand_ty, 0)).toIntern()); |
| 9296 | const is_in_range = try block.addBinOp(.cmp_lte, operand, zero_inst); | |
| 9296 | const is_in_range = try block.addBinOp(.cmp_eq, operand, zero_inst); | |
| 9297 | 9297 | break :ok is_in_range; |
| 9298 | 9298 | }; |
| 9299 | 9299 | try sema.addSafetyCheck(block, src, ok, .integer_out_of_bounds); |
test/cases/safety/@intCast negative to u0.zig	 created+22| ... | ... | @@ -0,0 +1,22 @@ |
| 1 | const std = @import("std"); | |
| 2 | ||
| 3 | pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn { | |
| 4 | _ = stack_trace; | |
| 5 | if (std.mem.eql(u8, message, "integer does not fit in destination type")) { | |
| 6 | std.process.exit(0); | |
| 7 | } | |
| 8 | std.process.exit(1); | |
| 9 | } | |
| 10 | ||
| 11 | pub fn main() !void { | |
| 12 | bar(1, -1); | |
| 13 | return error.TestFailed; | |
| 14 | } | |
| 15 | ||
| 16 | fn bar(one: u1, not_zero: i32) void { | |
| 17 | const x = one << @intCast(not_zero); | |
| 18 | _ = x; | |
| 19 | } | |
| 20 | // run | |
| 21 | // backend=selfhosted,llvm | |
| 22 | // target=x86_64-linux,aarch64-linux |
test/cases/safety/@intCast positive to u0.zig	 created+22| ... | ... | @@ -0,0 +1,22 @@ |
| 1 | const std = @import("std"); | |
| 2 | ||
| 3 | pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn { | |
| 4 | _ = stack_trace; | |
| 5 | if (std.mem.eql(u8, message, "integer does not fit in destination type")) { | |
| 6 | std.process.exit(0); | |
| 7 | } | |
| 8 | std.process.exit(1); | |
| 9 | } | |
| 10 | ||
| 11 | pub fn main() !void { | |
| 12 | bar(1, 1); | |
| 13 | return error.TestFailed; | |
| 14 | } | |
| 15 | ||
| 16 | fn bar(one: u1, not_zero: i32) void { | |
| 17 | const x = one << @intCast(not_zero); | |
| 18 | _ = x; | |
| 19 | } | |
| 20 | // run | |
| 21 | // backend=selfhosted,llvm | |
| 22 | // target=x86_64-linux,aarch64-linux |
test/cases/safety/@intCast to u0.zig	 deleted-22| ... | ... | @@ -1,22 +0,0 @@ |
| 1 | const std = @import("std"); | |
| 2 | ||
| 3 | pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn { | |
| 4 | _ = stack_trace; | |
| 5 | if (std.mem.eql(u8, message, "integer does not fit in destination type")) { | |
| 6 | std.process.exit(0); | |
| 7 | } | |
| 8 | std.process.exit(1); | |
| 9 | } | |
| 10 | ||
| 11 | pub fn main() !void { | |
| 12 | bar(1, 1); | |
| 13 | return error.TestFailed; | |
| 14 | } | |
| 15 | ||
| 16 | fn bar(one: u1, not_zero: i32) void { | |
| 17 | const x = one << @intCast(not_zero); | |
| 18 | _ = x; | |
| 19 | } | |
| 20 | // run | |
| 21 | // backend=selfhosted,llvm | |
| 22 | // target=x86_64-linux,aarch64-linux |