Skip to content

Commit c85f32a

Browse files
committed
Add custom ICE message that points to Clippy repo
This utilizes rust-lang/rust#60584 by setting our own `panic_hook` and pointing to our own issue tracker instead of the rustc issue tracker. This also adds a new internal lint to test the ICE message. **Potential downsides** * This essentially copies rustc's `report_ice` function as `report_clippy_ice`. I think that's how it's meant to be implemented, but maybe @jonas-schievink could have a look as well =) The downside of more-or-less copying this function is that we have to maintain it as well now. The original function can be found [here][original]. * `driver` now depends directly on `rustc` and `rustc_errors` Closes rust-lang#2734 [original]: https://github.com/rust-lang/rust/blob/59367b074f1523353dddefa678ffe3cac9fd4e50/src/librustc_driver/lib.rs#L1185
1 parent b4f1769 commit c85f32a

File tree

6 files changed

+120
-2
lines changed

6 files changed

+120
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ clippy_lints = { version = "0.0.212", path = "clippy_lints" }
3838
regex = "1"
3939
semver = "0.9"
4040
rustc_tools_util = { version = "0.2.0", path = "rustc_tools_util"}
41+
lazy_static = "1.0"
4142

4243
[dev-dependencies]
4344
cargo_metadata = "0.9.0"
4445
compiletest_rs = { version = "0.3.24", features = ["tmp"] }
45-
lazy_static = "1.0"
4646
clippy-mini-macro-test = { version = "0.2", path = "mini-macro" }
4747
serde = { version = "1.0", features = ["derive"] }
4848
derive-new = "0.5"

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
951951
store.register_late_pass(|| box mutable_debug_assertion::DebugAssertWithMutCall);
952952
store.register_late_pass(|| box exit::Exit);
953953
store.register_late_pass(|| box to_digit_is_some::ToDigitIsSome);
954+
store.register_early_pass(|| box utils::internal_lints::ProduceIce);
954955

955956
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
956957
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
@@ -1043,6 +1044,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
10431044
LintId::of(&utils::internal_lints::COMPILER_LINT_FUNCTIONS),
10441045
LintId::of(&utils::internal_lints::LINT_WITHOUT_LINT_PASS),
10451046
LintId::of(&utils::internal_lints::OUTER_EXPN_EXPN_DATA),
1047+
LintId::of(&utils::internal_lints::PRODUCE_ICE),
10461048
]);
10471049

10481050
store.register_group(true, "clippy::all", Some("clippy"), vec![

clippy_lints/src/utils/internal_lints.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ use rustc::lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintAr
1111
use rustc::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
1212
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1313
use rustc_errors::Applicability;
14+
use syntax::ast;
1415
use syntax::ast::{Crate as AstCrate, ItemKind, Name};
1516
use syntax::source_map::Span;
1617
use syntax_pos::symbol::SymbolStr;
18+
use syntax::visit::FnKind;
1719

1820
declare_clippy_lint! {
1921
/// **What it does:** Checks for various things we like to keep tidy in clippy.
@@ -99,6 +101,24 @@ declare_clippy_lint! {
99101
"using `cx.outer_expn().expn_data()` instead of `cx.outer_expn_data()`"
100102
}
101103

104+
declare_clippy_lint! {
105+
/// **What it does:** Not an actual lint. This lint is only meant for testing our customized internal compiler
106+
/// error message by calling `panic`.
107+
///
108+
/// **Why is this bad?** ICE in large quantities can damage your teeth
109+
///
110+
/// **Known problems:** None
111+
///
112+
/// **Example:**
113+
/// Bad:
114+
/// ```rust,ignore
115+
/// 🍦🍦🍦🍦🍦
116+
/// ```
117+
pub PRODUCE_ICE,
118+
internal,
119+
"this message should not appear anywhere as we ICE before and don't emit the lint"
120+
}
121+
102122
declare_lint_pass!(ClippyLintsInternal => [CLIPPY_LINTS_INTERNAL]);
103123

104124
impl EarlyLintPass for ClippyLintsInternal {
@@ -302,3 +322,22 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OuterExpnDataPass {
302322
}
303323
}
304324
}
325+
326+
declare_lint_pass!(ProduceIce => [PRODUCE_ICE]);
327+
328+
impl EarlyLintPass for ProduceIce {
329+
fn check_fn(&mut self, _: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: &ast::FnDecl, _: Span, _: ast::NodeId) {
330+
if is_trigger_fn(fn_kind) {
331+
panic!("Testing the ICE message");
332+
}
333+
}
334+
}
335+
336+
fn is_trigger_fn(fn_kind: FnKind<'_>) -> bool {
337+
match fn_kind {
338+
FnKind::ItemFn(ident, ..) | FnKind::Method(ident, ..) => {
339+
ident.name.as_str() == "should_trigger_an_ice_in_clippy"
340+
},
341+
FnKind::Closure(..) => false,
342+
}
343+
}

src/driver.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44
// FIXME: switch to something more ergonomic here, once available.
55
// (Currently there is no way to opt into sysroot crates without `extern crate`.)
66
#[allow(unused_extern_crates)]
7+
extern crate rustc;
8+
#[allow(unused_extern_crates)]
79
extern crate rustc_driver;
810
#[allow(unused_extern_crates)]
11+
extern crate rustc_errors;
12+
#[allow(unused_extern_crates)]
913
extern crate rustc_interface;
1014

15+
use rustc::ty::TyCtxt;
1116
use rustc_interface::interface;
1217
use rustc_tools_util::*;
1318

19+
use lazy_static::lazy_static;
20+
use std::borrow::Cow;
21+
use std::panic;
1422
use std::path::{Path, PathBuf};
1523
use std::process::{exit, Command};
1624

@@ -214,9 +222,62 @@ You can use tool lints to allow or deny lints from your code, eg.:
214222
);
215223
}
216224

