check double negation

This commit is contained in:
nx2k3 2023-02-27 13:25:03 +00:00
parent 13a741afac
commit 0883973d2a
4 changed files with 91 additions and 85 deletions

View File

@ -284,6 +284,7 @@ impl<'a> Parser<'a> {
if self.prev_token == token::BinOp(token::Minus)
&& self.token == token::BinOp(token::Minus)
&& self.prev_token.span.between(self.token.span).is_empty()
&& !self.look_ahead(1, |tok| tok.can_begin_expr())
{
let op_span = self.prev_token.span.to(self.token.span);
// Eat the second `-`
@ -560,7 +561,10 @@ impl<'a> Parser<'a> {
token::Not => make_it!(this, attrs, |this, _| this.parse_unary_expr(lo, UnOp::Not)),
// `~expr`
token::Tilde => make_it!(this, attrs, |this, _| this.recover_tilde_expr(lo)),
// // `-expr`
// token::BinOp(token::Minus) => {
// make_it!(this, attrs, |this, _| this.parse_unary_expr(lo, UnOp::Neg))
// }
// `*expr`
token::BinOp(token::Star) => {
make_it!(this, attrs, |this, _| this.parse_unary_expr(lo, UnOp::Deref))
@ -604,17 +608,20 @@ impl<'a> Parser<'a> {
}
// Recover from `--x`:
token::BinOp(token::Minus)
if this.look_ahead(1, |t| *t == token::BinOp(token::Minus)) =>
if this.look_ahead(1, |t| *t == token::BinOp(token::Minus))
&& !this.token.can_begin_expr() =>
{
let starts_stmt = this.prev_token == token::Semi
|| this.prev_token == token::CloseDelim(Delimiter::Brace);
let pre_span = this.token.span.to(this.look_ahead(1, |t| t.span));
// if !this.token.can_begin_expr() {
// Eat both `-`s.
this.bump();
this.bump();
let operand_expr = this.parse_dot_or_call_expr(Default::default())?;
this.recover_from_prefix_decrement(operand_expr, pre_span, starts_stmt)
// }
}
// `-expr`
token::BinOp(token::Minus) => {

View File

@ -0,0 +1,48 @@
// run-pass
fn test1() {
let i = 0;
let c = i + --i;
println!("{c}");
}
fn test2() {
let i = 9;
let c = -- i + --i;
println!("{c}");
}
fn test3(){
let i=10;
println!("{}",i--i);
}
fn test4(){
let i=10;
println!("{}",--i);
}
struct Foo {
bar: Bar,
}
struct Bar {
qux: i32,
}
fn test5() {
let foo = Foo { bar: Bar { qux: 0 } };
let c=--foo.bar.qux;
println!("{c}");
}
fn test6(){
let x=2;
let y=--x;
println!("{y}");
}
fn main(){
test1();
test2();
test3();
test4();
test5();
test6();
}

View File

@ -1,18 +1,3 @@
fn test1() {
let mut i = 0;
let _ = i + --i; //~ ERROR Rust has no prefix decrement operator
}
fn test2() {
let mut i = 0;
let _ = --i + i; //~ ERROR Rust has no prefix decrement operator
}
fn test3() {
let mut i = 0;
let _ = --i + --i; //~ ERROR Rust has no prefix decrement operator
}
fn test4() {
let mut i = 0;
let _ = i + i--; //~ ERROR Rust has no postfix decrement operator
@ -21,32 +6,36 @@ fn test4() {
fn test5() {
let mut i = 0;
let _ = i-- + i; //~ ERROR Rust has no postfix decrement operator
let _ = i-- + i--; //~ ERROR Rust has no postfix decrement operator
}
fn test6() {
let mut i = 0;
let _ = i-- + i--; //~ ERROR Rust has no postfix decrement operator
let _ = --i + i--; //~ ERROR Rust has no postfix decrement operator
}
fn test7() {
let mut i = 0;
let _ = --i + i--; //~ ERROR Rust has no prefix decrement operator
let _ = i-- + --i; //~ ERROR Rust has no postfix decrement operator
}
fn test8() {
let mut i = 0;
let _ = i-- + --i; //~ ERROR Rust has no postfix decrement operator
let _ = (1 + 2 + i)--; //~ ERROR Rust has no postfix decrement operator
}
fn test9() {
let mut i = 0;
let _ = (1 + 2 + i)--; //~ ERROR Rust has no postfix decrement operator
}
fn test10() {
let mut i = 0;
let _ = (i-- + 1) + 2; //~ ERROR Rust has no postfix decrement operator
}
fn test14(){
let i=10;
while i!=0{
i--; //~ ERROR Rust has no postfix decrement operator
}
}
fn main() { }

View File

@ -1,55 +1,11 @@
error: Rust has no prefix decrement operator
--> $DIR/issue-dec.rs:3:17
|
LL | let _ = i + --i;
| ^^ not a valid prefix operator
|
help: use `-= 1` instead
|
LL | let _ = i + { i -= 1; i };
| ~ +++++++++
error: Rust has no prefix decrement operator
--> $DIR/issue-dec.rs:8:13
|
LL | let _ = --i + i;
| ^^ not a valid prefix operator
|
help: use `-= 1` instead
|
LL | let _ = { i -= 1; i } + i;
| ~ +++++++++
error: Rust has no prefix decrement operator
--> $DIR/issue-dec.rs:13:13
|
LL | let _ = --i + --i;
| ^^ not a valid prefix operator
|
help: use `-= 1` instead
|
LL | let _ = { i -= 1; i } + --i;
| ~ +++++++++
error: Rust has no postfix decrement operator
--> $DIR/issue-dec.rs:18:18
--> $DIR/issue-108495-dec.rs:3:18
|
LL | let _ = i + i--;
| ^^ not a valid postfix operator
error: Rust has no postfix decrement operator
--> $DIR/issue-dec.rs:24:14
|
LL | let _ = i-- + i;
| ^^ not a valid postfix operator
|
help: use `-= 1` instead
|
LL | let _ = { let tmp = i; i -= 1; tmp } + i;
| +++++++++++ ~~~~~~~~~~~~~~~
error: Rust has no postfix decrement operator
--> $DIR/issue-dec.rs:29:14
--> $DIR/issue-108495-dec.rs:9:14
|
LL | let _ = i-- + i--;
| ^^ not a valid postfix operator
@ -59,19 +15,14 @@ help: use `-= 1` instead
LL | let _ = { let tmp = i; i -= 1; tmp } + i--;
| +++++++++++ ~~~~~~~~~~~~~~~
error: Rust has no prefix decrement operator
--> $DIR/issue-dec.rs:34:13
error: Rust has no postfix decrement operator
--> $DIR/issue-108495-dec.rs:14:20
|
LL | let _ = --i + i--;
| ^^ not a valid prefix operator
|
help: use `-= 1` instead
|
LL | let _ = { i -= 1; i } + i--;
| ~ +++++++++
| ^^ not a valid postfix operator
error: Rust has no postfix decrement operator
--> $DIR/issue-dec.rs:39:14
--> $DIR/issue-108495-dec.rs:19:14
|
LL | let _ = i-- + --i;
| ^^ not a valid postfix operator
@ -82,7 +33,7 @@ LL | let _ = { let tmp = i; i -= 1; tmp } + --i;
| +++++++++++ ~~~~~~~~~~~~~~~
error: Rust has no postfix decrement operator
--> $DIR/issue-dec.rs:44:24
--> $DIR/issue-108495-dec.rs:24:24
|
LL | let _ = (1 + 2 + i)--;
| ^^ not a valid postfix operator
@ -93,7 +44,7 @@ LL | let _ = { let tmp = (1 + 2 + i); (1 + 2 + i) -= 1; tmp };
| +++++++++++ ~~~~~~~~~~~~~~~~~~~~~~~~~
error: Rust has no postfix decrement operator
--> $DIR/issue-dec.rs:49:15
--> $DIR/issue-108495-dec.rs:29:15
|
LL | let _ = (i-- + 1) + 2;
| ^^ not a valid postfix operator
@ -103,5 +54,16 @@ help: use `-= 1` instead
LL | let _ = ({ let tmp = i; i -= 1; tmp } + 1) + 2;
| +++++++++++ ~~~~~~~~~~~~~~~
error: aborting due to 10 previous errors
error: Rust has no postfix decrement operator
--> $DIR/issue-108495-dec.rs:37:10
|
LL | i--;
| ^^ not a valid postfix operator
|
help: use `-= 1` instead
|
LL | i -= 1;
| ~~~~
error: aborting due to 7 previous errors