Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c1b4d62
rustc: Compile the `fmt_macros` crate as an rlib
alexcrichton Jul 19, 2019
4242206
Re-enable assertions in PPC dist builder
mati865 Jul 19, 2019
e2eb957
Allow lifetime elision in `Pin<&(mut) Self>`
taiki-e May 26, 2019
a1fd4fa
Remove query for `.pin_type()`
taiki-e May 27, 2019
7b9a65e
Make is_self_ty a method on SelfVisitor
taiki-e May 29, 2019
2f64404
Use Set1<Region> instead of Option<Region>
taiki-e May 29, 2019
3096568
add a bevy of new test cases
nikomatsakis Jun 28, 2019
258498a
Update src/test/ui/self/elision/README.md
taiki-e Jul 15, 2019
c1f22c0
Add main functions and check-pass annotations
taiki-e Jul 15, 2019
aab9edc
Minor clean up
taiki-e Jul 15, 2019
8507b8e
Add test for multiple ref-self
taiki-e Jul 15, 2019
1e29052
Add tests for `self: (&)AssocType`
taiki-e Jul 26, 2019
34f59eb
Fix typo
taiki-e Jul 26, 2019
05f67a2
arbitrary_self_types lifetime elision: --bless --compare-mode=nll
taiki-e Jul 27, 2019
3eeec1c
Break dependencies between `syntax_ext` and some other crates
petrochenkov Jul 17, 2019
4ad0daa
Move proc macro server into libsyntax
petrochenkov Jul 18, 2019
f6eda99
Move test harness generation into libsyntax_ext
petrochenkov Jul 18, 2019
4d535bd
Move standard library injection into libsyntax_ext
petrochenkov Jul 18, 2019
b5a0e6e
syntax_ext: `proc_macro_decls` -> `proc_macro_harness`
petrochenkov Jul 18, 2019
c0df742
tidy: libcoretest.rs -> unit_tests.rs
petrochenkov Jul 25, 2019
6a4def0
tidy: Fix a regression in `#[test]` detection in libcore
petrochenkov Jul 25, 2019
aecaa03
tidy: Add a check for inline unit tests and benchmarks
petrochenkov Jul 26, 2019
75dfdcb
ci: download awscli from our mirror
pietroalbini Jul 27, 2019
5629d9f
Rollup merge of #61207 - taiki-e:arbitrary_self_types-lifetime-elisio…
Centril Jul 27, 2019
9d95302
Rollup merge of #62771 - petrochenkov:depext, r=eddyb
Centril Jul 27, 2019
b3846d2
Rollup merge of #62813 - alexcrichton:less-dylib, r=Mark-Simulacrum
Centril Jul 27, 2019
a686323
Rollup merge of #62949 - mati865:patch-1, r=alexcrichton
Centril Jul 27, 2019
65789f2
Rollup merge of #62996 - petrochenkov:outest, r=Mark-Simulacrum
Centril Jul 27, 2019
6fcf96b
Rollup merge of #63050 - pietroalbini:vendor-awscli, r=Mark-Simulacrum
Centril Jul 27, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Allow lifetime elision in Pin<&(mut) Self>
  • Loading branch information
taiki-e committed Jul 27, 2019
commit e2eb957be0f6c31585c31815e6c423e7c1d75856
29 changes: 28 additions & 1 deletion src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2174,7 +2174,34 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
false
};

if let hir::TyKind::Rptr(lifetime_ref, ref mt) = inputs[0].node {
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;
}
}
}
}
}
}
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) {
Expand Down
60 changes: 60 additions & 0 deletions src/test/ui/self/arbitrary_self_types_pin_lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// compile-pass

use std::pin::Pin;
use std::task::{Context, Poll};

struct Foo;

impl Foo {
fn pin_ref(self: Pin<&Self>) -> Pin<&Self> { self }

fn pin_mut(self: Pin<&mut Self>) -> Pin<&mut Self> { self }

fn pin_pin_pin_ref(self: Pin<Pin<Pin<&Self>>>) -> Pin<Pin<Pin<&Self>>> { self }

fn pin_ref_impl_trait(self: Pin<&Self>) -> impl Clone + '_ { self }

fn b(self: Pin<&Foo>, f: &Foo) -> Pin<&Foo> { self }
}

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

struct Bar<T: Unpin, U: Unpin> {
field1: T,
field2: U,
}

impl<T: Unpin, U: Unpin> Bar<T, U> {
fn fields(self: Pin<&mut Self>) -> (Pin<&mut T>, Pin<&mut U>) {
let this = self.get_mut();
(Pin::new(&mut this.field1), Pin::new(&mut this.field2))
}
}

trait AsyncBufRead {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>)
-> Poll<std::io::Result<&[u8]>>;
}

