Commit 258bee41bf

Jimmi Holst Christensen <jhc@dismail.de>
2022-11-23 16:36:20
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.
1 parent 81d2135
Changed files (2)
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;
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 {