From e9aa0e7540b2bc0e644dde84aeca41d055e7511f Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Thu, 26 Sep 2019 16:17:00 -0400 Subject: [PATCH 1/2] Use existing Handler to print query stack When the panic handler is run, the existing Handler may be in a weird state if it was responsible for triggering the panic. By using a freshly created Handler, we avoid trying to re-entrantly lock a HandlerInner, which was causing a double panic on ICEs. --- src/librustc/ty/query/plumbing.rs | 5 +++-- src/librustc_driver/lib.rs | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/librustc/ty/query/plumbing.rs b/src/librustc/ty/query/plumbing.rs index a1828bb5ab7a7..e2dff46d18b16 100644 --- a/src/librustc/ty/query/plumbing.rs +++ b/src/librustc/ty/query/plumbing.rs @@ -15,6 +15,7 @@ use errors::DiagnosticBuilder; use errors::Level; use errors::Diagnostic; use errors::FatalError; +use errors::Handler; use rustc_data_structures::fx::{FxHashMap}; use rustc_data_structures::sync::{Lrc, Lock}; use rustc_data_structures::sharded::Sharded; @@ -321,7 +322,7 @@ impl<'tcx> TyCtxt<'tcx> { }) } - pub fn try_print_query_stack() { + pub fn try_print_query_stack(handler: &Handler) { eprintln!("query stack during panic:"); tls::with_context_opt(|icx| { @@ -336,7 +337,7 @@ impl<'tcx> TyCtxt<'tcx> { query.info.query.name(), query.info.query.describe(icx.tcx))); diag.span = icx.tcx.sess.source_map().def_span(query.info.span).into(); - icx.tcx.sess.diagnostic().force_print_diagnostic(diag); + handler.force_print_diagnostic(diag); current_query = query.parent.clone(); i += 1; diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 4a8681367410e..d0a319df2e6dc 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -1231,7 +1231,7 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) { let backtrace = env::var_os("RUST_BACKTRACE").map(|x| &x != "0").unwrap_or(false); if backtrace { - TyCtxt::try_print_query_stack(); + TyCtxt::try_print_query_stack(&handler); } #[cfg(windows)] From 97906bcd5c9c5ba5d165c7330b2ee062a97f11cf Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Thu, 26 Sep 2019 16:55:07 -0400 Subject: [PATCH 2/2] Add note about global state in try_print_query_stack --- src/librustc/ty/query/plumbing.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/librustc/ty/query/plumbing.rs b/src/librustc/ty/query/plumbing.rs index e2dff46d18b16..d3e6789b826a9 100644 --- a/src/librustc/ty/query/plumbing.rs +++ b/src/librustc/ty/query/plumbing.rs @@ -325,6 +325,9 @@ impl<'tcx> TyCtxt<'tcx> { pub fn try_print_query_stack(handler: &Handler) { eprintln!("query stack during panic:"); + // Be careful reyling on global state here: this code is called from + // a panic hook, which means that the global `Handler` may be in a weird + // state if it was responsible for triggering the panic. tls::with_context_opt(|icx| { if let Some(icx) = icx { let mut current_query = icx.query.clone();