Skip to content

Commit ded10a1

Browse files
committed
will not suggest for postfix operator when can not handle precedences well
1 parent dee85a3 commit ded10a1

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,11 @@ impl<'a> Parser<'a> {
13161316
self.prefix_inc_dec_suggest(base_src, kind, spans).emit(&mut err)
13171317
}
13181318
UnaryFixity::Post => {
1319-
self.postfix_inc_dec_suggest(base_src, kind, spans).emit(&mut err)
1319+
// won't suggest since we can not handle the precedences
1320+
// for example: `a + b++` has been parsed (a + b)++ and we can not suggest here
1321+
if !matches!(base.kind, ExprKind::Binary(_, _, _)) {
1322+
self.postfix_inc_dec_suggest(base_src, kind, spans).emit(&mut err)
1323+
}
13201324
}
13211325
}
13221326
}

src/test/ui/parser/issue-104867-inc-dec-2.rs

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ fn test3() {
1616
fn test4() {
1717
let mut i = 0;
1818
let _ = i + i++; //~ ERROR Rust has no postfix increment operator
19+
// won't suggest since we can not handle the precedences
1920
}
2021

2122
fn test5() {
@@ -38,4 +39,14 @@ fn test8() {
3839
let _ = i++ + ++i; //~ ERROR Rust has no postfix increment operator
3940
}
4041

42+
fn test9() {
43+
let mut i = 0;
44+
let _ = (1 + 2 + i)++; //~ ERROR Rust has no postfix increment operator
45+
}
46+
47+
fn test10() {
48+
let mut i = 0;
49+
let _ = (i++ + 1) + 2; //~ ERROR Rust has no postfix increment operator
50+
}
51+
4152
fn main() { }

src/test/ui/parser/issue-104867-inc-dec-2.stderr

+27-10
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,9 @@ error: Rust has no postfix increment operator
3636
|
3737
LL | let _ = i + i++;
3838
| ^^ not a valid postfix operator
39-
|
40-
help: use `+= 1` instead
41-
|
42-
LL | let _ = { let tmp = i + i; i + i += 1; tmp };
43-
| +++++++++++ ~~~~~~~~~~~~~~~~~~~
4439

4540
error: Rust has no postfix increment operator
46-
--> $DIR/issue-104867-inc-dec-2.rs:23:14
41+
--> $DIR/issue-104867-inc-dec-2.rs:24:14
4742
|
4843
LL | let _ = i++ + i;
4944
| ^^ not a valid postfix operator
@@ -54,7 +49,7 @@ LL | let _ = { let tmp = i; i += 1; tmp } + i;
5449
| +++++++++++ ~~~~~~~~~~~~~~~
5550

5651
error: Rust has no postfix increment operator
57-
--> $DIR/issue-104867-inc-dec-2.rs:28:14
52+
--> $DIR/issue-104867-inc-dec-2.rs:29:14
5853
|
5954
LL | let _ = i++ + i++;
6055
| ^^ not a valid postfix operator
@@ -65,7 +60,7 @@ LL | let _ = { let tmp = i; i += 1; tmp } + i++;
6560
| +++++++++++ ~~~~~~~~~~~~~~~
6661

6762
error: Rust has no prefix increment operator
68-
--> $DIR/issue-104867-inc-dec-2.rs:33:13
63+
--> $DIR/issue-104867-inc-dec-2.rs:34:13
6964
|
7065
LL | let _ = ++i + i++;
7166
| ^^ not a valid prefix operator
@@ -76,7 +71,7 @@ LL | let _ = { i += 1; i } + i++;
7671
| ~ +++++++++
7772

7873
error: Rust has no postfix increment operator
79-
--> $DIR/issue-104867-inc-dec-2.rs:38:14
74+
--> $DIR/issue-104867-inc-dec-2.rs:39:14
8075
|
8176
LL | let _ = i++ + ++i;
8277
| ^^ not a valid postfix operator
@@ -86,5 +81,27 @@ help: use `+= 1` instead
8681
LL | let _ = { let tmp = i; i += 1; tmp } + ++i;
8782
| +++++++++++ ~~~~~~~~~~~~~~~
8883

89-
error: aborting due to 8 previous errors
84+
error: Rust has no postfix increment operator
85+
--> $DIR/issue-104867-inc-dec-2.rs:44:24
86+
|
87+
LL | let _ = (1 + 2 + i)++;
88+
| ^^ not a valid postfix operator
89+
|
90+
help: use `+= 1` instead
91+
|
92+
LL | let _ = { let tmp = (1 + 2 + i); (1 + 2 + i) += 1; tmp };
93+
| +++++++++++ ~~~~~~~~~~~~~~~~~~~~~~~~~
94+
95+
error: Rust has no postfix increment operator
96+
--> $DIR/issue-104867-inc-dec-2.rs:49:15
97+
|
98+
LL | let _ = (i++ + 1) + 2;
99+
| ^^ not a valid postfix operator
100+
|
101+
help: use `+= 1` instead
102+
|
103+
LL | let _ = ({ let tmp = i; i += 1; tmp } + 1) + 2;
104+
| +++++++++++ ~~~~~~~~~~~~~~~
105+
106+
error: aborting due to 10 previous errors
90107

0 commit comments

Comments
 (0)