-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow lifetime elision in arbitrary_self_types #60944
Closed
taiki-e
wants to merge
1
commit into
rust-lang:master
from
taiki-e:arbitrary_self_types-lifetime-elision
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 28 additions & 0 deletions
28
src/test/ui/self/arbitrary_self_types_impl_trait_lifetime.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,28 @@ | ||
// compile-fail | ||
|
||
#![feature(arbitrary_self_types)] | ||
|
||
use std::pin::Pin; | ||
|
||
#[derive(Debug)] | ||
struct Foo; | ||
#[derive(Debug)] | ||
struct Bar<'a>(&'a Foo); | ||
|
||
impl std::ops::Deref for Bar<'_> { | ||
type Target = Foo; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl Foo { | ||
fn f(self: Bar<'_>) -> impl std::fmt::Debug { | ||
self | ||
//~^ ERROR cannot infer an appropriate lifetime | ||
} | ||
} | ||
|
||
fn main() { | ||
{ Bar(&Foo).f() }; | ||
} |
23 changes: 23 additions & 0 deletions
23
src/test/ui/self/arbitrary_self_types_impl_trait_lifetime.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,23 @@ | ||
error: cannot infer an appropriate lifetime | ||
--> $DIR/arbitrary_self_types_impl_trait_lifetime.rs:21:9 | ||
| | ||
LL | fn f(self: Bar<'_>) -> impl std::fmt::Debug { | ||
| -------------------- this return type evaluates to the `'static` lifetime... | ||
LL | self | ||
| ^^^^ ...but this borrow... | ||
| | ||
note: ...can't outlive the anonymous lifetime #1 defined on the method body at 20:5 | ||
--> $DIR/arbitrary_self_types_impl_trait_lifetime.rs:20:5 | ||
| | ||
LL | / fn f(self: Bar<'_>) -> impl std::fmt::Debug { | ||
LL | | self | ||
LL | | | ||
LL | | } | ||
| |_____^ | ||
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 20:5 | ||
| | ||
LL | fn f(self: Bar<'_>) -> impl std::fmt::Debug + '_ { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
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,27 @@ | ||
#![feature(arbitrary_self_types)] | ||
|
||
#[derive(Debug)] | ||
struct Foo; | ||
#[derive(Debug)] | ||
struct Bar<'a>(&'a Foo); | ||
|
||
impl std::ops::Deref for Bar<'_> { | ||
type Target = Foo; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl Foo { | ||
fn a(self: &Box<Foo>, f: &Foo) -> &Foo { f } //~ ERROR E0106 | ||
|
||
fn b(self: &Box<Foo>, f: &Foo) -> &Box<Foo> { self } //~ ERROR E0106 | ||
|
||
fn c(this: &Box<Foo>, f: &Foo) -> &Foo { f } //~ ERROR E0106 | ||
} | ||
|
||
impl<'a> Bar<'a> { | ||
fn d(self: Self, f: &Foo, g: &Foo) -> (Bar<'a>, &Foo) { (self, f) } //~ ERROR E0106 | ||
} | ||
|
||
fn main() {} |
35 changes: 35 additions & 0 deletions
35
src/test/ui/self/arbitrary_self_types_inexact_lifetime.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,35 @@ | ||
error[E0106]: missing lifetime specifier | ||
--> $DIR/arbitrary_self_types_inexact_lifetime.rs:16:39 | ||
| | ||
LL | fn a(self: &Box<Foo>, f: &Foo) -> &Foo { f } | ||
| ^ expected lifetime parameter | ||
| | ||
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `self` or `f` | ||
|
||
error[E0106]: missing lifetime specifier | ||
--> $DIR/arbitrary_self_types_inexact_lifetime.rs:18:39 | ||
| | ||
LL | fn b(self: &Box<Foo>, f: &Foo) -> &Box<Foo> { self } | ||
| ^ expected lifetime parameter | ||
| | ||
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `self` or `f` | ||
|
||
error[E0106]: missing lifetime specifier | ||
--> $DIR/arbitrary_self_types_inexact_lifetime.rs:20:39 | ||
| | ||
LL | fn c(this: &Box<Foo>, f: &Foo) -> &Foo { f } | ||
| ^ expected lifetime parameter | ||
| | ||
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `this` or `f` | ||
|
||
error[E0106]: missing lifetime specifier | ||
--> $DIR/arbitrary_self_types_inexact_lifetime.rs:24:53 | ||
| | ||
LL | fn d(self: Self, f: &Foo, g: &Foo) -> (Bar<'a>, &Foo) { (self, f) } | ||
| ^ expected lifetime parameter | ||
| | ||
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `f` or `g` | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0106`. |
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,76 @@ | ||
// compile-pass | ||
|
||
#![feature(arbitrary_self_types)] | ||
|
||
use std::pin::Pin; | ||
|
||
#[derive(Debug)] | ||
struct Foo; | ||
#[derive(Debug)] | ||
struct Bar<'a>(&'a Foo); | ||
|
||
impl std::ops::Deref for Bar<'_> { | ||
type Target = Foo; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl Foo { | ||
fn a(&self) -> Bar<'_> { | ||
Bar(self) | ||
} | ||
|
||
fn b(c: &Self) -> Bar<'_> { | ||
Bar(c) | ||
} | ||
|
||
fn c(self: Bar<'_>) -> Bar<'_> { | ||
self | ||
} | ||
|
||
fn d(e: Bar<'_>) -> Bar<'_> { | ||
e | ||
} | ||
|
||
fn e(self: &Self) -> Bar<'_> { | ||
Bar(self) | ||
} | ||
|
||
fn f(self: Bar<'_>) -> impl std::fmt::Debug + '_ { | ||
self | ||
} | ||
} | ||
|
||
impl<'a> Bar<'a> { | ||
fn a(self: Bar<'a>, f: &Foo) -> (Bar<'a>, &Foo) { (self, f) } | ||
fn b(self: Self, f: &Foo) -> (Bar<'a>, &Foo) { (self, f) } | ||
fn d(self: Bar<'a>, f: &Foo) -> (Self, &Foo) { (self, f) } | ||
} | ||
|
||
impl Bar<'_> { | ||
fn e(self: Self, f: &Foo) -> (Self, &Foo) { (self, f) } | ||
} | ||
|
||
struct Baz<T: Unpin> { | ||
field: T, | ||
} | ||
|
||
impl<T: Unpin> Baz<T> { | ||
fn field(self: Pin<&mut Self>) -> Pin<&mut T> { | ||
let this = Pin::get_mut(self); | ||
Pin::new(&mut this.field) | ||
} | ||
} | ||
|
||
fn main() { | ||
let foo = Foo; | ||
{ foo.a() }; | ||
{ Foo::b(&foo) }; | ||
{ Bar(&foo).c() }; | ||
{ Foo::d(Bar(&foo)) }; | ||
{ foo.e() }; | ||
{ Bar(&foo).f() }; | ||
let mut baz = Baz { field: 0u8 }; | ||
{ Pin::new(&mut baz).field() }; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe rename this to
skip_self_arg
?