forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#105888 - skyzh:skyzh/suggest-lifetime-closure…
…, r=compiler-errors suggest lifetime for closure parameter type when mismatch This is a draft PR, will add test cases later and be ready for review. This PR fixes rust-lang#105675 by adding a diagnostics suggestion. Also a partial fix to rust-lang#105528. The following code will have a compile error now: ``` fn const_if_unit(input: bool) -> impl for<'a> FnOnce(&'a ()) -> usize { let x = |_| 1; x } ``` Before this PR: ``` error[E0308]: mismatched types --> src/lib.rs:3:5 | 3 | x | ^ one type is more general than the other | = note: expected trait `for<'a> FnOnce<(&'a (),)>` found trait `FnOnce<(&(),)>` note: this closure does not fulfill the lifetime requirements --> src/lib.rs:2:13 | 2 | let x = |_| 1; | ^^^ error: implementation of `FnOnce` is not general enough --> src/lib.rs:3:5 | 3 | x | ^ implementation of `FnOnce` is not general enough | = note: closure with signature `fn(&'2 ()) -> usize` must implement `FnOnce<(&'1 (),)>`, for any lifetime `'1`... = note: ...but it actually implements `FnOnce<(&'2 (),)>`, for some specific lifetime `'2` For more information about this error, try `rustc --explain E0308`. error: could not compile `rust-test` due to 2 previous errors ``` After this PR: ``` error[E0308]: mismatched types --> src/lib.rs:3:5 | 3 | x | ^ one type is more general than the other | = note: expected trait `for<'a> FnOnce<(&'a (),)>` found trait `FnOnce<(&(),)>` note: this closure does not fulfill the lifetime requirements --> src/lib.rs:2:13 | 2 | let x = |_| 1; | ^^^ help: consider changing the type of the closure parameters | 2 | let x = |_: &_| 1; | ~~~~~~~ error: implementation of `FnOnce` is not general enough --> src/lib.rs:3:5 | 3 | x | ^ implementation of `FnOnce` is not general enough | = note: closure with signature `fn(&'2 ()) -> usize` must implement `FnOnce<(&'1 (),)>`, for any lifetime `'1`... = note: ...but it actually implements `FnOnce<(&'2 (),)>`, for some specific lifetime `'2` For more information about this error, try `rustc --explain E0308`. error: could not compile `rust-test` due to 2 previous errors ``` After applying the suggestion, it compiles. The suggestion might not always be correct as the generation procedure of that suggestion is quite simple...
- Loading branch information
Showing
8 changed files
with
250 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
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,14 @@ | ||
fn thing(x: impl FnOnce(&u32, &u32, u32)) {} | ||
|
||
fn main() { | ||
let f = | _ , y: &u32 , z | (); | ||
thing(f); | ||
//~^ ERROR mismatched types | ||
//~^^ ERROR mismatched types | ||
let f = | x, y: _ , z: u32 | (); | ||
thing(f); | ||
//~^ ERROR mismatched types | ||
//~^^ ERROR mismatched types | ||
//~^^^ ERROR implementation of `FnOnce` is not general enough | ||
//~^^^^ ERROR implementation of `FnOnce` is not general enough | ||
} |
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,109 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/issue-105675.rs:5:5 | ||
| | ||
LL | thing(f); | ||
| ^^^^^^^^ one type is more general than the other | ||
| | ||
= note: expected trait `for<'a, 'b> FnOnce<(&'a u32, &'b u32, u32)>` | ||
found trait `for<'a> FnOnce<(&u32, &'a u32, u32)>` | ||
note: this closure does not fulfill the lifetime requirements | ||
--> $DIR/issue-105675.rs:4:13 | ||
| | ||
LL | let f = | _ , y: &u32 , z | (); | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
note: the lifetime requirement is introduced here | ||
--> $DIR/issue-105675.rs:1:18 | ||
| | ||
LL | fn thing(x: impl FnOnce(&u32, &u32, u32)) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
help: consider specifying the type of the closure parameters | ||
| | ||
LL | let f = |_: &_, y: &u32, z| (); | ||
| ~~~~~~~~~~~~~~~~~~~ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-105675.rs:5:5 | ||
| | ||
LL | thing(f); | ||
| ^^^^^^^^ one type is more general than the other | ||
| | ||
= note: expected trait `for<'a, 'b> FnOnce<(&'a u32, &'b u32, u32)>` | ||
found trait `for<'a> FnOnce<(&u32, &'a u32, u32)>` | ||
note: this closure does not fulfill the lifetime requirements | ||
--> $DIR/issue-105675.rs:4:13 | ||
| | ||
LL | let f = | _ , y: &u32 , z | (); | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
note: the lifetime requirement is introduced here | ||
--> $DIR/issue-105675.rs:1:18 | ||
| | ||
LL | fn thing(x: impl FnOnce(&u32, &u32, u32)) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-105675.rs:9:5 | ||
| | ||
LL | thing(f); | ||
| ^^^^^^^^ one type is more general than the other | ||
| | ||
= note: expected trait `for<'a, 'b> FnOnce<(&'a u32, &'b u32, u32)>` | ||
found trait `FnOnce<(&u32, &u32, u32)>` | ||
note: this closure does not fulfill the lifetime requirements | ||
--> $DIR/issue-105675.rs:8:13 | ||
| | ||
LL | let f = | x, y: _ , z: u32 | (); | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
note: the lifetime requirement is introduced here | ||
--> $DIR/issue-105675.rs:1:18 | ||
| | ||
LL | fn thing(x: impl FnOnce(&u32, &u32, u32)) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
help: consider specifying the type of the closure parameters | ||
| | ||
LL | let f = |x: &_, y: &_, z: u32| (); | ||
| ~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-105675.rs:9:5 | ||
| | ||
LL | thing(f); | ||
| ^^^^^^^^ one type is more general than the other | ||
| | ||
= note: expected trait `for<'a, 'b> FnOnce<(&'a u32, &'b u32, u32)>` | ||
found trait `FnOnce<(&u32, &u32, u32)>` | ||
note: this closure does not fulfill the lifetime requirements | ||
--> $DIR/issue-105675.rs:8:13 | ||
| | ||
LL | let f = | x, y: _ , z: u32 | (); | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
note: the lifetime requirement is introduced here | ||
--> $DIR/issue-105675.rs:1:18 | ||
| | ||
LL | fn thing(x: impl FnOnce(&u32, &u32, u32)) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
help: consider specifying the type of the closure parameters | ||
| | ||
LL | let f = |x: &_, y: &_, z: u32| (); | ||
| ~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: implementation of `FnOnce` is not general enough | ||
--> $DIR/issue-105675.rs:9:5 | ||
| | ||
LL | thing(f); | ||
| ^^^^^^^^ implementation of `FnOnce` is not general enough | ||
| | ||
= note: closure with signature `fn(&'2 u32, &u32, u32)` must implement `FnOnce<(&'1 u32, &u32, u32)>`, for any lifetime `'1`... | ||
= note: ...but it actually implements `FnOnce<(&'2 u32, &u32, u32)>`, for some specific lifetime `'2` | ||
|
||
error: implementation of `FnOnce` is not general enough | ||
--> $DIR/issue-105675.rs:9:5 | ||
| | ||
LL | thing(f); | ||
| ^^^^^^^^ implementation of `FnOnce` is not general enough | ||
| | ||
= note: closure with signature `fn(&u32, &'2 u32, u32)` must implement `FnOnce<(&u32, &'1 u32, u32)>`, for any lifetime `'1`... | ||
= note: ...but it actually implements `FnOnce<(&u32, &'2 u32, u32)>`, for some specific lifetime `'2` | ||
|
||
error: aborting due to 6 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
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