Skip to content

Commit

Permalink
Remove query for .pin_type()
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jul 27, 2019
1 parent e2eb957 commit a1fd4fa
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 36 deletions.
66 changes: 32 additions & 34 deletions src/librustc/middle/resolve_lifetime.rs
Expand Up @@ -2174,46 +2174,44 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
false
};

let mut self_arg = &inputs[0].node;

// Apply `self: &(mut) Self` elision rules even if nested in `Pin`.
loop {
if let hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) = *self_arg {
if let Res::Def(DefKind::Struct, def_id) = path.res {
if self.tcx.lang_items().pin_type() == Some(def_id) {
if let Some(args) = path
.segments
.last()
.and_then(|segment| segment.args.as_ref())
{
if args.args.len() == 1 {
if let GenericArg::Type(ty) = &args.args[0] {
self_arg = &ty.node;
// Keep dereferencing `self_arg` until we get to non-`Pin`
// types.
continue;
}
}
struct SelfVisitor<'a, F: FnMut(Res) -> bool> {
is_self_ty: F,
map: &'a NamedRegionMap,
lifetime: Option<Region>,
}

impl<'a, F: FnMut(Res) -> bool> Visitor<'a> for SelfVisitor<'a, F> {
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'a> {
NestedVisitorMap::None
}

fn visit_ty(&mut self, ty: &'a hir::Ty) {
if let hir::TyKind::Rptr(lifetime_ref, ref mt) = ty.node {
if let hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) = mt.ty.node
{
if (self.is_self_ty)(path.res) {
self.lifetime = self.map.defs.get(&lifetime_ref.hir_id).copied();
return;
}
}
}
intravisit::walk_ty(self, ty)
}
break;
}

if let hir::TyKind::Rptr(lifetime_ref, ref mt) = *self_arg {
if let hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) = mt.ty.node {
if is_self_ty(path.res) {
if let Some(&lifetime) = self.map.defs.get(&lifetime_ref.hir_id) {
let scope = Scope::Elision {
elide: Elide::Exact(lifetime),
s: self.scope,
};
self.with(scope, |_, this| this.visit_ty(output));
return;
}
}
}
let mut visitor = SelfVisitor {
is_self_ty,
map: self.map,
lifetime: None,
};
visitor.visit_ty(&inputs[0]);
if let Some(lifetime) = visitor.lifetime {
let scope = Scope::Elision {
elide: Elide::Exact(lifetime),
s: self.scope,
};
self.with(scope, |_, this| this.visit_ty(output));
return;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/self/arbitrary_self_types_pin_lifetime.rs
Expand Up @@ -19,7 +19,7 @@ impl Foo {

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

struct Bar<T: Unpin, U: Unpin> {
Expand Down
Expand Up @@ -10,4 +10,9 @@ impl Foo {
fn c(self: Pin<&Self>, f: &Foo, g: &Foo) -> (Pin<&Foo>, &Foo) { (self, f) } //~ ERROR E0623
}

type Alias<T> = Pin<T>;
impl Foo {
fn bar<'a>(self: Alias<&Self>, arg: &'a ()) -> &() { arg } //~ ERROR E0623
}

fn main() {}
Expand Up @@ -14,5 +14,13 @@ LL | fn c(self: Pin<&Self>, f: &Foo, g: &Foo) -> (Pin<&Foo>, &Foo) { (self,
| |
| this parameter and the return type are declared with different lifetimes...

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

error: aborting due to 3 previous errors

0 comments on commit a1fd4fa

Please sign in to comment.