-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #61207 - taiki-e:arbitrary_self_types-lifetime-elision-…
…2, r=<try> Allow lifetime elision in `Pin<&(mut) Self>` This replaces #60944. ~~This PR changes elision rules to apply `self: &(mut) Self` elision rules even if nested in `Pin`.~~ This PR changes `self: &(mut) S` elision rules to instead visit the type of `self` and look for `&(mut) S` (where `is_self_ty(S)`) within it Closes #52675 r? @eddyb cc @cramertj @Centril @withoutboats @scottmcm
- Loading branch information
Showing
7 changed files
with
185 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ()) -> Alias<&Self> { self } | ||
} | ||
|
||
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
13
src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() }; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
18 changes: 18 additions & 0 deletions
18
src/test/ui/self/arbitrary_self_types_pin_lifetime_mismatch.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// 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 | ||
} | ||
|
||
type Alias<T> = Pin<T>; | ||
impl Foo { | ||
fn bar<'a>(self: Alias<&Self>, arg: &'a ()) -> &() { arg } //~ ERROR E0623 | ||
} | ||
|
||
fn main() {} |
26 changes: 26 additions & 0 deletions
26
src/test/ui/self/arbitrary_self_types_pin_lifetime_mismatch.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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[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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |