@@ -203,8 +203,8 @@ where
203203 // available to downstream crates. This depends on whether we are in
204204 // share-generics mode and whether the current crate can even have
205205 // downstream crates.
206- let export_generics =
207- cx. tcx . sess . opts . share_generics ( ) && cx . tcx . local_crate_exports_generics ( ) ;
206+ let can_export_generics = cx . tcx . local_crate_exports_generics ( ) ;
207+ let always_export_generics = can_export_generics && cx. tcx . sess . opts . share_generics ( ) ;
208208
209209 let cgu_name_builder = & mut CodegenUnitNameBuilder :: new ( cx. tcx ) ;
210210 let cgu_name_cache = & mut FxHashMap :: default ( ) ;
@@ -244,7 +244,8 @@ where
244244 cx. tcx ,
245245 & mono_item,
246246 & mut can_be_internalized,
247- export_generics,
247+ can_export_generics,
248+ always_export_generics,
248249 ) ;
249250 if visibility == Visibility :: Hidden && can_be_internalized {
250251 internalization_candidates. insert ( mono_item) ;
@@ -727,12 +728,19 @@ fn mono_item_linkage_and_visibility<'tcx>(
727728 tcx : TyCtxt < ' tcx > ,
728729 mono_item : & MonoItem < ' tcx > ,
729730 can_be_internalized : & mut bool ,
730- export_generics : bool ,
731+ can_export_generics : bool ,
732+ always_export_generics : bool ,
731733) -> ( Linkage , Visibility ) {
732734 if let Some ( explicit_linkage) = mono_item. explicit_linkage ( tcx) {
733735 return ( explicit_linkage, Visibility :: Default ) ;
734736 }
735- let vis = mono_item_visibility ( tcx, mono_item, can_be_internalized, export_generics) ;
737+ let vis = mono_item_visibility (
738+ tcx,
739+ mono_item,
740+ can_be_internalized,
741+ can_export_generics,
742+ always_export_generics,
743+ ) ;
736744 ( Linkage :: External , vis)
737745}
738746
@@ -755,7 +763,8 @@ fn mono_item_visibility<'tcx>(
755763 tcx : TyCtxt < ' tcx > ,
756764 mono_item : & MonoItem < ' tcx > ,
757765 can_be_internalized : & mut bool ,
758- export_generics : bool ,
766+ can_export_generics : bool ,
767+ always_export_generics : bool ,
759768) -> Visibility {
760769 let instance = match mono_item {
761770 // This is pretty complicated; see below.
@@ -812,7 +821,11 @@ fn mono_item_visibility<'tcx>(
812821
813822 // Upstream `DefId` instances get different handling than local ones.
814823 let Some ( def_id) = def_id. as_local ( ) else {
815- return if export_generics && is_generic {
824+ return if is_generic
825+ && ( always_export_generics
826+ || ( can_export_generics
827+ && tcx. codegen_fn_attrs ( def_id) . inline == rustc_attr:: InlineAttr :: Never ) )
828+ {
816829 // If it is an upstream monomorphization and we export generics, we must make
817830 // it available to downstream crates.
818831 * can_be_internalized = false ;
@@ -823,7 +836,12 @@ fn mono_item_visibility<'tcx>(
823836 } ;
824837
825838 if is_generic {
826- if export_generics {
839+ // An inline(never) function won't get inlined in upstream crates anyway, so we might as
840+ // well export it.
841+ if always_export_generics
842+ || ( can_export_generics
843+ && tcx. codegen_fn_attrs ( def_id) . inline == rustc_attr:: InlineAttr :: Never )
844+ {
827845 if tcx. is_unreachable_local_definition ( def_id) {
828846 // This instance cannot be used from another crate.
829847 Visibility :: Hidden
0 commit comments