Skip to content

Commit 67a9adb

Browse files
committed
Refactor, handle fields better, add field tests
1 parent 80e57e2 commit 67a9adb

File tree

7 files changed

+66
-22
lines changed

7 files changed

+66
-22
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,15 @@ impl AttemptLocalParseRecovery {
156156
}
157157
}
158158

159+
/// Information for emitting suggestions and recovering from
160+
/// C-style `i++`, `--i`, etc.
159161
#[derive(Debug, Copy, Clone)]
160162
struct IncDecRecovery {
163+
/// This increment/decrement is not a subexpression.
161164
standalone: bool,
165+
/// Is this an increment or decrement?
162166
op: IncOrDec,
167+
/// Is this pre- or postfix?
163168
fixity: UnaryFixity,
164169
}
165170

@@ -1278,20 +1283,16 @@ impl<'a> Parser<'a> {
12781283
}
12791284

12801285
if kind.standalone {
1281-
self.inc_dec_standalone_recovery(base, err, kind, ident, spans)
1286+
self.inc_dec_standalone_recovery(base, err, kind, spans)
12821287
} else {
12831288
match kind.fixity {
1284-
UnaryFixity::Pre => {
1285-
self.prefix_inc_dec_suggest_and_recover(base, err, kind, ident, spans)
1286-
}
1287-
UnaryFixity::Post => {
1288-
self.postfix_inc_dec_suggest_and_recover(base, err, kind, ident, spans)
1289-
}
1289+
UnaryFixity::Pre => self.prefix_inc_dec_suggest(base, err, kind, ident, spans),
1290+
UnaryFixity::Post => self.postfix_inc_dec_suggest(base, err, kind, ident, spans),
12901291
}
12911292
}
12921293
}
12931294

1294-
fn prefix_inc_dec_suggest_and_recover(
1295+
fn prefix_inc_dec_suggest(
12951296
&mut self,
12961297
_base: P<Expr>,
12971298
mut err: DiagnosticBuilder<'a>,
@@ -1310,7 +1311,7 @@ impl<'a> Parser<'a> {
13101311
Err(err)
13111312
}
13121313

1313-
fn postfix_inc_dec_suggest_and_recover(
1314+
fn postfix_inc_dec_suggest(
13141315
&mut self,
13151316
_base: P<Expr>,
13161317
mut err: DiagnosticBuilder<'a>,
@@ -1334,7 +1335,6 @@ impl<'a> Parser<'a> {
13341335
_base: P<Expr>,
13351336
mut err: DiagnosticBuilder<'a>,
13361337
kind: IncDecRecovery,
1337-
_ident: Ident,
13381338
(pre_span, post_span): (Span, Span),
13391339
) -> PResult<'a, P<Expr>> {
13401340
err.multipart_suggestion(

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -592,13 +592,7 @@ impl<'a> Parser<'a> {
592592
this.bump();
593593
this.parse_prefix_expr(None)
594594
} // `+expr`
595-
token::Ident(..) if this.token.is_keyword(kw::Box) => {
596-
make_it!(this, attrs, |this, _| this.parse_box_expr(lo))
597-
}
598-
token::Ident(..) if this.is_mistaken_not_ident_negation() => {
599-
make_it!(this, attrs, |this, _| this.recover_not_expr(lo))
600-
}
601-
// Recover from `++x`
595+
// Recover from `++x`:
602596
token::BinOp(token::Plus)
603597
if this.look_ahead(1, |t| *t == token::BinOp(token::Plus)) =>
604598
{
@@ -608,9 +602,15 @@ impl<'a> Parser<'a> {
608602
this.bump();
609603
this.bump();
610604

611-
let operand_expr = this.parse_path_start_expr(Default::default())?;
605+
let operand_expr = this.parse_dot_or_call_expr(Default::default())?;
612606
this.maybe_recover_from_prefix_increment(operand_expr, pre_span, prev_is_semi)
613607
}
608+
token::Ident(..) if this.token.is_keyword(kw::Box) => {
609+
make_it!(this, attrs, |this, _| this.parse_box_expr(lo))
610+
}
611+
token::Ident(..) if this.is_mistaken_not_ident_negation() => {
612+
make_it!(this, attrs, |this, _| this.recover_not_expr(lo))
613+
}
614614
_ => return this.parse_dot_or_call_expr(Some(attrs)),
615615
}
616616
}

src/test/ui/parser/increment.stderr renamed to src/test/ui/parser/increment-autofix.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: Rust has no postfix increment operator
2-
--> $DIR/increment.rs:5:6
2+
--> $DIR/increment-autofix.rs:5:6
33
|
44
LL | i++;
55
| ^^ not a valid postfix operator
@@ -10,7 +10,7 @@ LL | { let tmp = i; i += 1; tmp };
1010
| +++++++++++ ~~~~~~~~~~~~~~~
1111

1212
error: Rust has no postfix increment operator
13-
--> $DIR/increment.rs:11:12
13+
--> $DIR/increment-autofix.rs:11:12
1414
|
1515
LL | while i++ < 5 {
1616
| ^^ not a valid postfix operator
@@ -21,7 +21,7 @@ LL | while { let tmp = i; i += 1; tmp } < 5 {
2121
| +++++++++++ ~~~~~~~~~~~~~~~
2222

2323
error: Rust has no prefix increment operator
24-
--> $DIR/increment.rs:19:5
24+
--> $DIR/increment-autofix.rs:19:5
2525
|
2626
LL | ++i;
2727
| ^^ not a valid prefix operator
@@ -33,7 +33,7 @@ LL + i += 1;
3333
|
3434

3535
error: Rust has no prefix increment operator
36-
--> $DIR/increment.rs:25:11
36+
--> $DIR/increment-autofix.rs:25:11
3737
|
3838
LL | while ++i < 5 {
3939
| ^^ not a valid prefix operator
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
struct Foo {
2+
bar: Bar,
3+
}
4+
5+
struct Bar {
6+
qux: i32,
7+
}
8+
9+
fn post_field() {
10+
let foo = Foo { bar: Bar { qux: 0 } };
11+
foo.bar.qux++;
12+
//~^ ERROR Rust has no postfix increment operator
13+
println!("{}", foo.bar.qux);
14+
}
15+
16+
fn pre_field() {
17+
let foo = Foo { bar: Bar { qux: 0 } };
18+
++foo.bar.qux;
19+
//~^ ERROR Rust has no prefix increment operator
20+
println!("{}", foo.bar.qux);
21+
}
22+
23+
fn main() {
24+
post_field();
25+
pre_field();
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: Rust has no postfix increment operator
2+
--> $DIR/increment-notfixed.rs:11:16
3+
|
4+
LL | foo.bar.qux++;
5+
| ^^ not a valid postfix operator
6+
|
7+
= help: use `+= 1` instead
8+
9+
error: Rust has no prefix increment operator
10+
--> $DIR/increment-notfixed.rs:18:5
11+
|
12+
LL | ++foo.bar.qux;
13+
| ^^ not a valid prefix operator
14+
|
15+
= help: use `+= 1` instead
16+
17+
error: aborting due to 2 previous errors
18+

0 commit comments

Comments
 (0)