authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-06-15 14:28:41+01:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2025-06-15 14:15:18-04:00
logd90068db5a18ef9de65ea9ec468e70e50a58f09d
treed474f1906b0be3a8a750ac5abdc27c034a7bff87
parent6ffa285fc35651cb7f4b738a0f6e84718b821fd8

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.

1 files changed, 14 insertions(+), 14 deletions(-)

src/Sema.zig+14-14
......@@ -8934,9 +8934,7 @@ fn zirEnumFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
89348934
89358935 try sema.requireRuntimeBlock(block, src, operand_src);
89368936 if (block.wantSafety()) {
8937 if (zcu.backendSupportsFeature(.panic_fn)) {
8938 _ = try sema.preparePanicId(src, .invalid_enum_value);
8939 }
8937 try sema.preparePanicId(src, .invalid_enum_value);
89408938 return block.addTyOp(.intcast_safe, dest_ty, operand);
89418939 }
89428940 return block.addTyOp(.intcast, dest_ty, operand);
......@@ -10340,9 +10338,7 @@ fn intCast(
1034010338
1034110339 try sema.requireRuntimeBlock(block, src, operand_src);
1034210340 if (block.wantSafety()) {
10343 if (zcu.backendSupportsFeature(.panic_fn)) {
10344 _ = try sema.preparePanicId(src, .integer_out_of_bounds);
10345 }
10341 try sema.preparePanicId(src, .integer_out_of_bounds);
1034610342 return block.addTyOp(.intcast_safe, dest_ty, operand);
1034710343 }
1034810344 return block.addTyOp(.intcast, dest_ty, operand);
......@@ -16395,9 +16391,7 @@ fn analyzeArithmetic(
1639516391 }
1639616392
1639716393 if (block.wantSafety() and want_safety and scalar_tag == .int) {
16398 if (air_tag != air_tag_safe and zcu.backendSupportsFeature(.panic_fn)) {
16399 _ = try sema.preparePanicId(src, .integer_overflow);
16400 }
16394 if (air_tag != air_tag_safe) try sema.preparePanicId(src, .integer_overflow);
1640116395 return block.addBinOp(air_tag_safe, casted_lhs, casted_rhs);
1640216396 }
1640316397 return block.addBinOp(air_tag, casted_lhs, casted_rhs);
......@@ -22194,9 +22188,7 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
2219422188 } }));
2219522189 }
2219622190 if (block.wantSafety()) {
22197 if (zcu.backendSupportsFeature(.panic_fn)) {
22198 _ = try sema.preparePanicId(src, .integer_part_out_of_bounds);
22199 }
22191 try sema.preparePanicId(src, .integer_part_out_of_bounds);
2220022192 return block.addTyOp(switch (block.float_mode) {
2220122193 .optimized => .int_from_float_optimized_safe,
2220222194 .strict => .int_from_float_safe,
......@@ -26861,7 +26853,15 @@ fn explainWhyTypeIsNotPacked(
2686126853/// Backends depend on panic decls being available when lowering safety-checked
2686226854/// instructions. This function ensures the panic function will be available to
2686326855/// be called during that time.
26864fn preparePanicId(sema: *Sema, src: LazySrcLoc, panic_id: Zcu.SimplePanicId) !InternPool.Index {
26856fn preparePanicId(sema: *Sema, src: LazySrcLoc, panic_id: Zcu.SimplePanicId) !void {
26857 // If the backend doesn't support `.panic_fn`, it doesn't want us to lower the panic handlers.
26858 // The backend will transform panics into traps instead.
26859 if (sema.pt.zcu.backendSupportsFeature(.panic_fn)) {
26860 _ = try sema.getPanicIdFunc(src, panic_id);
26861 }
26862}
26863
26864fn getPanicIdFunc(sema: *Sema, src: LazySrcLoc, panic_id: Zcu.SimplePanicId) !InternPool.Index {
2686526865 const zcu = sema.pt.zcu;
2686626866 try sema.ensureMemoizedStateResolved(src, .panic);
2686726867 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
2711027110 if (!sema.pt.zcu.backendSupportsFeature(.panic_fn)) {
2711127111 _ = try block.addNoOp(.trap);
2711227112 } else {
27113 const panic_fn = try sema.preparePanicId(src, panic_id);
27113 const panic_fn = try sema.getPanicIdFunc(src, panic_id);
2711427114 try sema.callBuiltin(block, src, Air.internedToRef(panic_fn), .auto, &.{}, .@"safety check");
2711527115 }
2711627116}