@@ -2068,12 +2068,19 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
20682068 )
20692069 } ) ;
20702070
2071- let one_bound = bounds. next ( ) ;
2071+ let Some ( ( bound_vars, assoc_item) ) = bounds. next ( ) else {
2072+ // This will error in HIR lowering.
2073+ self . tcx
2074+ . dcx ( )
2075+ . span_delayed_bug ( path. span , "no resolution for RTN path" ) ;
2076+ return ;
2077+ } ;
20722078
20732079 // Don't bail if we have identical bounds, which may be collected from
20742080 // something like `T: Bound + Bound`, or via elaborating supertraits.
2075- for second_bound in bounds {
2076- if Some ( & second_bound) != one_bound. as_ref ( ) {
2081+ for ( second_vars, second_assoc_item) in bounds {
2082+ if second_vars != bound_vars || second_assoc_item != assoc_item {
2083+ // This will error in HIR lowering.
20772084 self . tcx . dcx ( ) . span_delayed_bug (
20782085 path. span ,
20792086 "ambiguous resolution for RTN path" ,
@@ -2082,13 +2089,6 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
20822089 }
20832090 }
20842091
2085- let Some ( ( bound_vars, assoc_item) ) = one_bound else {
2086- self . tcx
2087- . dcx ( )
2088- . span_delayed_bug ( path. span , "no resolution for RTN path" ) ;
2089- return ;
2090- } ;
2091-
20922092 ( bound_vars, assoc_item. def_id , item_segment)
20932093 }
20942094 // If we have a self type alias (in an impl), try to resolve an
@@ -2164,75 +2164,80 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
21642164 std:: iter:: from_coroutine (
21652165 #[ coroutine]
21662166 move || {
2167- let mut next_scope = Some ( self . scope ) ;
2168- while let Some ( current_scope) = next_scope {
2169- next_scope = None ;
2170- let hir_id = match * current_scope {
2171- Scope :: Binder { s, hir_id, .. } => {
2172- next_scope = Some ( s) ;
2173- hir_id
2174- }
2175- Scope :: Body { s, .. }
2176- | Scope :: ObjectLifetimeDefault { s, .. }
2177- | Scope :: Supertrait { s, .. }
2178- | Scope :: TraitRefBoundary { s }
2179- | Scope :: LateBoundary { s, .. }
2180- | Scope :: Opaque { s, .. } => {
2181- next_scope = Some ( s) ;
2182- continue ;
2183- }
2184- Scope :: Root { opt_parent_item } => {
2185- if let Some ( parent_id) = opt_parent_item {
2186- self . tcx . local_def_id_to_hir_id ( parent_id)
2187- } else {
2188- continue ;
2189- }
2167+ let mut scope = self . scope ;
2168+ loop {
2169+ let hir_id = match * scope {
2170+ Scope :: Binder { hir_id, .. } => Some ( hir_id) ,
2171+ Scope :: Root { opt_parent_item : Some ( parent_def_id) } => {
2172+ Some ( self . tcx . local_def_id_to_hir_id ( parent_def_id) )
21902173 }
2174+ Scope :: Body { .. }
2175+ | Scope :: ObjectLifetimeDefault { .. }
2176+ | Scope :: Supertrait { .. }
2177+ | Scope :: TraitRefBoundary { .. }
2178+ | Scope :: LateBoundary { .. }
2179+ | Scope :: Opaque { .. }
2180+ | Scope :: Root { opt_parent_item : None } => None ,
21912181 } ;
2192- let node = self . tcx . hir_node ( hir_id) ;
2193- // If this is a `Self` bound in a trait, yield the trait itself.
2194- // Specifically, we don't need to look at any supertraits since
2195- // we already do that in `BoundVarContext::supertrait_hrtb_vars`.
2196- if let Res :: SelfTyParam { trait_ : _ } = expected_res
2197- && let hir:: Node :: Item ( item) = node
2198- && let hir:: ItemKind :: Trait ( ..) = item. kind
2199- {
2200- // Yield the trait's def id. Supertraits will be
2201- // elaborated from that.
2202- yield item. owner_id . def_id . to_def_id ( ) ;
2203- } else if let Some ( generics) = node. generics ( ) {
2204- for pred in generics. predicates {
2205- let hir:: WherePredicate :: BoundPredicate ( pred) = pred else {
2206- continue ;
2207- } ;
2208- let hir:: TyKind :: Path ( hir:: QPath :: Resolved ( None , bounded_path) ) =
2209- pred. bounded_ty . kind
2210- else {
2211- continue ;
2212- } ;
2213- // Match the expected res.
2214- if bounded_path. res != expected_res {
2215- continue ;
2216- }
2217- for pred in pred. bounds {
2218- match pred {
2219- hir:: GenericBound :: Trait ( poly_trait_ref) => {
2220- if let Some ( def_id) =
2221- poly_trait_ref. trait_ref . trait_def_id ( )
2222- {
2223- yield def_id;
2182+
2183+ if let Some ( hir_id) = hir_id {
2184+ let node = self . tcx . hir_node ( hir_id) ;
2185+ // If this is a `Self` bound in a trait, yield the trait itself.
2186+ // Specifically, we don't need to look at any supertraits since
2187+ // we already do that in `BoundVarContext::supertrait_hrtb_vars`.
2188+ if let Res :: SelfTyParam { trait_ : _ } = expected_res
2189+ && let hir:: Node :: Item ( item) = node
2190+ && let hir:: ItemKind :: Trait ( ..) = item. kind
2191+ {
2192+ // Yield the trait's def id. Supertraits will be
2193+ // elaborated from that.
2194+ yield item. owner_id . def_id . to_def_id ( ) ;
2195+ } else if let Some ( generics) = node. generics ( ) {
2196+ for pred in generics. predicates {
2197+ let hir:: WherePredicate :: BoundPredicate ( pred) = pred else {
2198+ continue ;
2199+ } ;
2200+ let hir:: TyKind :: Path ( hir:: QPath :: Resolved ( None , bounded_path) ) =
2201+ pred. bounded_ty . kind
2202+ else {
2203+ continue ;
2204+ } ;
2205+ // Match the expected res.
2206+ if bounded_path. res != expected_res {
2207+ continue ;
2208+ }
2209+ for pred in pred. bounds {
2210+ match pred {
2211+ hir:: GenericBound :: Trait ( poly_trait_ref) => {
2212+ if let Some ( def_id) =
2213+ poly_trait_ref. trait_ref . trait_def_id ( )
2214+ {
2215+ yield def_id;
2216+ }
22242217 }
2218+ hir:: GenericBound :: Outlives ( _)
2219+ | hir:: GenericBound :: Use ( _, _) => { }
22252220 }
2226- hir:: GenericBound :: Outlives ( _)
2227- | hir:: GenericBound :: Use ( _, _) => { }
22282221 }
22292222 }
22302223 }
22312224 }
2225+
2226+ match * scope {
2227+ Scope :: Binder { s, .. }
2228+ | Scope :: Body { s, .. }
2229+ | Scope :: ObjectLifetimeDefault { s, .. }
2230+ | Scope :: Supertrait { s, .. }
2231+ | Scope :: TraitRefBoundary { s }
2232+ | Scope :: LateBoundary { s, .. }
2233+ | Scope :: Opaque { s, .. } => {
2234+ scope = s;
2235+ }
2236+ Scope :: Root { .. } => break ,
2237+ }
22322238 }
22332239 } ,
22342240 )
2235- . fuse ( )
22362241 }
22372242}
22382243
0 commit comments