From 83991efe10d92c4b920d7b7fc75be98ed7854ad7 Mon Sep 17 00:00:00 2001 From: mlugg Date: Fri, 24 Jan 2025 02:19:28 +0000 Subject: compiler: yet more panic handler changes * `std.builtin.Panic` -> `std.builtin.panic`, because it is a namespace. * `root.Panic` -> `root.panic` for the same reason. There are type checks so that we still allow the legacy `pub fn panic` strategy in the 0.14.0 release. * `std.debug.SimplePanic` -> `std.debug.simple_panic`, same reason. * `std.debug.NoPanic` -> `std.debug.no_panic`, same reason. * `std.debug.FormattedPanic` is now a function `std.debug.FullPanic` which takes as input a `panicFn` and returns a namespace with all the panic functions. This handles the incredibly common case of just wanting to override how the message is printed, whilst keeping nice formatted panics. * Remove `std.builtin.panic.messages`; now, every safety panic has its own function. This reduces binary bloat, as calls to these functions no longer need to prepare any arguments (aside from the error return trace). * Remove some legacy declarations, since a zig1.wasm update has happened. Most of these were related to the panic handler, but a quick grep for "zig1" brought up a couple more results too. Also, add some missing type checks to Sema. Resolves: #22584 formatted -> full --- .../compile_errors/bad_panic_call_signature.zig | 46 +++++++ .../compile_errors/bad_panic_generic_signature.zig | 41 ++++++ test/cases/compile_errors/bad_panic_signature.zig | 28 ---- test/incremental/change_panic_handler | 30 +---- test/incremental/change_panic_handler_explicit | 141 +++++++++++++++++++++ 5 files changed, 231 insertions(+), 55 deletions(-) create mode 100644 test/cases/compile_errors/bad_panic_call_signature.zig create mode 100644 test/cases/compile_errors/bad_panic_generic_signature.zig delete mode 100644 test/cases/compile_errors/bad_panic_signature.zig create mode 100644 test/incremental/change_panic_handler_explicit (limited to 'test') diff --git a/test/cases/compile_errors/bad_panic_call_signature.zig b/test/cases/compile_errors/bad_panic_call_signature.zig new file mode 100644 index 0000000000..3536397241 --- /dev/null +++ b/test/cases/compile_errors/bad_panic_call_signature.zig @@ -0,0 +1,46 @@ +const simple_panic = std.debug.simple_panic; +pub const panic = struct { + pub fn call(msg: []const u8, bad1: usize, bad2: void) noreturn { + _ = msg; + _ = bad1; + _ = bad2; + @trap(); + } + pub const sentinelMismatch = simple_panic.sentinelMismatch; + pub const unwrapError = simple_panic.unwrapError; + pub const outOfBounds = simple_panic.outOfBounds; + pub const startGreaterThanEnd = simple_panic.startGreaterThanEnd; + pub const inactiveUnionField = simple_panic.inactiveUnionField; + pub const reachedUnreachable = simple_panic.reachedUnreachable; + pub const unwrapNull = simple_panic.unwrapNull; + pub const castToNull = simple_panic.castToNull; + pub const incorrectAlignment = simple_panic.incorrectAlignment; + pub const invalidErrorCode = simple_panic.invalidErrorCode; + pub const castTruncatedData = simple_panic.castTruncatedData; + pub const negativeToUnsigned = simple_panic.negativeToUnsigned; + pub const integerOverflow = simple_panic.integerOverflow; + pub const shlOverflow = simple_panic.shlOverflow; + pub const shrOverflow = simple_panic.shrOverflow; + pub const divideByZero = simple_panic.divideByZero; + pub const exactDivisionRemainder = simple_panic.exactDivisionRemainder; + pub const integerPartOutOfBounds = simple_panic.integerPartOutOfBounds; + pub const corruptSwitch = simple_panic.corruptSwitch; + pub const shiftRhsTooBig = simple_panic.shiftRhsTooBig; + pub const invalidEnumValue = simple_panic.invalidEnumValue; + pub const forLenMismatch = simple_panic.forLenMismatch; + pub const memcpyLenMismatch = simple_panic.memcpyLenMismatch; + pub const memcpyAlias = simple_panic.memcpyAlias; + pub const noreturnReturned = simple_panic.noreturnReturned; +}; + +export fn foo(a: u8) void { + @setRuntimeSafety(true); + _ = a + 1; // safety check to reference the panic handler +} + +const std = @import("std"); + +// error +// +// :3:9: error: expected type 'fn ([]const u8, ?*builtin.StackTrace, ?usize) noreturn', found 'fn ([]const u8, usize, void) noreturn' +// :3:9: note: parameter 1 'usize' cannot cast into '?*builtin.StackTrace' diff --git a/test/cases/compile_errors/bad_panic_generic_signature.zig b/test/cases/compile_errors/bad_panic_generic_signature.zig new file mode 100644 index 0000000000..92fa49c7f3 --- /dev/null +++ b/test/cases/compile_errors/bad_panic_generic_signature.zig @@ -0,0 +1,41 @@ +const simple_panic = std.debug.simple_panic; +pub const panic = struct { + pub fn sentinelMismatch() void {} // invalid + pub const call = simple_panic.call; + pub const unwrapError = simple_panic.unwrapError; + pub const outOfBounds = simple_panic.outOfBounds; + pub const startGreaterThanEnd = simple_panic.startGreaterThanEnd; + pub const inactiveUnionField = simple_panic.inactiveUnionField; + pub const reachedUnreachable = simple_panic.reachedUnreachable; + pub const unwrapNull = simple_panic.unwrapNull; + pub const castToNull = simple_panic.castToNull; + pub const incorrectAlignment = simple_panic.incorrectAlignment; + pub const invalidErrorCode = simple_panic.invalidErrorCode; + pub const castTruncatedData = simple_panic.castTruncatedData; + pub const negativeToUnsigned = simple_panic.negativeToUnsigned; + pub const integerOverflow = simple_panic.integerOverflow; + pub const shlOverflow = simple_panic.shlOverflow; + pub const shrOverflow = simple_panic.shrOverflow; + pub const divideByZero = simple_panic.divideByZero; + pub const exactDivisionRemainder = simple_panic.exactDivisionRemainder; + pub const integerPartOutOfBounds = simple_panic.integerPartOutOfBounds; + pub const corruptSwitch = simple_panic.corruptSwitch; + pub const shiftRhsTooBig = simple_panic.shiftRhsTooBig; + pub const invalidEnumValue = simple_panic.invalidEnumValue; + pub const forLenMismatch = simple_panic.forLenMismatch; + pub const memcpyLenMismatch = simple_panic.memcpyLenMismatch; + pub const memcpyAlias = simple_panic.memcpyAlias; + pub const noreturnReturned = simple_panic.noreturnReturned; +}; + +export fn foo(arr: *const [2]u8) void { + @setRuntimeSafety(true); + _ = arr[0..1 :0]; +} + +const std = @import("std"); + +// error +// +// :3:9: error: expected type 'fn (anytype, anytype) noreturn', found 'fn () void' +// :3:9: note: non-generic function cannot cast into a generic function diff --git a/test/cases/compile_errors/bad_panic_signature.zig b/test/cases/compile_errors/bad_panic_signature.zig deleted file mode 100644 index f9a30ef1d7..0000000000 --- a/test/cases/compile_errors/bad_panic_signature.zig +++ /dev/null @@ -1,28 +0,0 @@ -pub const Panic = struct { - pub const call = badPanicSignature; - pub const sentinelMismatch = std.debug.FormattedPanic.sentinelMismatch; - pub const unwrapError = std.debug.FormattedPanic.unwrapError; - pub const outOfBounds = std.debug.FormattedPanic.outOfBounds; - pub const startGreaterThanEnd = std.debug.FormattedPanic.startGreaterThanEnd; - pub const inactiveUnionField = std.debug.FormattedPanic.inactiveUnionField; - pub const messages = std.debug.FormattedPanic.messages; -}; - -fn badPanicSignature(msg: []const u8, bad1: usize, bad2: void) noreturn { - _ = msg; - _ = bad1; - _ = bad2; - @trap(); -} - -export fn foo(a: u8) void { - @setRuntimeSafety(true); - _ = a + 1; // safety check to reference the panic handler -} - -const std = @import("std"); - -// error -// -// :2:9: error: expected type 'fn ([]const u8, ?*builtin.StackTrace, ?usize) noreturn', found 'fn ([]const u8, usize, void) noreturn' -// :2:9: note: parameter 1 'usize' cannot cast into '?*builtin.StackTrace' diff --git a/test/incremental/change_panic_handler b/test/incremental/change_panic_handler index bab6eab793..3e0674a4d9 100644 --- a/test/incremental/change_panic_handler +++ b/test/incremental/change_panic_handler @@ -9,15 +9,7 @@ pub fn main() !u8 { _ = a + 1; return 1; } -pub const Panic = struct { - pub const call = myPanic; - pub const sentinelMismatch = std.debug.FormattedPanic.sentinelMismatch; - pub const unwrapError = std.debug.FormattedPanic.unwrapError; - pub const outOfBounds = std.debug.FormattedPanic.outOfBounds; - pub const startGreaterThanEnd = std.debug.FormattedPanic.startGreaterThanEnd; - pub const inactiveUnionField = std.debug.FormattedPanic.inactiveUnionField; - pub const messages = std.debug.FormattedPanic.messages; -}; +pub const panic = std.debug.FullPanic(myPanic); fn myPanic(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn { std.io.getStdOut().writer().print("panic message: {s}\n", .{msg}) catch {}; std.process.exit(0); @@ -33,15 +25,7 @@ pub fn main() !u8 { _ = a + 1; return 1; } -pub const Panic = struct { - pub const call = myPanic; - pub const sentinelMismatch = std.debug.FormattedPanic.sentinelMismatch; - pub const unwrapError = std.debug.FormattedPanic.unwrapError; - pub const outOfBounds = std.debug.FormattedPanic.outOfBounds; - pub const startGreaterThanEnd = std.debug.FormattedPanic.startGreaterThanEnd; - pub const inactiveUnionField = std.debug.FormattedPanic.inactiveUnionField; - pub const messages = std.debug.FormattedPanic.messages; -}; +pub const panic = std.debug.FullPanic(myPanic); fn myPanic(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn { std.io.getStdOut().writer().print("new panic message: {s}\n", .{msg}) catch {}; std.process.exit(0); @@ -57,15 +41,7 @@ pub fn main() !u8 { _ = a + 1; return 1; } -pub const Panic = struct { - pub const call = myPanicNew; - pub const sentinelMismatch = std.debug.FormattedPanic.sentinelMismatch; - pub const unwrapError = std.debug.FormattedPanic.unwrapError; - pub const outOfBounds = std.debug.FormattedPanic.outOfBounds; - pub const startGreaterThanEnd = std.debug.FormattedPanic.startGreaterThanEnd; - pub const inactiveUnionField = std.debug.FormattedPanic.inactiveUnionField; - pub const messages = std.debug.FormattedPanic.messages; -}; +pub const panic = std.debug.FullPanic(myPanicNew); fn myPanicNew(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn { std.io.getStdOut().writer().print("third panic message: {s}\n", .{msg}) catch {}; std.process.exit(0); diff --git a/test/incremental/change_panic_handler_explicit b/test/incremental/change_panic_handler_explicit new file mode 100644 index 0000000000..12258d7ce2 --- /dev/null +++ b/test/incremental/change_panic_handler_explicit @@ -0,0 +1,141 @@ +#target=x86_64-linux-selfhosted +#target=x86_64-linux-cbe +#target=x86_64-windows-cbe +#update=initial version +#file=main.zig +pub fn main() !u8 { + var a: u8 = undefined; + a = 255; + _ = a + 1; + return 1; +} +const no_panic = std.debug.no_panic; +pub const panic = struct { + pub const call = myPanic; + pub fn integerOverflow() noreturn { + @panic("integer overflow"); + } + pub const sentinelMismatch = no_panic.sentinelMismatch; + pub const unwrapError = no_panic.unwrapError; + pub const outOfBounds = no_panic.outOfBounds; + pub const startGreaterThanEnd = no_panic.startGreaterThanEnd; + pub const inactiveUnionField = no_panic.inactiveUnionField; + pub const reachedUnreachable = no_panic.reachedUnreachable; + pub const unwrapNull = no_panic.unwrapNull; + pub const castToNull = no_panic.castToNull; + pub const incorrectAlignment = no_panic.incorrectAlignment; + pub const invalidErrorCode = no_panic.invalidErrorCode; + pub const castTruncatedData = no_panic.castTruncatedData; + pub const negativeToUnsigned = no_panic.negativeToUnsigned; + pub const shlOverflow = no_panic.shlOverflow; + pub const shrOverflow = no_panic.shrOverflow; + pub const divideByZero = no_panic.divideByZero; + pub const exactDivisionRemainder = no_panic.exactDivisionRemainder; + pub const integerPartOutOfBounds = no_panic.integerPartOutOfBounds; + pub const corruptSwitch = no_panic.corruptSwitch; + pub const shiftRhsTooBig = no_panic.shiftRhsTooBig; + pub const invalidEnumValue = no_panic.invalidEnumValue; + pub const forLenMismatch = no_panic.forLenMismatch; + pub const memcpyLenMismatch = no_panic.memcpyLenMismatch; + pub const memcpyAlias = no_panic.memcpyAlias; + pub const noreturnReturned = no_panic.noreturnReturned; +}; +fn myPanic(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn { + std.io.getStdOut().writer().print("panic message: {s}\n", .{msg}) catch {}; + std.process.exit(0); +} +const std = @import("std"); +#expect_stdout="panic message: integer overflow\n" + +#update=change the panic handler body +#file=main.zig +pub fn main() !u8 { + var a: u8 = undefined; + a = 255; + _ = a + 1; + return 1; +} +const no_panic = std.debug.no_panic; +pub const panic = struct { + pub const call = myPanic; + pub fn integerOverflow() noreturn { + @panic("integer overflow"); + } + pub const sentinelMismatch = no_panic.sentinelMismatch; + pub const unwrapError = no_panic.unwrapError; + pub const outOfBounds = no_panic.outOfBounds; + pub const startGreaterThanEnd = no_panic.startGreaterThanEnd; + pub const inactiveUnionField = no_panic.inactiveUnionField; + pub const reachedUnreachable = no_panic.reachedUnreachable; + pub const unwrapNull = no_panic.unwrapNull; + pub const castToNull = no_panic.castToNull; + pub const incorrectAlignment = no_panic.incorrectAlignment; + pub const invalidErrorCode = no_panic.invalidErrorCode; + pub const castTruncatedData = no_panic.castTruncatedData; + pub const negativeToUnsigned = no_panic.negativeToUnsigned; + pub const shlOverflow = no_panic.shlOverflow; + pub const shrOverflow = no_panic.shrOverflow; + pub const divideByZero = no_panic.divideByZero; + pub const exactDivisionRemainder = no_panic.exactDivisionRemainder; + pub const integerPartOutOfBounds = no_panic.integerPartOutOfBounds; + pub const corruptSwitch = no_panic.corruptSwitch; + pub const shiftRhsTooBig = no_panic.shiftRhsTooBig; + pub const invalidEnumValue = no_panic.invalidEnumValue; + pub const forLenMismatch = no_panic.forLenMismatch; + pub const memcpyLenMismatch = no_panic.memcpyLenMismatch; + pub const memcpyAlias = no_panic.memcpyAlias; + pub const noreturnReturned = no_panic.noreturnReturned; +}; +fn myPanic(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn { + std.io.getStdOut().writer().print("new panic message: {s}\n", .{msg}) catch {}; + std.process.exit(0); +} +const std = @import("std"); +#expect_stdout="new panic message: integer overflow\n" + +#update=change the panic handler function value +#file=main.zig +pub fn main() !u8 { + var a: u8 = undefined; + a = 255; + _ = a + 1; + return 1; +} +const no_panic = std.debug.no_panic; +pub const panic = struct { + pub const call = myPanicNew; + pub fn integerOverflow() noreturn { + @panic("integer overflow"); + } + pub const sentinelMismatch = std.debug.no_panic.sentinelMismatch; + pub const unwrapError = std.debug.no_panic.unwrapError; + pub const outOfBounds = std.debug.no_panic.outOfBounds; + pub const startGreaterThanEnd = std.debug.no_panic.startGreaterThanEnd; + pub const inactiveUnionField = std.debug.no_panic.inactiveUnionField; + pub const messages = std.debug.no_panic.messages; + pub const reachedUnreachable = no_panic.reachedUnreachable; + pub const unwrapNull = no_panic.unwrapNull; + pub const castToNull = no_panic.castToNull; + pub const incorrectAlignment = no_panic.incorrectAlignment; + pub const invalidErrorCode = no_panic.invalidErrorCode; + pub const castTruncatedData = no_panic.castTruncatedData; + pub const negativeToUnsigned = no_panic.negativeToUnsigned; + pub const shlOverflow = no_panic.shlOverflow; + pub const shrOverflow = no_panic.shrOverflow; + pub const divideByZero = no_panic.divideByZero; + pub const exactDivisionRemainder = no_panic.exactDivisionRemainder; + pub const integerPartOutOfBounds = no_panic.integerPartOutOfBounds; + pub const corruptSwitch = no_panic.corruptSwitch; + pub const shiftRhsTooBig = no_panic.shiftRhsTooBig; + pub const invalidEnumValue = no_panic.invalidEnumValue; + pub const forLenMismatch = no_panic.forLenMismatch; + pub const memcpyLenMismatch = no_panic.memcpyLenMismatch; + pub const memcpyAlias = no_panic.memcpyAlias; + pub const noreturnReturned = no_panic.noreturnReturned; +}; +fn myPanicNew(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn { + std.io.getStdOut().writer().print("third panic message: {s}\n", .{msg}) catch {}; + std.process.exit(0); +} +const std = @import("std"); +#expect_stdout="third panic message: integer overflow\n" -- cgit v1.2.3 From 5a6666db5502079d9800ae3d41b790646238078e Mon Sep 17 00:00:00 2001 From: mlugg Date: Fri, 24 Jan 2025 04:20:55 +0000 Subject: all: update for `panic.unwrapError` and `panic.call` signature changes --- lib/std/builtin.zig | 7 ++- lib/std/debug.zig | 64 +++++++++++----------- lib/std/debug/no_panic.zig | 4 +- lib/std/debug/simple_panic.zig | 56 +++++++++---------- src/Sema.zig | 2 +- src/crash_report.zig | 4 +- .../compile_errors/bad_panic_call_signature.zig | 9 ++- test/incremental/change_panic_handler | 6 +- test/incremental/change_panic_handler_explicit | 6 +- test/src/Cases.zig | 1 - 10 files changed, 79 insertions(+), 80 deletions(-) (limited to 'test') diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index b2e973aea8..f860f7d891 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -1117,7 +1117,12 @@ pub const PanicFn = fn ([]const u8, ?*StackTrace, ?usize) noreturn; pub const panic: type = p: { if (@hasDecl(root, "panic")) { if (@TypeOf(root.panic) != type) { - break :p std.debug.FullPanic(root.panic); // Deprecated; make `panic` a namespace instead. + // Deprecated; make `panic` a namespace instead. + break :p std.debug.FullPanic(struct { + fn panic(msg: []const u8, ra: ?usize) noreturn { + root.panic(msg, @errorReturnTrace(), ra); + } + }.panic); } break :p root.panic; } diff --git a/lib/std/debug.zig b/lib/std/debug.zig index 0ff51a7339..7e68f16ee8 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -27,112 +27,112 @@ pub const no_panic = @import("debug/no_panic.zig"); /// A fully-featured panic handler namespace which lowers all panics to calls to `panicFn`. /// Safety panics will use formatted printing to provide a meaningful error message. /// The signature of `panicFn` should match that of `defaultPanic`. -pub fn FullPanic(comptime panicFn: fn ([]const u8, ?*std.builtin.StackTrace, ?usize) noreturn) type { +pub fn FullPanic(comptime panicFn: fn ([]const u8, ?usize) noreturn) type { return struct { pub const call = panicFn; pub fn sentinelMismatch(expected: anytype, found: @TypeOf(expected)) noreturn { @branchHint(.cold); - std.debug.panicExtra(null, @returnAddress(), "sentinel mismatch: expected {any}, found {any}", .{ + std.debug.panicExtra(@returnAddress(), "sentinel mismatch: expected {any}, found {any}", .{ expected, found, }); } - pub fn unwrapError(ert: ?*std.builtin.StackTrace, err: anyerror) noreturn { + pub fn unwrapError(err: anyerror) noreturn { @branchHint(.cold); - std.debug.panicExtra(ert, @returnAddress(), "attempt to unwrap error: {s}", .{@errorName(err)}); + std.debug.panicExtra(@returnAddress(), "attempt to unwrap error: {s}", .{@errorName(err)}); } pub fn outOfBounds(index: usize, len: usize) noreturn { @branchHint(.cold); - std.debug.panicExtra(null, @returnAddress(), "index out of bounds: index {d}, len {d}", .{ index, len }); + std.debug.panicExtra(@returnAddress(), "index out of bounds: index {d}, len {d}", .{ index, len }); } pub fn startGreaterThanEnd(start: usize, end: usize) noreturn { @branchHint(.cold); - std.debug.panicExtra(null, @returnAddress(), "start index {d} is larger than end index {d}", .{ start, end }); + std.debug.panicExtra(@returnAddress(), "start index {d} is larger than end index {d}", .{ start, end }); } pub fn inactiveUnionField(active: anytype, accessed: @TypeOf(active)) noreturn { @branchHint(.cold); - std.debug.panicExtra(null, @returnAddress(), "access of union field '{s}' while field '{s}' is active", .{ + std.debug.panicExtra(@returnAddress(), "access of union field '{s}' while field '{s}' is active", .{ @tagName(accessed), @tagName(active), }); } pub fn reachedUnreachable() noreturn { @branchHint(.cold); - call("reached unreachable code", null, @returnAddress()); + call("reached unreachable code", @returnAddress()); } pub fn unwrapNull() noreturn { @branchHint(.cold); - call("attempt to use null value", null, @returnAddress()); + call("attempt to use null value", @returnAddress()); } pub fn castToNull() noreturn { @branchHint(.cold); - call("cast causes pointer to be null", null, @returnAddress()); + call("cast causes pointer to be null", @returnAddress()); } pub fn incorrectAlignment() noreturn { @branchHint(.cold); - call("incorrect alignment", null, @returnAddress()); + call("incorrect alignment", @returnAddress()); } pub fn invalidErrorCode() noreturn { @branchHint(.cold); - call("invalid error code", null, @returnAddress()); + call("invalid error code", @returnAddress()); } pub fn castTruncatedData() noreturn { @branchHint(.cold); - call("integer cast truncated bits", null, @returnAddress()); + call("integer cast truncated bits", @returnAddress()); } pub fn negativeToUnsigned() noreturn { @branchHint(.cold); - call("attempt to cast negative value to unsigned integer", null, @returnAddress()); + call("attempt to cast negative value to unsigned integer", @returnAddress()); } pub fn integerOverflow() noreturn { @branchHint(.cold); - call("integer overflow", null, @returnAddress()); + call("integer overflow", @returnAddress()); } pub fn shlOverflow() noreturn { @branchHint(.cold); - call("left shift overflowed bits", null, @returnAddress()); + call("left shift overflowed bits", @returnAddress()); } pub fn shrOverflow() noreturn { @branchHint(.cold); - call("right shift overflowed bits", null, @returnAddress()); + call("right shift overflowed bits", @returnAddress()); } pub fn divideByZero() noreturn { @branchHint(.cold); - call("division by zero", null, @returnAddress()); + call("division by zero", @returnAddress()); } pub fn exactDivisionRemainder() noreturn { @branchHint(.cold); - call("exact division produced remainder", null, @returnAddress()); + call("exact division produced remainder", @returnAddress()); } pub fn integerPartOutOfBounds() noreturn { @branchHint(.cold); - call("integer part of floating point value out of bounds", null, @returnAddress()); + call("integer part of floating point value out of bounds", @returnAddress()); } pub fn corruptSwitch() noreturn { @branchHint(.cold); - call("switch on corrupt value", null, @returnAddress()); + call("switch on corrupt value", @returnAddress()); } pub fn shiftRhsTooBig() noreturn { @branchHint(.cold); - call("shift amount is greater than the type size", null, @returnAddress()); + call("shift amount is greater than the type size", @returnAddress()); } pub fn invalidEnumValue() noreturn { @branchHint(.cold); - call("invalid enum value", null, @returnAddress()); + call("invalid enum value", @returnAddress()); } pub fn forLenMismatch() noreturn { @branchHint(.cold); - call("for loop over objects with non-equal lengths", null, @returnAddress()); + call("for loop over objects with non-equal lengths", @returnAddress()); } pub fn memcpyLenMismatch() noreturn { @branchHint(.cold); - call("@memcpy arguments have non-equal lengths", null, @returnAddress()); + call("@memcpy arguments have non-equal lengths", @returnAddress()); } pub fn memcpyAlias() noreturn { @branchHint(.cold); - call("@memcpy arguments alias", null, @returnAddress()); + call("@memcpy arguments alias", @returnAddress()); } pub fn noreturnReturned() noreturn { @branchHint(.cold); - call("'noreturn' function returned", null, @returnAddress()); + call("'noreturn' function returned", @returnAddress()); } /// To be deleted after zig1.wasm update. @@ -531,13 +531,12 @@ pub fn assertReadable(slice: []const volatile u8) void { /// Equivalent to `@panic` but with a formatted message. pub fn panic(comptime format: []const u8, args: anytype) noreturn { @branchHint(.cold); - panicExtra(@errorReturnTrace(), @returnAddress(), format, args); + panicExtra(@returnAddress(), format, args); } /// Equivalent to `@panic` but with a formatted message, and with an explicitly -/// provided `@errorReturnTrace` and return address. +/// provided return address. pub fn panicExtra( - trace: ?*std.builtin.StackTrace, ret_addr: ?usize, comptime format: []const u8, args: anytype, @@ -556,7 +555,7 @@ pub fn panicExtra( break :blk &buf; }, }; - std.builtin.panic.call(msg, trace, ret_addr); + std.builtin.panic.call(msg, ret_addr); } /// Non-zero whenever the program triggered a panic. @@ -570,7 +569,6 @@ threadlocal var panic_stage: usize = 0; /// Dumps a stack trace to standard error, then aborts. pub fn defaultPanic( msg: []const u8, - error_return_trace: ?*const std.builtin.StackTrace, first_trace_addr: ?usize, ) noreturn { @branchHint(.cold); @@ -657,7 +655,7 @@ pub fn defaultPanic( } stderr.print("{s}\n", .{msg}) catch posix.abort(); - if (error_return_trace) |t| dumpStackTrace(t.*); + if (@errorReturnTrace()) |t| dumpStackTrace(t.*); dumpCurrentStackTrace(first_trace_addr orelse @returnAddress()); } diff --git a/lib/std/debug/no_panic.zig b/lib/std/debug/no_panic.zig index edd61ab974..690453b62a 100644 --- a/lib/std/debug/no_panic.zig +++ b/lib/std/debug/no_panic.zig @@ -5,7 +5,7 @@ const std = @import("../std.zig"); -pub fn call(_: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn { +pub fn call(_: []const u8, _: ?usize) noreturn { @branchHint(.cold); @trap(); } @@ -15,7 +15,7 @@ pub fn sentinelMismatch(_: anytype, _: anytype) noreturn { @trap(); } -pub fn unwrapError(_: ?*std.builtin.StackTrace, _: anyerror) noreturn { +pub fn unwrapError(_: anyerror) noreturn { @branchHint(.cold); @trap(); } diff --git a/lib/std/debug/simple_panic.zig b/lib/std/debug/simple_panic.zig index 6a6c12aa2d..aeeba5b1d2 100644 --- a/lib/std/debug/simple_panic.zig +++ b/lib/std/debug/simple_panic.zig @@ -11,9 +11,8 @@ const std = @import("../std.zig"); /// Prints the message to stderr without a newline and then traps. /// /// Explicit calls to `@panic` lower to calling this function. -pub fn call(msg: []const u8, ert: ?*std.builtin.StackTrace, ra: ?usize) noreturn { +pub fn call(msg: []const u8, ra: ?usize) noreturn { @branchHint(.cold); - _ = ert; _ = ra; std.debug.lockStdErr(); const stderr = std.io.getStdErr(); @@ -23,110 +22,109 @@ pub fn call(msg: []const u8, ert: ?*std.builtin.StackTrace, ra: ?usize) noreturn pub fn sentinelMismatch(expected: anytype, found: @TypeOf(expected)) noreturn { _ = found; - call("sentinel mismatch", null, null); + call("sentinel mismatch", null); } -pub fn unwrapError(ert: ?*std.builtin.StackTrace, err: anyerror) noreturn { - _ = ert; +pub fn unwrapError(err: anyerror) noreturn { _ = &err; - call("attempt to unwrap error", null, null); + call("attempt to unwrap error", null); } pub fn outOfBounds(index: usize, len: usize) noreturn { _ = index; _ = len; - call("index out of bounds", null, null); + call("index out of bounds", null); } pub fn startGreaterThanEnd(start: usize, end: usize) noreturn { _ = start; _ = end; - call("start index is larger than end index", null, null); + call("start index is larger than end index", null); } pub fn inactiveUnionField(active: anytype, accessed: @TypeOf(active)) noreturn { _ = accessed; - call("access of inactive union field", null, null); + call("access of inactive union field", null); } pub fn reachedUnreachable() noreturn { - call("reached unreachable code", null, null); + call("reached unreachable code", null); } pub fn unwrapNull() noreturn { - call("attempt to use null value", null, null); + call("attempt to use null value", null); } pub fn castToNull() noreturn { - call("cast causes pointer to be null", null, null); + call("cast causes pointer to be null", null); } pub fn incorrectAlignment() noreturn { - call("incorrect alignment", null, null); + call("incorrect alignment", null); } pub fn invalidErrorCode() noreturn { - call("invalid error code", null, null); + call("invalid error code", null); } pub fn castTruncatedData() noreturn { - call("integer cast truncated bits", null, null); + call("integer cast truncated bits", null); } pub fn negativeToUnsigned() noreturn { - call("attempt to cast negative value to unsigned integer", null, null); + call("attempt to cast negative value to unsigned integer", null); } pub fn integerOverflow() noreturn { - call("integer overflow", null, null); + call("integer overflow", null); } pub fn shlOverflow() noreturn { - call("left shift overflowed bits", null, null); + call("left shift overflowed bits", null); } pub fn shrOverflow() noreturn { - call("right shift overflowed bits", null, null); + call("right shift overflowed bits", null); } pub fn divideByZero() noreturn { - call("division by zero", null, null); + call("division by zero", null); } pub fn exactDivisionRemainder() noreturn { - call("exact division produced remainder", null, null); + call("exact division produced remainder", null); } pub fn integerPartOutOfBounds() noreturn { - call("integer part of floating point value out of bounds", null, null); + call("integer part of floating point value out of bounds", null); } pub fn corruptSwitch() noreturn { - call("switch on corrupt value", null, null); + call("switch on corrupt value", null); } pub fn shiftRhsTooBig() noreturn { - call("shift amount is greater than the type size", null, null); + call("shift amount is greater than the type size", null); } pub fn invalidEnumValue() noreturn { - call("invalid enum value", null, null); + call("invalid enum value", null); } pub fn forLenMismatch() noreturn { - call("for loop over objects with non-equal lengths", null, null); + call("for loop over objects with non-equal lengths", null); } pub fn memcpyLenMismatch() noreturn { - call("@memcpy arguments have non-equal lengths", null, null); + call("@memcpy arguments have non-equal lengths", null); } pub fn memcpyAlias() noreturn { - call("@memcpy arguments alias", null, null); + call("@memcpy arguments alias", null); } pub fn noreturnReturned() noreturn { - call("'noreturn' function returned", null, null); + call("'noreturn' function returned", null); } /// To be deleted after zig1.wasm update. diff --git a/src/Sema.zig b/src/Sema.zig index f2e52c2170..2c2841f27d 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -2584,7 +2584,7 @@ pub fn failWithOwnedErrorMsg(sema: *Sema, block: ?*Block, err_msg: *Zcu.ErrorMsg std.debug.print("compile error during Sema:\n", .{}); var error_bundle = wip_errors.toOwnedBundle("") catch @panic("out of memory"); error_bundle.renderToStdErr(.{ .ttyconf = .no_color }); - crash_report.compilerPanic("unexpected compile error occurred", null, null); + crash_report.compilerPanic("unexpected compile error occurred", null); } if (block) |start_block| { diff --git a/src/crash_report.zig b/src/crash_report.zig index 26096574d3..431381fce4 100644 --- a/src/crash_report.zig +++ b/src/crash_report.zig @@ -158,12 +158,12 @@ fn writeFilePath(file: *Zcu.File, writer: anytype) !void { try writer.writeAll(file.sub_file_path); } -pub fn compilerPanic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, maybe_ret_addr: ?usize) noreturn { +pub fn compilerPanic(msg: []const u8, maybe_ret_addr: ?usize) noreturn { @branchHint(.cold); PanicSwitch.preDispatch(); const ret_addr = maybe_ret_addr orelse @returnAddress(); const stack_ctx: StackContext = .{ .current = .{ .ret_addr = ret_addr } }; - PanicSwitch.dispatch(error_return_trace, stack_ctx, msg); + PanicSwitch.dispatch(@errorReturnTrace(), stack_ctx, msg); } /// Attaches a global SIGSEGV handler diff --git a/test/cases/compile_errors/bad_panic_call_signature.zig b/test/cases/compile_errors/bad_panic_call_signature.zig index 3536397241..b383ccf651 100644 --- a/test/cases/compile_errors/bad_panic_call_signature.zig +++ b/test/cases/compile_errors/bad_panic_call_signature.zig @@ -1,9 +1,8 @@ const simple_panic = std.debug.simple_panic; pub const panic = struct { - pub fn call(msg: []const u8, bad1: usize, bad2: void) noreturn { + pub fn call(msg: []const u8, bad: usize) noreturn { _ = msg; - _ = bad1; - _ = bad2; + _ = bad; @trap(); } pub const sentinelMismatch = simple_panic.sentinelMismatch; @@ -42,5 +41,5 @@ const std = @import("std"); // error // -// :3:9: error: expected type 'fn ([]const u8, ?*builtin.StackTrace, ?usize) noreturn', found 'fn ([]const u8, usize, void) noreturn' -// :3:9: note: parameter 1 'usize' cannot cast into '?*builtin.StackTrace' +// :3:9: error: expected type 'fn ([]const u8, ?usize) noreturn', found 'fn ([]const u8, usize) noreturn' +// :3:9: note: parameter 1 'usize' cannot cast into '?usize' diff --git a/test/incremental/change_panic_handler b/test/incremental/change_panic_handler index 3e0674a4d9..34a1f32dab 100644 --- a/test/incremental/change_panic_handler +++ b/test/incremental/change_panic_handler @@ -10,7 +10,7 @@ pub fn main() !u8 { return 1; } pub const panic = std.debug.FullPanic(myPanic); -fn myPanic(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn { +fn myPanic(msg: []const u8, _: ?usize) noreturn { std.io.getStdOut().writer().print("panic message: {s}\n", .{msg}) catch {}; std.process.exit(0); } @@ -26,7 +26,7 @@ pub fn main() !u8 { return 1; } pub const panic = std.debug.FullPanic(myPanic); -fn myPanic(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn { +fn myPanic(msg: []const u8, _: ?usize) noreturn { std.io.getStdOut().writer().print("new panic message: {s}\n", .{msg}) catch {}; std.process.exit(0); } @@ -42,7 +42,7 @@ pub fn main() !u8 { return 1; } pub const panic = std.debug.FullPanic(myPanicNew); -fn myPanicNew(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn { +fn myPanicNew(msg: []const u8, _: ?usize) noreturn { std.io.getStdOut().writer().print("third panic message: {s}\n", .{msg}) catch {}; std.process.exit(0); } diff --git a/test/incremental/change_panic_handler_explicit b/test/incremental/change_panic_handler_explicit index 12258d7ce2..5739c46c1b 100644 --- a/test/incremental/change_panic_handler_explicit +++ b/test/incremental/change_panic_handler_explicit @@ -40,7 +40,7 @@ pub const panic = struct { pub const memcpyAlias = no_panic.memcpyAlias; pub const noreturnReturned = no_panic.noreturnReturned; }; -fn myPanic(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn { +fn myPanic(msg: []const u8, _: ?usize) noreturn { std.io.getStdOut().writer().print("panic message: {s}\n", .{msg}) catch {}; std.process.exit(0); } @@ -86,7 +86,7 @@ pub const panic = struct { pub const memcpyAlias = no_panic.memcpyAlias; pub const noreturnReturned = no_panic.noreturnReturned; }; -fn myPanic(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn { +fn myPanic(msg: []const u8, _: ?usize) noreturn { std.io.getStdOut().writer().print("new panic message: {s}\n", .{msg}) catch {}; std.process.exit(0); } @@ -133,7 +133,7 @@ pub const panic = struct { pub const memcpyAlias = no_panic.memcpyAlias; pub const noreturnReturned = no_panic.noreturnReturned; }; -fn myPanicNew(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn { +fn myPanicNew(msg: []const u8, _: ?usize) noreturn { std.io.getStdOut().writer().print("third panic message: {s}\n", .{msg}) catch {}; std.process.exit(0); } diff --git a/test/src/Cases.zig b/test/src/Cases.zig index c49576b2f7..b57a476e89 100644 --- a/test/src/Cases.zig +++ b/test/src/Cases.zig @@ -358,7 +358,6 @@ pub fn addFromDir(ctx: *Cases, dir: std.fs.Dir, b: *std.Build) void { var current_file: []const u8 = "none"; ctx.addFromDirInner(dir, ¤t_file, b) catch |err| { std.debug.panicExtra( - @errorReturnTrace(), @returnAddress(), "test harness failed to process file '{s}': {s}\n", .{ current_file, @errorName(err) }, -- cgit v1.2.3