@@ -51,7 +51,8 @@ use rustc_metadata::creader::MetadataLoader;
5151use rustc_metadata:: locator;
5252use rustc_parse:: { new_parser_from_file, new_parser_from_source_str, unwrap_or_emit_fatal} ;
5353use rustc_session:: config:: {
54- nightly_options, ErrorOutputType , Input , OutFileName , OutputType , CG_OPTIONS , Z_OPTIONS ,
54+ nightly_options, ErrorOutputType , Input , OutFileName , OutputType , UnstableOptions , CG_OPTIONS ,
55+ Z_OPTIONS ,
5556} ;
5657use rustc_session:: getopts:: { self , Matches } ;
5758use rustc_session:: lint:: { Lint , LintId } ;
@@ -301,6 +302,8 @@ fn run_compiler(
301302 let Some ( matches) = handle_options( & default_early_dcx, & args) else { return Ok ( ( ) ) } ;
302303
303304 let sopts = config:: build_session_options( & mut default_early_dcx, & matches) ;
305+ // fully initialize ice path static once unstable options are available as context
306+ let ice_file = ice_path_with_config( Some ( & sopts. unstable_opts) ) ;
304307
305308 if let Some ( ref code) = matches. opt_str( "explain" ) {
306309 handle_explain( & default_early_dcx, diagnostics_registry( ) , code, sopts. color) ;
@@ -315,7 +318,7 @@ fn run_compiler(
315318 input: Input :: File ( PathBuf :: new( ) ) ,
316319 output_file: ofile,
317320 output_dir: odir,
318- ice_file: ice_path ( ) . clone ( ) ,
321+ ice_file,
319322 file_loader,
320323 locale_resources: DEFAULT_LOCALE_RESOURCES ,
321324 lint_caps: Default :: default ( ) ,
@@ -357,7 +360,11 @@ fn run_compiler(
357360 // printing some information without compiling, or exiting immediately
358361 // after parsing, etc.
359362 let early_exit = || {
360- if let Some ( guar) = sess. dcx( ) . has_errors( ) { Err ( guar) } else { Ok ( ( ) ) }
363+ if let Some ( guar) = sess. dcx( ) . has_errors( ) {
364+ Err ( guar)
365+ } else {
366+ Ok ( ( ) )
367+ }
361368 } ;
362369
363370 // This implements `-Whelp`. It should be handled very early, like
@@ -567,7 +574,11 @@ fn handle_explain(early_dcx: &EarlyDiagCtxt, registry: Registry, code: &str, col
567574fn show_md_content_with_pager( content: & str , color: ColorConfig ) {
568575 let mut fallback_to_println = false ;
569576 let pager_name = env:: var_os( "PAGER" ) . unwrap_or_else( || {
570- if cfg!( windows) { OsString :: from( "more.com" ) } else { OsString :: from( "less" ) }
577+ if cfg!( windows) {
578+ OsString :: from( "more.com" )
579+ } else {
580+ OsString :: from( "less" )
581+ }
571582 } ) ;
572583
573584 let mut cmd = Command :: new( & pager_name) ;
@@ -1306,16 +1317,26 @@ pub fn catch_with_exit_code(f: impl FnOnce() -> interface::Result<()>) -> i32 {
13061317
13071318static ICE_PATH : OnceLock <Option <PathBuf >> = OnceLock :: new( ) ;
13081319
1320+ // This function should only be called from the ICE hook.
1321+ //
1322+ // The intended behavior is that `run_compiler` will invoke `ice_path_with_config` early in the
1323+ // initialization process to properly initialize the ICE_PATH static based on parsed CLI flags.
1324+ //
1325+ // Subsequent calls to either function will then return the proper ICE path as configured by
1326+ // the environment and cli flags
13091327fn ice_path( ) -> & ' static Option <PathBuf > {
1328+ ice_path_with_config( None )
1329+ }
1330+
1331+ fn ice_path_with_config( config: Option <& UnstableOptions >) -> & ' static Option <PathBuf > {
1332+ if ICE_PATH . get( ) . is_some( ) && config. is_some( ) && cfg!( debug_assertions) {
1333+ warn!( "ICE_PATH has already been initialized -- files may be emitted at unintended paths" )
1334+ }
1335+
13101336 ICE_PATH . get_or_init( || {
13111337 if !rustc_feature:: UnstableFeatures :: from_environment( None ) . is_nightly_build( ) {
13121338 return None ;
13131339 }
1314- if let Some ( s) = std:: env:: var_os( "RUST_BACKTRACE" )
1315- && s == "0"
1316- {
1317- return None ;
1318- }
13191340 let mut path = match std:: env:: var_os( "RUSTC_ICE" ) {
13201341 Some ( s) => {
13211342 if s == "0" {
@@ -1324,7 +1345,10 @@ fn ice_path() -> &'static Option<PathBuf> {
13241345 }
13251346 PathBuf :: from( s)
13261347 }
1327- None => std:: env:: current_dir( ) . unwrap_or_default( ) ,
1348+ None => config
1349+ . and_then( |unstable_opts| unstable_opts. error_metrics. to_owned( ) )
1350+ . or_else( || std:: env:: current_dir( ) . ok( ) )
1351+ . unwrap_or_default( ) ,
13281352 } ;
13291353 let now: OffsetDateTime = SystemTime :: now( ) . into( ) ;
13301354 let file_now = now
0 commit comments