Refactor, handle fields better, add field tests
This commit is contained in:
parent
80e57e223e
commit
67a9adbb54
7 changed files with 66 additions and 22 deletions
|
@ -156,10 +156,15 @@ impl AttemptLocalParseRecovery {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Information for emitting suggestions and recovering from
|
||||||
|
/// C-style `i++`, `--i`, etc.
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
struct IncDecRecovery {
|
struct IncDecRecovery {
|
||||||
|
/// This increment/decrement is not a subexpression.
|
||||||
standalone: bool,
|
standalone: bool,
|
||||||
|
/// Is this an increment or decrement?
|
||||||
op: IncOrDec,
|
op: IncOrDec,
|
||||||
|
/// Is this pre- or postfix?
|
||||||
fixity: UnaryFixity,
|
fixity: UnaryFixity,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1278,20 +1283,16 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if kind.standalone {
|
if kind.standalone {
|
||||||
self.inc_dec_standalone_recovery(base, err, kind, ident, spans)
|
self.inc_dec_standalone_recovery(base, err, kind, spans)
|
||||||
} else {
|
} else {
|
||||||
match kind.fixity {
|
match kind.fixity {
|
||||||
UnaryFixity::Pre => {
|
UnaryFixity::Pre => self.prefix_inc_dec_suggest(base, err, kind, ident, spans),
|
||||||
self.prefix_inc_dec_suggest_and_recover(base, err, kind, ident, spans)
|
UnaryFixity::Post => self.postfix_inc_dec_suggest(base, err, kind, ident, spans),
|
||||||
}
|
|
||||||
UnaryFixity::Post => {
|
|
||||||
self.postfix_inc_dec_suggest_and_recover(base, err, kind, ident, spans)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prefix_inc_dec_suggest_and_recover(
|
fn prefix_inc_dec_suggest(
|
||||||
&mut self,
|
&mut self,
|
||||||
_base: P<Expr>,
|
_base: P<Expr>,
|
||||||
mut err: DiagnosticBuilder<'a>,
|
mut err: DiagnosticBuilder<'a>,
|
||||||
|
@ -1310,7 +1311,7 @@ impl<'a> Parser<'a> {
|
||||||
Err(err)
|
Err(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn postfix_inc_dec_suggest_and_recover(
|
fn postfix_inc_dec_suggest(
|
||||||
&mut self,
|
&mut self,
|
||||||
_base: P<Expr>,
|
_base: P<Expr>,
|
||||||
mut err: DiagnosticBuilder<'a>,
|
mut err: DiagnosticBuilder<'a>,
|
||||||
|
@ -1334,7 +1335,6 @@ impl<'a> Parser<'a> {
|
||||||
_base: P<Expr>,
|
_base: P<Expr>,
|
||||||
mut err: DiagnosticBuilder<'a>,
|
mut err: DiagnosticBuilder<'a>,
|
||||||
kind: IncDecRecovery,
|
kind: IncDecRecovery,
|
||||||
_ident: Ident,
|
|
||||||
(pre_span, post_span): (Span, Span),
|
(pre_span, post_span): (Span, Span),
|
||||||
) -> PResult<'a, P<Expr>> {
|
) -> PResult<'a, P<Expr>> {
|
||||||
err.multipart_suggestion(
|
err.multipart_suggestion(
|
||||||
|
|
|
@ -592,13 +592,7 @@ impl<'a> Parser<'a> {
|
||||||
this.bump();
|
this.bump();
|
||||||
this.parse_prefix_expr(None)
|
this.parse_prefix_expr(None)
|
||||||
} // `+expr`
|
} // `+expr`
|
||||||
token::Ident(..) if this.token.is_keyword(kw::Box) => {
|
// Recover from `++x`:
|
||||||
make_it!(this, attrs, |this, _| this.parse_box_expr(lo))
|
|
||||||
}
|
|
||||||
token::Ident(..) if this.is_mistaken_not_ident_negation() => {
|
|
||||||
make_it!(this, attrs, |this, _| this.recover_not_expr(lo))
|
|
||||||
}
|
|
||||||
// Recover from `++x`
|
|
||||||
token::BinOp(token::Plus)
|
token::BinOp(token::Plus)
|
||||||
if this.look_ahead(1, |t| *t == token::BinOp(token::Plus)) =>
|
if this.look_ahead(1, |t| *t == token::BinOp(token::Plus)) =>
|
||||||
{
|
{
|
||||||
|
@ -608,9 +602,15 @@ impl<'a> Parser<'a> {
|
||||||
this.bump();
|
this.bump();
|
||||||
this.bump();
|
this.bump();
|
||||||
|
|
||||||
let operand_expr = this.parse_path_start_expr(Default::default())?;
|
let operand_expr = this.parse_dot_or_call_expr(Default::default())?;
|
||||||
this.maybe_recover_from_prefix_increment(operand_expr, pre_span, prev_is_semi)
|
this.maybe_recover_from_prefix_increment(operand_expr, pre_span, prev_is_semi)
|
||||||
}
|
}
|
||||||
|
token::Ident(..) if this.token.is_keyword(kw::Box) => {
|
||||||
|
make_it!(this, attrs, |this, _| this.parse_box_expr(lo))
|
||||||
|
}
|
||||||
|
token::Ident(..) if this.is_mistaken_not_ident_negation() => {
|
||||||
|
make_it!(this, attrs, |this, _| this.recover_not_expr(lo))
|
||||||
|
}
|
||||||
_ => return this.parse_dot_or_call_expr(Some(attrs)),
|
_ => return this.parse_dot_or_call_expr(Some(attrs)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
error: Rust has no postfix increment operator
|
error: Rust has no postfix increment operator
|
||||||
--> $DIR/increment.rs:5:6
|
--> $DIR/increment-autofix.rs:5:6
|
||||||
|
|
|
|
||||||
LL | i++;
|
LL | i++;
|
||||||
| ^^ not a valid postfix operator
|
| ^^ not a valid postfix operator
|
||||||
|
@ -10,7 +10,7 @@ LL | { let tmp = i; i += 1; tmp };
|
||||||
| +++++++++++ ~~~~~~~~~~~~~~~
|
| +++++++++++ ~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
error: Rust has no postfix increment operator
|
error: Rust has no postfix increment operator
|
||||||
--> $DIR/increment.rs:11:12
|
--> $DIR/increment-autofix.rs:11:12
|
||||||
|
|
|
|
||||||
LL | while i++ < 5 {
|
LL | while i++ < 5 {
|
||||||
| ^^ not a valid postfix operator
|
| ^^ not a valid postfix operator
|
||||||
|
@ -21,7 +21,7 @@ LL | while { let tmp = i; i += 1; tmp } < 5 {
|
||||||
| +++++++++++ ~~~~~~~~~~~~~~~
|
| +++++++++++ ~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
error: Rust has no prefix increment operator
|
error: Rust has no prefix increment operator
|
||||||
--> $DIR/increment.rs:19:5
|
--> $DIR/increment-autofix.rs:19:5
|
||||||
|
|
|
|
||||||
LL | ++i;
|
LL | ++i;
|
||||||
| ^^ not a valid prefix operator
|
| ^^ not a valid prefix operator
|
||||||
|
@ -33,7 +33,7 @@ LL + i += 1;
|
||||||
|
|
|
|
||||||
|
|
||||||
error: Rust has no prefix increment operator
|
error: Rust has no prefix increment operator
|
||||||
--> $DIR/increment.rs:25:11
|
--> $DIR/increment-autofix.rs:25:11
|
||||||
|
|
|
|
||||||
LL | while ++i < 5 {
|
LL | while ++i < 5 {
|
||||||
| ^^ not a valid prefix operator
|
| ^^ not a valid prefix operator
|
26
src/test/ui/parser/increment-notfixed.rs
Normal file
26
src/test/ui/parser/increment-notfixed.rs
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
struct Foo {
|
||||||
|
bar: Bar,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Bar {
|
||||||
|
qux: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn post_field() {
|
||||||
|
let foo = Foo { bar: Bar { qux: 0 } };
|
||||||
|
foo.bar.qux++;
|
||||||
|
//~^ ERROR Rust has no postfix increment operator
|
||||||
|
println!("{}", foo.bar.qux);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pre_field() {
|
||||||
|
let foo = Foo { bar: Bar { qux: 0 } };
|
||||||
|
++foo.bar.qux;
|
||||||
|
//~^ ERROR Rust has no prefix increment operator
|
||||||
|
println!("{}", foo.bar.qux);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
post_field();
|
||||||
|
pre_field();
|
||||||
|
}
|
18
src/test/ui/parser/increment-notfixed.stderr
Normal file
18
src/test/ui/parser/increment-notfixed.stderr
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
error: Rust has no postfix increment operator
|
||||||
|
--> $DIR/increment-notfixed.rs:11:16
|
||||||
|
|
|
||||||
|
LL | foo.bar.qux++;
|
||||||
|
| ^^ not a valid postfix operator
|
||||||
|
|
|
||||||
|
= help: use `+= 1` instead
|
||||||
|
|
||||||
|
error: Rust has no prefix increment operator
|
||||||
|
--> $DIR/increment-notfixed.rs:18:5
|
||||||
|
|
|
||||||
|
LL | ++foo.bar.qux;
|
||||||
|
| ^^ not a valid prefix operator
|
||||||
|
|
|
||||||
|
= help: use `+= 1` instead
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue