Commit 73403d897c

Andrew Kelley <andrew@ziglang.org>
2021-10-08 04:20:15
stage2: add --debug-compile-errors CLI option
This is useful when debugging stage2 and getting an unexpected compile error, or when rendering a compile error crashes the compiler.
1 parent 75cecef
src/Compilation.zig
@@ -82,6 +82,9 @@ disable_c_depfile: bool,
 time_report: bool,
 stack_report: bool,
 unwind_tables: bool,
+test_evented_io: bool,
+debug_compiler_runtime_libs: bool,
+debug_compile_errors: bool,
 
 c_source_files: []const CSourceFile,
 clang_argv: []const []const u8,
@@ -138,8 +141,6 @@ mutex: std.Thread.Mutex = .{},
 
 test_filter: ?[]const u8,
 test_name_prefix: ?[]const u8,
-test_evented_io: bool,
-debug_compiler_runtime_libs: bool,
 
 emit_asm: ?EmitLoc,
 emit_llvm_ir: ?EmitLoc,
@@ -727,6 +728,7 @@ pub const InitOptions = struct {
     is_test: bool = false,
     test_evented_io: bool = false,
     debug_compiler_runtime_libs: bool = false,
+    debug_compile_errors: bool = false,
     /// Normally when you create a `Compilation`, Zig will automatically build
     /// and link in required dependencies, such as compiler-rt and libc. When
     /// building such dependencies themselves, this flag must be set to avoid
@@ -1471,6 +1473,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
             .test_name_prefix = options.test_name_prefix,
             .test_evented_io = options.test_evented_io,
             .debug_compiler_runtime_libs = options.debug_compiler_runtime_libs,
+            .debug_compile_errors = options.debug_compile_errors,
             .work_queue_wait_group = undefined,
             .astgen_wait_group = undefined,
         };
src/crash_report.zig
@@ -151,7 +151,7 @@ fn writeFullyQualifiedDeclWithFile(decl: *Decl, stream: anytype) !void {
     try decl.renderFullyQualifiedDebugName(stream);
 }
 
-fn compilerPanic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn {
+pub fn compilerPanic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn {
     PanicSwitch.preDispatch();
     @setCold(true);
     const ret_addr = @returnAddress();
src/main.zig
@@ -433,6 +433,7 @@ const usage_build_generic =
     \\  --verbose-cimport            Enable compiler debug output for C imports
     \\  --verbose-llvm-cpu-features  Enable compiler debug output for LLVM CPU features
     \\  --debug-log [scope]          Enable printing debug/info log messages for scope
+    \\  --debug-compile-errors       Crash with helpful diagnostics at the first compile error
     \\
 ;
 
@@ -549,6 +550,7 @@ fn buildOutputType(
     var single_threaded = false;
     var function_sections = false;
     var watch = false;
+    var debug_compile_errors = false;
     var verbose_link = std.process.hasEnvVarConstant("ZIG_VERBOSE_LINK");
     var verbose_cc = std.process.hasEnvVarConstant("ZIG_VERBOSE_CC");
     var verbose_air = false;
@@ -1078,6 +1080,8 @@ fn buildOutputType(
                         linker_allow_shlib_undefined = false;
                     } else if (mem.eql(u8, arg, "-Bsymbolic")) {
                         linker_bind_global_refs_locally = true;
+                    } else if (mem.eql(u8, arg, "--debug-compile-errors")) {
+                        debug_compile_errors = true;
                     } else if (mem.eql(u8, arg, "--verbose-link")) {
                         verbose_link = true;
                     } else if (mem.eql(u8, arg, "--verbose-cc")) {
@@ -2134,6 +2138,7 @@ fn buildOutputType(
         .disable_lld_caching = !have_enable_cache,
         .subsystem = subsystem,
         .wasi_exec_model = wasi_exec_model,
+        .debug_compile_errors = debug_compile_errors,
     }) catch |err| {
         fatal("unable to create compilation: {s}", .{@errorName(err)});
     };
src/Sema.zig
@@ -1213,6 +1213,15 @@ pub fn fail(
 fn failWithOwnedErrorMsg(sema: *Sema, err_msg: *Module.ErrorMsg) CompileError {
     @setCold(true);
 
+    if (crash_report.is_enabled and sema.mod.comp.debug_compile_errors) {
+        std.debug.print("compile error during Sema: {s}, src: {s}:{}\n", .{
+            err_msg.msg,
+            err_msg.src_loc.file_scope.sub_file_path,
+            err_msg.src_loc.lazy,
+        });
+        crash_report.compilerPanic("unexpected compile error occurred", null);
+    }
+
     const mod = sema.mod;
 
     {