-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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 #104531 - ohno418:recover-fn-traits-with-lifetime-par…
…ams, r=estebank Provide a better error and a suggestion for `Fn` traits with lifetime params Given `Fn`-family traits with lifetime params in trait bounds like `fn f(_: impl Fn<'a>(&'a str) -> bool)`, we currently produce many unhelpful errors. This PR allows these situations to suggest simply using Higher-Rank Trait Bounds like `for<'a> Fn(&'a str) -> bool`. Fixes #103490.
- Loading branch information
Showing
4 changed files
with
176 additions
and
3 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
20 changes: 20 additions & 0 deletions
20
src/test/ui/higher-rank-trait-bounds/hrtb-malformed-lifetime-generics.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,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 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 | ||
|