diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2025-06-15 14:28:41 +0100 |
|---|---|---|
| committer | Jacob Young <jacobly0@users.noreply.github.com> | 2025-06-15 14:15:18 -0400 |
| commit | d90068db5a18ef9de65ea9ec468e70e50a58f09d (patch) | |
| tree | d474f1906b0be3a8a750ac5abdc27c034a7bff87 /src/Sema.zig | |
| parent | 6ffa285fc35651cb7f4b738a0f6e84718b821fd8 (diff) | |
| download | zig-d90068db5a18ef9de65ea9ec468e70e50a58f09d.tar.gz zig-d90068db5a18ef9de65ea9ec468e70e50a58f09d.zip | |
Sema: tiny refactor
There will be more call sites to `preparePanicId` as we transition away
from safety checks in Sema towards safety checked instructions; it's
silly for them to all have this clunky usage.
Diffstat (limited to 'src/Sema.zig')
| -rw-r--r-- | src/Sema.zig | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index 8308a17e95..b917e9a31f 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -8934,9 +8934,7 @@ fn zirEnumFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError try sema.requireRuntimeBlock(block, src, operand_src); if (block.wantSafety()) { - if (zcu.backendSupportsFeature(.panic_fn)) { - _ = try sema.preparePanicId(src, .invalid_enum_value); - } + try sema.preparePanicId(src, .invalid_enum_value); return block.addTyOp(.intcast_safe, dest_ty, operand); } return block.addTyOp(.intcast, dest_ty, operand); @@ -10340,9 +10338,7 @@ fn intCast( try sema.requireRuntimeBlock(block, src, operand_src); if (block.wantSafety()) { - if (zcu.backendSupportsFeature(.panic_fn)) { - _ = try sema.preparePanicId(src, .integer_out_of_bounds); - } + try sema.preparePanicId(src, .integer_out_of_bounds); return block.addTyOp(.intcast_safe, dest_ty, operand); } return block.addTyOp(.intcast, dest_ty, operand); @@ -16395,9 +16391,7 @@ fn analyzeArithmetic( } if (block.wantSafety() and want_safety and scalar_tag == .int) { - if (air_tag != air_tag_safe and zcu.backendSupportsFeature(.panic_fn)) { - _ = try sema.preparePanicId(src, .integer_overflow); - } + if (air_tag != air_tag_safe) try sema.preparePanicId(src, .integer_overflow); return block.addBinOp(air_tag_safe, casted_lhs, casted_rhs); } return block.addBinOp(air_tag, casted_lhs, casted_rhs); @@ -22194,9 +22188,7 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro } })); } if (block.wantSafety()) { - if (zcu.backendSupportsFeature(.panic_fn)) { - _ = try sema.preparePanicId(src, .integer_part_out_of_bounds); - } + try sema.preparePanicId(src, .integer_part_out_of_bounds); return block.addTyOp(switch (block.float_mode) { .optimized => .int_from_float_optimized_safe, .strict => .int_from_float_safe, @@ -26861,7 +26853,15 @@ fn explainWhyTypeIsNotPacked( /// Backends depend on panic decls being available when lowering safety-checked /// instructions. This function ensures the panic function will be available to /// be called during that time. -fn preparePanicId(sema: *Sema, src: LazySrcLoc, panic_id: Zcu.SimplePanicId) !InternPool.Index { +fn preparePanicId(sema: *Sema, src: LazySrcLoc, panic_id: Zcu.SimplePanicId) !void { + // If the backend doesn't support `.panic_fn`, it doesn't want us to lower the panic handlers. + // The backend will transform panics into traps instead. + if (sema.pt.zcu.backendSupportsFeature(.panic_fn)) { + _ = try sema.getPanicIdFunc(src, panic_id); + } +} + +fn getPanicIdFunc(sema: *Sema, src: LazySrcLoc, panic_id: Zcu.SimplePanicId) !InternPool.Index { const zcu = sema.pt.zcu; try sema.ensureMemoizedStateResolved(src, .panic); const panic_func = zcu.builtin_decl_values.get(panic_id.toBuiltin()); @@ -27110,7 +27110,7 @@ fn safetyPanic(sema: *Sema, block: *Block, src: LazySrcLoc, panic_id: Zcu.Simple if (!sema.pt.zcu.backendSupportsFeature(.panic_fn)) { _ = try block.addNoOp(.trap); } else { - const panic_fn = try sema.preparePanicId(src, panic_id); + const panic_fn = try sema.getPanicIdFunc(src, panic_id); try sema.callBuiltin(block, src, Air.internedToRef(panic_fn), .auto, &.{}, .@"safety check"); } } |
