1
Fork 0

Auto merge of #60584 - jonas-schievink:ice-panic-hook, r=oli-obk

Use `panic::set_hook` to print the ICE message

This allows custom frontends and backends to override the hook with their own, for example to point people to a different issue tracker.

ICE messages are printed in a slightly different order now. Nightly prints:

```
thread 'rustc' panicked at 'called `Option::unwrap()` on a `None` value', src/libcore/option.rs:347:21
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
error: aborting due to 2 previous errors

Some errors have detailed explanations: E0277, E0658.
For more information about an error, try `rustc --explain E0277`.

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.36.0-nightly (08bfe1612 2019-05-02) running on x86_64-unknown-linux-gnu
```

After this PR, rustc prints:

```
thread 'rustc' panicked at 'called `Option::unwrap()` on a `None` value', src/libcore/option.rs:347:21
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.36.0-dev running on x86_64-unknown-linux-gnu

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0277, E0658.
For more information about an error, try `rustc --explain E0277`.
```
This commit is contained in:
bors 2019-09-15 00:10:08 +00:00
commit 572d3d9634
14 changed files with 120 additions and 98 deletions

View file

@ -3014,7 +3014,6 @@ dependencies = [
"fmt_macros", "fmt_macros",
"graphviz", "graphviz",
"jobserver", "jobserver",
"lazy_static 1.3.0",
"log", "log",
"measureme", "measureme",
"num_cpus", "num_cpus",
@ -3370,6 +3369,7 @@ version = "0.0.0"
dependencies = [ dependencies = [
"env_logger", "env_logger",
"graphviz", "graphviz",
"lazy_static 1.3.0",
"log", "log",
"rustc", "rustc",
"rustc_ast_borrowck", "rustc_ast_borrowck",

View file

@ -15,7 +15,6 @@ bitflags = "1.0"
fmt_macros = { path = "../libfmt_macros" } fmt_macros = { path = "../libfmt_macros" }
graphviz = { path = "../libgraphviz" } graphviz = { path = "../libgraphviz" }
jobserver = "0.1" jobserver = "0.1"
lazy_static = "1.0.0"
num_cpus = "1.0" num_cpus = "1.0"
scoped-tls = "1.0" scoped-tls = "1.0"
log = { version = "0.4", features = ["release_max_level_info", "std"] } log = { version = "0.4", features = ["release_max_level_info", "std"] }

View file

@ -67,7 +67,6 @@
#[macro_use] extern crate bitflags; #[macro_use] extern crate bitflags;
extern crate getopts; extern crate getopts;
#[macro_use] extern crate lazy_static;
#[macro_use] extern crate scoped_tls; #[macro_use] extern crate scoped_tls;
#[cfg(windows)] #[cfg(windows)]
extern crate libc; extern crate libc;

View file

@ -5,17 +5,13 @@ use rustc_data_structures::{fx::FxHashMap, sync::Lock};
use std::cell::{RefCell, Cell}; use std::cell::{RefCell, Cell};
use std::fmt::Debug; use std::fmt::Debug;
use std::hash::Hash; use std::hash::Hash;
use std::panic;
use std::env;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use std::sync::mpsc::{Sender}; use std::sync::mpsc::{Sender};
use syntax_pos::{SpanData}; use syntax_pos::{SpanData};
use syntax::symbol::{Symbol, sym}; use syntax::symbol::{Symbol, sym};
use rustc_macros::HashStable; use rustc_macros::HashStable;
use crate::ty::TyCtxt;
use crate::dep_graph::{DepNode}; use crate::dep_graph::{DepNode};
use lazy_static;
use crate::session::Session; use crate::session::Session;
#[cfg(test)] #[cfg(test)]
@ -31,39 +27,6 @@ pub struct ErrorReported;
thread_local!(static TIME_DEPTH: Cell<usize> = Cell::new(0)); thread_local!(static TIME_DEPTH: Cell<usize> = Cell::new(0));
lazy_static! {
static ref DEFAULT_HOOK: Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static> = {
let hook = panic::take_hook();
panic::set_hook(Box::new(panic_hook));
hook
};
}
fn panic_hook(info: &panic::PanicInfo<'_>) {
(*DEFAULT_HOOK)(info);
let backtrace = env::var_os("RUST_BACKTRACE").map(|x| &x != "0").unwrap_or(false);
if backtrace {
TyCtxt::try_print_query_stack();
}
#[cfg(windows)]
unsafe {
if env::var("RUSTC_BREAK_ON_ICE").is_ok() {
extern "system" {
fn DebugBreak();
}
// Trigger a debugger if we crashed during bootstrap.
DebugBreak();
}
}
}
pub fn install_panic_hook() {
lazy_static::initialize(&DEFAULT_HOOK);
}
/// Parameters to the `Dump` variant of type `ProfileQueriesMsg`. /// Parameters to the `Dump` variant of type `ProfileQueriesMsg`.
#[derive(Clone,Debug)] #[derive(Clone,Debug)]
pub struct ProfQDumpParams { pub struct ProfQDumpParams {

View file

@ -11,6 +11,7 @@ crate-type = ["dylib"]
[dependencies] [dependencies]
graphviz = { path = "../libgraphviz" } graphviz = { path = "../libgraphviz" }
lazy_static = "1.0"
log = "0.4" log = "0.4"
env_logger = { version = "0.6", default-features = false } env_logger = { version = "0.6", default-features = false }
rustc = { path = "../librustc" } rustc = { path = "../librustc" }

View file

@ -20,6 +20,8 @@ pub extern crate getopts;
extern crate libc; extern crate libc;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
#[macro_use]
extern crate lazy_static;
pub extern crate rustc_plugin_impl as plugin; pub extern crate rustc_plugin_impl as plugin;
@ -35,8 +37,8 @@ use rustc::session::{early_error, early_warn};
use rustc::lint::Lint; use rustc::lint::Lint;
use rustc::lint; use rustc::lint;
use rustc::hir::def_id::LOCAL_CRATE; use rustc::hir::def_id::LOCAL_CRATE;
use rustc::util::common::{ErrorReported, install_panic_hook, print_time_passes_entry}; use rustc::ty::TyCtxt;
use rustc::util::common::{set_time_depth, time}; use rustc::util::common::{set_time_depth, time, print_time_passes_entry, ErrorReported};
use rustc_metadata::locator; use rustc_metadata::locator;
use rustc_metadata::cstore::CStore; use rustc_metadata::cstore::CStore;
use rustc_codegen_utils::codegen_backend::CodegenBackend; use rustc_codegen_utils::codegen_backend::CodegenBackend;
@ -161,8 +163,6 @@ pub fn run_compiler(
None => return Ok(()), None => return Ok(()),
}; };
install_panic_hook();
let (sopts, cfg) = config::build_session_options_and_crate_config(&matches); let (sopts, cfg) = config::build_session_options_and_crate_config(&matches);
let mut dummy_config = |sopts, cfg, diagnostic_output| { let mut dummy_config = |sopts, cfg, diagnostic_output| {
@ -1151,63 +1151,107 @@ fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
} }
} }
/// Runs a procedure which will detect panics in the compiler and print nicer /// Runs a closure and catches unwinds triggered by fatal errors.
/// error messages rather than just failing the test.
/// ///
/// The diagnostic emitter yielded to the procedure should be used for reporting /// The compiler currently unwinds with a special sentinel value to abort
/// errors of the compiler. /// compilation on fatal errors. This function catches that sentinel and turns
pub fn report_ices_to_stderr_if_any<F: FnOnce() -> R, R>(f: F) -> Result<R, ErrorReported> { /// the panic into a `Result` instead.
pub fn catch_fatal_errors<F: FnOnce() -> R, R>(f: F) -> Result<R, ErrorReported> {
catch_unwind(panic::AssertUnwindSafe(f)).map_err(|value| { catch_unwind(panic::AssertUnwindSafe(f)).map_err(|value| {
if value.is::<errors::FatalErrorMarker>() { if value.is::<errors::FatalErrorMarker>() {
ErrorReported ErrorReported
} else { } else {
// Thread panicked without emitting a fatal diagnostic panic::resume_unwind(value);
eprintln!("");
let emitter = Box::new(errors::emitter::EmitterWriter::stderr(
errors::ColorConfig::Auto,
None,
false,
false,
None,
));
let handler = errors::Handler::with_emitter(true, None, emitter);
// a .span_bug or .bug call has already printed what
// it wants to print.
if !value.is::<errors::ExplicitBug>() {
handler.emit(&MultiSpan::new(),
"unexpected panic",
errors::Level::Bug);
}
let mut xs: Vec<Cow<'static, str>> = vec![
"the compiler unexpectedly panicked. this is a bug.".into(),
format!("we would appreciate a bug report: {}", BUG_REPORT_URL).into(),
format!("rustc {} running on {}",
option_env!("CFG_VERSION").unwrap_or("unknown_version"),
config::host_triple()).into(),
];
if let Some((flags, excluded_cargo_defaults)) = extra_compiler_flags() {
xs.push(format!("compiler flags: {}", flags.join(" ")).into());
if excluded_cargo_defaults {
xs.push("some of the compiler flags provided by cargo are hidden".into());
}
}
for note in &xs {
handler.emit(&MultiSpan::new(),
note,
errors::Level::Note);
}
panic::resume_unwind(Box::new(errors::FatalErrorMarker));
} }
}) })
} }
lazy_static! {
static ref DEFAULT_HOOK: Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static> = {
let hook = panic::take_hook();
panic::set_hook(Box::new(|info| report_ice(info, BUG_REPORT_URL)));
hook
};
}
/// Prints the ICE message, including backtrace and query stack.
///
/// The message will point the user at `bug_report_url` to report the ICE.
///
/// When `install_ice_hook` is called, this function will be called as the panic
/// hook.
pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
// Invoke the default handler, which prints the actual panic message and optionally a backtrace
(*DEFAULT_HOOK)(info);
// Separate the output with an empty line
eprintln!();
let emitter = Box::new(errors::emitter::EmitterWriter::stderr(
errors::ColorConfig::Auto,
None,
false,
false,
None,
));
let handler = errors::Handler::with_emitter(true, None, emitter);
// a .span_bug or .bug call has already printed what
// it wants to print.
if !info.payload().is::<errors::ExplicitBug>() {
handler.emit(&MultiSpan::new(),
"unexpected panic",
errors::Level::Bug);
}
let mut xs: Vec<Cow<'static, str>> = vec![
"the compiler unexpectedly panicked. this is a bug.".into(),
format!("we would appreciate a bug report: {}", bug_report_url).into(),
format!("rustc {} running on {}",
option_env!("CFG_VERSION").unwrap_or("unknown_version"),
config::host_triple()).into(),
];
if let Some((flags, excluded_cargo_defaults)) = extra_compiler_flags() {
xs.push(format!("compiler flags: {}", flags.join(" ")).into());
if excluded_cargo_defaults {
xs.push("some of the compiler flags provided by cargo are hidden".into());
}
}
for note in &xs {
handler.emit(&MultiSpan::new(),
note,
errors::Level::Note);
}
// If backtraces are enabled, also print the query stack
let backtrace = env::var_os("RUST_BACKTRACE").map(|x| &x != "0").unwrap_or(false);
if backtrace {
TyCtxt::try_print_query_stack();
}
#[cfg(windows)]
unsafe {
if env::var("RUSTC_BREAK_ON_ICE").is_ok() {
extern "system" {
fn DebugBreak();
}
// Trigger a debugger if we crashed during bootstrap
DebugBreak();
}
}
}
/// Installs a panic hook that will print the ICE message on unexpected panics.
///
/// A custom rustc driver can skip calling this to set up a custom ICE hook.
pub fn install_ice_hook() {
lazy_static::initialize(&DEFAULT_HOOK);
}
/// This allows tools to enable rust logging without having to magically match rustc's /// This allows tools to enable rust logging without having to magically match rustc's
/// log crate version /// log crate version
pub fn init_rustc_env_logger() { pub fn init_rustc_env_logger() {
@ -1218,7 +1262,8 @@ pub fn main() {
let start = Instant::now(); let start = Instant::now();
init_rustc_env_logger(); init_rustc_env_logger();
let mut callbacks = TimePassesCallbacks::default(); let mut callbacks = TimePassesCallbacks::default();
let result = report_ices_to_stderr_if_any(|| { install_ice_hook();
let result = catch_fatal_errors(|| {
let args = env::args_os().enumerate() let args = env::args_os().enumerate()
.map(|(i, arg)| arg.into_string().unwrap_or_else(|arg| { .map(|(i, arg)| arg.into_string().unwrap_or_else(|arg| {
early_error(ErrorOutputType::default(), early_error(ErrorOutputType::default(),

View file

@ -473,7 +473,7 @@ where R: 'static + Send,
// First, parse the crate and extract all relevant information. // First, parse the crate and extract all relevant information.
info!("starting to run rustc"); info!("starting to run rustc");
let result = rustc_driver::report_ices_to_stderr_if_any(move || { let result = rustc_driver::catch_fatal_errors(move || {
let crate_name = options.crate_name.clone(); let crate_name = options.crate_name.clone();
let crate_version = options.crate_version.clone(); let crate_version = options.crate_version.clone();
let (mut krate, renderinfo, renderopts) = core::run_core(options); let (mut krate, renderinfo, renderopts) = core::run_core(options);

View file

@ -24,7 +24,7 @@ impl rustc_driver::Callbacks for TestCalls<'_> {
fn main() { fn main() {
let mut count = 1; let mut count = 1;
let args = vec!["compiler-calls".to_string(), "foo.rs".to_string()]; let args = vec!["compiler-calls".to_string(), "foo.rs".to_string()];
rustc_driver::report_ices_to_stderr_if_any(|| { rustc_driver::catch_fatal_errors(|| {
rustc_driver::run_compiler(&args, &mut TestCalls { count: &mut count }, None, None).ok(); rustc_driver::run_compiler(&args, &mut TestCalls { count: &mut count }, None, None).ok();
}).ok(); }).ok();
assert_eq!(count, 2); assert_eq!(count, 2);

View file

@ -3,6 +3,11 @@
// FIXME https://github.com/rust-lang/rust/issues/59998 // FIXME https://github.com/rust-lang/rust/issues/59998
// normalize-stderr-test "thread.*panicked.*proc_macro_server.rs.*\n" -> "" // normalize-stderr-test "thread.*panicked.*proc_macro_server.rs.*\n" -> ""
// normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> "" // normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
// normalize-stderr-test "\nerror: internal compiler error.*\n\n" -> ""
// normalize-stderr-test "note:.*unexpectedly panicked.*\n\n" -> ""
// normalize-stderr-test "note: we would appreciate a bug report.*\n\n" -> ""
// normalize-stderr-test "note: compiler flags.*\n\n" -> ""
// normalize-stderr-test "note: rustc.*running on.*\n\n" -> ""
#[macro_use] #[macro_use]
extern crate invalid_punct_ident; extern crate invalid_punct_ident;

View file

@ -1,5 +1,5 @@
error: proc macro panicked error: proc macro panicked
--> $DIR/invalid-punct-ident-1.rs:10:1 --> $DIR/invalid-punct-ident-1.rs:15:1
| |
LL | invalid_punct!(); LL | invalid_punct!();
| ^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^

View file

@ -3,6 +3,11 @@
// FIXME https://github.com/rust-lang/rust/issues/59998 // FIXME https://github.com/rust-lang/rust/issues/59998
// normalize-stderr-test "thread.*panicked.*proc_macro_server.rs.*\n" -> "" // normalize-stderr-test "thread.*panicked.*proc_macro_server.rs.*\n" -> ""
// normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> "" // normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
// normalize-stderr-test "\nerror: internal compiler error.*\n\n" -> ""
// normalize-stderr-test "note:.*unexpectedly panicked.*\n\n" -> ""
// normalize-stderr-test "note: we would appreciate a bug report.*\n\n" -> ""
// normalize-stderr-test "note: compiler flags.*\n\n" -> ""
// normalize-stderr-test "note: rustc.*running on.*\n\n" -> ""
#[macro_use] #[macro_use]
extern crate invalid_punct_ident; extern crate invalid_punct_ident;

View file

@ -1,5 +1,5 @@
error: proc macro panicked error: proc macro panicked
--> $DIR/invalid-punct-ident-2.rs:10:1 --> $DIR/invalid-punct-ident-2.rs:15:1
| |
LL | invalid_ident!(); LL | invalid_ident!();
| ^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^

View file

@ -3,6 +3,11 @@
// FIXME https://github.com/rust-lang/rust/issues/59998 // FIXME https://github.com/rust-lang/rust/issues/59998
// normalize-stderr-test "thread.*panicked.*proc_macro_server.rs.*\n" -> "" // normalize-stderr-test "thread.*panicked.*proc_macro_server.rs.*\n" -> ""
// normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> "" // normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
// normalize-stderr-test "\nerror: internal compiler error.*\n\n" -> ""
// normalize-stderr-test "note:.*unexpectedly panicked.*\n\n" -> ""
// normalize-stderr-test "note: we would appreciate a bug report.*\n\n" -> ""
// normalize-stderr-test "note: compiler flags.*\n\n" -> ""
// normalize-stderr-test "note: rustc.*running on.*\n\n" -> ""
#[macro_use] #[macro_use]
extern crate invalid_punct_ident; extern crate invalid_punct_ident;

View file

@ -1,5 +1,5 @@
error: proc macro panicked error: proc macro panicked
--> $DIR/invalid-punct-ident-3.rs:10:1 --> $DIR/invalid-punct-ident-3.rs:15:1
| |
LL | invalid_raw_ident!(); LL | invalid_raw_ident!();
| ^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^