Skip to content

Commit 7287f92

Browse files
committed
Emit structured suggestions for field accesses too
1 parent 67a9adb commit 7287f92

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,45 +1266,36 @@ impl<'a> Parser<'a> {
12661266
Ok(base)
12671267
};
12681268

1269-
let ExprKind::Path(_, path) = &base.kind
1270-
else { return help_base_case(err, base) };
1271-
let [segment] = path.segments.as_slice()
1272-
else { return help_base_case(err, base) };
1273-
let ident = segment.ident;
1274-
12751269
// (pre, post)
12761270
let spans = match kind.fixity {
1277-
UnaryFixity::Pre => (op_span, ident.span.shrink_to_hi()),
1278-
UnaryFixity::Post => (ident.span.shrink_to_lo(), op_span),
1271+
UnaryFixity::Pre => (op_span, base.span.shrink_to_hi()),
1272+
UnaryFixity::Post => (base.span.shrink_to_lo(), op_span),
12791273
};
12801274

1281-
if ident.is_reserved() {
1282-
return help_base_case(err, base);
1283-
}
1284-
12851275
if kind.standalone {
1286-
self.inc_dec_standalone_recovery(base, err, kind, spans)
1276+
self.inc_dec_standalone_recovery(err, kind, spans)
12871277
} else {
1278+
let Ok(base_src) = self.span_to_snippet(base.span)
1279+
else { return help_base_case(err, base) };
12881280
match kind.fixity {
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),
1281+
UnaryFixity::Pre => self.prefix_inc_dec_suggest(base_src, err, kind, spans),
1282+
UnaryFixity::Post => self.postfix_inc_dec_suggest(base_src, err, kind, spans),
12911283
}
12921284
}
12931285
}
12941286

12951287
fn prefix_inc_dec_suggest(
12961288
&mut self,
1297-
_base: P<Expr>,
1289+
base_src: String,
12981290
mut err: DiagnosticBuilder<'a>,
12991291
kind: IncDecRecovery,
1300-
ident: Ident,
13011292
(pre_span, post_span): (Span, Span),
13021293
) -> PResult<'a, P<Expr>> {
13031294
err.multipart_suggestion(
13041295
&format!("use `{}= 1` instead", kind.op.chr()),
13051296
vec![
13061297
(pre_span, "{ ".to_string()),
1307-
(post_span, format!(" {}= 1; {} }}", kind.op.chr(), ident)),
1298+
(post_span, format!(" {}= 1; {} }}", kind.op.chr(), base_src)),
13081299
],
13091300
Applicability::MachineApplicable,
13101301
);
@@ -1313,17 +1304,16 @@ impl<'a> Parser<'a> {
13131304

13141305
fn postfix_inc_dec_suggest(
13151306
&mut self,
1316-
_base: P<Expr>,
1307+
base_src: String,
13171308
mut err: DiagnosticBuilder<'a>,
13181309
kind: IncDecRecovery,
1319-
ident: Ident,
13201310
(pre_span, post_span): (Span, Span),
13211311
) -> PResult<'a, P<Expr>> {
13221312
err.multipart_suggestion(
13231313
&format!("use `{}= 1` instead", kind.op.chr()),
13241314
vec![
13251315
(pre_span, "{ let tmp = ".to_string()),
1326-
(post_span, format!("; {} {}= 1; tmp }}", ident, kind.op.chr())),
1316+
(post_span, format!("; {} {}= 1; tmp }}", base_src, kind.op.chr())),
13271317
],
13281318
Applicability::MachineApplicable,
13291319
);
@@ -1332,7 +1322,6 @@ impl<'a> Parser<'a> {
13321322

13331323
fn inc_dec_standalone_recovery(
13341324
&mut self,
1335-
_base: P<Expr>,
13361325
mut err: DiagnosticBuilder<'a>,
13371326
kind: IncDecRecovery,
13381327
(pre_span, post_span): (Span, Span),

src/test/ui/parser/increment-notfixed.stderr

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@ error: Rust has no postfix increment operator
44
LL | foo.bar.qux++;
55
| ^^ not a valid postfix operator
66
|
7-
= help: use `+= 1` instead
7+
help: use `+= 1` instead
8+
|
9+
LL | { let tmp = foo.bar.qux; foo.bar.qux += 1; tmp };
10+
| +++++++++++ ~~~~~~~~~~~~~~~~~~~~~~~~~
811

912
error: Rust has no prefix increment operator
1013
--> $DIR/increment-notfixed.rs:18:5
1114
|
1215
LL | ++foo.bar.qux;
1316
| ^^ not a valid prefix operator
1417
|
15-
= help: use `+= 1` instead
18+
help: use `+= 1` instead
19+
|
20+
LL - ++foo.bar.qux;
21+
LL + foo.bar.qux += 1;
22+
|
1623

1724
error: aborting due to 2 previous errors
1825

0 commit comments

Comments
 (0)