Skip to content

Commit 914e4a3

Browse files
committed
Remove query for .pin_type()
1 parent 46c752a commit 914e4a3

4 files changed

+47
-36
lines changed

src/librustc/middle/resolve_lifetime.rs

+32-34
Original file line numberDiff line numberDiff line change
@@ -2149,46 +2149,44 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
21492149
false
21502150
};
21512151

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

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

src/test/ui/self/arbitrary_self_types_pin_lifetime.rs

+1-1
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

+5
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

+9-1
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)