@@ -2126,48 +2126,77 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
2126
2126
// First (determined here), if `self` is by-reference, then the
2127
2127
// implied output region is the region of the self parameter.
2128
2128
if has_self {
2129
- // Look for `self: &'a Self` - also desugared from `&'a self`,
2130
- // and if that matches, use it for elision and return early.
2131
- let is_self_ty = |res : Res | {
2132
- if let Res :: SelfTy ( ..) = res {
2133
- return true ;
2134
- }
2135
-
2136
- // Can't always rely on literal (or implied) `Self` due
2137
- // to the way elision rules were originally specified.
2138
- let impl_self = impl_self. map ( |ty| & ty. node ) ;
2139
- if let Some ( & hir:: TyKind :: Path ( hir:: QPath :: Resolved ( None , ref path) ) ) = impl_self {
2140
- match path. res {
2141
- // Whitelist the types that unambiguously always
2142
- // result in the same type constructor being used
2143
- // (it can't differ between `Self` and `self`).
2144
- Res :: Def ( DefKind :: Struct , _)
2145
- | Res :: Def ( DefKind :: Union , _)
2146
- | Res :: Def ( DefKind :: Enum , _)
2147
- | Res :: PrimTy ( _) => {
2148
- return res == path. res
2129
+ struct SelfVisitor < ' a > {
2130
+ map : & ' a NamedRegionMap ,
2131
+ impl_self : Option < & ' a hir:: TyKind > ,
2132
+ lifetime : Set1 < Region > ,
2133
+ }
2134
+
2135
+ impl SelfVisitor < ' _ > {
2136
+ // Look for `self: &'a Self` - also desugared from `&'a self`,
2137
+ // and if that matches, use it for elision and return early.
2138
+ fn is_self_ty ( & self , res : Res ) -> bool {
2139
+ if let Res :: SelfTy ( ..) = res {
2140
+ return true ;
2141
+ }
2142
+
2143
+ // Can't always rely on literal (or implied) `Self` due
2144
+ // to the way elision rules were originally specified.
2145
+ if let Some ( & hir:: TyKind :: Path ( hir:: QPath :: Resolved ( None , ref path) ) ) =
2146
+ self . impl_self
2147
+ {
2148
+ match path. res {
2149
+ // Whitelist the types that unambiguously always
2150
+ // result in the same type constructor being used
2151
+ // (it can't differ between `Self` and `self`).
2152
+ Res :: Def ( DefKind :: Struct , _)
2153
+ | Res :: Def ( DefKind :: Union , _)
2154
+ | Res :: Def ( DefKind :: Enum , _)
2155
+ | Res :: PrimTy ( _) => {
2156
+ return res == path. res
2157
+ }
2158
+ _ => { }
2149
2159
}
2150
- _ => { }
2151
2160
}
2161
+
2162
+ false
2152
2163
}
2164
+ }
2153
2165
2154
- false
2155
- } ;
2166
+ impl < ' a > Visitor < ' a > for SelfVisitor < ' a > {
2167
+ fn nested_visit_map < ' this > ( & ' this mut self ) -> NestedVisitorMap < ' this , ' a > {
2168
+ NestedVisitorMap :: None
2169
+ }
2156
2170
2157
- if let hir:: TyKind :: Rptr ( lifetime_ref, ref mt) = inputs[ 0 ] . node {
2158
- if let hir:: TyKind :: Path ( hir:: QPath :: Resolved ( None , ref path) ) = mt. ty . node {
2159
- if is_self_ty ( path. res ) {
2160
- if let Some ( & lifetime) = self . map . defs . get ( & lifetime_ref. hir_id ) {
2161
- let scope = Scope :: Elision {
2162
- elide : Elide :: Exact ( lifetime) ,
2163
- s : self . scope ,
2164
- } ;
2165
- self . with ( scope, |_, this| this. visit_ty ( output) ) ;
2166
- return ;
2171
+ fn visit_ty ( & mut self , ty : & ' a hir:: Ty ) {
2172
+ if let hir:: TyKind :: Rptr ( lifetime_ref, ref mt) = ty. node {
2173
+ if let hir:: TyKind :: Path ( hir:: QPath :: Resolved ( None , ref path) ) = mt. ty . node
2174
+ {
2175
+ if self . is_self_ty ( path. res ) {
2176
+ if let Some ( lifetime) = self . map . defs . get ( & lifetime_ref. hir_id ) {
2177
+ self . lifetime . insert ( * lifetime) ;
2178
+ }
2179
+ }
2167
2180
}
2168
2181
}
2182
+ intravisit:: walk_ty ( self , ty)
2169
2183
}
2170
2184
}
2185
+
2186
+ let mut visitor = SelfVisitor {
2187
+ map : self . map ,
2188
+ impl_self : impl_self. map ( |ty| & ty. node ) ,
2189
+ lifetime : Set1 :: Empty ,
2190
+ } ;
2191
+ visitor. visit_ty ( & inputs[ 0 ] ) ;
2192
+ if let Set1 :: One ( lifetime) = visitor. lifetime {
2193
+ let scope = Scope :: Elision {
2194
+ elide : Elide :: Exact ( lifetime) ,
2195
+ s : self . scope ,
2196
+ } ;
2197
+ self . with ( scope, |_, this| this. visit_ty ( output) ) ;
2198
+ return ;
2199
+ }
2171
2200
}
2172
2201
2173
2202
// Second, if there was exactly one lifetime (either a substitution or a
0 commit comments