Skip to content

Commit 0bc8878

Browse files
committed
Make is_self_ty a method on SelfVisitor
1 parent a9a50bc commit 0bc8878

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

src/librustc/middle/resolve_lifetime.rs

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,41 +2146,44 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
21462146
// First (determined here), if `self` is by-reference, then the
21472147
// implied output region is the region of the self parameter.
21482148
if has_self {
2149-
// Look for `self: &'a Self` - also desugared from `&'a self`,
2150-
// and if that matches, use it for elision and return early.
2151-
let is_self_ty = |res: Res| {
2152-
if let Res::SelfTy(..) = res {
2153-
return true;
2154-
}
2155-
2156-
// Can't always rely on literal (or implied) `Self` due
2157-
// to the way elision rules were originally specified.
2158-
let impl_self = impl_self.map(|ty| &ty.node);
2159-
if let Some(&hir::TyKind::Path(hir::QPath::Resolved(None, ref path))) = impl_self {
2160-
match path.res {
2161-
// Whitelist the types that unambiguously always
2162-
// result in the same type constructor being used
2163-
// (it can't differ between `Self` and `self`).
2164-
Res::Def(DefKind::Struct, _)
2165-
| Res::Def(DefKind::Union, _)
2166-
| Res::Def(DefKind::Enum, _)
2167-
| Res::PrimTy(_) => {
2168-
return res == path.res
2169-
}
2170-
_ => {}
2149+
struct SelfVisitor<'a> {
2150+
map: &'a NamedRegionMap,
2151+
impl_self: Option<&'a hir::TyKind>,
2152+
lifetime: Option<Region>,
2153+
}
2154+
2155+
impl SelfVisitor<'_> {
2156+
// Look for `self: &'a Self` - also desugared from `&'a self`,
2157+
// and if that matches, use it for elision and return early.
2158+
fn is_self_ty(&self, res: Res) -> bool {
2159+
if let Res::SelfTy(..) = res {
2160+
return true;
21712161
}
2172-
}
21732162

2174-
false
2175-
};
2163+
// Can't always rely on literal (or implied) `Self` due
2164+
// to the way elision rules were originally specified.
2165+
if let Some(&hir::TyKind::Path(hir::QPath::Resolved(None, ref path))) =
2166+
self.impl_self
2167+
{
2168+
match path.res {
2169+
// Whitelist the types that unambiguously always
2170+
// result in the same type constructor being used
2171+
// (it can't differ between `Self` and `self`).
2172+
Res::Def(DefKind::Struct, _)
2173+
| Res::Def(DefKind::Union, _)
2174+
| Res::Def(DefKind::Enum, _)
2175+
| Res::PrimTy(_) => {
2176+
return res == path.res
2177+
}
2178+
_ => {}
2179+
}
2180+
}
21762181

2177-
struct SelfVisitor<'a, F: FnMut(Res) -> bool> {
2178-
is_self_ty: F,
2179-
map: &'a NamedRegionMap,
2180-
lifetime: Option<Region>,
2182+
false
2183+
}
21812184
}
21822185

2183-
impl<'a, F: FnMut(Res) -> bool> Visitor<'a> for SelfVisitor<'a, F> {
2186+
impl<'a> Visitor<'a> for SelfVisitor<'a> {
21842187
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'a> {
21852188
NestedVisitorMap::None
21862189
}
@@ -2189,7 +2192,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
21892192
if let hir::TyKind::Rptr(lifetime_ref, ref mt) = ty.node {
21902193
if let hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) = mt.ty.node
21912194
{
2192-
if (self.is_self_ty)(path.res) {
2195+
if self.is_self_ty(path.res) {
21932196
self.lifetime = self.map.defs.get(&lifetime_ref.hir_id).copied();
21942197
return;
21952198
}
@@ -2200,8 +2203,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
22002203
}
22012204

22022205
let mut visitor = SelfVisitor {
2203-
is_self_ty,
22042206
map: self.map,
2207+
impl_self: impl_self.map(|ty| &ty.node),
22052208
lifetime: None,
22062209
};
22072210
visitor.visit_ty(&inputs[0]);

0 commit comments

Comments
 (0)