Skip to content

Commit 6cfe98f

Browse files
committed
Improve error checking on unary plus
1 parent 5eacec9 commit 6cfe98f

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
2323
use rustc_span::{BytePos, Pos};
2424
use std::mem;
2525

26+
use tracing::debug;
27+
2628
/// Possibly accepts an `token::Interpolated` expression (a pre-parsed expression
2729
/// dropped into the token stream, which happens while parsing the result of
2830
/// macro expansion). Placement of these is not as complex as I feared it would
@@ -355,6 +357,7 @@ impl<'a> Parser<'a> {
355357
/// but the next token implies this should be parsed as an expression.
356358
/// For example: `if let Some(x) = x { x } else { 0 } / 2`.
357359
fn error_found_expr_would_be_stmt(&self, lhs: &Expr) {
360+
debug!("error_found_expr_would_be_stmt(lhs: {:?})", lhs);
358361
let mut err = self.struct_span_err(
359362
self.token.span,
360363
&format!("expected expression, found `{}`", pprust::token_to_string(&self.token),),
@@ -516,6 +519,15 @@ impl<'a> Parser<'a> {
516519
token::BinOp(token::And) | token::AndAnd => {
517520
make_it!(this, attrs, |this, _| this.parse_borrow_expr(lo))
518521
}
522+
token::BinOp(token::Plus) => {
523+
this.struct_span_err(lo, "leading `+` is not supported")
524+
.span_label(lo, "unexpected `+`")
525+
.span_suggestion_short(lo, "remove the `+`", "".to_string(), Applicability::MachineApplicable)
526+
.emit();
527+
this.bump();
528+
529+
this.parse_prefix_expr(None)
530+
} // `+expr`
519531
token::Ident(..) if this.token.is_keyword(kw::Box) => {
520532
make_it!(this, attrs, |this, _| this.parse_box_expr(lo))
521533
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-rustfix
2+
#[allow(unused_parens)]
3+
fn main() {
4+
let _ = 1; //~ ERROR leading `+` is not supported
5+
let _ = -(1+2)*3; //~ ERROR leading `+` is not supported
6+
let _ = -(1+2)*3; //~ ERROR leading `+` is not supported
7+
//~| ERROR leading `+` is not supported
8+
let _ = --(1+2)*3; //~ ERROR leading `+` is not supported
9+
//~| ERROR leading `+` is not supported
10+
let _ = (&"hello"); //~ ERROR leading `+` is not supported
11+
let _ = [3, 4+6]; //~ ERROR leading `+` is not supported
12+
//~| ERROR leading `+` is not supported
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-rustfix
2+
#[allow(unused_parens)]
3+
fn main() {
4+
let _ = +1; //~ ERROR leading `+` is not supported
5+
let _ = -+(1+2)*3; //~ ERROR leading `+` is not supported
6+
let _ = +-+(1+2)*3; //~ ERROR leading `+` is not supported
7+
//~| ERROR leading `+` is not supported
8+
let _ = -+-+(1+2)*3; //~ ERROR leading `+` is not supported
9+
//~| ERROR leading `+` is not supported
10+
let _ = (+&"hello"); //~ ERROR leading `+` is not supported
11+
let _ = +[+3, 4+6]; //~ ERROR leading `+` is not supported
12+
//~| ERROR leading `+` is not supported
13+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
error: leading `+` is not supported
2+
--> $DIR/issue-88276-unary-plus.rs:4:13
3+
|
4+
LL | let _ = +1;
5+
| ^
6+
| |
7+
| unexpected `+`
8+
| help: remove the `+`
9+
10+
error: leading `+` is not supported
11+
--> $DIR/issue-88276-unary-plus.rs:5:14
12+
|
13+
LL | let _ = -+(1+2)*3;
14+
| ^
15+
| |
16+
| unexpected `+`
17+
| help: remove the `+`
18+
19+
error: leading `+` is not supported
20+
--> $DIR/issue-88276-unary-plus.rs:6:13
21+
|
22+
LL | let _ = +-+(1+2)*3;
23+
| ^
24+
| |
25+
| unexpected `+`
26+
| help: remove the `+`
27+
28+
error: leading `+` is not supported
29+
--> $DIR/issue-88276-unary-plus.rs:6:15
30+
|
31+
LL | let _ = +-+(1+2)*3;
32+
| ^
33+
| |
34+
| unexpected `+`
35+
| help: remove the `+`
36+
37+
error: leading `+` is not supported
38+
--> $DIR/issue-88276-unary-plus.rs:8:14
39+
|
40+
LL | let _ = -+-+(1+2)*3;
41+
| ^
42+
| |
43+
| unexpected `+`
44+
| help: remove the `+`
45+
46+
error: leading `+` is not supported
47+
--> $DIR/issue-88276-unary-plus.rs:8:16
48+
|
49+
LL | let _ = -+-+(1+2)*3;
50+
| ^
51+
| |
52+
| unexpected `+`
53+
| help: remove the `+`
54+
55+
error: leading `+` is not supported
56+
--> $DIR/issue-88276-unary-plus.rs:10:14
57+
|
58+
LL | let _ = (+&"hello");
59+
| ^
60+
| |
61+
| unexpected `+`
62+
| help: remove the `+`
63+
64+
error: leading `+` is not supported
65+
--> $DIR/issue-88276-unary-plus.rs:11:13
66+
|
67+
LL | let _ = +[+3, 4+6];
68+
| ^
69+
| |
70+
| unexpected `+`
71+
| help: remove the `+`
72+
73+
error: leading `+` is not supported
74+
--> $DIR/issue-88276-unary-plus.rs:11:15
75+
|
76+
LL | let _ = +[+3, 4+6];
77+
| ^
78+
| |
79+
| unexpected `+`
80+
| help: remove the `+`
81+
82+
error: aborting due to 9 previous errors
83+

0 commit comments

Comments
 (0)