-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 impl Fn() -> impl Trait
in return position
#93582
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8b494f4
Allow `impl Fn() -> impl Trait` in return position
WaffleLapkin 7a4ba2f
Add more tests for `impl Fn() -> impl Trait`
WaffleLapkin 00f2277
Add even more tests for `impl Fn() -> impl Trait`
WaffleLapkin cc752f5
Feature gate `impl_trait_in_fn_trait_return`
WaffleLapkin d116859
--bless
WaffleLapkin 690e037
add a test for gate `impl_trait_in_fn_trait_return`
WaffleLapkin e93982a
adopt to compiler changes
WaffleLapkin 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
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
6 changes: 6 additions & 0 deletions
6
src/test/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.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,6 @@ | ||
fn f() -> impl Fn() -> impl Sized { || () } | ||
//~^ ERROR `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait return | ||
fn g() -> &'static dyn Fn() -> impl Sized { &|| () } | ||
//~^ ERROR `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait return | ||
|
||
fn main() {} |
15 changes: 15 additions & 0 deletions
15
src/test/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.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,15 @@ | ||
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait return | ||
--> $DIR/feature-gate-impl_trait_in_fn_trait_return.rs:1:24 | ||
| | ||
LL | fn f() -> impl Fn() -> impl Sized { || () } | ||
| ^^^^^^^^^^ | ||
|
||
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait return | ||
--> $DIR/feature-gate-impl_trait_in_fn_trait_return.rs:3:32 | ||
| | ||
LL | fn g() -> &'static dyn Fn() -> impl Sized { &|| () } | ||
| ^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0562`. |
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,8 @@ | ||
#![feature(impl_trait_in_fn_trait_return)] | ||
use std::fmt::Debug; | ||
|
||
fn a() -> impl Fn(&u8) -> impl Debug { | ||
|x| x //~ ERROR hidden type for `impl Debug` captures lifetime that does not appear in bounds | ||
} | ||
|
||
fn main() {} |
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,11 @@ | ||
error[E0700]: hidden type for `impl Debug` captures lifetime that does not appear in bounds | ||
--> $DIR/impl-fn-hrtb-bounds-2.rs:5:9 | ||
| | ||
LL | |x| x | ||
| --- ^ | ||
| | | ||
| hidden type `&u8` captures the anonymous lifetime #1 defined here | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0700`. |
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,24 @@ | ||
#![feature(impl_trait_in_fn_trait_return)] | ||
use std::fmt::Debug; | ||
|
||
fn a() -> impl Fn(&u8) -> (impl Debug + '_) { | ||
//~^ ERROR higher kinded lifetime bounds on nested opaque types are not supported yet | ||
|x| x | ||
} | ||
|
||
fn b() -> impl for<'a> Fn(&'a u8) -> (impl Debug + 'a) { | ||
//~^ ERROR higher kinded lifetime bounds on nested opaque types are not supported yet | ||
|x| x | ||
} | ||
|
||
fn c() -> impl for<'a> Fn(&'a u8) -> (impl Debug + '_) { | ||
//~^ ERROR higher kinded lifetime bounds on nested opaque types are not supported yet | ||
|x| x | ||
} | ||
|
||
fn d() -> impl Fn() -> (impl Debug + '_) { | ||
//~^ ERROR missing lifetime specifier | ||
|| () | ||
} | ||
|
||
fn main() {} |
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,51 @@ | ||
error[E0106]: missing lifetime specifier | ||
--> $DIR/impl-fn-hrtb-bounds.rs:19:38 | ||
| | ||
LL | fn d() -> impl Fn() -> (impl Debug + '_) { | ||
| ^^ expected named lifetime parameter | ||
| | ||
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from | ||
help: consider using the `'static` lifetime | ||
| | ||
LL | fn d() -> impl Fn() -> (impl Debug + 'static) { | ||
| ~~~~~~~ | ||
|
||
error: higher kinded lifetime bounds on nested opaque types are not supported yet | ||
--> $DIR/impl-fn-hrtb-bounds.rs:4:41 | ||
| | ||
LL | fn a() -> impl Fn(&u8) -> (impl Debug + '_) { | ||
| ^^ | ||
| | ||
note: lifetime declared here | ||
--> $DIR/impl-fn-hrtb-bounds.rs:4:19 | ||
| | ||
LL | fn a() -> impl Fn(&u8) -> (impl Debug + '_) { | ||
| ^ | ||
|
||
error: higher kinded lifetime bounds on nested opaque types are not supported yet | ||
--> $DIR/impl-fn-hrtb-bounds.rs:9:52 | ||
| | ||
LL | fn b() -> impl for<'a> Fn(&'a u8) -> (impl Debug + 'a) { | ||
| ^^ | ||
| | ||
note: lifetime declared here | ||
--> $DIR/impl-fn-hrtb-bounds.rs:9:20 | ||
| | ||
LL | fn b() -> impl for<'a> Fn(&'a u8) -> (impl Debug + 'a) { | ||
| ^^ | ||
|
||
error: higher kinded lifetime bounds on nested opaque types are not supported yet | ||
--> $DIR/impl-fn-hrtb-bounds.rs:14:52 | ||
| | ||
LL | fn c() -> impl for<'a> Fn(&'a u8) -> (impl Debug + '_) { | ||
| ^^ | ||
| | ||
note: lifetime declared here | ||
--> $DIR/impl-fn-hrtb-bounds.rs:14:20 | ||
| | ||
LL | fn c() -> impl for<'a> Fn(&'a u8) -> (impl Debug + '_) { | ||
| ^^ | ||
|
||
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,15 @@ | ||
#![feature(impl_trait_in_fn_trait_return)] | ||
use std::fmt::Debug; | ||
|
||
fn a() -> impl Fn(&u8) -> impl Debug + '_ { | ||
//~^ ERROR ambiguous `+` in a type | ||
//~^^ ERROR higher kinded lifetime bounds on nested opaque types are not supported yet | ||
|x| x | ||
} | ||
|
||
fn b() -> impl Fn() -> impl Debug + Send { | ||
//~^ ERROR ambiguous `+` in a type | ||
|| () | ||
} | ||
|
||
fn main() {} |
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: ambiguous `+` in a type | ||
--> $DIR/impl-fn-parsing-ambiguities.rs:4:27 | ||
| | ||
LL | fn a() -> impl Fn(&u8) -> impl Debug + '_ { | ||
| ^^^^^^^^^^^^^^^ help: use parentheses to disambiguate: `(impl Debug + '_)` | ||
|
||
error: ambiguous `+` in a type | ||
--> $DIR/impl-fn-parsing-ambiguities.rs:10:24 | ||
| | ||
LL | fn b() -> impl Fn() -> impl Debug + Send { | ||
| ^^^^^^^^^^^^^^^^^ help: use parentheses to disambiguate: `(impl Debug + Send)` | ||
|
||
error: higher kinded lifetime bounds on nested opaque types are not supported yet | ||
--> $DIR/impl-fn-parsing-ambiguities.rs:4:40 | ||
| | ||
LL | fn a() -> impl Fn(&u8) -> impl Debug + '_ { | ||
| ^^ | ||
| | ||
note: lifetime declared here | ||
--> $DIR/impl-fn-parsing-ambiguities.rs:4:19 | ||
| | ||
LL | fn a() -> impl Fn(&u8) -> impl Debug + '_ { | ||
| ^ | ||
|
||
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,15 @@ | ||
#![feature(impl_trait_in_fn_trait_return)] | ||
use std::fmt::Debug; | ||
|
||
fn a<'a>() -> impl Fn(&'a u8) -> (impl Debug + '_) { | ||
//~^ ERROR cannot resolve opaque type | ||
|
||
|x| x | ||
//~^ ERROR concrete type differs from previous defining opaque type use | ||
} | ||
|
||
fn _b<'a>() -> impl Fn(&'a u8) -> (impl Debug + 'a) { | ||
a() | ||
} | ||
|
||
fn main() {} |
24 changes: 24 additions & 0 deletions
24
src/test/ui/impl-trait/impl-fn-predefined-lifetimes.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,24 @@ | ||
error: concrete type differs from previous defining opaque type use | ||
--> $DIR/impl-fn-predefined-lifetimes.rs:7:9 | ||
| | ||
LL | |x| x | ||
| ^ expected `impl Debug + '_`, got `&u8` | ||
| | ||
note: previous use here | ||
--> $DIR/impl-fn-predefined-lifetimes.rs:7:5 | ||
| | ||
LL | |x| x | ||
| ^^^^^ | ||
|
||
error[E0720]: cannot resolve opaque type | ||
--> $DIR/impl-fn-predefined-lifetimes.rs:4:35 | ||
| | ||
LL | fn a<'a>() -> impl Fn(&'a u8) -> (impl Debug + '_) { | ||
| ^^^^^^^^^^^^^^^ recursive opaque type | ||
... | ||
LL | |x| x | ||
| ----- returning here with type `[closure@$DIR/impl-fn-predefined-lifetimes.rs:7:5: 7:8]` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0720`. |
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 @@ | ||
// run-pass | ||
#![feature(impl_trait_in_fn_trait_return)] | ||
use std::fmt::Debug; | ||
|
||
fn f_debug() -> impl Fn() -> impl Debug { | ||
|| () | ||
} | ||
|
||
fn ff_debug() -> impl Fn() -> impl Fn() -> impl Debug { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add some tests about // This should error because the returned closure is capturing
// its argument, which was not declared as part of the signature.
fn f_debug() -> impl Fn(&u8) -> impl Debug {
|x| x
} // This should (maybe) error because or parsing ambiguities
fn f_debug() -> impl Fn(&u8) -> impl Debug + '_ {
|x| x
} // This should (maybe) error because or parsing ambiguities
fn f_debug() -> impl Fn() -> impl Debug + Send {
|| ()
} // This should work
fn f_debug() -> impl Fn(&u8) -> (impl Debug + '_) {
|x| x
} // This should work
fn f_debug() -> impl Fn() -> (impl Debug + Send) {
|| ()
} |
||
|| f_debug() | ||
} | ||
|
||
fn multi() -> impl Fn() -> (impl Debug + Send) { | ||
|| () | ||
} | ||
|
||
fn main() { | ||
// Check that `ff_debug` is `() -> (() -> Debug)` and not `(() -> ()) -> Debug` | ||
let debug = ff_debug()()(); | ||
assert_eq!(format!("{:?}", debug), "()"); | ||
|
||
let x = multi()(); | ||
assert_eq!(format!("{:?}", x), "()"); | ||
fn assert_send(_: &impl Send) {} | ||
assert_send(&x); | ||
} |
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.
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.
Hmm, I'm kind of surprised by this error -- I expect this function to be equivalent to the one below...?
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.
To be honest: I do not know what's going on 😄
Note though that the same function written with a normal trait also produces an error:
So I don't think it's an issue related to this PR.
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.
edit: this comment makes no sense, see below why this continues to fail to compile
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.
Also, the fact that this fails to compile currently (cannot resolve opaque type) is due to #101968 (comment)