Rollup merge of #88553 - theo-lw:issue-88276, r=estebank

Improve diagnostics for unary plus operators (#88276)

This pull request improves the diagnostics emitted on parsing a unary plus operator. See #88276.

Before:

```
error: expected expression, found `+`
 --> src/main.rs:2:13
  |
2 |     let x = +1;
  |             ^ expected expression
```

After:

```
error: leading `+` is not supported
 --> main.rs:2:13
  |
2 |     let x = +1;
  |             ^
  |             |
  |             unexpected `+`
  |             help: try removing the `+`
```
This commit is contained in:
Jack Huey 2021-09-08 12:24:16 -04:00 committed by GitHub
commit 77ac329a08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 105 additions and 6 deletions

View File

@ -586,6 +586,13 @@ impl Token {
self.is_non_raw_ident_where(|id| id.name.is_bool_lit())
}
pub fn is_numeric_lit(&self) -> bool {
matches!(
self.kind,
Literal(Lit { kind: LitKind::Integer, .. }) | Literal(Lit { kind: LitKind::Float, .. })
)
}
/// Returns `true` if the token is a non-raw identifier for which `pred` holds.
pub fn is_non_raw_ident_where(&self, pred: impl FnOnce(Ident) -> bool) -> bool {
match self.ident() {

View File

@ -516,6 +516,26 @@ impl<'a> Parser<'a> {
token::BinOp(token::And) | token::AndAnd => {
make_it!(this, attrs, |this, _| this.parse_borrow_expr(lo))
}
token::BinOp(token::Plus) if this.look_ahead(1, |tok| tok.is_numeric_lit()) => {
let mut err = this.struct_span_err(lo, "leading `+` is not supported");
err.span_label(lo, "unexpected `+`");
// a block on the LHS might have been intended to be an expression instead
if let Some(sp) = this.sess.ambiguous_block_expr_parse.borrow().get(&lo) {
this.sess.expr_parentheses_needed(&mut err, *sp);
} else {
err.span_suggestion_verbose(
lo,
"try removing the `+`",
"".to_string(),
Applicability::MachineApplicable,
);
}
err.emit();
this.bump();
this.parse_prefix_expr(None)
} // `+expr`
token::Ident(..) if this.token.is_keyword(kw::Box) => {
make_it!(this, attrs, |this, _| this.parse_box_expr(lo))
}

View File

@ -1,8 +1,14 @@
error: expected expression, found `+`
error: leading `+` is not supported
--> $DIR/issue-36499.rs:4:9
|
LL | 2 + +2;
| ^ expected expression
| ^ unexpected `+`
|
help: try removing the `+`
|
LL - 2 + +2;
LL + 2 + 2;
|
error: aborting due to previous error

View File

@ -10,7 +10,7 @@ fn foo() -> i32 {
}
fn bar() -> i32 {
({2}) + 2 //~ ERROR expected expression, found `+`
({2}) + 2 //~ ERROR leading `+` is not supported
//~^ ERROR mismatched types
}

View File

@ -10,7 +10,7 @@ fn foo() -> i32 {
}
fn bar() -> i32 {
{2} + 2 //~ ERROR expected expression, found `+`
{2} + 2 //~ ERROR leading `+` is not supported
//~^ ERROR mismatched types
}

View File

@ -9,11 +9,11 @@ help: parentheses are required to parse this as an expression
LL | ({2}) + {2}
| + +
error: expected expression, found `+`
error: leading `+` is not supported
--> $DIR/expr-as-stmt.rs:13:9
|
LL | {2} + 2
| ^ expected expression
| ^ unexpected `+`
|
help: parentheses are required to parse this as an expression
|

View File

@ -0,0 +1,8 @@
// run-rustfix
#[allow(unused_parens)]
fn main() {
let _ = 1; //~ ERROR leading `+` is not supported
let _ = (1.0 + 2.0) * 3.0; //~ ERROR leading `+` is not supported
//~| ERROR leading `+` is not supported
let _ = [3, 4+6]; //~ ERROR leading `+` is not supported
}

View File

@ -0,0 +1,8 @@
// run-rustfix
#[allow(unused_parens)]
fn main() {
let _ = +1; //~ ERROR leading `+` is not supported
let _ = (1.0 + +2.0) * +3.0; //~ ERROR leading `+` is not supported
//~| ERROR leading `+` is not supported
let _ = [+3, 4+6]; //~ ERROR leading `+` is not supported
}

View File

@ -0,0 +1,50 @@
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:4:13
|
LL | let _ = +1;
| ^ unexpected `+`
|
help: try removing the `+`
|
LL - let _ = +1;
LL + let _ = 1;
|
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:5:20
|
LL | let _ = (1.0 + +2.0) * +3.0;
| ^ unexpected `+`
|
help: try removing the `+`
|
LL - let _ = (1.0 + +2.0) * +3.0;
LL + let _ = (1.0 + 2.0) * +3.0;
|
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:5:28
|
LL | let _ = (1.0 + +2.0) * +3.0;
| ^ unexpected `+`
|
help: try removing the `+`
|
LL - let _ = (1.0 + +2.0) * +3.0;
LL + let _ = (1.0 + +2.0) * 3.0;
|
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:7:14
|
LL | let _ = [+3, 4+6];
| ^ unexpected `+`
|
help: try removing the `+`
|
LL - let _ = [+3, 4+6];
LL + let _ = [3, 4+6];
|
error: aborting due to 4 previous errors