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