Skip to content
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

Merged
merged 5 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/librustc_parse/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,28 @@ impl<'a> Parser<'a> {
}

/// Parses the RHS of a local variable declaration (e.g., '= 14;').
fn parse_initializer(&mut self, skip_eq: bool) -> PResult<'a, Option<P<Expr>>> {
if self.eat(&token::Eq) || skip_eq { Ok(Some(self.parse_expr()?)) } else { Ok(None) }
fn parse_initializer(&mut self, eq_optional: bool) -> PResult<'a, Option<P<Expr>>> {
let eq_consumed = match self.token.kind {
token::BinOpEq(..) => {
// Recover `let x <op>= 1` as `let x = 1`
self.struct_span_err(
self.token.span,
"can't reassign to an uninitialized variable",
)
.span_suggestion_short(
self.token.span,
"initialize the variable",
"=".to_string(),
Applicability::MaybeIncorrect,
)
.emit();
self.bump();
true
}
_ => self.eat(&token::Eq),
};

Ok(if eq_consumed || eq_optional { Some(self.parse_expr()?) } else { None })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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) }

}

/// Parses a block. No inner attributes are allowed.
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/parser/let-binop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
let a: i8 *= 1; //~ ERROR can't reassign to an uninitialized variable
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
Copy link
Contributor

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;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=e06e05de1651f41857588ffad51deaa8:

error: can't reassign to an uninitialized variable
 --> src/main.rs:3:7
  |
3 | let d += 2;
  |       ^^ help: initialize the variable

Copy link
Contributor

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).

Copy link
Contributor

@pickfire pickfire Jul 1, 2020

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 +=?

Copy link
Contributor

@Lonami Lonami Jul 1, 2020

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)

let _ = c;
}
20 changes: 20 additions & 0 deletions src/test/ui/parser/let-binop.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: can't reassign to an uninitialized variable
--> $DIR/let-binop.rs:2:15
|
LL | let a: i8 *= 1;
| ^^ help: initialize the variable

error: can't reassign to an uninitialized variable
--> $DIR/let-binop.rs:4:11
|
LL | let b += 1;
| ^^ help: initialize the variable

error: can't reassign to an uninitialized variable
--> $DIR/let-binop.rs:6:11
|
LL | let c *= 1;
| ^^ help: initialize the variable

error: aborting due to 3 previous errors