Skip to content

Commit 0883973

Browse files
committed
check double negation
1 parent 13a741a commit 0883973

File tree

4 files changed

+90
-84
lines changed

4 files changed

+90
-84
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ impl<'a> Parser<'a> {
284284
if self.prev_token == token::BinOp(token::Minus)
285285
&& self.token == token::BinOp(token::Minus)
286286
&& self.prev_token.span.between(self.token.span).is_empty()
287+
&& !self.look_ahead(1, |tok| tok.can_begin_expr())
287288
{
288289
let op_span = self.prev_token.span.to(self.token.span);
289290
// Eat the second `-`
@@ -560,7 +561,10 @@ impl<'a> Parser<'a> {
560561
token::Not => make_it!(this, attrs, |this, _| this.parse_unary_expr(lo, UnOp::Not)),
561562
// `~expr`
562563
token::Tilde => make_it!(this, attrs, |this, _| this.recover_tilde_expr(lo)),
563-
564+
// // `-expr`
565+
// token::BinOp(token::Minus) => {
566+
// make_it!(this, attrs, |this, _| this.parse_unary_expr(lo, UnOp::Neg))
567+
// }
564568
// `*expr`
565569
token::BinOp(token::Star) => {
566570
make_it!(this, attrs, |this, _| this.parse_unary_expr(lo, UnOp::Deref))
@@ -604,17 +608,20 @@ impl<'a> Parser<'a> {
604608
}
605609
// Recover from `--x`:
606610
token::BinOp(token::Minus)
607-
if this.look_ahead(1, |t| *t == token::BinOp(token::Minus)) =>
611+
if this.look_ahead(1, |t| *t == token::BinOp(token::Minus))
612+
&& !this.token.can_begin_expr() =>
608613
{
609614
let starts_stmt = this.prev_token == token::Semi
610615
|| this.prev_token == token::CloseDelim(Delimiter::Brace);
611616
let pre_span = this.token.span.to(this.look_ahead(1, |t| t.span));
617+
// if !this.token.can_begin_expr() {
612618
// Eat both `-`s.
613619
this.bump();
614620
this.bump();
615-
616621
let operand_expr = this.parse_dot_or_call_expr(Default::default())?;
617622
this.recover_from_prefix_decrement(operand_expr, pre_span, starts_stmt)
623+
624+
// }
618625
}
619626
// `-expr`
620627
token::BinOp(token::Minus) => {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// run-pass
2+
fn test1() {
3+
let i = 0;
4+
let c = i + --i;
5+
println!("{c}");
6+
}
7+
fn test2() {
8+
let i = 9;
9+
let c = -- i + --i;
10+
println!("{c}");
11+
}
12+
13+
fn test3(){
14+
let i=10;
15+
println!("{}",i--i);
16+
}
17+
fn test4(){
18+
let i=10;
19+
println!("{}",--i);
20+
21+
}
22+
struct Foo {
23+
bar: Bar,
24+
}
25+
26+
struct Bar {
27+
qux: i32,
28+
}
29+
30+
fn test5() {
31+
let foo = Foo { bar: Bar { qux: 0 } };
32+
let c=--foo.bar.qux;
33+
println!("{c}");
34+
}
35+
36+
fn test6(){
37+
let x=2;
38+
let y=--x;
39+
println!("{y}");
40+
}
41+
fn main(){
42+
test1();
43+
test2();
44+
test3();
45+
test4();
46+
test5();
47+
test6();
48+
}

tests/ui/parser/issue-108495-dec.rs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
fn test1() {
2-
let mut i = 0;
3-
let _ = i + --i; //~ ERROR Rust has no prefix decrement operator
4-
}
5-
6-
fn test2() {
7-
let mut i = 0;
8-
let _ = --i + i; //~ ERROR Rust has no prefix decrement operator
9-
}
10-
11-
fn test3() {
12-
let mut i = 0;
13-
let _ = --i + --i; //~ ERROR Rust has no prefix decrement operator
14-
}
15-
161
fn test4() {
172
let mut i = 0;
183
let _ = i + i--; //~ ERROR Rust has no postfix decrement operator
@@ -21,32 +6,36 @@ fn test4() {
216

227
fn test5() {
238
let mut i = 0;
24-
let _ = i-- + i; //~ ERROR Rust has no postfix decrement operator
9+
let _ = i-- + i--; //~ ERROR Rust has no postfix decrement operator
2510
}
2611

2712
fn test6() {
2813
let mut i = 0;
29-
let _ = i-- + i--; //~ ERROR Rust has no postfix decrement operator
14+
let _ = --i + i--; //~ ERROR Rust has no postfix decrement operator
3015
}
3116

3217
fn test7() {
3318
let mut i = 0;
34-
let _ = --i + i--; //~ ERROR Rust has no prefix decrement operator
19+
let _ = i-- + --i; //~ ERROR Rust has no postfix decrement operator
3520
}
3621

3722
fn test8() {
3823
let mut i = 0;
39-
let _ = i-- + --i; //~ ERROR Rust has no postfix decrement operator
24+
let _ = (1 + 2 + i)--; //~ ERROR Rust has no postfix decrement operator
4025
}
4126

4227
fn test9() {
4328
let mut i = 0;
44-
let _ = (1 + 2 + i)--; //~ ERROR Rust has no postfix decrement operator
29+
let _ = (i-- + 1) + 2; //~ ERROR Rust has no postfix decrement operator
4530
}
4631

47-
fn test10() {
48-
let mut i = 0;
49-
let _ = (i-- + 1) + 2; //~ ERROR Rust has no postfix decrement operator
32+
33+
34+
fn test14(){
35+
let i=10;
36+
while i!=0{
37+
i--; //~ ERROR Rust has no postfix decrement operator
38+
}
5039
}
5140

5241
fn main() { }
Lines changed: 20 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,11 @@
1-
error: Rust has no prefix decrement operator
2-
--> $DIR/issue-dec.rs:3:17
3-
|
4-
LL | let _ = i + --i;
5-
| ^^ not a valid prefix operator
6-
|
7-
help: use `-= 1` instead
8-
|
9-
LL | let _ = i + { i -= 1; i };
10-
| ~ +++++++++
11-
12-
error: Rust has no prefix decrement operator
13-
--> $DIR/issue-dec.rs:8:13
14-
|
15-
LL | let _ = --i + i;
16-
| ^^ not a valid prefix operator
17-
|
18-
help: use `-= 1` instead
19-
|
20-
LL | let _ = { i -= 1; i } + i;
21-
| ~ +++++++++
22-
23-
error: Rust has no prefix decrement operator
24-
--> $DIR/issue-dec.rs:13:13
25-
|
26-
LL | let _ = --i + --i;
27-
| ^^ not a valid prefix operator
28-
|
29-
help: use `-= 1` instead
30-
|
31-
LL | let _ = { i -= 1; i } + --i;
32-
| ~ +++++++++
33-
341
error: Rust has no postfix decrement operator
35-
--> $DIR/issue-dec.rs:18:18
2+
--> $DIR/issue-108495-dec.rs:3:18
363
|
374
LL | let _ = i + i--;
385
| ^^ not a valid postfix operator
396

407
error: Rust has no postfix decrement operator
41-
--> $DIR/issue-dec.rs:24:14
42-
|
43-
LL | let _ = i-- + i;
44-
| ^^ not a valid postfix operator
45-
|
46-
help: use `-= 1` instead
47-
|
48-
LL | let _ = { let tmp = i; i -= 1; tmp } + i;
49-
| +++++++++++ ~~~~~~~~~~~~~~~
50-
51-
error: Rust has no postfix decrement operator
52-
--> $DIR/issue-dec.rs:29:14
8+
--> $DIR/issue-108495-dec.rs:9:14
539
|
5410
LL | let _ = i-- + i--;
5511
| ^^ not a valid postfix operator
@@ -59,19 +15,14 @@ help: use `-= 1` instead
5915
LL | let _ = { let tmp = i; i -= 1; tmp } + i--;
6016
| +++++++++++ ~~~~~~~~~~~~~~~
6117

62-
error: Rust has no prefix decrement operator
63-
--> $DIR/issue-dec.rs:34:13
18+
error: Rust has no postfix decrement operator
19+
--> $DIR/issue-108495-dec.rs:14:20
6420
|
6521
LL | let _ = --i + i--;
66-
| ^^ not a valid prefix operator
67-
|
68-
help: use `-= 1` instead
69-
|
70-
LL | let _ = { i -= 1; i } + i--;
71-
| ~ +++++++++
22+
| ^^ not a valid postfix operator
7223

7324
error: Rust has no postfix decrement operator
74-
--> $DIR/issue-dec.rs:39:14
25+
--> $DIR/issue-108495-dec.rs:19:14
7526
|
7627
LL | let _ = i-- + --i;
7728
| ^^ not a valid postfix operator
@@ -82,7 +33,7 @@ LL | let _ = { let tmp = i; i -= 1; tmp } + --i;
8233
| +++++++++++ ~~~~~~~~~~~~~~~
8334

8435
error: Rust has no postfix decrement operator
85-
--> $DIR/issue-dec.rs:44:24
36+
--> $DIR/issue-108495-dec.rs:24:24
8637
|
8738
LL | let _ = (1 + 2 + i)--;
8839
| ^^ not a valid postfix operator
@@ -93,7 +44,7 @@ LL | let _ = { let tmp = (1 + 2 + i); (1 + 2 + i) -= 1; tmp };
9344
| +++++++++++ ~~~~~~~~~~~~~~~~~~~~~~~~~
9445

9546
error: Rust has no postfix decrement operator
96-
--> $DIR/issue-dec.rs:49:15
47+
--> $DIR/issue-108495-dec.rs:29:15
9748
|
9849
LL | let _ = (i-- + 1) + 2;
9950
| ^^ not a valid postfix operator
@@ -103,5 +54,16 @@ help: use `-= 1` instead
10354
LL | let _ = ({ let tmp = i; i -= 1; tmp } + 1) + 2;
10455
| +++++++++++ ~~~~~~~~~~~~~~~
10556

106-
error: aborting due to 10 previous errors
57+
error: Rust has no postfix decrement operator
58+
--> $DIR/issue-108495-dec.rs:37:10
59+
|
60+
LL | i--;
61+
| ^^ not a valid postfix operator
62+
|
63+
help: use `-= 1` instead
64+
|
65+
LL | i -= 1;
66+
| ~~~~
67+
68+
error: aborting due to 7 previous errors
10769

0 commit comments

Comments
 (0)