@@ -208,8 +208,8 @@ where
208208 // available to downstream crates. This depends on whether we are in
209209 // share-generics mode and whether the current crate can even have
210210 // downstream crates.
211- let export_generics =
212- cx. tcx . sess . opts . share_generics ( ) && cx . tcx . local_crate_exports_generics ( ) ;
211+ let can_export_generics = cx . tcx . local_crate_exports_generics ( ) ;
212+ let always_export_generics = can_export_generics && cx. tcx . sess . opts . share_generics ( ) ;
213213
214214 let cgu_name_builder = & mut CodegenUnitNameBuilder :: new ( cx. tcx ) ;
215215 let cgu_name_cache = & mut UnordMap :: default ( ) ;
@@ -249,7 +249,8 @@ where
249249 cx. tcx ,
250250 & mono_item,
251251 & mut can_be_internalized,
252- export_generics,
252+ can_export_generics,
253+ always_export_generics,
253254 ) ;
254255 if visibility == Visibility :: Hidden && can_be_internalized {
255256 internalization_candidates. insert ( mono_item) ;
@@ -739,12 +740,19 @@ fn mono_item_linkage_and_visibility<'tcx>(
739740 tcx : TyCtxt < ' tcx > ,
740741 mono_item : & MonoItem < ' tcx > ,
741742 can_be_internalized : & mut bool ,
742- export_generics : bool ,
743+ can_export_generics : bool ,
744+ always_export_generics : bool ,
743745) -> ( Linkage , Visibility ) {
744746 if let Some ( explicit_linkage) = mono_item. explicit_linkage ( tcx) {
745747 return ( explicit_linkage, Visibility :: Default ) ;
746748 }
747- let vis = mono_item_visibility ( tcx, mono_item, can_be_internalized, export_generics) ;
749+ let vis = mono_item_visibility (
750+ tcx,
751+ mono_item,
752+ can_be_internalized,
753+ can_export_generics,
754+ always_export_generics,
755+ ) ;
748756 ( Linkage :: External , vis)
749757}
750758
@@ -767,7 +775,8 @@ fn mono_item_visibility<'tcx>(
767775 tcx : TyCtxt < ' tcx > ,
768776 mono_item : & MonoItem < ' tcx > ,
769777 can_be_internalized : & mut bool ,
770- export_generics : bool ,
778+ can_export_generics : bool ,
779+ always_export_generics : bool ,
771780) -> Visibility {
772781 let instance = match mono_item {
773782 // This is pretty complicated; see below.
@@ -826,7 +835,11 @@ fn mono_item_visibility<'tcx>(
826835
827836 // Upstream `DefId` instances get different handling than local ones.
828837 let Some ( def_id) = def_id. as_local ( ) else {
829- return if export_generics && is_generic {
838+ return if is_generic
839+ && ( always_export_generics
840+ || ( can_export_generics
841+ && tcx. codegen_fn_attrs ( def_id) . inline == rustc_attr:: InlineAttr :: Never ) )
842+ {
830843 // If it is an upstream monomorphization and we export generics, we must make
831844 // it available to downstream crates.
832845 * can_be_internalized = false ;
@@ -837,7 +850,10 @@ fn mono_item_visibility<'tcx>(
837850 } ;
838851
839852 if is_generic {
840- if export_generics {
853+ if always_export_generics
854+ || ( can_export_generics
855+ && tcx. codegen_fn_attrs ( def_id) . inline == rustc_attr:: InlineAttr :: Never )
856+ {
841857 if tcx. is_unreachable_local_definition ( def_id) {
842858 // This instance cannot be used from another crate.
843859 Visibility :: Hidden
0 commit comments