@@ -5,6 +5,7 @@ use rustc_ast::{LitKind, MetaItemKind};
55use rustc_codegen_ssa:: traits:: CodegenBackend ;
66use rustc_data_structures:: defer;
77use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
8+ use rustc_data_structures:: jobserver;
89use rustc_data_structures:: stable_hasher:: StableHasher ;
910use rustc_data_structures:: sync:: Lrc ;
1011use rustc_errors:: registry:: Registry ;
@@ -21,7 +22,7 @@ use rustc_session::config::{self, Cfg, CheckCfg, ExpectedValues, Input, OutFileN
2122use rustc_session:: filesearch:: { self , sysroot_candidates} ;
2223use rustc_session:: parse:: ParseSess ;
2324use rustc_session:: { lint, CompilerIO , EarlyDiagCtxt , Session } ;
24- use rustc_span:: source_map:: FileLoader ;
25+ use rustc_span:: source_map:: { FileLoader , RealFileLoader , SourceMapInputs } ;
2526use rustc_span:: symbol:: sym;
2627use rustc_span:: FileName ;
2728use std:: path:: PathBuf ;
@@ -323,6 +324,18 @@ pub struct Config {
323324 pub expanded_args : Vec < String > ,
324325}
325326
327+ /// Initialize jobserver before getting `jobserver::client` and `build_session`.
328+ pub ( crate ) fn initialize_checked_jobserver ( early_dcx : & EarlyDiagCtxt ) {
329+ jobserver:: initialize_checked ( |err| {
330+ #[ allow( rustc:: untranslatable_diagnostic) ]
331+ #[ allow( rustc:: diagnostic_outside_of_impl) ]
332+ early_dcx
333+ . early_struct_warn ( err)
334+ . with_note ( "the build environment is likely misconfigured" )
335+ . emit ( )
336+ } ) ;
337+ }
338+
326339// JUSTIFICATION: before session exists, only config
327340#[ allow( rustc:: bad_opt_access) ]
328341#[ allow( rustc:: untranslatable_diagnostic) ] // FIXME: make this translatable
@@ -334,20 +347,25 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
334347
335348 // Check jobserver before run_in_thread_pool_with_globals, which call jobserver::acquire_thread
336349 let early_dcx = EarlyDiagCtxt :: new ( config. opts . error_format ) ;
337- early_dcx. initialize_checked_jobserver ( ) ;
350+ initialize_checked_jobserver ( & early_dcx) ;
351+
352+ crate :: callbacks:: setup_callbacks ( ) ;
353+
354+ let sysroot = filesearch:: materialize_sysroot ( config. opts . maybe_sysroot . clone ( ) ) ;
355+ let target = config:: build_target_config ( & early_dcx, & config. opts , & sysroot) ;
356+ let file_loader = config. file_loader . unwrap_or_else ( || Box :: new ( RealFileLoader ) ) ;
357+ let path_mapping = config. opts . file_path_mapping ( ) ;
358+ let hash_kind = config. opts . unstable_opts . src_hash_algorithm ( & target) ;
338359
339360 util:: run_in_thread_pool_with_globals (
340361 config. opts . edition ,
341362 config. opts . unstable_opts . threads ,
363+ SourceMapInputs { file_loader, path_mapping, hash_kind } ,
342364 |current_gcx| {
343- crate :: callbacks :: setup_callbacks ( ) ;
344-
365+ // The previous `early_dcx` can't be reused here because it doesn't
366+ // impl `Send`. Creating a new one is fine.
345367 let early_dcx = EarlyDiagCtxt :: new ( config. opts . error_format ) ;
346368
347- let sysroot = filesearch:: materialize_sysroot ( config. opts . maybe_sysroot . clone ( ) ) ;
348-
349- let target = config:: build_target_config ( & early_dcx, & config. opts , & sysroot) ;
350-
351369 let codegen_backend = match config. make_codegen_backend {
352370 None => util:: get_codegen_backend (
353371 & early_dcx,
@@ -372,9 +390,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
372390 config. opts . unstable_opts . translate_directionality_markers ,
373391 ) {
374392 Ok ( bundle) => bundle,
375- Err ( e) => {
376- early_dcx. early_fatal ( format ! ( "failed to load fluent bundle: {e}" ) ) ;
377- }
393+ Err ( e) => early_dcx. early_fatal ( format ! ( "failed to load fluent bundle: {e}" ) ) ,
378394 } ;
379395
380396 let mut locale_resources = Vec :: from ( config. locale_resources ) ;
@@ -393,7 +409,6 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
393409 config. registry . clone ( ) ,
394410 locale_resources,
395411 config. lint_caps ,
396- config. file_loader ,
397412 target,
398413 sysroot,
399414 util:: rustc_version_str ( ) . unwrap_or ( "unknown" ) ,
@@ -440,45 +455,43 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
440455 current_gcx,
441456 } ;
442457
443- rustc_span:: set_source_map ( compiler. sess . psess . clone_source_map ( ) , move || {
444- // There are two paths out of `f`.
445- // - Normal exit.
446- // - Panic, e.g. triggered by `abort_if_errors`.
447- //
448- // We must run `finish_diagnostics` in both cases.
449- let res = {
450- // If `f` panics, `finish_diagnostics` will run during
451- // unwinding because of the `defer`.
452- let mut guar = None ;
453- let sess_abort_guard = defer ( || {
454- guar = compiler. sess . finish_diagnostics ( & config. registry ) ;
455- } ) ;
456-
457- let res = f ( & compiler) ;
458-
459- // If `f` doesn't panic, `finish_diagnostics` will run
460- // normally when `sess_abort_guard` is dropped.
461- drop ( sess_abort_guard) ;
462-
463- // If `finish_diagnostics` emits errors (e.g. stashed
464- // errors) we can't return an error directly, because the
465- // return type of this function is `R`, not `Result<R, E>`.
466- // But we need to communicate the errors' existence to the
467- // caller, otherwise the caller might mistakenly think that
468- // no errors occurred and return a zero exit code. So we
469- // abort (panic) instead, similar to if `f` had panicked.
470- if guar. is_some ( ) {
471- compiler. sess . dcx ( ) . abort_if_errors ( ) ;
472- }
458+ // There are two paths out of `f`.
459+ // - Normal exit.
460+ // - Panic, e.g. triggered by `abort_if_errors`.
461+ //
462+ // We must run `finish_diagnostics` in both cases.
463+ let res = {
464+ // If `f` panics, `finish_diagnostics` will run during
465+ // unwinding because of the `defer`.
466+ let mut guar = None ;
467+ let sess_abort_guard = defer ( || {
468+ guar = compiler. sess . finish_diagnostics ( & config. registry ) ;
469+ } ) ;
470+
471+ let res = f ( & compiler) ;
472+
473+ // If `f` doesn't panic, `finish_diagnostics` will run
474+ // normally when `sess_abort_guard` is dropped.
475+ drop ( sess_abort_guard) ;
476+
477+ // If `finish_diagnostics` emits errors (e.g. stashed
478+ // errors) we can't return an error directly, because the
479+ // return type of this function is `R`, not `Result<R, E>`.
480+ // But we need to communicate the errors' existence to the
481+ // caller, otherwise the caller might mistakenly think that
482+ // no errors occurred and return a zero exit code. So we
483+ // abort (panic) instead, similar to if `f` had panicked.
484+ if guar. is_some ( ) {
485+ compiler. sess . dcx ( ) . abort_if_errors ( ) ;
486+ }
473487
474- res
475- } ;
488+ res
489+ } ;
476490
477- let prof = compiler. sess . prof . clone ( ) ;
478- prof. generic_activity ( "drop_compiler" ) . run ( move || drop ( compiler) ) ;
491+ let prof = compiler. sess . prof . clone ( ) ;
492+ prof. generic_activity ( "drop_compiler" ) . run ( move || drop ( compiler) ) ;
479493
480- res
481- } )
494+ res
482495 } ,
483496 )
484497}
0 commit comments