compiler: combine @intCast safety checks

`castTruncatedData` was a poorly worded error (all shrinking casts
"truncate bits", it's just that we assume those bits to be zext/sext of
the other bits!), and `negativeToUnsigned` was a pointless distinction
which forced the compiler to emit worse code (since two separate safety
checks were required for casting e.g. 'i32' to 'u16') and wasn't even
implemented correctly. This commit combines those safety panics into one
function, `integerOutOfBounds`. The name maybe isn't perfect, but that's
not hugely important; what matters is the new default message, which is
clearer than the old ones: "integer does not fit in destination type".
This commit is contained in:
mlugg
2025-06-01 07:41:24 +01:00
parent 6daa37ded9
commit c1a5caa454
20 changed files with 46 additions and 74 deletions

View File

@@ -15,8 +15,7 @@ pub const panic = struct {
pub const castToNull = simple_panic.castToNull;
pub const incorrectAlignment = simple_panic.incorrectAlignment;
pub const invalidErrorCode = simple_panic.invalidErrorCode;
pub const castTruncatedData = simple_panic.castTruncatedData;
pub const negativeToUnsigned = simple_panic.negativeToUnsigned;
pub const integerOutOfBounds = simple_panic.integerOutOfBounds;
pub const integerOverflow = simple_panic.integerOverflow;
pub const shlOverflow = simple_panic.shlOverflow;
pub const shrOverflow = simple_panic.shrOverflow;
@@ -27,8 +26,6 @@ pub const panic = struct {
pub const shiftRhsTooBig = simple_panic.shiftRhsTooBig;
pub const invalidEnumValue = simple_panic.invalidEnumValue;
pub const forLenMismatch = simple_panic.forLenMismatch;
/// Delete after next zig1.wasm update
pub const memcpyLenMismatch = copyLenMismatch;
pub const copyLenMismatch = simple_panic.copyLenMismatch;
pub const memcpyAlias = simple_panic.memcpyAlias;
pub const noreturnReturned = simple_panic.noreturnReturned;

View File

@@ -11,8 +11,7 @@ pub const panic = struct {
pub const castToNull = simple_panic.castToNull;
pub const incorrectAlignment = simple_panic.incorrectAlignment;
pub const invalidErrorCode = simple_panic.invalidErrorCode;
pub const castTruncatedData = simple_panic.castTruncatedData;
pub const negativeToUnsigned = simple_panic.negativeToUnsigned;
pub const integerOutOfBounds = simple_panic.integerOutOfBounds;
pub const integerOverflow = simple_panic.integerOverflow;
pub const shlOverflow = simple_panic.shlOverflow;
pub const shrOverflow = simple_panic.shrOverflow;
@@ -23,8 +22,6 @@ pub const panic = struct {
pub const shiftRhsTooBig = simple_panic.shiftRhsTooBig;
pub const invalidEnumValue = simple_panic.invalidEnumValue;
pub const forLenMismatch = simple_panic.forLenMismatch;
/// Delete after next zig1.wasm update
pub const memcpyLenMismatch = copyLenMismatch;
pub const copyLenMismatch = simple_panic.copyLenMismatch;
pub const memcpyAlias = simple_panic.memcpyAlias;
pub const noreturnReturned = simple_panic.noreturnReturned;

View File

@@ -2,7 +2,7 @@ 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 cast truncated bits")) {
if (std.mem.eql(u8, message, "integer does not fit in destination type")) {
std.process.exit(0);
}
std.process.exit(1);

View File

@@ -2,7 +2,7 @@ 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, "attempt to cast negative value to unsigned integer")) {
if (std.mem.eql(u8, message, "integer does not fit in destination type")) {
std.process.exit(0);
}
std.process.exit(1);

View File

@@ -2,7 +2,7 @@ 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, "attempt to cast negative value to unsigned integer")) {
if (std.mem.eql(u8, message, "integer does not fit in destination type")) {
std.process.exit(0);
}
std.process.exit(1);

View File

@@ -2,7 +2,7 @@ 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, "attempt to cast negative value to unsigned integer")) {
if (std.mem.eql(u8, message, "integer does not fit in destination type")) {
std.process.exit(0);
}
std.process.exit(1);

View File

@@ -2,7 +2,7 @@ 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 cast truncated bits")) {
if (std.mem.eql(u8, message, "integer does not fit in destination type")) {
std.process.exit(0);
}
std.process.exit(1);

View File

@@ -2,7 +2,7 @@ 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 cast truncated bits")) {
if (std.mem.eql(u8, message, "integer does not fit in destination type")) {
std.process.exit(0);
}
std.process.exit(1);

View File

@@ -2,7 +2,7 @@ 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 cast truncated bits")) {
if (std.mem.eql(u8, message, "integer does not fit in destination type")) {
std.process.exit(0);
}
std.process.exit(1);

View File

@@ -2,7 +2,7 @@ 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 cast truncated bits")) {
if (std.mem.eql(u8, message, "integer does not fit in destination type")) {
std.process.exit(0);
}
std.process.exit(1);

View File

@@ -2,7 +2,7 @@ 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 cast truncated bits")) {
if (std.mem.eql(u8, message, "integer does not fit in destination type")) {
std.process.exit(0);
}
std.process.exit(1);