authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-01-24 04:20:55+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-01-24 22:33:23+00:00
log5a6666db5502079d9800ae3d41b790646238078e
tree7f7ce3014e7c35c50dc35b0a5296da590dbb0d05
parentdd334d5ee5344cfe773047ceeca6cbcfc896f57c
signaturelock-open Commit is signed but in an unrecognized format.

all: update for `panic.unwrapError` and `panic.call` signature changes


10 files changed, 79 insertions(+), 80 deletions(-)

lib/std/builtin.zig+6-1
......@@ -1117,7 +1117,12 @@ pub const PanicFn = fn ([]const u8, ?*StackTrace, ?usize) noreturn;
11171117pub const panic: type = p: {
11181118 if (@hasDecl(root, "panic")) {
11191119 if (@TypeOf(root.panic) != type) {
1120 break :p std.debug.FullPanic(root.panic); // Deprecated; make `panic` a namespace instead.
1120 // Deprecated; make `panic` a namespace instead.
1121 break :p std.debug.FullPanic(struct {
1122 fn panic(msg: []const u8, ra: ?usize) noreturn {
1123 root.panic(msg, @errorReturnTrace(), ra);
1124 }
1125 }.panic);
11211126 }
11221127 break :p root.panic;
11231128 }
lib/std/debug.zig+31-33
......@@ -27,112 +27,112 @@ pub const no_panic = @import("debug/no_panic.zig");
2727/// A fully-featured panic handler namespace which lowers all panics to calls to `panicFn`.
2828/// Safety panics will use formatted printing to provide a meaningful error message.
2929/// The signature of `panicFn` should match that of `defaultPanic`.
30pub fn FullPanic(comptime panicFn: fn ([]const u8, ?*std.builtin.StackTrace, ?usize) noreturn) type {
30pub fn FullPanic(comptime panicFn: fn ([]const u8, ?usize) noreturn) type {
3131 return struct {
3232 pub const call = panicFn;
3333 pub fn sentinelMismatch(expected: anytype, found: @TypeOf(expected)) noreturn {
3434 @branchHint(.cold);
35 std.debug.panicExtra(null, @returnAddress(), "sentinel mismatch: expected {any}, found {any}", .{
35 std.debug.panicExtra(@returnAddress(), "sentinel mismatch: expected {any}, found {any}", .{
3636 expected, found,
3737 });
3838 }
39 pub fn unwrapError(ert: ?*std.builtin.StackTrace, err: anyerror) noreturn {
39 pub fn unwrapError(err: anyerror) noreturn {
4040 @branchHint(.cold);
41 std.debug.panicExtra(ert, @returnAddress(), "attempt to unwrap error: {s}", .{@errorName(err)});
41 std.debug.panicExtra(@returnAddress(), "attempt to unwrap error: {s}", .{@errorName(err)});
4242 }
4343 pub fn outOfBounds(index: usize, len: usize) noreturn {
4444 @branchHint(.cold);
45 std.debug.panicExtra(null, @returnAddress(), "index out of bounds: index {d}, len {d}", .{ index, len });
45 std.debug.panicExtra(@returnAddress(), "index out of bounds: index {d}, len {d}", .{ index, len });
4646 }
4747 pub fn startGreaterThanEnd(start: usize, end: usize) noreturn {
4848 @branchHint(.cold);
49 std.debug.panicExtra(null, @returnAddress(), "start index {d} is larger than end index {d}", .{ start, end });
49 std.debug.panicExtra(@returnAddress(), "start index {d} is larger than end index {d}", .{ start, end });
5050 }
5151 pub fn inactiveUnionField(active: anytype, accessed: @TypeOf(active)) noreturn {
5252 @branchHint(.cold);
53 std.debug.panicExtra(null, @returnAddress(), "access of union field '{s}' while field '{s}' is active", .{
53 std.debug.panicExtra(@returnAddress(), "access of union field '{s}' while field '{s}' is active", .{
5454 @tagName(accessed), @tagName(active),
5555 });
5656 }
5757 pub fn reachedUnreachable() noreturn {
5858 @branchHint(.cold);
59 call("reached unreachable code", null, @returnAddress());
59 call("reached unreachable code", @returnAddress());
6060 }
6161 pub fn unwrapNull() noreturn {
6262 @branchHint(.cold);
63 call("attempt to use null value", null, @returnAddress());
63 call("attempt to use null value", @returnAddress());
6464 }
6565 pub fn castToNull() noreturn {
6666 @branchHint(.cold);
67 call("cast causes pointer to be null", null, @returnAddress());
67 call("cast causes pointer to be null", @returnAddress());
6868 }
6969 pub fn incorrectAlignment() noreturn {
7070 @branchHint(.cold);
71 call("incorrect alignment", null, @returnAddress());
71 call("incorrect alignment", @returnAddress());
7272 }
7373 pub fn invalidErrorCode() noreturn {
7474 @branchHint(.cold);
75 call("invalid error code", null, @returnAddress());
75 call("invalid error code", @returnAddress());
7676 }
7777 pub fn castTruncatedData() noreturn {
7878 @branchHint(.cold);
79 call("integer cast truncated bits", null, @returnAddress());
79 call("integer cast truncated bits", @returnAddress());
8080 }
8181 pub fn negativeToUnsigned() noreturn {
8282 @branchHint(.cold);
83 call("attempt to cast negative value to unsigned integer", null, @returnAddress());
83 call("attempt to cast negative value to unsigned integer", @returnAddress());
8484 }
8585 pub fn integerOverflow() noreturn {
8686 @branchHint(.cold);
87 call("integer overflow", null, @returnAddress());
87 call("integer overflow", @returnAddress());
8888 }
8989 pub fn shlOverflow() noreturn {
9090 @branchHint(.cold);
91 call("left shift overflowed bits", null, @returnAddress());
91 call("left shift overflowed bits", @returnAddress());
9292 }
9393 pub fn shrOverflow() noreturn {
9494 @branchHint(.cold);
95 call("right shift overflowed bits", null, @returnAddress());
95 call("right shift overflowed bits", @returnAddress());
9696 }
9797 pub fn divideByZero() noreturn {
9898 @branchHint(.cold);
99 call("division by zero", null, @returnAddress());
99 call("division by zero", @returnAddress());
100100 }
101101 pub fn exactDivisionRemainder() noreturn {
102102 @branchHint(.cold);
103 call("exact division produced remainder", null, @returnAddress());
103 call("exact division produced remainder", @returnAddress());
104104 }
105105 pub fn integerPartOutOfBounds() noreturn {
106106 @branchHint(.cold);
107 call("integer part of floating point value out of bounds", null, @returnAddress());
107 call("integer part of floating point value out of bounds", @returnAddress());
108108 }
109109 pub fn corruptSwitch() noreturn {
110110 @branchHint(.cold);
111 call("switch on corrupt value", null, @returnAddress());
111 call("switch on corrupt value", @returnAddress());
112112 }
113113 pub fn shiftRhsTooBig() noreturn {
114114 @branchHint(.cold);
115 call("shift amount is greater than the type size", null, @returnAddress());
115 call("shift amount is greater than the type size", @returnAddress());
116116 }
117117 pub fn invalidEnumValue() noreturn {
118118 @branchHint(.cold);
119 call("invalid enum value", null, @returnAddress());
119 call("invalid enum value", @returnAddress());
120120 }
121121 pub fn forLenMismatch() noreturn {
122122 @branchHint(.cold);
123 call("for loop over objects with non-equal lengths", null, @returnAddress());
123 call("for loop over objects with non-equal lengths", @returnAddress());
124124 }
125125 pub fn memcpyLenMismatch() noreturn {
126126 @branchHint(.cold);
127 call("@memcpy arguments have non-equal lengths", null, @returnAddress());
127 call("@memcpy arguments have non-equal lengths", @returnAddress());
128128 }
129129 pub fn memcpyAlias() noreturn {
130130 @branchHint(.cold);
131 call("@memcpy arguments alias", null, @returnAddress());
131 call("@memcpy arguments alias", @returnAddress());
132132 }
133133 pub fn noreturnReturned() noreturn {
134134 @branchHint(.cold);
135 call("'noreturn' function returned", null, @returnAddress());
135 call("'noreturn' function returned", @returnAddress());
136136 }
137137
138138 /// To be deleted after zig1.wasm update.
......@@ -531,13 +531,12 @@ pub fn assertReadable(slice: []const volatile u8) void {
531531/// Equivalent to `@panic` but with a formatted message.
532532pub fn panic(comptime format: []const u8, args: anytype) noreturn {
533533 @branchHint(.cold);
534 panicExtra(@errorReturnTrace(), @returnAddress(), format, args);
534 panicExtra(@returnAddress(), format, args);
535535}
536536
537537/// Equivalent to `@panic` but with a formatted message, and with an explicitly
538/// provided `@errorReturnTrace` and return address.
538/// provided return address.
539539pub fn panicExtra(
540 trace: ?*std.builtin.StackTrace,
541540 ret_addr: ?usize,
542541 comptime format: []const u8,
543542 args: anytype,
......@@ -556,7 +555,7 @@ pub fn panicExtra(
556555 break :blk &buf;
557556 },
558557 };
559 std.builtin.panic.call(msg, trace, ret_addr);
558 std.builtin.panic.call(msg, ret_addr);
560559}
561560
562561/// Non-zero whenever the program triggered a panic.
......@@ -570,7 +569,6 @@ threadlocal var panic_stage: usize = 0;
570569/// Dumps a stack trace to standard error, then aborts.
571570pub fn defaultPanic(
572571 msg: []const u8,
573 error_return_trace: ?*const std.builtin.StackTrace,
574572 first_trace_addr: ?usize,
575573) noreturn {
576574 @branchHint(.cold);
......@@ -657,7 +655,7 @@ pub fn defaultPanic(
657655 }
658656 stderr.print("{s}\n", .{msg}) catch posix.abort();
659657
660 if (error_return_trace) |t| dumpStackTrace(t.*);
658 if (@errorReturnTrace()) |t| dumpStackTrace(t.*);
661659 dumpCurrentStackTrace(first_trace_addr orelse @returnAddress());
662660 }
663661
lib/std/debug/no_panic.zig+2-2
......@@ -5,7 +5,7 @@
55
66const std = @import("../std.zig");
77
8pub fn call(_: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
8pub fn call(_: []const u8, _: ?usize) noreturn {
99 @branchHint(.cold);
1010 @trap();
1111}
......@@ -15,7 +15,7 @@ pub fn sentinelMismatch(_: anytype, _: anytype) noreturn {
1515 @trap();
1616}
1717
18pub fn unwrapError(_: ?*std.builtin.StackTrace, _: anyerror) noreturn {
18pub fn unwrapError(_: anyerror) noreturn {
1919 @branchHint(.cold);
2020 @trap();
2121}
lib/std/debug/simple_panic.zig+27-29
......@@ -11,9 +11,8 @@ const std = @import("../std.zig");
1111/// Prints the message to stderr without a newline and then traps.
1212///
1313/// Explicit calls to `@panic` lower to calling this function.
14pub fn call(msg: []const u8, ert: ?*std.builtin.StackTrace, ra: ?usize) noreturn {
14pub fn call(msg: []const u8, ra: ?usize) noreturn {
1515 @branchHint(.cold);
16 _ = ert;
1716 _ = ra;
1817 std.debug.lockStdErr();
1918 const stderr = std.io.getStdErr();
......@@ -23,110 +22,109 @@ pub fn call(msg: []const u8, ert: ?*std.builtin.StackTrace, ra: ?usize) noreturn
2322
2423pub fn sentinelMismatch(expected: anytype, found: @TypeOf(expected)) noreturn {
2524 _ = found;
26 call("sentinel mismatch", null, null);
25 call("sentinel mismatch", null);
2726}
2827
29pub fn unwrapError(ert: ?*std.builtin.StackTrace, err: anyerror) noreturn {
30 _ = ert;
28pub fn unwrapError(err: anyerror) noreturn {
3129 _ = &err;
32 call("attempt to unwrap error", null, null);
30 call("attempt to unwrap error", null);
3331}
3432
3533pub fn outOfBounds(index: usize, len: usize) noreturn {
3634 _ = index;
3735 _ = len;
38 call("index out of bounds", null, null);
36 call("index out of bounds", null);
3937}
4038
4139pub fn startGreaterThanEnd(start: usize, end: usize) noreturn {
4240 _ = start;
4341 _ = end;
44 call("start index is larger than end index", null, null);
42 call("start index is larger than end index", null);
4543}
4644
4745pub fn inactiveUnionField(active: anytype, accessed: @TypeOf(active)) noreturn {
4846 _ = accessed;
49 call("access of inactive union field", null, null);
47 call("access of inactive union field", null);
5048}
5149
5250pub fn reachedUnreachable() noreturn {
53 call("reached unreachable code", null, null);
51 call("reached unreachable code", null);
5452}
5553
5654pub fn unwrapNull() noreturn {
57 call("attempt to use null value", null, null);
55 call("attempt to use null value", null);
5856}
5957
6058pub fn castToNull() noreturn {
61 call("cast causes pointer to be null", null, null);
59 call("cast causes pointer to be null", null);
6260}
6361
6462pub fn incorrectAlignment() noreturn {
65 call("incorrect alignment", null, null);
63 call("incorrect alignment", null);
6664}
6765
6866pub fn invalidErrorCode() noreturn {
69 call("invalid error code", null, null);
67 call("invalid error code", null);
7068}
7169
7270pub fn castTruncatedData() noreturn {
73 call("integer cast truncated bits", null, null);
71 call("integer cast truncated bits", null);
7472}
7573
7674pub fn negativeToUnsigned() noreturn {
77 call("attempt to cast negative value to unsigned integer", null, null);
75 call("attempt to cast negative value to unsigned integer", null);
7876}
7977
8078pub fn integerOverflow() noreturn {
81 call("integer overflow", null, null);
79 call("integer overflow", null);
8280}
8381
8482pub fn shlOverflow() noreturn {
85 call("left shift overflowed bits", null, null);
83 call("left shift overflowed bits", null);
8684}
8785
8886pub fn shrOverflow() noreturn {
89 call("right shift overflowed bits", null, null);
87 call("right shift overflowed bits", null);
9088}
9189
9290pub fn divideByZero() noreturn {
93 call("division by zero", null, null);
91 call("division by zero", null);
9492}
9593
9694pub fn exactDivisionRemainder() noreturn {
97 call("exact division produced remainder", null, null);
95 call("exact division produced remainder", null);
9896}
9997
10098pub fn integerPartOutOfBounds() noreturn {
101 call("integer part of floating point value out of bounds", null, null);
99 call("integer part of floating point value out of bounds", null);
102100}
103101
104102pub fn corruptSwitch() noreturn {
105 call("switch on corrupt value", null, null);
103 call("switch on corrupt value", null);
106104}
107105
108106pub fn shiftRhsTooBig() noreturn {
109 call("shift amount is greater than the type size", null, null);
107 call("shift amount is greater than the type size", null);
110108}
111109
112110pub fn invalidEnumValue() noreturn {
113 call("invalid enum value", null, null);
111 call("invalid enum value", null);
114112}
115113
116114pub fn forLenMismatch() noreturn {
117 call("for loop over objects with non-equal lengths", null, null);
115 call("for loop over objects with non-equal lengths", null);
118116}
119117
120118pub fn memcpyLenMismatch() noreturn {
121 call("@memcpy arguments have non-equal lengths", null, null);
119 call("@memcpy arguments have non-equal lengths", null);
122120}
123121
124122pub fn memcpyAlias() noreturn {
125 call("@memcpy arguments alias", null, null);
123 call("@memcpy arguments alias", null);
126124}
127125
128126pub fn noreturnReturned() noreturn {
129 call("'noreturn' function returned", null, null);
127 call("'noreturn' function returned", null);
130128}
131129
132130/// To be deleted after zig1.wasm update.
src/Sema.zig+1-1
......@@ -2584,7 +2584,7 @@ pub fn failWithOwnedErrorMsg(sema: *Sema, block: ?*Block, err_msg: *Zcu.ErrorMsg
25842584 std.debug.print("compile error during Sema:\n", .{});
25852585 var error_bundle = wip_errors.toOwnedBundle("") catch @panic("out of memory");
25862586 error_bundle.renderToStdErr(.{ .ttyconf = .no_color });
2587 crash_report.compilerPanic("unexpected compile error occurred", null, null);
2587 crash_report.compilerPanic("unexpected compile error occurred", null);
25882588 }
25892589
25902590 if (block) |start_block| {
src/crash_report.zig+2-2
......@@ -158,12 +158,12 @@ fn writeFilePath(file: *Zcu.File, writer: anytype) !void {
158158 try writer.writeAll(file.sub_file_path);
159159}
160160
161pub fn compilerPanic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, maybe_ret_addr: ?usize) noreturn {
161pub fn compilerPanic(msg: []const u8, maybe_ret_addr: ?usize) noreturn {
162162 @branchHint(.cold);
163163 PanicSwitch.preDispatch();
164164 const ret_addr = maybe_ret_addr orelse @returnAddress();
165165 const stack_ctx: StackContext = .{ .current = .{ .ret_addr = ret_addr } };
166 PanicSwitch.dispatch(error_return_trace, stack_ctx, msg);
166 PanicSwitch.dispatch(@errorReturnTrace(), stack_ctx, msg);
167167}
168168
169169/// Attaches a global SIGSEGV handler
test/cases/compile_errors/bad_panic_call_signature.zig+4-5
......@@ -1,9 +1,8 @@
11const simple_panic = std.debug.simple_panic;
22pub const panic = struct {
3 pub fn call(msg: []const u8, bad1: usize, bad2: void) noreturn {
3 pub fn call(msg: []const u8, bad: usize) noreturn {
44 _ = msg;
5 _ = bad1;
6 _ = bad2;
5 _ = bad;
76 @trap();
87 }
98 pub const sentinelMismatch = simple_panic.sentinelMismatch;
......@@ -42,5 +41,5 @@ const std = @import("std");
4241
4342// error
4443//
45// :3:9: error: expected type 'fn ([]const u8, ?*builtin.StackTrace, ?usize) noreturn', found 'fn ([]const u8, usize, void) noreturn'
46// :3:9: note: parameter 1 'usize' cannot cast into '?*builtin.StackTrace'
44// :3:9: error: expected type 'fn ([]const u8, ?usize) noreturn', found 'fn ([]const u8, usize) noreturn'
45// :3:9: note: parameter 1 'usize' cannot cast into '?usize'
test/incremental/change_panic_handler+3-3
......@@ -10,7 +10,7 @@ pub fn main() !u8 {
1010 return 1;
1111}
1212pub const panic = std.debug.FullPanic(myPanic);
13fn myPanic(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
13fn myPanic(msg: []const u8, _: ?usize) noreturn {
1414 std.io.getStdOut().writer().print("panic message: {s}\n", .{msg}) catch {};
1515 std.process.exit(0);
1616}
......@@ -26,7 +26,7 @@ pub fn main() !u8 {
2626 return 1;
2727}
2828pub const panic = std.debug.FullPanic(myPanic);
29fn myPanic(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
29fn myPanic(msg: []const u8, _: ?usize) noreturn {
3030 std.io.getStdOut().writer().print("new panic message: {s}\n", .{msg}) catch {};
3131 std.process.exit(0);
3232}
......@@ -42,7 +42,7 @@ pub fn main() !u8 {
4242 return 1;
4343}
4444pub const panic = std.debug.FullPanic(myPanicNew);
45fn myPanicNew(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
45fn myPanicNew(msg: []const u8, _: ?usize) noreturn {
4646 std.io.getStdOut().writer().print("third panic message: {s}\n", .{msg}) catch {};
4747 std.process.exit(0);
4848}
test/incremental/change_panic_handler_explicit+3-3
......@@ -40,7 +40,7 @@ pub const panic = struct {
4040 pub const memcpyAlias = no_panic.memcpyAlias;
4141 pub const noreturnReturned = no_panic.noreturnReturned;
4242};
43fn myPanic(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
43fn myPanic(msg: []const u8, _: ?usize) noreturn {
4444 std.io.getStdOut().writer().print("panic message: {s}\n", .{msg}) catch {};
4545 std.process.exit(0);
4646}
......@@ -86,7 +86,7 @@ pub const panic = struct {
8686 pub const memcpyAlias = no_panic.memcpyAlias;
8787 pub const noreturnReturned = no_panic.noreturnReturned;
8888};
89fn myPanic(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
89fn myPanic(msg: []const u8, _: ?usize) noreturn {
9090 std.io.getStdOut().writer().print("new panic message: {s}\n", .{msg}) catch {};
9191 std.process.exit(0);
9292}
......@@ -133,7 +133,7 @@ pub const panic = struct {
133133 pub const memcpyAlias = no_panic.memcpyAlias;
134134 pub const noreturnReturned = no_panic.noreturnReturned;
135135};
136fn myPanicNew(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
136fn myPanicNew(msg: []const u8, _: ?usize) noreturn {
137137 std.io.getStdOut().writer().print("third panic message: {s}\n", .{msg}) catch {};
138138 std.process.exit(0);
139139}
test/src/Cases.zig-1
......@@ -358,7 +358,6 @@ pub fn addFromDir(ctx: *Cases, dir: std.fs.Dir, b: *std.Build) void {
358358 var current_file: []const u8 = "none";
359359 ctx.addFromDirInner(dir, &current_file, b) catch |err| {
360360 std.debug.panicExtra(
361 @errorReturnTrace(),
362361 @returnAddress(),
363362 "test harness failed to process file '{s}': {s}\n",
364363 .{ current_file, @errorName(err) },