-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #97720 - cjgillot:all-fresh, r=petrochenkov
Always create elided lifetime parameters for functions Anonymous and elided lifetimes in functions are sometimes (async fns) --and sometimes not (regular fns)-- desugared to implicit generic parameters. This difference of treatment makes it some downstream analyses more complicated to handle. This step is a pre-requisite to perform lifetime elision resolution on AST. There is currently an inconsistency in the treatment of argument-position impl-trait for functions and async fns: ```rust trait Foo<'a> {} fn foo(t: impl Foo<'_>) {} //~ ERROR missing lifetime specifier async fn async_foo(t: impl Foo<'_>) {} //~ OK fn bar(t: impl Iterator<Item = &'_ u8>) {} //~ ERROR missing lifetime specifier async fn async_bar(t: impl Iterator<Item = &'_ u8>) {} //~ OK ``` The current implementation reports "missing lifetime specifier" on `foo`, but **accepts it** in `async_foo`. This PR **proposes to accept** the anonymous lifetime in both cases as an extra generic lifetime parameter. This change would be insta-stable, so let's ping t-lang. Anonymous lifetimes in GAT bindings keep being forbidden: ```rust fn foo(t: impl Foo<Assoc<'_> = Bar<'_>>) {} ^^ ^^ forbidden ok ``` I started a discussion here: https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Anonymous.20lifetimes.20in.20universal.20impl-trait/near/284968606 r? ``@petrochenkov``
- Loading branch information
Showing
20 changed files
with
163 additions
and
56 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
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
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
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
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
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
21 changes: 21 additions & 0 deletions
21
src/test/ui/suggestions/impl-trait-missing-lifetime-gated.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,21 @@ | ||
// edition:2021 | ||
// gate-test-anonymous_lifetime_in_impl_trait | ||
// Verify the behaviour of `feature(anonymous_lifetime_in_impl_trait)`. | ||
|
||
fn f(_: impl Iterator<Item = &'_ ()>) {} | ||
//~^ ERROR anonymous lifetimes in `impl Trait` are unstable | ||
|
||
fn g(x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() } | ||
//~^ ERROR anonymous lifetimes in `impl Trait` are unstable | ||
//~| ERROR missing lifetime specifier | ||
|
||
// Anonymous lifetimes in async fn are already allowed. | ||
// This is understood as `fn foo<'_1>(_: impl Iterator<Item = &'_1 ()>) {}`. | ||
async fn h(_: impl Iterator<Item = &'_ ()>) {} | ||
|
||
// Anonymous lifetimes in async fn are already allowed. | ||
// But that lifetime does not participate in resolution. | ||
async fn i(x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() } | ||
//~^ ERROR missing lifetime specifier | ||
|
||
fn main() {} |
44 changes: 44 additions & 0 deletions
44
src/test/ui/suggestions/impl-trait-missing-lifetime-gated.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,44 @@ | ||
error[E0658]: anonymous lifetimes in `impl Trait` are unstable | ||
--> $DIR/impl-trait-missing-lifetime-gated.rs:5:31 | ||
| | ||
LL | fn f(_: impl Iterator<Item = &'_ ()>) {} | ||
| ^^ | ||
| | ||
= help: add `#![feature(anonymous_lifetime_in_impl_trait)]` to the crate attributes to enable | ||
|
||
error[E0106]: missing lifetime specifier | ||
--> $DIR/impl-trait-missing-lifetime-gated.rs:8:50 | ||
| | ||
LL | fn g(x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() } | ||
| ^^ expected named lifetime parameter | ||
| | ||
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments | ||
help: consider using the `'static` lifetime | ||
| | ||
LL | fn g(x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() } | ||
| ~~~~~~~ | ||
|
||
error[E0658]: anonymous lifetimes in `impl Trait` are unstable | ||
--> $DIR/impl-trait-missing-lifetime-gated.rs:8:31 | ||
| | ||
LL | fn g(x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() } | ||
| ^^ | ||
| | ||
= help: add `#![feature(anonymous_lifetime_in_impl_trait)]` to the crate attributes to enable | ||
|
||
error[E0106]: missing lifetime specifier | ||
--> $DIR/impl-trait-missing-lifetime-gated.rs:18:56 | ||
| | ||
LL | async fn i(x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() } | ||
| ^^ expected named lifetime parameter | ||
| | ||
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments | ||
help: consider using the `'static` lifetime | ||
| | ||
LL | async fn i(x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() } | ||
| ~~~~~~~ | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
Some errors have detailed explanations: E0106, E0658. | ||
For more information about an 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 |
---|---|---|
@@ -1,2 +1,19 @@ | ||
fn f(_: impl Iterator<Item = &'_ ()>) {} //~ ERROR missing lifetime specifier | ||
// edition:2021 | ||
|
||
#![feature(anonymous_lifetime_in_impl_trait)] | ||
|
||
// This is understood as `fn foo<'_1>(_: impl Iterator<Item = &'_1 ()>) {}`. | ||
fn f(_: impl Iterator<Item = &'_ ()>) {} | ||
|
||
// But that lifetime does not participate in resolution. | ||
fn g(x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() } | ||
//~^ ERROR missing lifetime specifier | ||
|
||
// This is understood as `fn foo<'_1>(_: impl Iterator<Item = &'_1 ()>) {}`. | ||
async fn h(_: impl Iterator<Item = &'_ ()>) {} | ||
|
||
// But that lifetime does not participate in resolution. | ||
async fn i(x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() } | ||
//~^ ERROR missing lifetime specifier | ||
|
||
fn main() {} |
27 changes: 20 additions & 7 deletions
27
src/test/ui/suggestions/impl-trait-missing-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 |
---|---|---|
@@ -1,14 +1,27 @@ | ||
error[E0106]: missing lifetime specifier | ||
--> $DIR/impl-trait-missing-lifetime.rs:1:31 | ||
--> $DIR/impl-trait-missing-lifetime.rs:9:50 | ||
| | ||
LL | fn f(_: impl Iterator<Item = &'_ ()>) {} | ||
| ^^ expected named lifetime parameter | ||
LL | fn g(x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() } | ||
| ^^ expected named lifetime parameter | ||
| | ||
help: consider introducing a named lifetime parameter | ||
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments | ||
help: consider using the `'static` lifetime | ||
| | ||
LL | fn f<'a>(_: impl Iterator<Item = &'a ()>) {} | ||
| ++++ ~~ | ||
LL | fn g(x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() } | ||
| ~~~~~~~ | ||
|
||
error: aborting due to previous error | ||
error[E0106]: missing lifetime specifier | ||
--> $DIR/impl-trait-missing-lifetime.rs:16:56 | ||
| | ||
LL | async fn i(x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() } | ||
| ^^ expected named lifetime parameter | ||
| | ||
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments | ||
help: consider using the `'static` lifetime | ||
| | ||
LL | async fn i(x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() } | ||
| ~~~~~~~ | ||
|
||
error: aborting due to 2 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
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
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
Oops, something went wrong.