rustc_parse: improve the error diagnostic for "missing let in let chain"#151493
rustc_parse: improve the error diagnostic for "missing let in let chain"#151493rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Conversation
|
r? @chenyukang rustbot has assigned @chenyukang. Use |
|
My approach seems not to be perfect and I have been stucked on this for a while. This is the issue this is supposed to fix - >#117977 |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
6b5f379 to
f1467c2
Compare
|
r? @estebank |
|
This is an improvement for initially it gives but now it gives |
|
Interestingly, still give the same result. |
There was a problem hiding this comment.
I'm not sure I follow your last comment. The diagnostic you're showing doesn't correspond with the code. I think you meant the following?
fn main() {
let x = Some(42);
if Some(_) = x
&& let Some(x) = x
{}
}If so, this is the output I see:
error: expected expression, found `let` statement
--> foo.rs:4:12
|
4 | && let Some(x) = x
| ^^^
|
= note: -Ztrack-diagnostics: created at /rustc-dev/4fa80a5e733e2202d7ca4c203c2fdfda41cfe7dc/compiler/rustc_parse/src/parser/expr.rs:2775:43
= note: only supported directly in conditions of `if` and `while` expressions
error[E0308]: mismatched types
--> foo.rs:3:18
|
3 | if Some(_) = x
| ^ expected `bool`, found `Option<{integer}>`
|
= note: -Ztrack-diagnostics: created at /rustc-dev/4fa80a5e733e2202d7ca4c203c2fdfda41cfe7dc/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:1905:35
= note: expected type `bool`
found enum `Option<{integer}>`
error[E0308]: mismatched types
--> foo.rs:3:8
|
3 | if Some(_) = x
| ________^^^^^^^___-
| | |
| | expected `bool`, found `Option<_>`
4 | | && let Some(x) = x
| |__________________________- this expression has type `bool`
|
= note: -Ztrack-diagnostics: created at /rustc-dev/4fa80a5e733e2202d7ca4c203c2fdfda41cfe7dc/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:1905:35
= note: expected type `bool`
found enum `Option<_>`
error[E0308]: mismatched types
--> foo.rs:3:8
|
3 | if Some(_) = x
| ________^
4 | | && let Some(x) = x
| |__________________________^ expected `bool`, found `()`
|
= note: -Ztrack-diagnostics: created at /rustc-dev/4fa80a5e733e2202d7ca4c203c2fdfda41cfe7dc/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:1905:35
help: consider adding `let`
|
3 | if let Some(_) = x
| +++
And I think it's coming from a different part of the parser, in parse_expr_let, so it would require different specific handling.
| help: a function with a similar name exists | ||
| | | ||
| LL - if (i + j) = i {} | ||
| LL + if (a + j) = i {} | ||
| | |
There was a problem hiding this comment.
Unrelated: Noticing this suggestion and it is quite bad :-/
| error: binary assignment operation `+=` cannot be used in a let chain | ||
| --> $DIR/let-chains-assign-add-incorrect.rs:8:31 | ||
| | | ||
| LL | if let _ = 1 && true && y += 2 {}; | ||
| | ---------------------- ^^ cannot use `+=` in a let chain | ||
| | | | ||
| | you are add-assigning the right-hand side expression to the result of this let-chain | ||
| | | ||
| help: you might have meant to compare with `==` instead of assigning with `+=` | ||
| | | ||
| LL - if let _ = 1 && true && y += 2 {}; | ||
| LL + if let _ = 1 && true && y == 2 {}; | ||
| | |
There was a problem hiding this comment.
This change makes me think that we might want to restrict the silencing to cases where we had any suggestions. The need to remove the run-rustfix generally points at us giving less ideal output. That being said, this code as is is not great to begin with, and == would likely be intended... Can you see if we can make it so that we don't cause changes on this output while still silencing the others?
There was a problem hiding this comment.
Yeah, that is valid. Looks quite weird to me also, I knew we had to do some restrictions, I will do that.
| missing_let: self.missing_let, | ||
| comparison: self.comparison, | ||
| }); | ||
| self.found_incorrect_let_chain = Some(guar); |
There was a problem hiding this comment.
I think all you'd have to do is gate this behind missing_let or comparison being Some.
There was a problem hiding this comment.
Yeah, thanks. I did gate it earlier, but not around Some parts, so it didn’t work. I’ll gate it around it now.
Yeah, I meant this. Yeah, it needs to be handle in different part of the code. |
Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
f1467c2 to
9ca8ed3
Compare
|
@bors r+ @Unique-Usman would you want to take a look at improving the case that is left (where the first condition is an assignment/not a let and then a let is found later)? It is ok if you find that it might require a more significant refactor and then decide it isn't worth it. |
Definitely, I will take a look, thanks for the review. |
…uwer Rollup of 12 pull requests Successful merges: - #150491 (resolve: Mark items under exported ambiguous imports as exported) - #150720 (Do not suggest `derive` if there is already an impl) - #150968 (compiler-builtins: Remove the no-f16-f128 feature) - #151493 ([RFC] rustc_parse: improve the error diagnostic for "missing let in let chain") - #151660 (Bump `std`'s `backtrace`'s `rustc-demangle`) - #151696 (Borrowck: Simplify SCC annotation computation, placeholder rewriting) - #151704 (Implement `set_output_kind` for Emscripten linker) - #151706 (Remove Fuchsia from target OS list in unix.rs for sleep) - #151769 (fix undefined behavior in VecDeque::splice) - #151779 (stdarch subtree update) - #151449 ([rustdoc] Add regression test for #151411) - #151773 (clean up checks for constant promotion of integer division/remainder operations)
Rollup merge of #151493 - Unique-Usman:ua/missinglet, r=estebank [RFC] rustc_parse: improve the error diagnostic for "missing let in let chain"
…uwer Rollup of 12 pull requests Successful merges: - rust-lang/rust#150491 (resolve: Mark items under exported ambiguous imports as exported) - rust-lang/rust#150720 (Do not suggest `derive` if there is already an impl) - rust-lang/rust#150968 (compiler-builtins: Remove the no-f16-f128 feature) - rust-lang/rust#151493 ([RFC] rustc_parse: improve the error diagnostic for "missing let in let chain") - rust-lang/rust#151660 (Bump `std`'s `backtrace`'s `rustc-demangle`) - rust-lang/rust#151696 (Borrowck: Simplify SCC annotation computation, placeholder rewriting) - rust-lang/rust#151704 (Implement `set_output_kind` for Emscripten linker) - rust-lang/rust#151706 (Remove Fuchsia from target OS list in unix.rs for sleep) - rust-lang/rust#151769 (fix undefined behavior in VecDeque::splice) - rust-lang/rust#151779 (stdarch subtree update) - rust-lang/rust#151449 ([rustdoc] Add regression test for rust-lang/rust#151411) - rust-lang/rust#151773 (clean up checks for constant promotion of integer division/remainder operations)
…uwer Rollup of 12 pull requests Successful merges: - rust-lang/rust#150491 (resolve: Mark items under exported ambiguous imports as exported) - rust-lang/rust#150720 (Do not suggest `derive` if there is already an impl) - rust-lang/rust#150968 (compiler-builtins: Remove the no-f16-f128 feature) - rust-lang/rust#151493 ([RFC] rustc_parse: improve the error diagnostic for "missing let in let chain") - rust-lang/rust#151660 (Bump `std`'s `backtrace`'s `rustc-demangle`) - rust-lang/rust#151696 (Borrowck: Simplify SCC annotation computation, placeholder rewriting) - rust-lang/rust#151704 (Implement `set_output_kind` for Emscripten linker) - rust-lang/rust#151706 (Remove Fuchsia from target OS list in unix.rs for sleep) - rust-lang/rust#151769 (fix undefined behavior in VecDeque::splice) - rust-lang/rust#151779 (stdarch subtree update) - rust-lang/rust#151449 ([rustdoc] Add regression test for rust-lang/rust#151411) - rust-lang/rust#151773 (clean up checks for constant promotion of integer division/remainder operations)
…uwer Rollup of 12 pull requests Successful merges: - rust-lang/rust#150491 (resolve: Mark items under exported ambiguous imports as exported) - rust-lang/rust#150720 (Do not suggest `derive` if there is already an impl) - rust-lang/rust#150968 (compiler-builtins: Remove the no-f16-f128 feature) - rust-lang/rust#151493 ([RFC] rustc_parse: improve the error diagnostic for "missing let in let chain") - rust-lang/rust#151660 (Bump `std`'s `backtrace`'s `rustc-demangle`) - rust-lang/rust#151696 (Borrowck: Simplify SCC annotation computation, placeholder rewriting) - rust-lang/rust#151704 (Implement `set_output_kind` for Emscripten linker) - rust-lang/rust#151706 (Remove Fuchsia from target OS list in unix.rs for sleep) - rust-lang/rust#151769 (fix undefined behavior in VecDeque::splice) - rust-lang/rust#151779 (stdarch subtree update) - rust-lang/rust#151449 ([rustdoc] Add regression test for rust-lang/rust#151411) - rust-lang/rust#151773 (clean up checks for constant promotion of integer division/remainder operations)
No description provided.