authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-08-13 09:54:58+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-08-14 11:43:21-07:00
log8f3ccbbe367bea66d7f0f364a957870eb2cc95a0
tree33dad0b953904d39e75490b05f414bdbc09869bb
parent1054e67f01e0f2eff1adf4982189ebba0a3696ab

Sema: provide source location when analyzing panic handler

The panic handler decl_val was previously given a `unneeded` source location, which was then added to the reference trace, resulting in a crash if the source location was used in the reference trace. This commit makes two trivial changes: * Don't add unneeded source locations to the ref table (panic in debug, silently ignore in release) * Pass a real source location when analyzing the panic handler

2 files changed, 27 insertions(+), 2 deletions(-)

src/Sema.zig+10-2
......@@ -25202,7 +25202,7 @@ fn panicWithMsg(sema: *Sema, block: *Block, src: LazySrcLoc, msg_inst: Air.Inst.
2520225202 try sema.prepareSimplePanic(block);
2520325203
2520425204 const panic_func = mod.funcInfo(mod.panic_func_index);
25205 const panic_fn = try sema.analyzeDeclVal(block, .unneeded, panic_func.owner_decl);
25205 const panic_fn = try sema.analyzeDeclVal(block, src, panic_func.owner_decl);
2520625206 const null_stack_trace = Air.internedToRef(mod.null_stack_trace);
2520725207
2520825208 const opt_usize_ty = try mod.optionalType(.usize_type);
......@@ -30702,7 +30702,15 @@ fn addReferencedBy(
3070230702 src: LazySrcLoc,
3070330703 decl_index: Decl.Index,
3070430704) !void {
30705 if (sema.mod.comp.reference_trace == @as(u32, 0)) return;
30705 if (sema.mod.comp.reference_trace == 0) return;
30706 if (src == .unneeded) {
30707 // We can't use NeededSourceLocation, since sites handling that assume it means a compile
30708 // error. Our long-term strategy here is to gradually transition from NeededSourceLocation
30709 // into having more LazySrcLoc tags. In the meantime, let release compilers just ignore this
30710 // reference (a slightly-incomplete error is better than a crash!), but trigger a panic in
30711 // debug so we can fix this case.
30712 if (std.debug.runtime_safety) unreachable else return;
30713 }
3070630714 try sema.mod.reference_table.put(sema.gpa, decl_index, .{
3070730715 .referencer = block.src_decl,
3070830716 .src = src,
test/cases/compile_errors/panic_has_source_location.zig created+17
......@@ -0,0 +1,17 @@
1const std = @import("std");
2
3export fn foo() void {
4 // This should appear in the reference trace
5 // (and definitely shouldn't crash due to an unneeded source location!)
6 @panic("oh no");
7}
8
9pub fn panic(_: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
10 @compileError("panic");
11}
12
13// error
14// backend=stage2
15// target=native
16//
17// :10:5: error: panic