@@ -25,7 +25,7 @@ use rustc_data_structures::profiling::{
2525use rustc_data_structures:: sync:: SeqCst ;
2626use rustc_errors:: registry:: { InvalidErrorCode , Registry } ;
2727use rustc_errors:: {
28- DiagnosticMessage , ErrorGuaranteed , PResult , SubdiagnosticMessage , TerminalUrl ,
28+ DiagnosticMessage , ErrorGuaranteed , Handler , PResult , SubdiagnosticMessage , TerminalUrl ,
2929} ;
3030use rustc_feature:: find_gated_cfg;
3131use rustc_fluent_macro:: fluent_messages;
@@ -55,7 +55,7 @@ use std::panic::{self, catch_unwind};
5555use std:: path:: PathBuf ;
5656use std:: process:: { self , Command , Stdio } ;
5757use std:: str;
58- use std:: sync:: LazyLock ;
58+ use std:: sync:: OnceLock ;
5959use std:: time:: Instant ;
6060
6161// This import blocks the use of panicking `print` and `println` in all the code
@@ -119,7 +119,7 @@ pub const EXIT_SUCCESS: i32 = 0;
119119/// Exit status code used for compilation failures and invalid flags.
120120pub const EXIT_FAILURE : i32 = 1 ;
121121
122- const BUG_REPORT_URL : & str = "https://github.com/rust-lang/rust/issues/new\
122+ pub const DEFAULT_BUG_REPORT_URL : & str = "https://github.com/rust-lang/rust/issues/new\
123123 ?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md";
124124
125125const ICE_REPORT_COMPILER_FLAGS : & [ & str ] = & [ "-Z" , "-C" , "--crate-type" ] ;
@@ -1195,43 +1195,66 @@ pub fn catch_with_exit_code(f: impl FnOnce() -> interface::Result<()>) -> i32 {
11951195 }
11961196}
11971197
1198- static DEFAULT_HOOK : LazyLock < Box < dyn Fn ( & panic:: PanicInfo < ' _ > ) + Sync + Send + ' static > > =
1199- LazyLock :: new ( || {
1200- let hook = panic:: take_hook ( ) ;
1201- panic:: set_hook ( Box :: new ( |info| {
1202- // If the error was caused by a broken pipe then this is not a bug.
1203- // Write the error and return immediately. See #98700.
1204- #[ cfg( windows) ]
1205- if let Some ( msg) = info. payload ( ) . downcast_ref :: < String > ( ) {
1206- if msg. starts_with ( "failed printing to stdout: " ) && msg. ends_with ( "(os error 232)" )
1207- {
1208- early_error_no_abort ( ErrorOutputType :: default ( ) , & msg) ;
1209- return ;
1210- }
1211- } ;
1198+ /// Stores the default panic hook, from before [`install_ice_hook`] was called.
1199+ static DEFAULT_HOOK : OnceLock < Box < dyn Fn ( & panic:: PanicInfo < ' _ > ) + Sync + Send + ' static > > =
1200+ OnceLock :: new ( ) ;
1201+
1202+ /// Installs a panic hook that will print the ICE message on unexpected panics.
1203+ ///
1204+ /// The hook is intended to be useable even by external tools. You can pass a custom
1205+ /// `bug_report_url`, or report arbitrary info in `extra_info`. Note that `extra_info` is called in
1206+ /// a context where *the thread is currently panicking*, so it must not panic or the process will
1207+ /// abort.
1208+ ///
1209+ /// If you have no extra info to report, pass the empty closure `|_| ()` as the argument to
1210+ /// extra_info.
1211+ ///
1212+ /// A custom rustc driver can skip calling this to set up a custom ICE hook.
1213+ pub fn install_ice_hook ( bug_report_url : & ' static str , extra_info : fn ( & Handler ) ) {
1214+ // If the user has not explicitly overridden "RUST_BACKTRACE", then produce
1215+ // full backtraces. When a compiler ICE happens, we want to gather
1216+ // as much information as possible to present in the issue opened
1217+ // by the user. Compiler developers and other rustc users can
1218+ // opt in to less-verbose backtraces by manually setting "RUST_BACKTRACE"
1219+ // (e.g. `RUST_BACKTRACE=1`)
1220+ if std:: env:: var ( "RUST_BACKTRACE" ) . is_err ( ) {
1221+ std:: env:: set_var ( "RUST_BACKTRACE" , "full" ) ;
1222+ }
12121223
1213- // Invoke the default handler, which prints the actual panic message and optionally a backtrace
1214- // Don't do this for delayed bugs, which already emit their own more useful backtrace.
1215- if !info. payload ( ) . is :: < rustc_errors:: DelayedBugPanic > ( ) {
1216- ( * DEFAULT_HOOK ) ( info) ;
1224+ let default_hook = DEFAULT_HOOK . get_or_init ( panic:: take_hook) ;
12171225
1218- // Separate the output with an empty line
1219- eprintln ! ( ) ;
1226+ panic:: set_hook ( Box :: new ( move |info| {
1227+ // If the error was caused by a broken pipe then this is not a bug.
1228+ // Write the error and return immediately. See #98700.
1229+ #[ cfg( windows) ]
1230+ if let Some ( msg) = info. payload ( ) . downcast_ref :: < String > ( ) {
1231+ if msg. starts_with ( "failed printing to stdout: " ) && msg. ends_with ( "(os error 232)" ) {
1232+ early_error_no_abort ( ErrorOutputType :: default ( ) , & msg) ;
1233+ return ;
12201234 }
1235+ } ;
12211236
1222- // Print the ICE message
1223- report_ice ( info, BUG_REPORT_URL ) ;
1224- } ) ) ;
1225- hook
1226- } ) ;
1237+ // Invoke the default handler, which prints the actual panic message and optionally a backtrace
1238+ // Don't do this for delayed bugs, which already emit their own more useful backtrace.
1239+ if !info. payload ( ) . is :: < rustc_errors:: DelayedBugPanic > ( ) {
1240+ ( * default_hook) ( info) ;
1241+
1242+ // Separate the output with an empty line
1243+ eprintln ! ( ) ;
1244+ }
1245+
1246+ // Print the ICE message
1247+ report_ice ( info, bug_report_url, extra_info) ;
1248+ } ) ) ;
1249+ }
12271250
12281251/// Prints the ICE message, including query stack, but without backtrace.
12291252///
12301253/// The message will point the user at `bug_report_url` to report the ICE.
12311254///
12321255/// When `install_ice_hook` is called, this function will be called as the panic
12331256/// hook.
1234- pub fn report_ice ( info : & panic:: PanicInfo < ' _ > , bug_report_url : & str ) {
1257+ pub fn report_ice ( info : & panic:: PanicInfo < ' _ > , bug_report_url : & str , extra_info : fn ( & Handler ) ) {
12351258 let fallback_bundle =
12361259 rustc_errors:: fallback_fluent_bundle ( crate :: DEFAULT_LOCALE_RESOURCES . to_vec ( ) , false ) ;
12371260 let emitter = Box :: new ( rustc_errors:: emitter:: EmitterWriter :: stderr (
@@ -1276,29 +1299,17 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
12761299
12771300 interface:: try_print_query_stack ( & handler, num_frames) ;
12781301
1302+ // We don't trust this callback not to panic itself, so run it at the end after we're sure we've
1303+ // printed all the relevant info.
1304+ extra_info ( & handler) ;
1305+
12791306 #[ cfg( windows) ]
12801307 if env:: var ( "RUSTC_BREAK_ON_ICE" ) . is_ok ( ) {
12811308 // Trigger a debugger if we crashed during bootstrap
12821309 unsafe { windows:: Win32 :: System :: Diagnostics :: Debug :: DebugBreak ( ) } ;
12831310 }
12841311}
12851312
1286- /// Installs a panic hook that will print the ICE message on unexpected panics.
1287- ///
1288- /// A custom rustc driver can skip calling this to set up a custom ICE hook.
1289- pub fn install_ice_hook ( ) {
1290- // If the user has not explicitly overridden "RUST_BACKTRACE", then produce
1291- // full backtraces. When a compiler ICE happens, we want to gather
1292- // as much information as possible to present in the issue opened
1293- // by the user. Compiler developers and other rustc users can
1294- // opt in to less-verbose backtraces by manually setting "RUST_BACKTRACE"
1295- // (e.g. `RUST_BACKTRACE=1`)
1296- if std:: env:: var ( "RUST_BACKTRACE" ) . is_err ( ) {
1297- std:: env:: set_var ( "RUST_BACKTRACE" , "full" ) ;
1298- }
1299- LazyLock :: force ( & DEFAULT_HOOK ) ;
1300- }
1301-
13021313/// This allows tools to enable rust logging without having to magically match rustc's
13031314/// tracing crate version.
13041315pub fn init_rustc_env_logger ( ) {
@@ -1369,7 +1380,7 @@ pub fn main() -> ! {
13691380 init_rustc_env_logger ( ) ;
13701381 signal_handler:: install ( ) ;
13711382 let mut callbacks = TimePassesCallbacks :: default ( ) ;
1372- install_ice_hook ( ) ;
1383+ install_ice_hook ( DEFAULT_BUG_REPORT_URL , |_| ( ) ) ;
13731384 let exit_code = catch_with_exit_code ( || {
13741385 let args = env:: args_os ( )
13751386 . enumerate ( )
0 commit comments