@@ -2,16 +2,16 @@ use crate::astconv::{AstConv, OnlySelfBounds, PredicateFilter};
22use crate :: bounds:: Bounds ;
33use crate :: collect:: ItemCtxt ;
44use crate :: constrained_generic_params as cgp;
5- use hir:: { HirId , Lifetime , Node } ;
5+ use hir:: { HirId , Node } ;
66use rustc_data_structures:: fx:: FxIndexSet ;
77use rustc_hir as hir;
88use rustc_hir:: def:: DefKind ;
99use rustc_hir:: def_id:: { DefId , LocalDefId } ;
1010use rustc_hir:: intravisit:: { self , Visitor } ;
1111use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
12- use rustc_middle:: ty:: { GenericPredicates , Generics , ImplTraitInTraitData , ToPredicate } ;
12+ use rustc_middle:: ty:: { GenericPredicates , ImplTraitInTraitData , ToPredicate } ;
1313use rustc_span:: symbol:: Ident ;
14- use rustc_span:: { Span , Symbol , DUMMY_SP } ;
14+ use rustc_span:: { Span , DUMMY_SP } ;
1515
1616/// Returns a list of all type predicates (explicit and implicit) for the definition with
1717/// ID `def_id`. This includes all predicates returned by `predicates_defined_on`, plus
@@ -55,17 +55,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
5555 use rustc_hir:: * ;
5656
5757 match tcx. opt_rpitit_info ( def_id. to_def_id ( ) ) {
58- Some ( ImplTraitInTraitData :: Trait { opaque_def_id, fn_def_id } ) => {
59- let opaque_ty_id = tcx. hir ( ) . local_def_id_to_hir_id ( opaque_def_id. expect_local ( ) ) ;
60- let opaque_ty_node = tcx. hir ( ) . get ( opaque_ty_id) ;
61- let Node :: Item ( & Item {
62- kind : ItemKind :: OpaqueTy ( OpaqueTy { lifetime_mapping, .. } ) ,
63- ..
64- } ) = opaque_ty_node
65- else {
66- bug ! ( "unexpected {opaque_ty_node:?}" )
67- } ;
68-
58+ Some ( ImplTraitInTraitData :: Trait { fn_def_id, .. } ) => {
6959 let mut predicates = Vec :: new ( ) ;
7060
7161 // RPITITs should inherit the predicates of their parent. This is
@@ -78,13 +68,12 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
7868
7969 // We also install bidirectional outlives predicates for the RPITIT
8070 // to keep the duplicates lifetimes from opaque lowering in sync.
71+ // We only need to compute bidirectional outlives for the duplicated
72+ // opaque lifetimes, which explains the slicing below.
8173 compute_bidirectional_outlives_predicates (
8274 tcx,
83- def_id,
84- lifetime_mapping. iter ( ) . map ( |( lifetime, def_id) | {
85- ( * * lifetime, ( * def_id, lifetime. ident . name , lifetime. ident . span ) )
86- } ) ,
87- tcx. generics_of ( def_id. to_def_id ( ) ) ,
75+ & tcx. generics_of ( def_id. to_def_id ( ) ) . params
76+ [ tcx. generics_of ( fn_def_id) . params . len ( ) ..] ,
8877 & mut predicates,
8978 ) ;
9079
@@ -351,21 +340,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
351340 } ;
352341 debug ! ( ?lifetimes) ;
353342
354- let lifetime_mapping = std:: iter:: zip ( lifetimes, ast_generics. params )
355- . map ( |( arg, dup) | {
356- let hir:: GenericArg :: Lifetime ( arg) = arg else { bug ! ( ) } ;
357- ( * * arg, dup)
358- } )
359- . filter ( |( _, dup) | matches ! ( dup. kind, hir:: GenericParamKind :: Lifetime { .. } ) )
360- . map ( |( lifetime, dup) | ( lifetime, ( dup. def_id , dup. name . ident ( ) . name , dup. span ) ) ) ;
361-
362- compute_bidirectional_outlives_predicates (
363- tcx,
364- def_id,
365- lifetime_mapping,
366- generics,
367- & mut predicates,
368- ) ;
343+ compute_bidirectional_outlives_predicates ( tcx, & generics. params , & mut predicates) ;
369344 debug ! ( ?predicates) ;
370345 }
371346
@@ -379,41 +354,28 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
379354/// enforce that these lifetimes stay in sync.
380355fn compute_bidirectional_outlives_predicates < ' tcx > (
381356 tcx : TyCtxt < ' tcx > ,
382- item_def_id : LocalDefId ,
383- lifetime_mapping : impl Iterator < Item = ( Lifetime , ( LocalDefId , Symbol , Span ) ) > ,
384- generics : & Generics ,
357+ opaque_own_params : & [ ty:: GenericParamDef ] ,
385358 predicates : & mut Vec < ( ty:: Clause < ' tcx > , Span ) > ,
386359) {
387- let icx = ItemCtxt :: new ( tcx, item_def_id) ;
388-
389- for ( arg, ( dup_def, name, span) ) in lifetime_mapping {
390- let orig_region = icx. astconv ( ) . ast_region_to_region ( & arg, None ) ;
391- if !matches ! ( orig_region. kind( ) , ty:: ReEarlyBound ( ..) ) {
392- // There is no late-bound lifetime to actually match up here, since the lifetime doesn't
393- // show up in the opaque's parent's args.
394- continue ;
360+ for param in opaque_own_params {
361+ let orig_lifetime = tcx. map_rpit_lifetime_to_fn_lifetime ( param. def_id . expect_local ( ) ) ;
362+ if let ty:: ReEarlyBound ( ..) = * orig_lifetime {
363+ let dup_lifetime = ty:: Region :: new_early_bound (
364+ tcx,
365+ ty:: EarlyBoundRegion { def_id : param. def_id , index : param. index , name : param. name } ,
366+ ) ;
367+ let span = tcx. def_span ( param. def_id ) ;
368+ predicates. push ( (
369+ ty:: ClauseKind :: RegionOutlives ( ty:: OutlivesPredicate ( orig_lifetime, dup_lifetime) )
370+ . to_predicate ( tcx) ,
371+ span,
372+ ) ) ;
373+ predicates. push ( (
374+ ty:: ClauseKind :: RegionOutlives ( ty:: OutlivesPredicate ( dup_lifetime, orig_lifetime) )
375+ . to_predicate ( tcx) ,
376+ span,
377+ ) ) ;
395378 }
396-
397- let Some ( dup_index) = generics. param_def_id_to_index ( icx. tcx , dup_def. to_def_id ( ) ) else {
398- bug ! ( )
399- } ;
400-
401- let dup_region = ty:: Region :: new_early_bound (
402- tcx,
403- ty:: EarlyBoundRegion { def_id : dup_def. to_def_id ( ) , index : dup_index, name } ,
404- ) ;
405-
406- predicates. push ( (
407- ty:: ClauseKind :: RegionOutlives ( ty:: OutlivesPredicate ( orig_region, dup_region) )
408- . to_predicate ( tcx) ,
409- span,
410- ) ) ;
411-
412- predicates. push ( (
413- ty:: ClauseKind :: RegionOutlives ( ty:: OutlivesPredicate ( dup_region, orig_region) )
414- . to_predicate ( tcx) ,
415- span,
416- ) ) ;
417379 }
418380}
419381
0 commit comments