@@ -185,6 +185,7 @@ use rustc_middle::ty::{
185185} ;
186186use rustc_middle:: ty:: { GenericArgKind , GenericArgs } ;
187187use rustc_middle:: { middle:: codegen_fn_attrs:: CodegenFnAttrFlags , mir:: visit:: TyContext } ;
188+ use rustc_session:: config:: CrateType ;
188189use rustc_session:: config:: EntryFnType ;
189190use rustc_session:: lint:: builtin:: LARGE_ASSIGNMENTS ;
190191use rustc_session:: Limit ;
@@ -317,6 +318,7 @@ fn collect_roots(tcx: TyCtxt<'_>, mode: MonoItemCollectionMode) -> Vec<MonoItem<
317318 }
318319
319320 collector. push_extra_entry_roots ( ) ;
321+ collector. push_extra_roots_from_mir_only_rlibs ( ) ;
320322 }
321323
322324 // We can only codegen items that are instantiable - items all of
@@ -976,15 +978,15 @@ fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) ->
976978 } ;
977979
978980 if tcx. is_foreign_item ( def_id) {
979- if tcx. sess . opts . unstable_opts . mir_only_libs {
981+ if tcx. building_mir_only_rlib ( ) {
980982 return tcx. is_mir_available ( instance. def_id ( ) ) ;
981983 } else {
982984 // Foreign items are always linked against, there's no way of instantiating them.
983985 return false ;
984986 }
985987 }
986988
987- if tcx. sess . opts . unstable_opts . mir_only_libs {
989+ if tcx. building_mir_only_rlib ( ) {
988990 let has_mir = tcx. is_mir_available ( instance. def_id ( ) ) ;
989991 return has_mir || matches ! ( tcx. def_kind( instance. def_id( ) ) , DefKind :: Static ( _) ) ;
990992 }
@@ -1281,7 +1283,6 @@ impl<'v> RootCollector<'_, 'v> {
12811283 let Some ( ( main_def_id, EntryFnType :: Main { .. } ) ) = self . entry_fn else {
12821284 return ;
12831285 } ;
1284-
12851286 let start_def_id = self . tcx . require_lang_item ( LangItem :: Start , None ) ;
12861287 let main_ret_ty = self . tcx . fn_sig ( main_def_id) . no_bound_vars ( ) . unwrap ( ) . output ( ) ;
12871288
@@ -1305,34 +1306,50 @@ impl<'v> RootCollector<'_, 'v> {
13051306 . unwrap ( ) ;
13061307
13071308 self . output . push ( create_fn_mono_item ( self . tcx , start_instance, DUMMY_SP ) ) ;
1309+ }
13081310
1311+ fn push_extra_roots_from_mir_only_rlibs ( & mut self ) {
13091312 // An upstream extern function may be used anywhere in the dependency tree, so we
13101313 // cannot do any reachability analysis on them. We blindly monomorphize every
13111314 // extern function declared anywhere in our dependency tree. We must give them
13121315 // GloballyShared codegen because we don't know if the only call to an upstream
13131316 // extern function is also upstream: We don't have reachability information. All we
13141317 // can do is codegen all extern functions and pray for the linker to delete the
13151318 // ones that are reachable.
1316- if self . tcx . building_mir_only_bin ( ) {
1317- for ( symbol, _info) in
1318- self . tcx . crates ( ( ) ) . into_iter ( ) . flat_map ( |krate| self . tcx . exported_symbols ( * krate) )
1319- {
1320- let def_id = match symbol {
1321- ExportedSymbol :: NonGeneric ( def_id) => def_id,
1322- _ => {
1323- continue ;
1324- }
1325- } ;
1326- if self . tcx . def_kind ( def_id) != DefKind :: Fn {
1319+ if !self . tcx . crate_types ( ) . iter ( ) . any ( |c| !matches ! ( c, CrateType :: Rlib ) ) {
1320+ return ;
1321+ }
1322+
1323+ for ( symbol, _info) in self
1324+ . tcx
1325+ . mir_only_crates ( ( ) )
1326+ . into_iter ( )
1327+ . flat_map ( |krate| self . tcx . exported_symbols ( * krate) )
1328+ {
1329+ let def_id = match symbol {
1330+ ExportedSymbol :: NonGeneric ( def_id) => def_id,
1331+ ExportedSymbol :: ThreadLocalShim ( def_id) => {
1332+ let item = MonoItem :: Fn ( Instance {
1333+ def : InstanceDef :: ThreadLocalShim ( * def_id) ,
1334+ args : GenericArgs :: empty ( ) ,
1335+ } ) ;
1336+ self . output . push ( respan ( DUMMY_SP , item) ) ;
13271337 continue ;
13281338 }
1329- let instance = Instance :: mono ( self . tcx , * def_id) ;
1330- // FIXME: This is probably not the right span. What is?
1331- let item = create_fn_mono_item ( self . tcx , instance, DUMMY_SP ) ;
1332- // FIXME: Do we need this check?
1333- if !self . output . iter ( ) . any ( |out| out. node . def_id ( ) == * def_id) {
1334- self . output . push ( item) ;
1339+ _ => continue ,
1340+ } ;
1341+ match self . tcx . def_kind ( def_id) {
1342+ DefKind :: Fn | DefKind :: AssocFn => {
1343+ let instance = Instance :: mono ( self . tcx , * def_id) ;
1344+ let item = create_fn_mono_item ( self . tcx , instance, DUMMY_SP ) ;
1345+ if !self . output . iter ( ) . any ( |out| out. node . def_id ( ) == * def_id) {
1346+ self . output . push ( item) ;
1347+ }
1348+ }
1349+ DefKind :: Static ( _) => {
1350+ self . output . push ( dummy_spanned ( MonoItem :: Static ( * def_id) ) ) ;
13351351 }
1352+ _ => { }
13361353 }
13371354 }
13381355 }
0 commit comments