struct Baz(Vec<u8>);

impl AsyncBufRead for Baz {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>)
-> Poll<std::io::Result<&[u8]>>
{
Poll::Ready(Ok(&self.get_mut().0))
}
}

fn main() {
let mut foo = Foo;
{ Pin::new(&foo).pin_ref() };
{ Pin::new(&mut foo).pin_mut() };
{ Pin::new(Pin::new(Pin::new(&foo))).pin_pin_pin_ref() };
{ Pin::new(&foo).pin_ref_impl_trait() };
let mut bar = Bar { field1: 0u8, field2: 1u8 };
{ Pin::new(&mut bar).fields() };
}
13 changes: 13 additions & 0 deletions src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// compile-fail

use std::pin::Pin;

struct Foo;

impl Foo {
fn f(self: Pin<&Self>) -> impl Clone { self } //~ ERROR cannot infer an appropriate lifetime
}

fn main() {
{ Pin::new(&Foo).f() };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: cannot infer an appropriate lifetime
--> $DIR/arbitrary_self_types_pin_lifetime_impl_trait.rs:8:44
|
LL | fn f(self: Pin<&Self>) -> impl Clone { self }
| ---------- ^^^^ ...but this borrow...
| |
| this return type evaluates to the `'static` lifetime...
|
note: ...can't outlive the anonymous lifetime #1 defined on the method body at 8:5
--> $DIR/arbitrary_self_types_pin_lifetime_impl_trait.rs:8:5
|
LL | fn f(self: Pin<&Self>) -> impl Clone { self }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: you can add a constraint to the return type to make it last less than `'static` and match the anonymous lifetime #1 defined on the method body at 8:5
|
LL | fn f(self: Pin<&Self>) -> impl Clone + '_ { self }
| ^^^^^^^^^^^^^^^

error: aborting due to previous error

13 changes: 13 additions & 0 deletions src/test/ui/self/arbitrary_self_types_pin_lifetime_mismatch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// compile-fail

use std::pin::Pin;

struct Foo;

impl Foo {
fn a(self: Pin<&Foo>, f: &Foo) -> &Foo { f } //~ ERROR E0623

fn c(self: Pin<&Self>, f: &Foo, g: &Foo) -> (Pin<&Foo>, &Foo) { (self, f) } //~ ERROR E0623
}

fn main() {}
18 changes: 18 additions & 0 deletions src/test/ui/self/arbitrary_self_types_pin_lifetime_mismatch.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0623]: lifetime mismatch
--> $DIR/arbitrary_self_types_pin_lifetime_mismatch.rs:8:46
|
LL | fn a(self: Pin<&Foo>, f: &Foo) -> &Foo { f }
| ---- ---- ^ ...but data from `f` is returned here
| |
| this parameter and the return type are declared with different lifetimes...

error[E0623]: lifetime mismatch
--> $DIR/arbitrary_self_types_pin_lifetime_mismatch.rs:10:76
|
LL | fn c(self: Pin<&Self>, f: &Foo, g: &Foo) -> (Pin<&Foo>, &Foo) { (self, f) }
| ---- ----------------- ^ ...but data from `f` is returned here
| |
| this parameter and the return type are declared with different lifetimes...

error: aborting due to 2 previous errors

13 changes: 13 additions & 0 deletions src/test/ui/self/self_lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// compile-pass

struct Foo<'a>(&'a ());
impl<'a> Foo<'a> {
fn foo<'b>(self: &'b Foo<'a>) -> &() { self.0 }
}

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

fn main() {}