authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2022-11-23 16:36:20+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-11-23 14:57:39-05:00
log258bee41bf58c9001ceaefc974fa594a26ac0fc5
tree52e858faeec541aca8323315d0a681b1cb5cab66
parent81d2135ca6ebd71b8c121a19957c8fbf7f87125b

Get panic messages from builtin instead of creating anon decls

The TODO comment in safetyPanic mentions introducing the concept of reference-counted decls. That sounds like Zig current semantics for normal declarations. By placing the panic messages in builtin there is no need for another concept in the compiler.

2 files changed, 29 insertions(+), 33 deletions(-)

lib/std/builtin.zig+20
...@@ -869,6 +869,26 @@ pub noinline fn returnError(st: *StackTrace) void {...@@ -869,6 +869,26 @@ pub noinline fn returnError(st: *StackTrace) void {
869 addErrRetTraceAddr(st, @returnAddress());869 addErrRetTraceAddr(st, @returnAddress());
870}870}
871871
872pub const panic_messages = struct {
873 pub const unreach = "reached unreachable code";
874 pub const unwrap_null = "attempt to use null value";
875 pub const cast_to_null = "cast causes pointer to be null";
876 pub const incorrect_alignment = "incorrect alignment";
877 pub const invalid_error_code = "invalid error code";
878 pub const cast_truncated_data = "integer cast truncated bits";
879 pub const negative_to_unsigned = "attempt to cast negative value to unsigned integer";
880 pub const integer_overflow = "integer overflow";
881 pub const shl_overflow = "left shift overflowed bits";
882 pub const shr_overflow = "right shift overflowed bits";
883 pub const divide_by_zero = "division by zero";
884 pub const exact_division_remainder = "exact division produced remainder";
885 pub const inactive_union_field = "access of inactive union field";
886 pub const integer_part_out_of_bounds = "integer part of floating point value out of bounds";
887 pub const corrupt_switch = "switch on corrupt value";
888 pub const shift_rhs_too_big = "shift amount is greater than the type size";
889 pub const invalid_enum_value = "invalid enum value";
890};
891
872pub inline fn addErrRetTraceAddr(st: *StackTrace, addr: usize) void {892pub inline fn addErrRetTraceAddr(st: *StackTrace, addr: usize) void {
873 if (st.index < st.instruction_addresses.len)893 if (st.index < st.instruction_addresses.len)
874 st.instruction_addresses[st.index] = addr;894 st.instruction_addresses[st.index] = addr;
src/Sema.zig+9-33
...@@ -22349,40 +22349,16 @@ fn safetyPanic(...@@ -22349,40 +22349,16 @@ fn safetyPanic(
22349 src: LazySrcLoc,22349 src: LazySrcLoc,
22350 panic_id: PanicId,22350 panic_id: PanicId,
22351) CompileError!Zir.Inst.Index {22351) CompileError!Zir.Inst.Index {
22352 const msg = switch (panic_id) {22352 const panic_messages_ty = try sema.getBuiltinType("panic_messages");
22353 .unreach => "reached unreachable code",22353 const msg_decl_index = (try sema.namespaceLookup(
22354 .unwrap_null => "attempt to use null value",22354 block,
22355 .cast_to_null => "cast causes pointer to be null",22355 src,
22356 .incorrect_alignment => "incorrect alignment",22356 panic_messages_ty.getNamespace().?,
22357 .invalid_error_code => "invalid error code",22357 @tagName(panic_id),
22358 .cast_truncated_data => "integer cast truncated bits",22358 )).?;
22359 .negative_to_unsigned => "attempt to cast negative value to unsigned integer",
22360 .integer_overflow => "integer overflow",
22361 .shl_overflow => "left shift overflowed bits",
22362 .shr_overflow => "right shift overflowed bits",
22363 .divide_by_zero => "division by zero",
22364 .exact_division_remainder => "exact division produced remainder",
22365 .inactive_union_field => "access of inactive union field",
22366 .integer_part_out_of_bounds => "integer part of floating point value out of bounds",
22367 .corrupt_switch => "switch on corrupt value",
22368 .shift_rhs_too_big => "shift amount is greater than the type size",
22369 .invalid_enum_value => "invalid enum value",
22370 };
22371
22372 const msg_inst = msg_inst: {
22373 // TODO instead of making a new decl for every panic in the entire compilation,
22374 // introduce the concept of a reference-counted decl for these
22375 var anon_decl = try block.startAnonDecl();
22376 defer anon_decl.deinit();
22377 break :msg_inst try sema.analyzeDeclRef(try anon_decl.finish(
22378 try Type.Tag.array_u8.create(anon_decl.arena(), msg.len),
22379 try Value.Tag.bytes.create(anon_decl.arena(), msg),
22380 0, // default alignment
22381 ));
22382 };
2238322359
22384 const casted_msg_inst = try sema.coerce(block, Type.initTag(.const_slice_u8), msg_inst, src);22360 const msg_inst = try sema.analyzeDeclVal(block, src, msg_decl_index);
22385 return sema.panicWithMsg(block, src, casted_msg_inst);22361 return sema.panicWithMsg(block, src, msg_inst);
22386}22362}
2238722363
22388fn emitBackwardBranch(sema: *Sema, block: *Block, src: LazySrcLoc) !void {22364fn emitBackwardBranch(sema: *Sema, block: *Block, src: LazySrcLoc) !void {