Skip to content

Commit 65eb7e5

Browse files
committed
Use verbose suggestions and only match if the + is seen before a numeric literal
1 parent bc9877c commit 65eb7e5

File tree

9 files changed

+55
-92
lines changed

9 files changed

+55
-92
lines changed

compiler/rustc_ast/src/token.rs

+4
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,10 @@ impl Token {
586586
self.is_non_raw_ident_where(|id| id.name.is_bool_lit())
587587
}
588588

589+
pub fn is_numeric_lit(&self) -> bool {
590+
matches!(self.kind, Literal(Lit { kind: LitKind::Integer, ..}) | Literal(Lit { kind: LitKind::Float, ..}))
591+
}
592+
589593
/// Returns `true` if the token is a non-raw identifier for which `pred` holds.
590594
pub fn is_non_raw_ident_where(&self, pred: impl FnOnce(Ident) -> bool) -> bool {
591595
match self.ident() {

compiler/rustc_parse/src/parser/expr.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -516,16 +516,15 @@ impl<'a> Parser<'a> {
516516
token::BinOp(token::And) | token::AndAnd => {
517517
make_it!(this, attrs, |this, _| this.parse_borrow_expr(lo))
518518
}
519-
token::BinOp(token::Plus) => {
519+
token::BinOp(token::Plus) if this.look_ahead(1, |tok| tok.is_numeric_lit()) => {
520520
let mut err = this.struct_span_err(lo, "leading `+` is not supported");
521521
err.span_label(lo, "unexpected `+`");
522522

523523
// a block on the LHS might have been intended to be an expression instead
524-
let sp = this.sess.source_map().start_point(lo);
525-
if let Some(sp) = this.sess.ambiguous_block_expr_parse.borrow().get(&sp) {
524+
if let Some(sp) = this.sess.ambiguous_block_expr_parse.borrow().get(&lo) {
526525
this.sess.expr_parentheses_needed(&mut err, *sp);
527526
} else {
528-
err.span_suggestion(
527+
err.span_suggestion_verbose(
529528
lo,
530529
"try removing the `+`",
531530
"".to_string(),

src/test/ui/associated-types/issue-36499.stderr

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ error: leading `+` is not supported
22
--> $DIR/issue-36499.rs:4:9
33
|
44
LL | 2 + +2;
5-
| ^
6-
| |
7-
| unexpected `+`
8-
| help: try removing the `+`
5+
| ^ unexpected `+`
6+
|
7+
help: try removing the `+`
8+
|
9+
LL - 2 + +2;
10+
LL + 2 + 2;
11+
|
912

1013
error: aborting due to previous error
1114

src/test/ui/parser/expr-as-stmt.fixed

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![allow(unused_must_use)]
66

77
fn foo() -> i32 {
8-
({2}) + {2} //~ ERROR leading `+` is not supported
8+
({2}) + {2} //~ ERROR expected expression, found `+`
99
//~^ ERROR mismatched types
1010
}
1111

@@ -16,7 +16,7 @@ fn bar() -> i32 {
1616

1717
fn zul() -> u32 {
1818
let foo = 3;
19-
({ 42 }) + foo; //~ ERROR leading `+` is not supported
19+
({ 42 }) + foo; //~ ERROR expected expression, found `+`
2020
//~^ ERROR mismatched types
2121
32
2222
}

src/test/ui/parser/expr-as-stmt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![allow(unused_must_use)]
66

77
fn foo() -> i32 {
8-
{2} + {2} //~ ERROR leading `+` is not supported
8+
{2} + {2} //~ ERROR expected expression, found `+`
99
//~^ ERROR mismatched types
1010
}
1111

@@ -16,7 +16,7 @@ fn bar() -> i32 {
1616

1717
fn zul() -> u32 {
1818
let foo = 3;
19-
{ 42 } + foo; //~ ERROR leading `+` is not supported
19+
{ 42 } + foo; //~ ERROR expected expression, found `+`
2020
//~^ ERROR mismatched types
2121
32
2222
}

src/test/ui/parser/expr-as-stmt.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: leading `+` is not supported
1+
error: expected expression, found `+`
22
--> $DIR/expr-as-stmt.rs:8:9
33
|
44
LL | {2} + {2}
5-
| ^ unexpected `+`
5+
| ^ expected expression
66
|
77
help: parentheses are required to parse this as an expression
88
|
@@ -20,11 +20,11 @@ help: parentheses are required to parse this as an expression
2020
LL | ({2}) + 2
2121
| + +
2222

23-
error: leading `+` is not supported
23+
error: expected expression, found `+`
2424
--> $DIR/expr-as-stmt.rs:19:12
2525
|
2626
LL | { 42 } + foo;
27-
| ^ unexpected `+`
27+
| ^ expected expression
2828
|
2929
help: parentheses are required to parse this as an expression
3030
|

src/test/ui/parser/issue-88276-unary-plus.fixed

+1-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
#[allow(unused_parens)]
33
fn main() {
44
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
5+
let _ = (1.0 + 2.0) * 3.0; //~ ERROR leading `+` is not supported
96
//~| ERROR leading `+` is not supported
10-
let _ = (&"hello"); //~ ERROR leading `+` is not supported
117
let _ = [3, 4+6]; //~ ERROR leading `+` is not supported
12-
//~| ERROR leading `+` is not supported
138
}

src/test/ui/parser/issue-88276-unary-plus.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
#[allow(unused_parens)]
33
fn main() {
44
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
5+
let _ = (1.0 + +2.0) * +3.0; //~ ERROR leading `+` is not supported
96
//~| 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
7+
let _ = [+3, 4+6]; //~ ERROR leading `+` is not supported
138
}

src/test/ui/parser/issue-88276-unary-plus.stderr

+30-63
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,49 @@ error: leading `+` is not supported
22
--> $DIR/issue-88276-unary-plus.rs:4:13
33
|
44
LL | let _ = +1;
5-
| ^
6-
| |
7-
| unexpected `+`
8-
| help: try removing the `+`
9-
10-
error: leading `+` is not supported
11-
--> $DIR/issue-88276-unary-plus.rs:5:14
5+
| ^ unexpected `+`
126
|
13-
LL | let _ = -+(1+2)*3;
14-
| ^
15-
| |
16-
| unexpected `+`
17-
| help: try removing the `+`
18-
19-
error: leading `+` is not supported
20-
--> $DIR/issue-88276-unary-plus.rs:6:14
7+
help: try removing the `+`
218
|
22-
LL | let _ = -+-+(1+2)*3;
23-
| ^
24-
| |
25-
| unexpected `+`
26-
| help: try removing the `+`
9+
LL - let _ = +1;
10+
LL + let _ = 1;
11+
|
2712

2813
error: leading `+` is not supported
29-
--> $DIR/issue-88276-unary-plus.rs:6:16
14+
--> $DIR/issue-88276-unary-plus.rs:5:20
3015
|
31-
LL | let _ = -+-+(1+2)*3;
32-
| ^
33-
| |
34-
| unexpected `+`
35-
| help: try removing the `+`
36-
37-
error: leading `+` is not supported
38-
--> $DIR/issue-88276-unary-plus.rs:8:18
16+
LL | let _ = (1.0 + +2.0) * +3.0;
17+
| ^ unexpected `+`
3918
|
40-
LL | let _ = (1 + +2) * +3;
41-
| ^
42-
| |
43-
| unexpected `+`
44-
| help: try removing the `+`
45-
46-
error: leading `+` is not supported
47-
--> $DIR/issue-88276-unary-plus.rs:8:24
19+
help: try removing the `+`
4820
|
49-
LL | let _ = (1 + +2) * +3;
50-
| ^
51-
| |
52-
| unexpected `+`
53-
| help: try removing the `+`
21+
LL - let _ = (1.0 + +2.0) * +3.0;
22+
LL + let _ = (1.0 + 2.0) * +3.0;
23+
|
5424

5525
error: leading `+` is not supported
56-
--> $DIR/issue-88276-unary-plus.rs:10:14
26+
--> $DIR/issue-88276-unary-plus.rs:5:28
5727
|
58-
LL | let _ = (+&"hello");
59-
| ^
60-
| |
61-
| unexpected `+`
62-
| help: try removing the `+`
63-
64-
error: leading `+` is not supported
65-
--> $DIR/issue-88276-unary-plus.rs:11:13
28+
LL | let _ = (1.0 + +2.0) * +3.0;
29+
| ^ unexpected `+`
6630
|
67-
LL | let _ = +[+3, 4+6];
68-
| ^
69-
| |
70-
| unexpected `+`
71-
| help: try removing the `+`
31+
help: try removing the `+`
32+
|
33+
LL - let _ = (1.0 + +2.0) * +3.0;
34+
LL + let _ = (1.0 + +2.0) * 3.0;
35+
|
7236

7337
error: leading `+` is not supported
74-
--> $DIR/issue-88276-unary-plus.rs:11:15
38+
--> $DIR/issue-88276-unary-plus.rs:7:14
39+
|
40+
LL | let _ = [+3, 4+6];
41+
| ^ unexpected `+`
42+
|
43+
help: try removing the `+`
7544
|
76-
LL | let _ = +[+3, 4+6];
77-
| ^
78-
| |
79-
| unexpected `+`
80-
| help: try removing the `+`
45+
LL - let _ = [+3, 4+6];
46+
LL + let _ = [3, 4+6];
47+
|
8148

82-
error: aborting due to 9 previous errors
49+
error: aborting due to 4 previous errors
8350

0 commit comments

Comments
 (0)