Skip to content

Commit 4212835

Browse files
committed
Add heuristic to avoid treating x + +2 as increment
1 parent 29a5c36 commit 4212835

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed

compiler/rustc_parse/src/parser/expr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ impl<'a> Parser<'a> {
269269

270270
if self.prev_token == token::BinOp(token::Plus)
271271
&& self.token == token::BinOp(token::Plus)
272+
&& self.prev_token.span.between(self.token.span).is_empty()
272273
{
273274
let op_span = self.prev_token.span.to(self.token.span);
274275
// Eat the second `+`

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

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
error: Rust has no postfix increment operator
2-
--> $DIR/issue-36499.rs:4:7
1+
error: leading `+` is not supported
2+
--> $DIR/issue-36499.rs:4:9
33
|
44
LL | 2 + +2;
5-
| ^^^ not a valid postfix operator
5+
| ^ unexpected `+`
66
|
7-
help: use `+= 1` instead
8-
|
9-
LL | { let tmp = 2 ; 2 += 1; tmp }2;
10-
| +++++++++++ ~~~~~~~~~~~~~~~
11-
help: or, if you don't need to use it as an expression, change it to this
7+
help: try removing the `+`
128
|
139
LL - 2 + +2;
14-
LL + 2 += 12;
10+
LL + 2 + 2;
1511
|
1612

1713
error: aborting due to previous error

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

+5-9
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,16 @@ LL - let _ = +1;
1010
LL + let _ = 1;
1111
|
1212

13-
error: Rust has no postfix increment operator
14-
--> $DIR/issue-88276-unary-plus.rs:5:18
13+
error: leading `+` is not supported
14+
--> $DIR/issue-88276-unary-plus.rs:5:20
1515
|
1616
LL | let _ = (1.0 + +2.0) * +3.0;
17-
| ^^^ not a valid postfix operator
18-
|
19-
help: use `+= 1` instead
17+
| ^ unexpected `+`
2018
|
21-
LL | let _ = ({ let tmp = 1.0 ; 1.0 += 1; tmp }2.0) * +3.0;
22-
| +++++++++++ ~~~~~~~~~~~~~~~~~
23-
help: or, if you don't need to use it as an expression, change it to this
19+
help: try removing the `+`
2420
|
2521
LL - let _ = (1.0 + +2.0) * +3.0;
26-
LL + let _ = (1.0 += 12.0) * +3.0;
22+
LL + let _ = (1.0 + 2.0) * +3.0;
2723
|
2824

2925
error: leading `+` is not supported

0 commit comments

Comments
 (0)