-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Improve diagnostics for let x += 1
#71976
Conversation
Feel free to ignore the correction, I just noticed it when I was perusing the code and figured I may as well make my first mini contribution :-) |
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.
Some minor nitpicks.
src/test/ui/parser/let-binop.stderr
Outdated
LL | b += 1; | ||
| -- | ||
|
||
error[E0067]: can't reassign to a uninitialized variable |
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.
I would prefer different wording here, but I'm not sure what :-/
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.
I agree the wording isn't ideal but I don't have any better ideas right now. The E0067 error description is: An invalid left-hand side expression was used on an assignment operation.
however I think it's harder to understand. I might be biased though :)
This cannot be an error at parse time. This should probably be combined with 870a7de and reported somewhere at resolution or HIR lowering time. |
A trait object cannot be part of a pattern, can it? |
@estebank |
I would be ok if this PR only recovered |
@petrochenkov do you mean leaving |
@mibac138 (Also if the recovery logic is larger than a few lines, then it's preferable to move it to |
When parsing `let x: i8 += 1` the compiler interprets `i8` as a trait which makes it more complicated to do error recovery. More advanced error recovery is not implemented in this commit.
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@estebank @petrochenkov this looks good for a merge? |
@bors r+ |
📌 Commit 98532a3 has been approved by |
Improve diagnostics for `let x += 1` Fixes(?) rust-lang#66736 The code responsible for the `E0404` errors is [here](https://github.com/rust-lang/rust/blob/master/src/librustc_parse/parser/ty.rs#L399-L424) which I don't think can be easily modified to prevent emitting an error in one specific case. Because of this I couldn't get rid of `E0404` and instead added `E0067` along with a help message which will fix the problem. r? @estebank
Improve diagnostics for `let x += 1` Fixes(?) rust-lang#66736 The code responsible for the `E0404` errors is [here](https://github.com/rust-lang/rust/blob/master/src/librustc_parse/parser/ty.rs#L399-L424) which I don't think can be easily modified to prevent emitting an error in one specific case. Because of this I couldn't get rid of `E0404` and instead added `E0067` along with a help message which will fix the problem. r? @estebank
Improve diagnostics for `let x += 1` Fixes(?) rust-lang#66736 The code responsible for the `E0404` errors is [here](https://github.com/rust-lang/rust/blob/master/src/librustc_parse/parser/ty.rs#L399-L424) which I don't think can be easily modified to prevent emitting an error in one specific case. Because of this I couldn't get rid of `E0404` and instead added `E0067` along with a help message which will fix the problem. r? @estebank
_ => self.eat(&token::Eq), | ||
}; | ||
|
||
Ok(if eq_consumed || eq_optional { Some(self.parse_expr()?) } else { None }) |
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.
Ok(if eq_consumed || eq_optional { Some(self.parse_expr()?) } else { None }) | |
if eq_consumed || eq_optional { self.parse_expr().map(Some) } else { Ok(None) } |
Improve diagnostics for `let x += 1` Fixes(?) rust-lang#66736 The code responsible for the `E0404` errors is [here](https://github.com/rust-lang/rust/blob/master/src/librustc_parse/parser/ty.rs#L399-L424) which I don't think can be easily modified to prevent emitting an error in one specific case. Because of this I couldn't get rid of `E0404` and instead added `E0067` along with a help message which will fix the problem. r? @estebank
Improve diagnostics for `let x += 1` Fixes(?) rust-lang#66736 The code responsible for the `E0404` errors is [here](https://github.com/rust-lang/rust/blob/master/src/librustc_parse/parser/ty.rs#L399-L424) which I don't think can be easily modified to prevent emitting an error in one specific case. Because of this I couldn't get rid of `E0404` and instead added `E0067` along with a help message which will fix the problem. r? @estebank
…arth Rollup of 17 pull requests Successful merges: - rust-lang#70551 (Make all uses of ty::Error delay a span bug) - rust-lang#71338 (Expand "recursive opaque type" diagnostic) - rust-lang#71976 (Improve diagnostics for `let x += 1`) - rust-lang#72279 (add raw_ref macros) - rust-lang#72628 (Add tests for 'impl Default for [T; N]') - rust-lang#72804 (Further tweak lifetime errors involving `dyn Trait` and `impl Trait` in return position) - rust-lang#72814 (remove visit_terminator_kind from MIR visitor) - rust-lang#72836 (Complete the std::time documentation to warn about the inconsistencies between OS) - rust-lang#72968 (Only highlight doc search results via mouseover if mouse has moved) - rust-lang#73034 (Export `#[inline]` fns with extern indicators) - rust-lang#73315 (Clean up some weird command strings) - rust-lang#73320 (Make new type param suggestion more targetted) - rust-lang#73361 (Tweak "non-primitive cast" error) - rust-lang#73425 (Mention functions pointers in the documentation) - rust-lang#73428 (Fix typo in librustc_ast docs) - rust-lang#73447 (Improve document for `Result::as_deref(_mut)` methods) - rust-lang#73476 (Added tooltip for should_panic code examples) Failed merges: r? @ghost
let _ = a; | ||
let b += 1; //~ ERROR can't reassign to an uninitialized variable | ||
let _ = b; | ||
let c *= 1; //~ ERROR can't reassign to an uninitialized variable |
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.
What happens if it's already initialized?
let d = 1;
let d += 2;
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.
error: can't reassign to an uninitialized variable
--> src/main.rs:3:7
|
3 | let d += 2;
| ^^ help: initialize the variable
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.
Yes, but one could argue I just initialized it above (same name).
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.
Maybe change the wording for initialized variables? Can't reassign to initialized variable, it should only be either let d = 2
or d += 2
, can we even use let
with +=
?
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.
+=
cannot be used with let
to my knowledge. Perhaps we could radically change the wording approach:
note: only `=` can be used in `let` bindings
help: change this `+=` for `=`
help: remove `let` (if it was initialized before)
…avidtwco Improve the wording for the `can't reassign` error Follow-up for rust-lang#71976 (comment). Fixes rust-lang#66736
…idtwco Improve the wording for the `can't reassign` error Follow-up for rust-lang#71976 (comment). Fixes rust-lang#66736
Fixes(?) #66736
The code responsible for the
E0404
errors is here which I don't think can be easily modified to prevent emitting an error in one specific case. Because of this I couldn't get rid ofE0404
and instead addedE0067
along with a help message which will fix the problem.r? @estebank