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 unary plus operators (#88276) #88553

Merged
merged 7 commits into from
Sep 8, 2021
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
7 changes: 7 additions & 0 deletions compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
20 changes: 20 additions & 0 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 `+`");
Comment on lines +520 to +521
Copy link
Contributor

Choose a reason for hiding this comment

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

We might want to replace this with

Suggested change
let mut err = this.struct_span_err(lo, "leading `+` is not supported");
err.span_label(lo, "unexpected `+`");
let mut err = this.unexpected().unwrap_err();

Copy link
Contributor Author

@theo-lw theo-lw Sep 3, 2021

Choose a reason for hiding this comment

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

I'm getting some unexpected results after changing this to unexpected::<P<Expr>>():

error: expected `#`, found `+`
 --> src/test/ui/parser/issue-88276-unary-plus.rs:4:13
  |
4 |     let _ = +1; //~ ERROR leading `+` is not supported
  |             ^ expected `#`
  |

I think this message is misleading because the programmer likely intended to type let _ = 1; and not let _ = #1. It might be better to keep these lines as is.


// 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))
}
Expand Down
10 changes: 8 additions & 2 deletions src/test/ui/associated-types/issue-36499.stderr
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion src/test/ui/parser/expr-as-stmt.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/parser/expr-as-stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/expr-as-stmt.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/parser/issue-88276-unary-plus.fixed
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 8 additions & 0 deletions src/test/ui/parser/issue-88276-unary-plus.rs
Original file line number Diff line number Diff line change
@@ -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
}
50 changes: 50 additions & 0 deletions src/test/ui/parser/issue-88276-unary-plus.stderr
Original file line number Diff line number Diff line change
@@ -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