authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-29 15:57:34+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-29 21:44:08+02:00
loge60db701d13a50798222bbef2d4560245f975671
treefb3f0e65bb9fb8937771b230775c669e70efaaac
parented734299269d50083db27d68598ced7df42b8631

Sema: add option to disable formatted panics

Closes #13174

4 files changed, 64 insertions(+), 29 deletions(-)

lib/std/builtin.zig+4
......@@ -891,6 +891,10 @@ pub const panic_messages = struct {
891891 pub const corrupt_switch = "switch on corrupt value";
892892 pub const shift_rhs_too_big = "shift amount is greater than the type size";
893893 pub const invalid_enum_value = "invalid enum value";
894 pub const sentinel_mismatch = "sentinel mismatch";
895 pub const unwrap_error = "attempt to unwrap error";
896 pub const index_out_of_bounds = "index out of bounds";
897 pub const start_index_greater_than_end = "start index is larger than end index";
894898};
895899
896900pub noinline fn returnError(st: *StackTrace) void {
src/Compilation.zig+5
......@@ -101,6 +101,7 @@ debug_compile_errors: bool,
101101job_queued_compiler_rt_lib: bool = false,
102102job_queued_compiler_rt_obj: bool = false,
103103alloc_failure_occurred: bool = false,
104formatted_panics: bool = false,
104105
105106c_source_files: []const CSourceFile,
106107clang_argv: []const []const u8,
......@@ -937,6 +938,7 @@ pub const InitOptions = struct {
937938 use_stage1: ?bool = null,
938939 single_threaded: ?bool = null,
939940 strip: ?bool = null,
941 formatted_panics: ?bool = null,
940942 rdynamic: bool = false,
941943 function_sections: bool = false,
942944 no_builtin: bool = false,
......@@ -1457,6 +1459,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
14571459 .Debug => @as(u8, 0),
14581460 else => @as(u8, 3),
14591461 };
1462 const formatted_panics = options.formatted_panics orelse (options.optimize_mode == .Debug);
14601463
14611464 // We put everything into the cache hash that *cannot be modified
14621465 // during an incremental update*. For example, one cannot change the
......@@ -1551,6 +1554,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
15511554 hash.addOptionalBytes(options.test_name_prefix);
15521555 hash.add(options.skip_linker_dependencies);
15531556 hash.add(options.parent_compilation_link_libc);
1557 hash.add(formatted_panics);
15541558
15551559 // In the case of incremental cache mode, this `zig_cache_artifact_directory`
15561560 // is computed based on a hash of non-linker inputs, and it is where all
......@@ -1957,6 +1961,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
19571961 .owned_link_dir = owned_link_dir,
19581962 .color = options.color,
19591963 .reference_trace = options.reference_trace,
1964 .formatted_panics = formatted_panics,
19601965 .time_report = options.time_report,
19611966 .stack_report = options.stack_report,
19621967 .unwind_tables = unwind_tables,
src/Sema.zig+47-29
......@@ -667,9 +667,9 @@ pub const Block = struct {
667667 return result_index;
668668 }
669669
670 fn addUnreachable(block: *Block, src: LazySrcLoc, safety_check: bool) !void {
670 fn addUnreachable(block: *Block, safety_check: bool) !void {
671671 if (safety_check and block.wantSafety()) {
672 _ = try block.sema.safetyPanic(block, src, .unreach);
672 try block.sema.safetyPanic(block, .unreach);
673673 } else {
674674 _ = try block.addNoOp(.unreach);
675675 }
......@@ -5003,7 +5003,8 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index, force_comptime: bo
50035003 if (block.is_comptime or force_comptime) {
50045004 return sema.fail(block, src, "encountered @panic at comptime", .{});
50055005 }
5006 return sema.panicWithMsg(block, src, msg_inst);
5006 try sema.panicWithMsg(block, src, msg_inst);
5007 return always_noreturn;
50075008}
50085009
50095010fn zirLoop(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -7962,7 +7963,7 @@ fn analyzeErrUnionPayload(
79627963 if (safety_check and block.wantSafety() and
79637964 !err_union_ty.errorUnionSet().errorSetIsEmpty())
79647965 {
7965 try sema.panicUnwrapError(block, src, operand, .unwrap_errunion_err, .is_non_err);
7966 try sema.panicUnwrapError(block, operand, .unwrap_errunion_err, .is_non_err);
79667967 }
79677968
79687969 return block.addTyOp(.unwrap_errunion_payload, payload_ty, operand);
......@@ -8047,7 +8048,7 @@ fn analyzeErrUnionPayloadPtr(
80478048 if (safety_check and block.wantSafety() and
80488049 !err_union_ty.errorUnionSet().errorSetIsEmpty())
80498050 {
8050 try sema.panicUnwrapError(block, src, operand, .unwrap_errunion_err_ptr, .is_non_err_ptr);
8051 try sema.panicUnwrapError(block, operand, .unwrap_errunion_err_ptr, .is_non_err_ptr);
80518052 }
80528053
80538054 const air_tag: Air.Inst.Tag = if (initializing)
......@@ -9542,7 +9543,7 @@ fn zirSwitchCapture(
95429543 .ErrorSet => if (block.switch_else_err_ty) |some| {
95439544 return sema.bitCast(block, some, operand, operand_src);
95449545 } else {
9545 try block.addUnreachable(operand_src, false);
9546 try block.addUnreachable(false);
95469547 return Air.Inst.Ref.unreachable_value;
95479548 },
95489549 else => return operand,
......@@ -10975,7 +10976,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1097510976 // that it is unreachable.
1097610977 if (case_block.wantSafety()) {
1097710978 try sema.zirDbgStmt(&case_block, cond_dbg_node_index);
10978 _ = try sema.safetyPanic(&case_block, src, .corrupt_switch);
10979 try sema.safetyPanic(&case_block, .corrupt_switch);
1097910980 } else {
1098010981 _ = try case_block.addNoOp(.unreach);
1098110982 }
......@@ -11304,6 +11305,11 @@ fn maybeErrorUnwrap(sema: *Sema, block: *Block, body: []const Zir.Inst.Index, op
1130411305 const inst_data = sema.code.instructions.items(.data)[inst].@"unreachable";
1130511306 const src = inst_data.src();
1130611307
11308 if (!sema.mod.comp.formatted_panics) {
11309 try sema.safetyPanic(block, .unwrap_error);
11310 return true;
11311 }
11312
1130711313 const panic_fn = try sema.getBuiltin("panicUnwrapError");
1130811314 const err_return_trace = try sema.getErrorReturnTrace(block);
1130911315 const args: [2]Air.Inst.Ref = .{ err_return_trace, operand };
......@@ -16513,7 +16519,7 @@ fn zirUnreachable(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1651316519 return sema.fail(block, src, "reached unreachable code", .{});
1651416520 }
1651516521 // TODO Add compile error for @optimizeFor occurring too late in a scope.
16516 try block.addUnreachable(src, true);
16522 try block.addUnreachable(true);
1651716523 return always_noreturn;
1651816524}
1651916525
......@@ -22128,6 +22134,10 @@ pub const PanicId = enum {
2212822134 corrupt_switch,
2212922135 shift_rhs_too_big,
2213022136 invalid_enum_value,
22137 sentinel_mismatch,
22138 unwrap_error,
22139 index_out_of_bounds,
22140 start_index_greater_than_end,
2213122141};
2213222142
2213322143fn addSafetyCheck(
......@@ -22152,12 +22162,7 @@ fn addSafetyCheck(
2215222162
2215322163 defer fail_block.instructions.deinit(gpa);
2215422164
22155 // This function doesn't actually need a src location but if
22156 // the panic function interface ever changes passing `.unneeded` here
22157 // will cause confusing panics.
22158 const src = sema.src;
22159 _ = try sema.safetyPanic(&fail_block, src, panic_id);
22160
22165 try sema.safetyPanic(&fail_block, panic_id);
2216122166 try sema.addSafetyCheckExtra(parent_block, ok, &fail_block);
2216222167}
2216322168
......@@ -22221,7 +22226,7 @@ fn panicWithMsg(
2222122226 block: *Block,
2222222227 src: LazySrcLoc,
2222322228 msg_inst: Air.Inst.Ref,
22224) !Zir.Inst.Index {
22229) !void {
2222522230 const mod = sema.mod;
2222622231 const arena = sema.arena;
2222722232
......@@ -22232,7 +22237,7 @@ fn panicWithMsg(
2223222237 // TODO implement this feature in all the backends and then delete this branch
2223322238 _ = try block.addNoOp(.breakpoint);
2223422239 _ = try block.addNoOp(.unreach);
22235 return always_noreturn;
22240 return;
2223622241 }
2223722242 const panic_fn = try sema.getBuiltin("panic");
2223822243 const unresolved_stack_trace_ty = try sema.getBuiltinType("StackTrace");
......@@ -22248,19 +22253,20 @@ fn panicWithMsg(
2224822253 );
2224922254 const args: [3]Air.Inst.Ref = .{ msg_inst, null_stack_trace, .null_value };
2225022255 _ = try sema.analyzeCall(block, panic_fn, src, src, .auto, false, &args, null);
22251 return always_noreturn;
2225222256}
2225322257
2225422258fn panicUnwrapError(
2225522259 sema: *Sema,
2225622260 parent_block: *Block,
22257 src: LazySrcLoc,
2225822261 operand: Air.Inst.Ref,
2225922262 unwrap_err_tag: Air.Inst.Tag,
2226022263 is_non_err_tag: Air.Inst.Tag,
2226122264) !void {
2226222265 assert(!parent_block.is_comptime);
2226322266 const ok = try parent_block.addUnOp(is_non_err_tag, operand);
22267 if (!sema.mod.comp.formatted_panics) {
22268 return sema.addSafetyCheck(parent_block, ok, .unwrap_error);
22269 }
2226422270 const gpa = sema.gpa;
2226522271
2226622272 var fail_block: Block = .{
......@@ -22289,7 +22295,7 @@ fn panicUnwrapError(
2228922295 const err = try fail_block.addTyOp(unwrap_err_tag, Type.anyerror, operand);
2229022296 const err_return_trace = try sema.getErrorReturnTrace(&fail_block);
2229122297 const args: [2]Air.Inst.Ref = .{ err_return_trace, err };
22292 _ = try sema.analyzeCall(&fail_block, panic_fn, src, src, .auto, false, &args, null);
22298 _ = try sema.analyzeCall(&fail_block, panic_fn, sema.src, sema.src, .auto, false, &args, null);
2229322299 }
2229422300 }
2229522301 try sema.addSafetyCheckExtra(parent_block, ok, &fail_block);
......@@ -22304,7 +22310,10 @@ fn panicIndexOutOfBounds(
2230422310) !void {
2230522311 assert(!parent_block.is_comptime);
2230622312 const ok = try parent_block.addBinOp(cmp_op, index, len);
22307 try sema.safetyPanicFormatted(parent_block, ok, "panicOutOfBounds", &.{ index, len });
22313 if (!sema.mod.comp.formatted_panics) {
22314 return sema.addSafetyCheck(parent_block, ok, .index_out_of_bounds);
22315 }
22316 try sema.safetyCheckFormatted(parent_block, ok, "panicOutOfBounds", &.{ index, len });
2230822317}
2230922318
2231022319fn panicStartLargerThanEnd(
......@@ -22315,7 +22324,10 @@ fn panicStartLargerThanEnd(
2231522324) !void {
2231622325 assert(!parent_block.is_comptime);
2231722326 const ok = try parent_block.addBinOp(.cmp_lte, start, end);
22318 try sema.safetyPanicFormatted(parent_block, ok, "panicStartGreaterThanEnd", &.{ start, end });
22327 if (!sema.mod.comp.formatted_panics) {
22328 return sema.addSafetyCheck(parent_block, ok, .start_index_greater_than_end);
22329 }
22330 try sema.safetyCheckFormatted(parent_block, ok, "panicStartGreaterThanEnd", &.{ start, end });
2231922331}
2232022332
2232122333fn panicInactiveUnionField(
......@@ -22326,7 +22338,10 @@ fn panicInactiveUnionField(
2232622338) !void {
2232722339 assert(!parent_block.is_comptime);
2232822340 const ok = try parent_block.addBinOp(.cmp_eq, active_tag, wanted_tag);
22329 try sema.safetyPanicFormatted(parent_block, ok, "panicInactiveUnionField", &.{ active_tag, wanted_tag });
22341 if (!sema.mod.comp.formatted_panics) {
22342 return sema.addSafetyCheck(parent_block, ok, .inactive_union_field);
22343 }
22344 try sema.safetyCheckFormatted(parent_block, ok, "panicInactiveUnionField", &.{ active_tag, wanted_tag });
2233022345}
2233122346
2233222347fn panicSentinelMismatch(
......@@ -22369,16 +22384,20 @@ fn panicSentinelMismatch(
2236922384 return;
2237022385 };
2237122386
22372 try sema.safetyPanicFormatted(parent_block, ok, "panicSentinelMismatch", &.{ expected_sentinel, actual_sentinel });
22387 if (!sema.mod.comp.formatted_panics) {
22388 return sema.addSafetyCheck(parent_block, ok, .sentinel_mismatch);
22389 }
22390 try sema.safetyCheckFormatted(parent_block, ok, "panicSentinelMismatch", &.{ expected_sentinel, actual_sentinel });
2237322391}
2237422392
22375fn safetyPanicFormatted(
22393fn safetyCheckFormatted(
2237622394 sema: *Sema,
2237722395 parent_block: *Block,
2237822396 ok: Air.Inst.Ref,
2237922397 func: []const u8,
2238022398 args: []const Air.Inst.Ref,
2238122399) CompileError!void {
22400 assert(sema.mod.comp.formatted_panics);
2238222401 const gpa = sema.gpa;
2238322402
2238422403 var fail_block: Block = .{
......@@ -22413,19 +22432,18 @@ fn safetyPanicFormatted(
2241322432fn safetyPanic(
2241422433 sema: *Sema,
2241522434 block: *Block,
22416 src: LazySrcLoc,
2241722435 panic_id: PanicId,
22418) CompileError!Zir.Inst.Index {
22436) CompileError!void {
2241922437 const panic_messages_ty = try sema.getBuiltinType("panic_messages");
2242022438 const msg_decl_index = (try sema.namespaceLookup(
2242122439 block,
22422 src,
22440 sema.src,
2242322441 panic_messages_ty.getNamespace().?,
2242422442 @tagName(panic_id),
2242522443 )).?;
2242622444
22427 const msg_inst = try sema.analyzeDeclVal(block, src, msg_decl_index);
22428 return sema.panicWithMsg(block, src, msg_inst);
22445 const msg_inst = try sema.analyzeDeclVal(block, sema.src, msg_decl_index);
22446 try sema.panicWithMsg(block, sema.src, msg_inst);
2242922447}
2243022448
2243122449fn emitBackwardBranch(sema: *Sema, block: *Block, src: LazySrcLoc) !void {
src/main.zig+8
......@@ -406,6 +406,8 @@ const usage_build_generic =
406406 \\ -fno-function-sections All functions go into same section
407407 \\ -fstrip Omit debug symbols
408408 \\ -fno-strip Keep debug symbols
409 \\ -fformatted-panics Enable formatted safety panics
410 \\ -fno-formatted-panics Disable formatted safety panics
409411 \\ -ofmt=[mode] Override target object format
410412 \\ elf Executable and Linking Format
411413 \\ c C source code
......@@ -632,6 +634,7 @@ fn buildOutputType(
632634 var have_version = false;
633635 var compatibility_version: ?std.builtin.Version = null;
634636 var strip: ?bool = null;
637 var formatted_panics: ?bool = null;
635638 var function_sections = false;
636639 var no_builtin = false;
637640 var watch = false;
......@@ -1242,6 +1245,10 @@ fn buildOutputType(
12421245 strip = true;
12431246 } else if (mem.eql(u8, arg, "-fno-strip")) {
12441247 strip = false;
1248 } else if (mem.eql(u8, arg, "-fformatted-panics")) {
1249 formatted_panics = true;
1250 } else if (mem.eql(u8, arg, "-fno-formatted-panics")) {
1251 formatted_panics = false;
12451252 } else if (mem.eql(u8, arg, "-fsingle-threaded")) {
12461253 single_threaded = true;
12471254 } else if (mem.eql(u8, arg, "-fno-single-threaded")) {
......@@ -2938,6 +2945,7 @@ fn buildOutputType(
29382945 .stack_size_override = stack_size_override,
29392946 .image_base_override = image_base_override,
29402947 .strip = strip,
2948 .formatted_panics = formatted_panics,
29412949 .single_threaded = single_threaded,
29422950 .function_sections = function_sections,
29432951 .no_builtin = no_builtin,