Skip to content

Commit 3d0d96b

Browse files
taiki-enikomatsakis
authored andcommitted
Make is_self_ty a method on SelfVisitor
1 parent 7ddc394 commit 3d0d96b

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

src/librustc/middle/resolve_lifetime.rs

+35-32
Original file line numberDiff line numberDiff line change
@@ -2126,41 +2126,44 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
21262126
// First (determined here), if `self` is by-reference, then the
21272127
// implied output region is the region of the self parameter.
21282128
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
2149-
}
2150-
_ => {}
2129+
struct SelfVisitor<'a> {
2130+
map: &'a NamedRegionMap,
2131+
impl_self: Option<&'a hir::TyKind>,
2132+
lifetime: Option<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;
21512141
}
2152-
}
21532142

2154-
false
2155-
};
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+
_ => {}
2159+
}
2160+
}
21562161

2157-
struct SelfVisitor<'a, F: FnMut(Res) -> bool> {
2158-
is_self_ty: F,
2159-
map: &'a NamedRegionMap,
2160-
lifetime: Option<Region>,
2162+
false
2163+
}
21612164
}
21622165

2163-
impl<'a, F: FnMut(Res) -> bool> Visitor<'a> for SelfVisitor<'a, F> {
2166+
impl<'a> Visitor<'a> for SelfVisitor<'a> {
21642167
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'a> {
21652168
NestedVisitorMap::None
21662169
}
@@ -2169,7 +2172,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
21692172
if let hir::TyKind::Rptr(lifetime_ref, ref mt) = ty.node {
21702173
if let hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) = mt.ty.node
21712174
{
2172-
if (self.is_self_ty)(path.res) {
2175+
if self.is_self_ty(path.res) {
21732176
self.lifetime = self.map.defs.get(&lifetime_ref.hir_id).copied();
21742177
return;
21752178
}
@@ -2180,8 +2183,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
21802183
}
21812184

21822185
let mut visitor = SelfVisitor {
2183-
is_self_ty,
21842186
map: self.map,
2187+
impl_self: impl_self.map(|ty| &ty.node),
21852188
lifetime: None,
21862189
};
21872190
visitor.visit_ty(&inputs[0]);

0 commit comments

Comments
 (0)