diff options
| author | Jimmi Holst Christensen <jhc@dismail.de> | 2022-11-23 16:36:20 +0100 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-11-23 14:57:39 -0500 |
| commit | 258bee41bf58c9001ceaefc974fa594a26ac0fc5 (patch) | |
| tree | 52e858faeec541aca8323315d0a681b1cb5cab66 | |
| parent | 81d2135ca6ebd71b8c121a19957c8fbf7f87125b (diff) | |
| download | zig-258bee41bf58c9001ceaefc974fa594a26ac0fc5.tar.gz zig-258bee41bf58c9001ceaefc974fa594a26ac0fc5.zip | |
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.
| -rw-r--r-- | lib/std/builtin.zig | 20 | ||||
| -rw-r--r-- | src/Sema.zig | 42 |
2 files changed, 29 insertions, 33 deletions
diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index 33604dfee7..d777bb8c9e 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -869,6 +869,26 @@ pub noinline fn returnError(st: *StackTrace) void { addErrRetTraceAddr(st, @returnAddress()); } +pub const panic_messages = struct { + pub const unreach = "reached unreachable code"; + pub const unwrap_null = "attempt to use null value"; + pub const cast_to_null = "cast causes pointer to be null"; + pub const incorrect_alignment = "incorrect alignment"; + pub const invalid_error_code = "invalid error code"; + pub const cast_truncated_data = "integer cast truncated bits"; + pub const negative_to_unsigned = "attempt to cast negative value to unsigned integer"; + pub const integer_overflow = "integer overflow"; + pub const shl_overflow = "left shift overflowed bits"; + pub const shr_overflow = "right shift overflowed bits"; + pub const divide_by_zero = "division by zero"; + pub const exact_division_remainder = "exact division produced remainder"; + pub const inactive_union_field = "access of inactive union field"; + pub const integer_part_out_of_bounds = "integer part of floating point value out of bounds"; + pub const corrupt_switch = "switch on corrupt value"; + pub const shift_rhs_too_big = "shift amount is greater than the type size"; + pub const invalid_enum_value = "invalid enum value"; +}; + pub inline fn addErrRetTraceAddr(st: *StackTrace, addr: usize) void { if (st.index < st.instruction_addresses.len) st.instruction_addresses[st.index] = addr; diff --git a/src/Sema.zig b/src/Sema.zig index a1c7fa7b91..ec254c50c7 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -22349,40 +22349,16 @@ fn safetyPanic( src: LazySrcLoc, panic_id: PanicId, ) CompileError!Zir.Inst.Index { - const msg = switch (panic_id) { - .unreach => "reached unreachable code", - .unwrap_null => "attempt to use null value", - .cast_to_null => "cast causes pointer to be null", - .incorrect_alignment => "incorrect alignment", - .invalid_error_code => "invalid error code", - .cast_truncated_data => "integer cast truncated bits", - .negative_to_unsigned => "attempt to cast negative value to unsigned integer", - .integer_overflow => "integer overflow", - .shl_overflow => "left shift overflowed bits", - .shr_overflow => "right shift overflowed bits", - .divide_by_zero => "division by zero", - .exact_division_remainder => "exact division produced remainder", - .inactive_union_field => "access of inactive union field", - .integer_part_out_of_bounds => "integer part of floating point value out of bounds", - .corrupt_switch => "switch on corrupt value", - .shift_rhs_too_big => "shift amount is greater than the type size", - .invalid_enum_value => "invalid enum value", - }; - - const msg_inst = msg_inst: { - // TODO instead of making a new decl for every panic in the entire compilation, - // introduce the concept of a reference-counted decl for these - var anon_decl = try block.startAnonDecl(); - defer anon_decl.deinit(); - break :msg_inst try sema.analyzeDeclRef(try anon_decl.finish( - try Type.Tag.array_u8.create(anon_decl.arena(), msg.len), - try Value.Tag.bytes.create(anon_decl.arena(), msg), - 0, // default alignment - )); - }; + const panic_messages_ty = try sema.getBuiltinType("panic_messages"); + const msg_decl_index = (try sema.namespaceLookup( + block, + src, + panic_messages_ty.getNamespace().?, + @tagName(panic_id), + )).?; - const casted_msg_inst = try sema.coerce(block, Type.initTag(.const_slice_u8), msg_inst, src); - return sema.panicWithMsg(block, src, casted_msg_inst); + const msg_inst = try sema.analyzeDeclVal(block, src, msg_decl_index); + return sema.panicWithMsg(block, src, msg_inst); } fn emitBackwardBranch(sema: *Sema, block: *Block, src: LazySrcLoc) !void { |
