Skip to content

Commit 7ddc394

Browse files
taiki-enikomatsakis
authored andcommitted
Remove query for .pin_type()
1 parent 5c7e4c7 commit 7ddc394

File tree

4 files changed

+47
-36
lines changed

4 files changed

+47
-36
lines changed

src/librustc/middle/resolve_lifetime.rs

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,46 +2154,44 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
21542154
false
21552155
};
21562156

2157-
let mut self_arg = &inputs[0].node;
2158-
2159-
// Apply `self: &(mut) Self` elision rules even if nested in `Pin`.
2160-
loop {
2161-
if let hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) = *self_arg {
2162-
if let Res::Def(DefKind::Struct, def_id) = path.res {
2163-
if self.tcx.lang_items().pin_type() == Some(def_id) {
2164-
if let Some(args) = path
2165-
.segments
2166-
.last()
2167-
.and_then(|segment| segment.args.as_ref())
2168-
{
2169-
if args.args.len() == 1 {
2170-
if let GenericArg::Type(ty) = &args.args[0] {
2171-
self_arg = &ty.node;
2172-
// Keep dereferencing `self_arg` until we get to non-`Pin`
2173-
// types.
2174-
continue;
2175-
}
2176-
}
2157+
struct SelfVisitor<'a, F: FnMut(Res) -> bool> {
2158+
is_self_ty: F,
2159+
map: &'a NamedRegionMap,
2160+
lifetime: Option<Region>,
2161+
}
2162+
2163+
impl<'a, F: FnMut(Res) -> bool> Visitor<'a> for SelfVisitor<'a, F> {
2164+
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'a> {
2165+
NestedVisitorMap::None
2166+
}
2167+
2168+
fn visit_ty(&mut self, ty: &'a hir::Ty) {
2169+
if let hir::TyKind::Rptr(lifetime_ref, ref mt) = ty.node {
2170+
if let hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) = mt.ty.node
2171+
{
2172+
if (self.is_self_ty)(path.res) {
2173+
self.lifetime = self.map.defs.get(&lifetime_ref.hir_id).copied();
2174+
return;
21772175
}
21782176
}
21792177
}
2178+
intravisit::walk_ty(self, ty)
21802179
}
2181-
break;
21822180
}
21832181

2184-
if let hir::TyKind::Rptr(lifetime_ref, ref mt) = *self_arg {
2185-
if let hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) = mt.ty.node {
2186-
if is_self_ty(path.res) {
2187-
if let Some(&lifetime) = self.map.defs.get(&lifetime_ref.hir_id) {
2188-
let scope = Scope::Elision {
2189-
elide: Elide::Exact(lifetime),
2190-
s: self.scope,
2191-
};
2192-
self.with(scope, |_, this| this.visit_ty(output));
2193-
return;
2194-
}
2195-
}
2196-
}
2182+
let mut visitor = SelfVisitor {
2183+
is_self_ty,
2184+
map: self.map,
2185+
lifetime: None,
2186+
};
2187+
visitor.visit_ty(&inputs[0]);
2188+
if let Some(lifetime) = visitor.lifetime {
2189+
let scope = Scope::Elision {
2190+
elide: Elide::Exact(lifetime),
2191+
s: self.scope,
2192+
};
2193+
self.with(scope, |_, this| this.visit_ty(output));
2194+
return;
21972195
}
21982196
}
21992197

src/test/ui/self/arbitrary_self_types_pin_lifetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl Foo {
1919

2020
type Alias<T> = Pin<T>;
2121
impl Foo {
22-
fn bar<'a>(self: Alias<&Self>, arg: &'a ()) -> &() { arg }
22+
fn bar<'a>(self: Alias<&Self>, arg: &'a ()) -> Alias<&Self> { self }
2323
}
2424

2525
struct Bar<T: Unpin, U: Unpin> {

src/test/ui/self/arbitrary_self_types_pin_lifetime_mismatch.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ impl Foo {
1010
fn c(self: Pin<&Self>, f: &Foo, g: &Foo) -> (Pin<&Foo>, &Foo) { (self, f) } //~ ERROR E0623
1111
}
1212

13+
type Alias<T> = Pin<T>;
14+
impl Foo {
15+
fn bar<'a>(self: Alias<&Self>, arg: &'a ()) -> &() { arg } //~ ERROR E0623
16+
}
17+
1318
fn main() {}

src/test/ui/self/arbitrary_self_types_pin_lifetime_mismatch.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,13 @@ LL | fn c(self: Pin<&Self>, f: &Foo, g: &Foo) -> (Pin<&Foo>, &Foo) { (self,
1414
| |
1515
| this parameter and the return type are declared with different lifetimes...
1616

17-
error: aborting due to 2 previous errors
17+
error[E0623]: lifetime mismatch
18+
--> $DIR/arbitrary_self_types_pin_lifetime_mismatch.rs:15:58
19+
|
20+
LL | fn bar<'a>(self: Alias<&Self>, arg: &'a ()) -> &() { arg }
21+
| ------ --- ^^^ ...but data from `arg` is returned here
22+
| |
23+
| this parameter and the return type are declared with different lifetimes...
24+
25+
error: aborting due to 3 previous errors
1826

0 commit comments

Comments
 (0)