suggest lifetime for closure parameter type when mismatch#105888
suggest lifetime for closure parameter type when mismatch#105888bors merged 4 commits intorust-lang:masterfrom
Conversation
|
r? @TaKO8Ki (rustbot has picked a reviewer for you, use r? to override) |
|
@rustbot label +A-suggestion-diagnostics +T-compiler |
|
Originally I thought we should fix this bug and make the original code compiles. But it seems that we will need to decide whether |
7c339b0 to
db651f7
Compare
This comment has been minimized.
This comment has been minimized.
|
Test case updated. This suggestion may also fix some previous issues. e.g, for |
8c397d3 to
2b43cba
Compare
|
@rustbot ready |
|
☔ The latest upstream changes (presumably #107290) made this pull request unmergeable. Please resolve the merge conflicts. |
|
This needs a rebase. Marking this as waiting-on-author. Please use @rustbot author |
|
@bors r+ |
…re, 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...
Signed-off-by: Alex Chi <iskyzh@gmail.com>
Signed-off-by: Alex Chi <iskyzh@gmail.com>
Signed-off-by: Alex Chi <iskyzh@gmail.com>
c025483 to
90dc6fe
Compare
|
@rustbot ready Looks like a merge conflict 🙈 Fixed |
|
@bors r+ |
|
☀️ Test successful - checks-actions |
|
Finished benchmarking commit (2a71115): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
|
This is a draft PR, will add test cases later and be ready for review.This PR fixes #105675 by adding a diagnostics suggestion. Also a partial fix to #105528.
The following code will have a compile error now:
Before this PR:
After this PR:
After applying the suggestion, it compiles. The suggestion might not always be correct as the generation procedure of that suggestion is quite simple...