Skip to content

Commit d90068d

Browse files
mluggjacobly0
authored andcommitted
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 parent 6ffa285 commit d90068d

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/Sema.zig

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
26864-
fn preparePanicId(sema: *Sema, src: LazySrcLoc, panic_id: Zcu.SimplePanicId) !InternPool.Index {
26856+
fn 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+
26864+
fn 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
}

0 commit comments

Comments
 (0)