225+
const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rust-clippy/issues/new";
226+
227+
lazy_static! {
228+
static ref ICE_HOOK: Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static> = {
229+
let hook = panic::take_hook();
230+
panic::set_hook(Box::new(|info| report_clippy_ice(info, BUG_REPORT_URL)));
231+
hook
232+
};
233+
}
234+
235+
fn report_clippy_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
236+
// Invoke our ICE handler, which prints the actual panic message and optionally a backtrace
237+
(*ICE_HOOK)(info);
238+
239+
// Separate the output with an empty line
240+
eprintln!();
241+
242+
let emitter = Box::new(rustc_errors::emitter::EmitterWriter::stderr(
243+
rustc_errors::ColorConfig::Auto,
244+
None,
245+
false,
246+
false,
247+
None,
248+
false,
249+
));
250+
let handler = rustc_errors::Handler::with_emitter(true, None, emitter);
251+
252+
// a .span_bug or .bug call has already printed what
253+
// it wants to print.
254+
if !info.payload().is::<rustc_errors::ExplicitBug>() {
255+
let d = rustc_errors::Diagnostic::new(rustc_errors::Level::Bug, "unexpected panic");
256+
handler.emit_diagnostic(&d);
257+
handler.abort_if_errors_and_should_abort();
258+
}
259+
260+
let xs: Vec<Cow<'static, str>> = vec![
261+
"the compiler unexpectedly panicked. this is a bug.".into(),
262+
format!("we would appreciate a bug report: {}", bug_report_url).into(),
263+
format!("rustc {}", option_env!("CFG_VERSION").unwrap_or("unknown_version")).into(),
264+
];
265+
266+
for note in &xs {
267+
handler.note_without_error(&note);
268+
}
269+
270+
// If backtraces are enabled, also print the query stack
271+
let backtrace = std::env::var_os("RUST_BACKTRACE").map(|x| &x != "0").unwrap_or(false);
272+
273+
if backtrace {
274+
TyCtxt::try_print_query_stack();
275+
}
276+
}
277+
217278
pub fn main() {
218279
rustc_driver::init_rustc_env_logger();
219-
rustc_driver::install_ice_hook();
280+
lazy_static::initialize(&ICE_HOOK);
220281
exit(
221282
rustc_driver::catch_fatal_errors(move || {
222283
use std::env;

tests/ui/custom_ice_message.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![deny(clippy::internal)]
2+
3+
fn should_trigger_an_ice_in_clippy() {}
4+
5+
fn main() {}

tests/ui/custom_ice_message.stderr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
thread 'rustc' panicked at 'Testing the ICE message', clippy_lints/src/utils/internal_lints.rs:333:13
2+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
3+
4+
error: internal compiler error: unexpected panic
5+
6+
note: the compiler unexpectedly panicked. this is a bug.
7+
8+
note: we would appreciate a bug report: https://github.com/rust-lang/rust-clippy/issues/new
9+
10+
note: rustc unknown_version
11+

0 commit comments

Comments
 (0)