1111// FIXME: switch to something more ergonomic here, once available.
1212// (Currently there is no way to opt into sysroot crates without `extern crate`.)
1313extern crate rustc_driver;
14- extern crate rustc_errors;
1514extern crate rustc_interface;
1615extern crate rustc_session;
1716extern crate rustc_span;
@@ -20,13 +19,10 @@ use rustc_interface::interface;
2019use rustc_session:: parse:: ParseSess ;
2120use rustc_span:: symbol:: Symbol ;
2221
23- use std:: borrow:: Cow ;
2422use std:: env;
2523use std:: ops:: Deref ;
26- use std:: panic;
2724use std:: path:: Path ;
2825use 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
199195const 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) ]
258198pub 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