Skip to content

Commit 07903fe

Browse files
committed
Make is_self_ty a method on SelfVisitor
1 parent ee53d9a commit 07903fe

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
@@ -2117,41 +2117,44 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
21172117
// First (determined here), if `self` is by-reference, then the
21182118
// implied output region is the region of the self parameter.
21192119
if has_self {
2120-
// Look for `self: &'a Self` - also desugared from `&'a self`,
2121-
// and if that matches, use it for elision and return early.
2122-
let is_self_ty = |res: Res| {
2123-
if let Res::SelfTy(..) = res {
2124-
return true;
2125-
}
2126-
2127-
// Can't always rely on literal (or implied) `Self` due
2128-
// to the way elision rules were originally specified.
2129-
let impl_self = impl_self.map(|ty| &ty.node);
2130-
if let Some(&hir::TyKind::Path(hir::QPath::Resolved(None, ref path))) = impl_self {
2131-
match path.res {
2132-
// Whitelist the types that unambiguously always
2133-
// result in the same type constructor being used
2134-
// (it can't differ between `Self` and `self`).
2135-
Res::Def(DefKind::Struct, _)
2136-
| Res::Def(DefKind::Union, _)
2137-
| Res::Def(DefKind::Enum, _)
2138-
| Res::PrimTy(_) => {
2139-
return res == path.res
2140-
}
2141-
_ => {}
2120+
struct SelfVisitor<'a> {
2121+
map: &'a NamedRegionMap,
2122+
impl_self: Option<&'a hir::TyKind>,
2123+
lifetime: Option<Region>,
2124+
}
2125+
2126+
impl SelfVisitor<'_> {
2127+
// Look for `self: &'a Self` - also desugared from `&'a self`,
2128+
// and if that matches, use it for elision and return early.
2129+
fn is_self_ty(&self, res: Res) -> bool {
2130+
if let Res::SelfTy(..) = res {
2131+
return true;
21422132
}
2143-
}
21442133

2145-
false
2146-
};
2134+
// Can't always rely on literal (or implied) `Self` due
2135+
// to the way elision rules were originally specified.
2136+
if let Some(&hir::TyKind::Path(hir::QPath::Resolved(None, ref path))) =
2137+
self.impl_self
2138+
{
2139+
match path.res {
2140+
// Whitelist the types that unambiguously always
2141+
// result in the same type constructor being used
2142+
// (it can't differ between `Self` and `self`).
2143+
Res::Def(DefKind::Struct, _)
2144+
| Res::Def(DefKind::Union, _)
2145+
| Res::Def(DefKind::Enum, _)
2146+
| Res::PrimTy(_) => {
2147+
return res == path.res
2148+
}
2149+
_ => {}
2150+
}
2151+
}
21472152

2148-
struct SelfVisitor<'a, F: FnMut(Res) -> bool> {
2149-
is_self_ty: F,
2150-
map: &'a NamedRegionMap,
2151-
lifetime: Option<Region>,
2153+
false
2154+
}
21522155
}
21532156

2154-
impl<'a, F: FnMut(Res) -> bool> Visitor<'a> for SelfVisitor<'a, F> {
2157+
impl<'a> Visitor<'a> for SelfVisitor<'a> {
21552158
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'a> {
21562159
NestedVisitorMap::None
21572160
}
@@ -2160,7 +2163,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
21602163
if let hir::TyKind::Rptr(lifetime_ref, ref mt) = ty.node {
21612164
if let hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) = mt.ty.node
21622165
{
2163-
if (self.is_self_ty)(path.res) {
2166+
if self.is_self_ty(path.res) {
21642167
self.lifetime = self.map.defs.get(&lifetime_ref.hir_id).copied();
21652168
return;
21662169
}
@@ -2171,8 +2174,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
21712174
}
21722175

21732176
let mut visitor = SelfVisitor {
2174-
is_self_ty,
21752177
map: self.map,
2178+
impl_self: impl_self.map(|ty| &ty.node),
21762179
lifetime: None,
21772180
};
21782181
visitor.visit_ty(&inputs[0]);

0 commit comments

Comments
 (0)