@@ -2070,12 +2070,19 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
20702070 )
20712071 } ) ;
20722072
2073- let one_bound = bounds. next ( ) ;
2073+ let Some ( ( bound_vars, assoc_item) ) = bounds. next ( ) else {
2074+ // This will error in HIR lowering.
2075+ self . tcx
2076+ . dcx ( )
2077+ . span_delayed_bug ( path. span , "no resolution for RTN path" ) ;
2078+ return ;
2079+ } ;
20742080
20752081 // Don't bail if we have identical bounds, which may be collected from
20762082 // something like `T: Bound + Bound`, or via elaborating supertraits.
2077- for second_bound in bounds {
2078- if Some ( & second_bound) != one_bound. as_ref ( ) {
2083+ for ( second_vars, second_assoc_item) in bounds {
2084+ if second_vars != bound_vars || second_assoc_item != assoc_item {
2085+ // This will error in HIR lowering.
20792086 self . tcx . dcx ( ) . span_delayed_bug (
20802087 path. span ,
20812088 "ambiguous resolution for RTN path" ,
@@ -2084,13 +2091,6 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
20842091 }
20852092 }
20862093
2087- let Some ( ( bound_vars, assoc_item) ) = one_bound else {
2088- self . tcx
2089- . dcx ( )
2090- . span_delayed_bug ( path. span , "no resolution for RTN path" ) ;
2091- return ;
2092- } ;
2093-
20942094 ( bound_vars, assoc_item. def_id , item_segment)
20952095 }
20962096 // If we have a self type alias (in an impl), try to resolve an
@@ -2166,75 +2166,81 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
21662166 std:: iter:: from_coroutine (
21672167 #[ coroutine]
21682168 move || {
2169- let mut next_scope = Some ( self . scope ) ;
2170- while let Some ( current_scope) = next_scope {
2171- next_scope = None ;
2172- let hir_id = match * current_scope {
2173- Scope :: Binder { s, hir_id, .. } => {
2174- next_scope = Some ( s) ;
2175- hir_id
2176- }
2177- Scope :: Body { s, .. }
2178- | Scope :: ObjectLifetimeDefault { s, .. }
2179- | Scope :: Supertrait { s, .. }
2180- | Scope :: TraitRefBoundary { s }
2181- | Scope :: LateBoundary { s, .. }
2182- | Scope :: Opaque { s, .. } => {
2183- next_scope = Some ( s) ;
2184- continue ;
2185- }
2186- Scope :: Root { opt_parent_item } => {
2187- if let Some ( parent_id) = opt_parent_item {
2188- self . tcx . local_def_id_to_hir_id ( parent_id)
2189- } else {
2190- continue ;
2191- }
2169+ let mut scope = self . scope ;
2170+ loop {
2171+ let hir_id = match * scope {
2172+ Scope :: Binder { hir_id, .. } => Some ( hir_id) ,
2173+ Scope :: Root { opt_parent_item : Some ( parent_def_id) } => {
2174+ Some ( self . tcx . local_def_id_to_hir_id ( parent_def_id) )
21922175 }
2176+ Scope :: Body { .. }
2177+ | Scope :: ObjectLifetimeDefault { .. }
2178+ | Scope :: Supertrait { .. }
2179+ | Scope :: TraitRefBoundary { .. }
2180+ | Scope :: LateBoundary { .. }
2181+ | Scope :: Opaque { .. }
2182+ | Scope :: Root { opt_parent_item : None } => None ,
21932183 } ;
2194- let node = self . tcx . hir_node ( hir_id) ;
2195- // If this is a `Self` bound in a trait, yield the trait itself.
2196- // Specifically, we don't need to look at any supertraits since
2197- // we already do that in `BoundVarContext::supertrait_hrtb_vars`.
2198- if let Res :: SelfTyParam { trait_ : _ } = expected_res
2199- && let hir:: Node :: Item ( item) = node
2200- && let hir:: ItemKind :: Trait ( ..) = item. kind
2201- {
2202- // Yield the trait's def id. Supertraits will be
2203- // elaborated from that.
2204- yield item. owner_id . def_id . to_def_id ( ) ;
2205- } else if let Some ( generics) = node. generics ( ) {
2206- for pred in generics. predicates {
2207- let hir:: WherePredicateKind :: BoundPredicate ( pred) = pred. kind else {
2208- continue ;
2209- } ;
2210- let hir:: TyKind :: Path ( hir:: QPath :: Resolved ( None , bounded_path) ) =
2211- pred. bounded_ty . kind
2212- else {
2213- continue ;
2214- } ;
2215- // Match the expected res.
2216- if bounded_path. res != expected_res {
2217- continue ;
2218- }
2219- for pred in pred. bounds {
2220- match pred {
2221- hir:: GenericBound :: Trait ( poly_trait_ref) => {
2222- if let Some ( def_id) =
2223- poly_trait_ref. trait_ref . trait_def_id ( )
2224- {
2225- yield def_id;
2184+
2185+ if let Some ( hir_id) = hir_id {
2186+ let node = self . tcx . hir_node ( hir_id) ;
2187+ // If this is a `Self` bound in a trait, yield the trait itself.
2188+ // Specifically, we don't need to look at any supertraits since
2189+ // we already do that in `BoundVarContext::supertrait_hrtb_vars`.
2190+ if let Res :: SelfTyParam { trait_ : _ } = expected_res
2191+ && let hir:: Node :: Item ( item) = node
2192+ && let hir:: ItemKind :: Trait ( ..) = item. kind
2193+ {
2194+ // Yield the trait's def id. Supertraits will be
2195+ // elaborated from that.
2196+ yield item. owner_id . def_id . to_def_id ( ) ;
2197+ } else if let Some ( generics) = node. generics ( ) {
2198+ for pred in generics. predicates {
2199+ let hir:: WherePredicateKind :: BoundPredicate ( pred) = pred. kind
2200+ else {
2201+ continue ;
2202+ } ;
2203+ let hir:: TyKind :: Path ( hir:: QPath :: Resolved ( None , bounded_path) ) =
2204+ pred. bounded_ty . kind
2205+ else {
2206+ continue ;
2207+ } ;
2208+ // Match the expected res.
2209+ if bounded_path. res != expected_res {
2210+ continue ;
2211+ }
2212+ for pred in pred. bounds {
2213+ match pred {
2214+ hir:: GenericBound :: Trait ( poly_trait_ref) => {
2215+ if let Some ( def_id) =
2216+ poly_trait_ref. trait_ref . trait_def_id ( )
2217+ {
2218+ yield def_id;
2219+ }
22262220 }
2221+ hir:: GenericBound :: Outlives ( _)
2222+ | hir:: GenericBound :: Use ( _, _) => { }
22272223 }
2228- hir:: GenericBound :: Outlives ( _)
2229- | hir:: GenericBound :: Use ( _, _) => { }
22302224 }
22312225 }
22322226 }
22332227 }
2228+
2229+ match * scope {
2230+ Scope :: Binder { s, .. }
2231+ | Scope :: Body { s, .. }
2232+ | Scope :: ObjectLifetimeDefault { s, .. }
2233+ | Scope :: Supertrait { s, .. }
2234+ | Scope :: TraitRefBoundary { s }
2235+ | Scope :: LateBoundary { s, .. }
2236+ | Scope :: Opaque { s, .. } => {
2237+ scope = s;
2238+ }
2239+ Scope :: Root { .. } => break ,
2240+ }
22342241 }
22352242 } ,
22362243 )
2237- . fuse ( )
22382244 }
22392245}
22402246
0 commit comments