Skip to content

Commit 0b7acaa

Browse files
authored
Rollup merge of #110989 - jyn514:bug-report-url, r=WaffleLapkin
Make the BUG_REPORT_URL configurable by tools This greatly simplifies how hard it is to set a custom bug report url; previously tools had to copy the entire hook implementation. I haven't changed clippy in case they want to make the change upstream instead of the subtree, but I'm happy to do so here if the maintainers want - cc ````@rust-lang/clippy```` Fixes rust-lang/rust#109486.
2 parents 5889ecd + a233bd5 commit 0b7acaa

File tree

1 file changed

+9
-61
lines changed

1 file changed

+9
-61
lines changed

src/driver.rs

+9-61
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// FIXME: switch to something more ergonomic here, once available.
1212
// (Currently there is no way to opt into sysroot crates without `extern crate`.)
1313
extern crate rustc_driver;
14-
extern crate rustc_errors;
1514
extern crate rustc_interface;
1615
extern crate rustc_session;
1716
extern crate rustc_span;
@@ -20,13 +19,10 @@ use rustc_interface::interface;
2019
use rustc_session::parse::ParseSess;
2120
use rustc_span::symbol::Symbol;
2221

23-
use std::borrow::Cow;
2422
use std::env;
2523
use std::ops::Deref;
26-
use std::panic;
2724
use std::path::Path;
2825
use std::process::exit;
29-
use std::sync::LazyLock;
3026

3127
/// If a command-line option matches `find_arg`, then apply the predicate `pred` on its value. If
3228
/// true, then return it. The parameter is assumed to be either `--arg=value` or `--arg value`.
@@ -198,66 +194,18 @@ You can use tool lints to allow or deny lints from your code, eg.:
198194

199195
const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rust-clippy/issues/new";
200196

201-
type PanicCallback = dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static;
202-
static ICE_HOOK: LazyLock<Box<PanicCallback>> = LazyLock::new(|| {
203-
let hook = panic::take_hook();
204-
panic::set_hook(Box::new(|info| report_clippy_ice(info, BUG_REPORT_URL)));
205-
hook
206-
});
207-
208-
fn report_clippy_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
209-
// Invoke our ICE handler, which prints the actual panic message and optionally a backtrace
210-
(*ICE_HOOK)(info);
211-
212-
// Separate the output with an empty line
213-
eprintln!();
214-
215-
let fallback_bundle = rustc_errors::fallback_fluent_bundle(rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(), false);
216-
let emitter = Box::new(rustc_errors::emitter::EmitterWriter::stderr(
217-
rustc_errors::ColorConfig::Auto,
218-
None,
219-
None,
220-
fallback_bundle,
221-
false,
222-
false,
223-
None,
224-
false,
225-
false,
226-
rustc_errors::TerminalUrl::No,
227-
));
228-
let handler = rustc_errors::Handler::with_emitter(true, None, emitter);
229-
230-
// a .span_bug or .bug call has already printed what
231-
// it wants to print.
232-
if !info.payload().is::<rustc_errors::ExplicitBug>() {
233-
let mut d = rustc_errors::Diagnostic::new(rustc_errors::Level::Bug, "unexpected panic");
234-
handler.emit_diagnostic(&mut d);
235-
}
236-
237-
let version_info = rustc_tools_util::get_version_info!();
238-
239-
let xs: Vec<Cow<'static, str>> = vec![
240-
"the compiler unexpectedly panicked. this is a bug.".into(),
241-
format!("we would appreciate a bug report: {bug_report_url}").into(),
242-
format!("Clippy version: {version_info}").into(),
243-
];
244-
245-
for note in &xs {
246-
handler.note_without_error(note.as_ref());
247-
}
248-
249-
// If backtraces are enabled, also print the query stack
250-
let backtrace = env::var_os("RUST_BACKTRACE").map_or(false, |x| &x != "0");
251-
252-
let num_frames = if backtrace { None } else { Some(2) };
253-
254-
interface::try_print_query_stack(&handler, num_frames);
255-
}
256-
257197
#[allow(clippy::too_many_lines)]
258198
pub fn main() {
259199
rustc_driver::init_rustc_env_logger();
260-
LazyLock::force(&ICE_HOOK);
200+
201+
rustc_driver::install_ice_hook(BUG_REPORT_URL, |handler| {
202+
// FIXME: this macro calls unwrap internally but is called in a panicking context! It's not
203+
// as simple as moving the call from the hook to main, because `install_ice_hook` doesn't
204+
// accept a generic closure.
205+
let version_info = rustc_tools_util::get_version_info!();
206+
handler.note_without_error(format!("Clippy version: {version_info}"));
207+
});
208+
261209
exit(rustc_driver::catch_with_exit_code(move || {
262210
let mut orig_args: Vec<String> = env::args().collect();
263211
let has_sysroot_arg = arg_value(&orig_args, "--sysroot", |_| true).is_some();

0 commit comments

Comments
 (0)