@@ -1326,27 +1326,40 @@ fn create_mono_items_for_default_impls<'tcx>(
1326
1326
return ;
1327
1327
}
1328
1328
1329
+ let Some ( trait_ref) = tcx. impl_trait_ref ( item. owner_id ) else {
1330
+ return ;
1331
+ } ;
1332
+
1333
+ // Lifetimes never affect trait selection, so we are allowed to eagerly
1334
+ // instantiate an instance of an impl method if the impl (and method,
1335
+ // which we check below) is only parameterized over lifetime. In that case,
1336
+ // we use the ReErased, which has no lifetime information associated with
1337
+ // it, to validate whether or not the impl is legal to instantiate at all.
1338
+ let only_region_params = |param : & ty:: GenericParamDef , _: & _ | match param. kind {
1339
+ GenericParamDefKind :: Lifetime => tcx. lifetimes . re_erased . into ( ) ,
1340
+ GenericParamDefKind :: Type { .. } | GenericParamDefKind :: Const { .. } => {
1341
+ unreachable ! (
1342
+ "`own_requires_monomorphization` check means that \
1343
+ we should have no type/const params"
1344
+ )
1345
+ }
1346
+ } ;
1347
+ let impl_substs = InternalSubsts :: for_item ( tcx, item. owner_id . to_def_id ( ) , only_region_params) ;
1348
+ let trait_ref = trait_ref. subst ( tcx, impl_substs) ;
1349
+
1329
1350
// Unlike 'lazy' monomorphization that begins by collecting items transitively
1330
1351
// called by `main` or other global items, when eagerly monomorphizing impl
1331
1352
// items, we never actually check that the predicates of this impl are satisfied
1332
1353
// in a empty reveal-all param env (i.e. with no assumptions).
1333
1354
//
1334
- // Even though this impl has no substitutions, because we don't consider higher-
1335
- // ranked predicates such as `for<'a> &'a mut [u8]: Copy` to be trivially false,
1336
- // we must now check that the impl has no impossible-to-satisfy predicates.
1337
- if tcx. subst_and_check_impossible_predicates ( (
1338
- item. owner_id . to_def_id ( ) ,
1339
- & InternalSubsts :: identity_for_item ( tcx, item. owner_id . to_def_id ( ) ) ,
1340
- ) ) {
1355
+ // Even though this impl has no type or const substitutions, because we don't
1356
+ // consider higher-ranked predicates such as `for<'a> &'a mut [u8]: Copy` to
1357
+ // be trivially false. We must now check that the impl has no impossible-to-satisfy
1358
+ // predicates.
1359
+ if tcx. subst_and_check_impossible_predicates ( ( item. owner_id . to_def_id ( ) , impl_substs) ) {
1341
1360
return ;
1342
1361
}
1343
1362
1344
- let Some ( trait_ref) = tcx. impl_trait_ref ( item. owner_id ) else {
1345
- return ;
1346
- } ;
1347
-
1348
- let trait_ref = trait_ref. subst_identity ( ) ;
1349
-
1350
1363
let param_env = ty:: ParamEnv :: reveal_all ( ) ;
1351
1364
let trait_ref = tcx. normalize_erasing_regions ( param_env, trait_ref) ;
1352
1365
let overridden_methods = tcx. impl_item_implementor_ids ( item. owner_id ) ;
@@ -1359,12 +1372,9 @@ fn create_mono_items_for_default_impls<'tcx>(
1359
1372
continue ;
1360
1373
}
1361
1374
1362
- let substs = InternalSubsts :: for_item ( tcx, method. def_id , |param, _| match param. kind {
1363
- GenericParamDefKind :: Lifetime => tcx. lifetimes . re_erased . into ( ) ,
1364
- GenericParamDefKind :: Type { .. } | GenericParamDefKind :: Const { .. } => {
1365
- trait_ref. substs [ param. index as usize ]
1366
- }
1367
- } ) ;
1375
+ // As mentioned above, the method is legal to eagerly instantiate if it
1376
+ // only has lifetime substitutions. This is validated by
1377
+ let substs = trait_ref. substs . extend_to ( tcx, method. def_id , only_region_params) ;
1368
1378
let instance = ty:: Instance :: expect_resolve ( tcx, param_env, method. def_id , substs) ;
1369
1379
1370
1380
let mono_item = create_fn_mono_item ( tcx, instance, DUMMY_SP ) ;
0 commit comments