Error recovery for let with +=

This commit is contained in:
mibac138 2020-05-07 05:00:59 +02:00
parent 48ff12acb1
commit 05d6531998
3 changed files with 42 additions and 35 deletions

View file

@ -222,44 +222,43 @@ impl<'a> Parser<'a> {
has_ty: bool,
skip_eq: bool,
) -> PResult<'a, Option<P<Expr>>> {
let parse = if !self.eat(&token::Eq) && !skip_eq {
// In case of code like `let x: i8 += 1`, `i8` is interpreted as a trait consuming the `+`
// from `+=`.
let ate_plus = self.prev_token.is_like_plus() && has_ty;
let parse = if !skip_eq && (ate_plus || matches!(self.token.kind, TokenKind::BinOpEq(_))) {
// Error recovery for `let x += 1`
if matches!(self.token.kind, TokenKind::BinOpEq(_)) {
let mut err = struct_span_err!(
self.sess.span_diagnostic,
self.token.span,
E0067,
"can't reassign to a uninitialized variable"
);
let mut err = struct_span_err!(
self.sess.span_diagnostic,
self.token.span,
E0067,
"can't reassign to a uninitialized variable"
);
err.span_suggestion_short(
self.token.span,
"replace with `=` to initialize the variable",
"=".to_string(),
if has_ty {
// for `let x: i8 += 1` it's highly likely that the `+` is a typo
Applicability::MachineApplicable
} else {
// for `let x += 1` it's a bit less likely that the `+` is a typo
Applicability::MaybeIncorrect
},
);
// In case of code like `let x += 1` it's possible the user may have meant to write `x += 1`
if !has_ty {
err.span_suggestion_short(
self.token.span,
"replace with `=` to initialize the variable",
"=".to_string(),
if has_ty {
// for `let x: i8 += 1` it's highly likely that the `+` is a typo
Applicability::MachineApplicable
} else {
// for `let x += 1` it's a bit less likely that the `+` is a typo
Applicability::MaybeIncorrect
},
let_span,
"remove to reassign to a previously initialized variable",
"".to_string(),
Applicability::MaybeIncorrect,
);
// In case of code like `let x += 1` it's possible the user may have meant to write `x += 1`
if !has_ty {
err.span_suggestion_short(
let_span,
"remove to reassign to a previously initialized variable",
"".to_string(),
Applicability::MaybeIncorrect,
);
}
err.emit();
self.bump();
true
} else {
false
}
} else {
err.emit();
self.bump();
true
} else {
self.eat(&token::Eq) || skip_eq
};
if parse { Ok(Some(self.parse_expr()?)) } else { Ok(None) }

View file

@ -3,5 +3,6 @@
fn main() {
let a: i8 += 1;
//~^ ERROR expected trait, found builtin type `i8`
//~| ERROR can't reassign to a uninitialized variable
let _ = a;
}

View file

@ -1,9 +1,16 @@
error[E0067]: can't reassign to a uninitialized variable
--> $DIR/let-binop-plus.rs:4:16
|
LL | let a: i8 += 1;
| ^ help: replace with `=` to initialize the variable
error[E0404]: expected trait, found builtin type `i8`
--> $DIR/let-binop-plus.rs:4:12
|
LL | let a: i8 += 1;
| ^^ not a trait
error: aborting due to previous error
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0404`.
Some errors have detailed explanations: E0067, E0404.
For more information about an error, try `rustc --explain E0067`.