|
4 | 4 | // FIXME: switch to something more ergonomic here, once available.
|
5 | 5 | // (Currently there is no way to opt into sysroot crates without `extern crate`.)
|
6 | 6 | #[allow(unused_extern_crates)]
|
| 7 | +extern crate rustc; |
| 8 | +#[allow(unused_extern_crates)] |
7 | 9 | extern crate rustc_driver;
|
8 | 10 | #[allow(unused_extern_crates)]
|
| 11 | +extern crate rustc_errors; |
| 12 | +#[allow(unused_extern_crates)] |
9 | 13 | extern crate rustc_interface;
|
10 | 14 |
|
| 15 | +use rustc::ty::TyCtxt; |
11 | 16 | use rustc_interface::interface;
|
12 | 17 | use rustc_tools_util::*;
|
13 | 18 |
|
| 19 | +use lazy_static::lazy_static; |
| 20 | +use std::borrow::Cow; |
| 21 | +use std::panic; |
14 | 22 | use std::path::{Path, PathBuf};
|
15 | 23 | use std::process::{exit, Command};
|
16 | 24 |
|
@@ -245,9 +253,62 @@ You can use tool lints to allow or deny lints from your code, eg.:
|
245 | 253 | );
|
246 | 254 | }
|
247 | 255 |
|
| 256 | +const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rust-clippy/issues/new"; |
| 257 | + |
| 258 | +lazy_static! { |
| 259 | + static ref ICE_HOOK: Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static> = { |
| 260 | + let hook = panic::take_hook(); |
| 261 | + panic::set_hook(Box::new(|info| report_clippy_ice(info, BUG_REPORT_URL))); |
| 262 | + hook |
| 263 | + }; |
| 264 | +} |
| 265 | + |
| 266 | +fn report_clippy_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) { |
| 267 | + // Invoke our ICE handler, which prints the actual panic message and optionally a backtrace |
| 268 | + (*ICE_HOOK)(info); |
| 269 | + |
| 270 | + // Separate the output with an empty line |
| 271 | + eprintln!(); |
| 272 | + |
| 273 | + let emitter = Box::new(rustc_errors::emitter::EmitterWriter::stderr( |
| 274 | + rustc_errors::ColorConfig::Auto, |
| 275 | + None, |
| 276 | + false, |
| 277 | + false, |
| 278 | + None, |
| 279 | + false, |
| 280 | + )); |
| 281 | + let handler = rustc_errors::Handler::with_emitter(true, None, emitter); |
| 282 | + |
| 283 | + // a .span_bug or .bug call has already printed what |
| 284 | + // it wants to print. |
| 285 | + if !info.payload().is::<rustc_errors::ExplicitBug>() { |
| 286 | + let d = rustc_errors::Diagnostic::new(rustc_errors::Level::Bug, "unexpected panic"); |
| 287 | + handler.emit_diagnostic(&d); |
| 288 | + handler.abort_if_errors_and_should_abort(); |
| 289 | + } |
| 290 | + |
| 291 | + let xs: Vec<Cow<'static, str>> = vec![ |
| 292 | + "the compiler unexpectedly panicked. this is a bug.".into(), |
| 293 | + format!("we would appreciate a bug report: {}", bug_report_url).into(), |
| 294 | + format!("rustc {}", option_env!("CFG_VERSION").unwrap_or("unknown_version")).into(), |
| 295 | + ]; |
| 296 | + |
| 297 | + for note in &xs { |
| 298 | + handler.note_without_error(¬e); |
| 299 | + } |
| 300 | + |
| 301 | + // If backtraces are enabled, also print the query stack |
| 302 | + let backtrace = std::env::var_os("RUST_BACKTRACE").map(|x| &x != "0").unwrap_or(false); |
| 303 | + |
| 304 | + if backtrace { |
| 305 | + TyCtxt::try_print_query_stack(); |
| 306 | + } |
| 307 | +} |
| 308 | + |
248 | 309 | pub fn main() {
|
249 | 310 | rustc_driver::init_rustc_env_logger();
|
250 |
| - rustc_driver::install_ice_hook(); |
| 311 | + lazy_static::initialize(&ICE_HOOK); |
251 | 312 | exit(
|
252 | 313 | rustc_driver::catch_fatal_errors(move || {
|
253 | 314 | use std::env;
|
|
0 commit comments