-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Provide a better error and a suggestion for Fn
traits with lifetime params
#104531
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
Merged
bors
merged 1 commit into
rust-lang:master
from
ohno418:recover-fn-traits-with-lifetime-params
Dec 29, 2022
Merged
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 hidden or 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 hidden or 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
20 changes: 20 additions & 0 deletions
20
src/test/ui/higher-rank-trait-bounds/hrtb-malformed-lifetime-generics.rs
This file contains hidden or 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 @@ | ||
// Test that Fn-family traits with lifetime parameters shouldn't compile and | ||
// we suggest the usage of higher-rank trait bounds instead. | ||
|
||
fn fa(_: impl Fn<'a>(&'a str) -> bool) {} | ||
//~^ ERROR `Fn` traits cannot take lifetime parameters | ||
|
||
fn fb(_: impl FnMut<'a, 'b>(&'a str, &'b str) -> bool) {} | ||
//~^ ERROR `Fn` traits cannot take lifetime parameters | ||
|
||
fn fc(_: impl std::fmt::Display + FnOnce<'a>(&'a str) -> bool + std::fmt::Debug) {} | ||
//~^ ERROR `Fn` traits cannot take lifetime parameters | ||
|
||
use std::ops::Fn as AliasedFn; | ||
fn fd(_: impl AliasedFn<'a>(&'a str) -> bool) {} | ||
//~^ ERROR `Fn` traits cannot take lifetime parameters | ||
|
||
fn fe<F>(_: F) where F: Fn<'a>(&'a str) -> bool {} | ||
//~^ ERROR `Fn` traits cannot take lifetime parameters | ||
|
||
fn main() {} |
62 changes: 62 additions & 0 deletions
62
src/test/ui/higher-rank-trait-bounds/hrtb-malformed-lifetime-generics.stderr
This file contains hidden or 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,62 @@ | ||
error: `Fn` traits cannot take lifetime parameters | ||
--> $DIR/hrtb-malformed-lifetime-generics.rs:4:17 | ||
| | ||
LL | fn fa(_: impl Fn<'a>(&'a str) -> bool) {} | ||
| ^^^^ | ||
| | ||
help: consider using a higher-ranked trait bound instead | ||
| | ||
LL - fn fa(_: impl Fn<'a>(&'a str) -> bool) {} | ||
LL + fn fa(_: impl for<'a> Fn(&'a str) -> bool) {} | ||
| | ||
|
||
error: `Fn` traits cannot take lifetime parameters | ||
--> $DIR/hrtb-malformed-lifetime-generics.rs:7:20 | ||
| | ||
LL | fn fb(_: impl FnMut<'a, 'b>(&'a str, &'b str) -> bool) {} | ||
| ^^^^^^^^ | ||
| | ||
help: consider using a higher-ranked trait bound instead | ||
| | ||
LL - fn fb(_: impl FnMut<'a, 'b>(&'a str, &'b str) -> bool) {} | ||
LL + fn fb(_: impl for<'a, 'b> FnMut(&'a str, &'b str) -> bool) {} | ||
| | ||
|
||
error: `Fn` traits cannot take lifetime parameters | ||
--> $DIR/hrtb-malformed-lifetime-generics.rs:10:41 | ||
| | ||
LL | fn fc(_: impl std::fmt::Display + FnOnce<'a>(&'a str) -> bool + std::fmt::Debug) {} | ||
| ^^^^ | ||
| | ||
help: consider using a higher-ranked trait bound instead | ||
| | ||
LL - fn fc(_: impl std::fmt::Display + FnOnce<'a>(&'a str) -> bool + std::fmt::Debug) {} | ||
LL + fn fc(_: impl std::fmt::Display + for<'a> FnOnce(&'a str) -> bool + std::fmt::Debug) {} | ||
| | ||
|
||
error: `Fn` traits cannot take lifetime parameters | ||
--> $DIR/hrtb-malformed-lifetime-generics.rs:14:24 | ||
| | ||
LL | fn fd(_: impl AliasedFn<'a>(&'a str) -> bool) {} | ||
| ^^^^ | ||
| | ||
help: consider using a higher-ranked trait bound instead | ||
| | ||
LL - fn fd(_: impl AliasedFn<'a>(&'a str) -> bool) {} | ||
LL + fn fd(_: impl for<'a> AliasedFn(&'a str) -> bool) {} | ||
| | ||
|
||
error: `Fn` traits cannot take lifetime parameters | ||
--> $DIR/hrtb-malformed-lifetime-generics.rs:17:27 | ||
| | ||
LL | fn fe<F>(_: F) where F: Fn<'a>(&'a str) -> bool {} | ||
| ^^^^ | ||
| | ||
help: consider using a higher-ranked trait bound instead | ||
| | ||
LL - fn fe<F>(_: F) where F: Fn<'a>(&'a str) -> bool {} | ||
LL + fn fe<F>(_: F) where F: for<'a> Fn(&'a str) -> bool {} | ||
| | ||
|
||
error: aborting due to 5 previous errors | ||
|
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.
This can now be
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.
Is this valid syntax? I got the following error:
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.
With
let
/else
syntax, it'sor
or
or something similar at your choosing.
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.
Ah, yeah, I mistyped what I meant. Thanks @fmease for clarifying it