aboutsummaryrefslogtreecommitdiff
path: root/src/Sema.zig
diff options
context:
space:
mode:
authormlugg <mlugg@mlugg.co.uk>2025-06-01 07:41:24 +0100
committermlugg <mlugg@mlugg.co.uk>2025-06-01 12:10:57 +0100
commitc1a5caa4545264b476951e844818f2abe103f41c (patch)
treefcacbb6cb46d76b9bb31a20a7c2149cb3802b7d7 /src/Sema.zig
parent6daa37ded905431f3a25508a42590031b0905ab9 (diff)
downloadzig-c1a5caa4545264b476951e844818f2abe103f41c.tar.gz
zig-c1a5caa4545264b476951e844818f2abe103f41c.zip
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".
Diffstat (limited to 'src/Sema.zig')
-rw-r--r--src/Sema.zig16
1 files changed, 6 insertions, 10 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index 3c4fc555cb..f051a62af3 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -10263,7 +10263,7 @@ fn zirIntCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
const dest_ty = try sema.resolveDestType(block, src, extra.lhs, .remove_eu_opt, "@intCast");
const operand = try sema.resolveInst(extra.rhs);
- return sema.intCast(block, block.nodeOffset(inst_data.src_node), dest_ty, src, operand, operand_src, true, false);
+ return sema.intCast(block, block.nodeOffset(inst_data.src_node), dest_ty, src, operand, operand_src);
}
fn intCast(
@@ -10274,8 +10274,6 @@ fn intCast(
dest_ty_src: LazySrcLoc,
operand: Air.Inst.Ref,
operand_src: LazySrcLoc,
- runtime_safety: bool,
- safety_panics_are_enum: bool,
) CompileError!Air.Inst.Ref {
const pt = sema.pt;
const zcu = pt.zcu;
@@ -10294,7 +10292,7 @@ fn intCast(
if ((try sema.typeHasOnePossibleValue(dest_ty))) |opv| {
// requirement: intCast(u0, input) iff input == 0
- if (runtime_safety and block.wantSafety()) {
+ if (block.wantSafety()) {
try sema.requireRuntimeBlock(block, src, operand_src);
const wanted_info = dest_scalar_ty.intInfo(zcu);
const wanted_bits = wanted_info.bits;
@@ -10311,7 +10309,7 @@ fn intCast(
const is_in_range = try block.addBinOp(.cmp_lte, operand, zero_inst);
break :ok is_in_range;
};
- try sema.addSafetyCheck(block, src, ok, if (safety_panics_are_enum) .invalid_enum_value else .cast_truncated_data);
+ try sema.addSafetyCheck(block, src, ok, .integer_out_of_bounds);
}
}
@@ -10319,10 +10317,9 @@ fn intCast(
}
try sema.requireRuntimeBlock(block, src, operand_src);
- if (runtime_safety and block.wantSafety()) {
+ if (block.wantSafety()) {
if (zcu.backendSupportsFeature(.panic_fn)) {
- _ = try sema.preparePanicId(src, .negative_to_unsigned);
- _ = try sema.preparePanicId(src, .cast_truncated_data);
+ _ = try sema.preparePanicId(src, .integer_out_of_bounds);
}
return block.addTyOp(.intcast_safe, dest_ty, operand);
}
@@ -37984,8 +37981,7 @@ fn getExpectedBuiltinFnType(sema: *Sema, decl: Zcu.BuiltinDecl) CompileError!Typ
.@"panic.castToNull",
.@"panic.incorrectAlignment",
.@"panic.invalidErrorCode",
- .@"panic.castTruncatedData",
- .@"panic.negativeToUnsigned",
+ .@"panic.integerOutOfBounds",
.@"panic.integerOverflow",
.@"panic.shlOverflow",
.@"panic.shrOverflow",