From 891368f6013398c9f10fe9383f41f02476a48e24 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Tue, 1 Feb 2022 19:27:16 -0500 Subject: [PATCH] Make rustc use `RUST_BACKTRACE=full` by default Compiler panics should be rare - when they do occur, we want the report filed by the user to contain as much information as possible. This is especially important when the panic is due to an incremental compilation bug, since we may not have enough information to reproduce it. This PR sets `RUST_BACKTRACE=full` inside `rustc` if the user has not explicitly set `RUST_BACKTRACE`. This is more verbose than `RUST_BACKTRACE=1`, but this may make it easier to debug incremental compilation issues. Users who find this too verbose can still manually set `RUST_BACKTRACE` before invoking the compiler. This only affects `rustc` (and any tool using `rustc_driver::install_ice_hook`). It does *not* affect any user crates or the standard library - backtraces will continue to be off by default in any application *compiled* by rustc. --- compiler/rustc_driver/src/lib.rs | 9 +++++++++ src/test/ui/panics/default-backtrace-ice.rs | 9 +++++++++ .../ui/panics/default-backtrace-ice.stderr | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 src/test/ui/panics/default-backtrace-ice.rs create mode 100644 src/test/ui/panics/default-backtrace-ice.stderr diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index 0f490c3310245..85de860ceaf97 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -1240,6 +1240,15 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) { /// /// A custom rustc driver can skip calling this to set up a custom ICE hook. pub fn install_ice_hook() { + // If the user has not explicitly overriden "RUST_BACKTRACE", then produce + // full backtraces. When a compiler ICE happens, we want to gather + // as much information as possible to present in the issue opened + // by the user. Compiler developers and other rustc users can + // opt in to less-verbose backtraces by manually setting "RUST_BACKTRACE" + // (e.g. `RUST_BACKTRACE=1`) + if std::env::var("RUST_BACKTRACE").is_err() { + std::env::set_var("RUST_BACKTRACE", "full"); + } SyncLazy::force(&DEFAULT_HOOK); } diff --git a/src/test/ui/panics/default-backtrace-ice.rs b/src/test/ui/panics/default-backtrace-ice.rs new file mode 100644 index 0000000000000..fd86a3f9dfaff --- /dev/null +++ b/src/test/ui/panics/default-backtrace-ice.rs @@ -0,0 +1,9 @@ +// unset-rustc-env:RUST_BACKTRACE +// compile-flags:-Z treat-err-as-bug=1 +// error-pattern:stack backtrace: +// failure-status:101 +// normalize-stderr-test "note: .*" -> "" +// normalize-stderr-test "thread 'rustc' .*" -> "" +// normalize-stderr-test " .*\n" -> "" + +fn main() { missing_ident; } diff --git a/src/test/ui/panics/default-backtrace-ice.stderr b/src/test/ui/panics/default-backtrace-ice.stderr new file mode 100644 index 0000000000000..a0025d7e221ae --- /dev/null +++ b/src/test/ui/panics/default-backtrace-ice.stderr @@ -0,0 +1,18 @@ +error[E0425]: cannot find value `missing_ident` in this scope +LL | fn main() { missing_ident; } + + +stack backtrace: + +error: internal compiler error: unexpected panic + + + + + + + + + +query stack during panic: +end of query stack