mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-26 00:34:06 +00:00
Rollup merge of #108496 - nx2k3:issue-108495-dec, r=WaffleLapkin
fix #108495, postfix decrement and prefix decrement has no warning Fixes #108495
This commit is contained in:
commit
1c3cc8bba5
@ -165,8 +165,6 @@ enum IsStandalone {
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
enum IncOrDec {
|
||||
Inc,
|
||||
// FIXME: `i--` recovery isn't implemented yet
|
||||
#[allow(dead_code)]
|
||||
Dec,
|
||||
}
|
||||
|
||||
@ -1357,6 +1355,20 @@ impl<'a> Parser<'a> {
|
||||
self.recover_from_inc_dec(operand_expr, kind, op_span)
|
||||
}
|
||||
|
||||
pub(super) fn recover_from_postfix_decrement(
|
||||
&mut self,
|
||||
operand_expr: P<Expr>,
|
||||
op_span: Span,
|
||||
start_stmt: bool,
|
||||
) -> PResult<'a, P<Expr>> {
|
||||
let kind = IncDecRecovery {
|
||||
standalone: if start_stmt { IsStandalone::Standalone } else { IsStandalone::Subexpr },
|
||||
op: IncOrDec::Dec,
|
||||
fixity: UnaryFixity::Post,
|
||||
};
|
||||
self.recover_from_inc_dec(operand_expr, kind, op_span)
|
||||
}
|
||||
|
||||
fn recover_from_inc_dec(
|
||||
&mut self,
|
||||
base: P<Expr>,
|
||||
|
@ -282,6 +282,18 @@ impl<'a> Parser<'a> {
|
||||
continue;
|
||||
}
|
||||
|
||||
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 `-`
|
||||
self.bump();
|
||||
lhs = self.recover_from_postfix_decrement(lhs, op_span, starts_stmt)?;
|
||||
continue;
|
||||
}
|
||||
|
||||
let op = op.node;
|
||||
// Special cases:
|
||||
if op == AssocOp::As {
|
||||
|
39
tests/ui/parser/issue-108495-dec.rs
Normal file
39
tests/ui/parser/issue-108495-dec.rs
Normal file
@ -0,0 +1,39 @@
|
||||
fn test0() {
|
||||
let mut i = 0;
|
||||
let _ = i + i--; //~ ERROR Rust has no postfix decrement operator
|
||||
// won't suggest since we can not handle the precedences
|
||||
}
|
||||
|
||||
fn test1() {
|
||||
let mut i = 0;
|
||||
let _ = i-- + i--; //~ ERROR Rust has no postfix decrement operator
|
||||
}
|
||||
|
||||
fn test2() {
|
||||
let mut i = 0;
|
||||
let _ = --i + i--; //~ ERROR Rust has no postfix decrement operator
|
||||
}
|
||||
|
||||
fn test3() {
|
||||
let mut i = 0;
|
||||
let _ = i-- + --i; //~ ERROR Rust has no postfix decrement operator
|
||||
}
|
||||
|
||||
fn test4() {
|
||||
let mut i = 0;
|
||||
let _ = (1 + 2 + i)--; //~ ERROR Rust has no postfix decrement operator
|
||||
}
|
||||
|
||||
fn test5() {
|
||||
let mut i = 0;
|
||||
let _ = (i-- + 1) + 2; //~ ERROR Rust has no postfix decrement operator
|
||||
}
|
||||
|
||||
fn test6(){
|
||||
let i=10;
|
||||
while i != 0 {
|
||||
i--; //~ ERROR Rust has no postfix decrement operator
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
69
tests/ui/parser/issue-108495-dec.stderr
Normal file
69
tests/ui/parser/issue-108495-dec.stderr
Normal file
@ -0,0 +1,69 @@
|
||||
error: Rust has no postfix decrement operator
|
||||
--> $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-108495-dec.rs:9: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-108495-dec.rs:14:20
|
||||
|
|
||||
LL | let _ = --i + i--;
|
||||
| ^^ not a valid postfix operator
|
||||
|
||||
error: Rust has no postfix decrement operator
|
||||
--> $DIR/issue-108495-dec.rs:19: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-108495-dec.rs:24:24
|
||||
|
|
||||
LL | let _ = (1 + 2 + i)--;
|
||||
| ^^ not a valid postfix operator
|
||||
|
|
||||
help: use `-= 1` instead
|
||||
|
|
||||
LL | let _ = { let tmp = (1 + 2 + i); (1 + 2 + i) -= 1; tmp };
|
||||
| +++++++++++ ~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
error: Rust has no postfix decrement operator
|
||||
--> $DIR/issue-108495-dec.rs:29:15
|
||||
|
|
||||
LL | let _ = (i-- + 1) + 2;
|
||||
| ^^ not a valid postfix operator
|
||||
|
|
||||
help: use `-= 1` instead
|
||||
|
|
||||
LL | let _ = ({ let tmp = i; i -= 1; tmp } + 1) + 2;
|
||||
| +++++++++++ ~~~~~~~~~~~~~~~
|
||||
|
||||
error: Rust has no postfix decrement operator
|
||||
--> $DIR/issue-108495-dec.rs:35:10
|
||||
|
|
||||
LL | i--;
|
||||
| ^^ not a valid postfix operator
|
||||
|
|
||||
help: use `-= 1` instead
|
||||
|
|
||||
LL | i -= 1;
|
||||
| ~~~~
|
||||
|
||||
error: aborting due to 7 previous errors
|
||||
|
Loading…
Reference in New Issue
Block a user