@@ -1426,72 +1426,62 @@ struct RootCollector<'a, 'tcx> {
14261426 entry_fn : Option < ( DefId , EntryFnType ) > ,
14271427}
14281428
1429- impl < ' v > RootCollector < ' _ , ' v > {
1429+ impl < ' tcx > RootCollector < ' _ , ' tcx > {
14301430 fn process_item ( & mut self , id : hir:: ItemId ) {
1431- match self . tcx . def_kind ( id. owner_id ) {
1431+ let tcx = self . tcx ;
1432+ let def_id = id. owner_id . to_def_id ( ) ;
1433+
1434+ match tcx. def_kind ( def_id) {
14321435 DefKind :: Enum | DefKind :: Struct | DefKind :: Union => {
1433- if self . strategy == MonoItemCollectionStrategy :: Eager
1434- && !self . tcx . generics_of ( id. owner_id ) . requires_monomorphization ( self . tcx )
1435- {
1436- debug ! ( "RootCollector: ADT drop-glue for `{id:?}`" , ) ;
1437- let id_args =
1438- ty:: GenericArgs :: for_item ( self . tcx , id. owner_id . to_def_id ( ) , |param, _| {
1439- match param. kind {
1440- GenericParamDefKind :: Lifetime => {
1441- self . tcx . lifetimes . re_erased . into ( )
1442- }
1443- GenericParamDefKind :: Type { .. }
1444- | GenericParamDefKind :: Const { .. } => {
1445- unreachable ! (
1446- "`own_requires_monomorphization` check means that \
1447- we should have no type/const params"
1448- )
1449- }
1450- }
1451- } ) ;
1452-
1453- // This type is impossible to instantiate, so we should not try to
1454- // generate a `drop_in_place` instance for it.
1455- if self . tcx . instantiate_and_check_impossible_predicates ( (
1456- id. owner_id . to_def_id ( ) ,
1457- id_args,
1458- ) ) {
1459- return ;
1460- }
1436+ if self . strategy != MonoItemCollectionStrategy :: Eager {
1437+ return ;
1438+ }
14611439
1462- let ty =
1463- self . tcx . type_of ( id. owner_id . to_def_id ( ) ) . instantiate ( self . tcx , id_args) ;
1464- assert ! ( !ty. has_non_region_param( ) ) ;
1465- visit_drop_use ( self . tcx , ty, true , DUMMY_SP , self . output ) ;
1440+ if tcx. generics_of ( def_id) . requires_monomorphization ( tcx) {
1441+ return ;
1442+ }
1443+ let args = ty:: GenericArgs :: for_item ( tcx, def_id, |param, _| {
1444+ expect_and_erase_regions ( tcx, param)
1445+ } ) ;
1446+ if tcx. instantiate_and_check_impossible_predicates ( ( def_id, args) ) {
1447+ return ;
14661448 }
1449+
1450+ let ty = tcx. type_of ( def_id) . instantiate ( tcx, args) ;
1451+ debug_assert ! ( !ty. has_non_region_param( ) ) ;
1452+
1453+ debug ! ( "RootCollector: ADT drop-glue for `{id:?}`" ) ;
1454+ visit_drop_use ( tcx, ty, true , DUMMY_SP , self . output ) ;
14671455 }
14681456 DefKind :: GlobalAsm => {
1469- debug ! (
1470- "RootCollector: ItemKind::GlobalAsm({})" ,
1471- self . tcx. def_path_str( id. owner_id)
1472- ) ;
1457+ debug ! ( "RootCollector: ItemKind::GlobalAsm({})" , tcx. def_path_str( def_id) ) ;
14731458 self . output . push ( dummy_spanned ( MonoItem :: GlobalAsm ( id) ) ) ;
14741459 }
14751460 DefKind :: Static { .. } => {
1476- let def_id = id. owner_id . to_def_id ( ) ;
1477- debug ! ( "RootCollector: ItemKind::Static({})" , self . tcx. def_path_str( def_id) ) ;
1461+ debug ! ( "RootCollector: ItemKind::Static({})" , tcx. def_path_str( def_id) ) ;
14781462 self . output . push ( dummy_spanned ( MonoItem :: Static ( def_id) ) ) ;
14791463 }
14801464 DefKind :: Const => {
1481- // Const items only generate mono items if they are actually used somewhere.
1482- // Just declaring them is insufficient.
1465+ if tcx. generics_of ( def_id) . requires_monomorphization ( tcx) {
1466+ return ;
1467+ }
14831468
1484- // But even just declaring them must collect the items they refer to
1485- // unless their generics require monomorphization.
1486- if !self . tcx . generics_of ( id. owner_id ) . own_requires_monomorphization ( )
1487- && let Ok ( val) = self . tcx . const_eval_poly ( id. owner_id . to_def_id ( ) )
1488- {
1489- collect_const_value ( self . tcx , val, self . output ) ;
1469+ let args = ty:: GenericArgs :: for_item ( tcx, def_id, |param, _| {
1470+ expect_and_erase_regions ( tcx, param)
1471+ } ) ;
1472+ if tcx. instantiate_and_check_impossible_predicates ( ( def_id, args) ) {
1473+ return ;
14901474 }
1475+
1476+ let Ok ( val) = tcx. const_eval_poly ( def_id) else { return } ;
1477+
1478+ // Const items only generate mono items if they are actually used somewhere.
1479+ // Just declaring them is insufficient.
1480+ collect_const_value ( tcx, val, self . output ) ;
14911481 }
14921482 DefKind :: Impl { .. } => {
14931483 if self . strategy == MonoItemCollectionStrategy :: Eager {
1494- create_mono_items_for_default_impls ( self . tcx , id, self . output ) ;
1484+ create_mono_items_for_default_impls ( tcx, id, self . output ) ;
14951485 }
14961486 }
14971487 DefKind :: Fn => {
@@ -1614,35 +1604,23 @@ fn create_mono_items_for_default_impls<'tcx>(
16141604 item : hir:: ItemId ,
16151605 output : & mut MonoItems < ' tcx > ,
16161606) {
1617- let Some ( impl_) = tcx. impl_trait_header ( item. owner_id ) else {
1618- return ;
1619- } ;
1620-
1607+ let impl_def_id = item. owner_id . to_def_id ( ) ;
1608+ let Some ( impl_) = tcx. impl_trait_header ( impl_def_id) else { return } ;
16211609 if matches ! ( impl_. polarity, ty:: ImplPolarity :: Negative ) {
16221610 return ;
16231611 }
16241612
1625- if tcx. generics_of ( item. owner_id ) . own_requires_monomorphization ( ) {
1626- return ;
1627- }
1628-
16291613 // Lifetimes never affect trait selection, so we are allowed to eagerly
16301614 // instantiate an instance of an impl method if the impl (and method,
16311615 // which we check below) is only parameterized over lifetime. In that case,
16321616 // we use the ReErased, which has no lifetime information associated with
16331617 // it, to validate whether or not the impl is legal to instantiate at all.
1634- let only_region_params = |param : & ty:: GenericParamDef , _: & _ | match param. kind {
1635- GenericParamDefKind :: Lifetime => tcx. lifetimes . re_erased . into ( ) ,
1636- GenericParamDefKind :: Type { .. } | GenericParamDefKind :: Const { .. } => {
1637- unreachable ! (
1638- "`own_requires_monomorphization` check means that \
1639- we should have no type/const params"
1640- )
1641- }
1642- } ;
1643- let impl_args = GenericArgs :: for_item ( tcx, item. owner_id . to_def_id ( ) , only_region_params) ;
1644- let trait_ref = impl_. trait_ref . instantiate ( tcx, impl_args) ;
1645-
1618+ if tcx. generics_of ( impl_def_id) . requires_monomorphization ( tcx) {
1619+ return ;
1620+ }
1621+ let impl_args = ty:: GenericArgs :: for_item ( tcx, impl_def_id, |param, _| {
1622+ expect_and_erase_regions ( tcx, param)
1623+ } ) ;
16461624 // Unlike 'lazy' monomorphization that begins by collecting items transitively
16471625 // called by `main` or other global items, when eagerly monomorphizing impl
16481626 // items, we never actually check that the predicates of this impl are satisfied
@@ -1652,13 +1630,15 @@ fn create_mono_items_for_default_impls<'tcx>(
16521630 // consider higher-ranked predicates such as `for<'a> &'a mut [u8]: Copy` to
16531631 // be trivially false. We must now check that the impl has no impossible-to-satisfy
16541632 // predicates.
1655- if tcx. instantiate_and_check_impossible_predicates ( ( item . owner_id . to_def_id ( ) , impl_args) ) {
1633+ if tcx. instantiate_and_check_impossible_predicates ( ( impl_def_id , impl_args) ) {
16561634 return ;
16571635 }
16581636
1637+ let trait_ref = impl_. trait_ref . instantiate ( tcx, impl_args) ;
1638+
16591639 let typing_env = ty:: TypingEnv :: fully_monomorphized ( ) ;
16601640 let trait_ref = tcx. normalize_erasing_regions ( typing_env, trait_ref) ;
1661- let overridden_methods = tcx. impl_item_implementor_ids ( item . owner_id ) ;
1641+ let overridden_methods = tcx. impl_item_implementor_ids ( impl_def_id ) ;
16621642 for method in tcx. provided_trait_methods ( trait_ref. def_id ) {
16631643 if overridden_methods. contains_key ( & method. def_id ) {
16641644 continue ;
@@ -1671,7 +1651,9 @@ fn create_mono_items_for_default_impls<'tcx>(
16711651 // As mentioned above, the method is legal to eagerly instantiate if it
16721652 // only has lifetime generic parameters. This is validated by calling
16731653 // `own_requires_monomorphization` on both the impl and method.
1674- let args = trait_ref. args . extend_to ( tcx, method. def_id , only_region_params) ;
1654+ let args = trait_ref
1655+ . args
1656+ . extend_to ( tcx, method. def_id , |param, _| expect_and_erase_regions ( tcx, param) ) ;
16751657 let instance = ty:: Instance :: expect_resolve ( tcx, typing_env, method. def_id , args, DUMMY_SP ) ;
16761658
16771659 let mono_item = create_fn_mono_item ( tcx, instance, DUMMY_SP ) ;
@@ -1681,6 +1663,18 @@ fn create_mono_items_for_default_impls<'tcx>(
16811663 }
16821664}
16831665
1666+ fn expect_and_erase_regions < ' tcx > (
1667+ tcx : TyCtxt < ' tcx > ,
1668+ param : & ty:: GenericParamDef ,
1669+ ) -> ty:: GenericArg < ' tcx > {
1670+ match param. kind {
1671+ GenericParamDefKind :: Lifetime => tcx. lifetimes . re_erased . into ( ) ,
1672+ GenericParamDefKind :: Type { .. } | GenericParamDefKind :: Const { .. } => {
1673+ bug ! ( "unexpected non-region param" )
1674+ }
1675+ }
1676+ }
1677+
16841678//=-----------------------------------------------------------------------------
16851679// Top-level entry point, tying it all together
16861680//=-----------------------------------------------------------------------------
0 commit comments