From 7287f929b9c4a684be6ea8980970de7693eef841 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Thu, 17 Feb 2022 14:19:59 -0800 Subject: [PATCH] Emit structured suggestions for field accesses too --- .../rustc_parse/src/parser/diagnostics.rs | 33 +++++++------------ src/test/ui/parser/increment-notfixed.stderr | 11 +++++-- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 38e67790d74..a8a1842f377 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1266,45 +1266,36 @@ fn recover_from_inc_dec( Ok(base) }; - let ExprKind::Path(_, path) = &base.kind - else { return help_base_case(err, base) }; - let [segment] = path.segments.as_slice() - else { return help_base_case(err, base) }; - let ident = segment.ident; - // (pre, post) let spans = match kind.fixity { - UnaryFixity::Pre => (op_span, ident.span.shrink_to_hi()), - UnaryFixity::Post => (ident.span.shrink_to_lo(), op_span), + UnaryFixity::Pre => (op_span, base.span.shrink_to_hi()), + UnaryFixity::Post => (base.span.shrink_to_lo(), op_span), }; - if ident.is_reserved() { - return help_base_case(err, base); - } - if kind.standalone { - self.inc_dec_standalone_recovery(base, err, kind, spans) + self.inc_dec_standalone_recovery(err, kind, spans) } else { + let Ok(base_src) = self.span_to_snippet(base.span) + else { return help_base_case(err, base) }; match kind.fixity { - UnaryFixity::Pre => self.prefix_inc_dec_suggest(base, err, kind, ident, spans), - UnaryFixity::Post => self.postfix_inc_dec_suggest(base, err, kind, ident, spans), + UnaryFixity::Pre => self.prefix_inc_dec_suggest(base_src, err, kind, spans), + UnaryFixity::Post => self.postfix_inc_dec_suggest(base_src, err, kind, spans), } } } fn prefix_inc_dec_suggest( &mut self, - _base: P, + base_src: String, mut err: DiagnosticBuilder<'a>, kind: IncDecRecovery, - ident: Ident, (pre_span, post_span): (Span, Span), ) -> PResult<'a, P> { err.multipart_suggestion( &format!("use `{}= 1` instead", kind.op.chr()), vec![ (pre_span, "{ ".to_string()), - (post_span, format!(" {}= 1; {} }}", kind.op.chr(), ident)), + (post_span, format!(" {}= 1; {} }}", kind.op.chr(), base_src)), ], Applicability::MachineApplicable, ); @@ -1313,17 +1304,16 @@ fn prefix_inc_dec_suggest( fn postfix_inc_dec_suggest( &mut self, - _base: P, + base_src: String, mut err: DiagnosticBuilder<'a>, kind: IncDecRecovery, - ident: Ident, (pre_span, post_span): (Span, Span), ) -> PResult<'a, P> { err.multipart_suggestion( &format!("use `{}= 1` instead", kind.op.chr()), vec![ (pre_span, "{ let tmp = ".to_string()), - (post_span, format!("; {} {}= 1; tmp }}", ident, kind.op.chr())), + (post_span, format!("; {} {}= 1; tmp }}", base_src, kind.op.chr())), ], Applicability::MachineApplicable, ); @@ -1332,7 +1322,6 @@ fn postfix_inc_dec_suggest( fn inc_dec_standalone_recovery( &mut self, - _base: P, mut err: DiagnosticBuilder<'a>, kind: IncDecRecovery, (pre_span, post_span): (Span, Span), diff --git a/src/test/ui/parser/increment-notfixed.stderr b/src/test/ui/parser/increment-notfixed.stderr index cf60075d00c..3110e242712 100644 --- a/src/test/ui/parser/increment-notfixed.stderr +++ b/src/test/ui/parser/increment-notfixed.stderr @@ -4,7 +4,10 @@ error: Rust has no postfix increment operator LL | foo.bar.qux++; | ^^ not a valid postfix operator | - = help: use `+= 1` instead +help: use `+= 1` instead + | +LL | { let tmp = foo.bar.qux; foo.bar.qux += 1; tmp }; + | +++++++++++ ~~~~~~~~~~~~~~~~~~~~~~~~~ error: Rust has no prefix increment operator --> $DIR/increment-notfixed.rs:18:5 @@ -12,7 +15,11 @@ error: Rust has no prefix increment operator LL | ++foo.bar.qux; | ^^ not a valid prefix operator | - = help: use `+= 1` instead +help: use `+= 1` instead + | +LL - ++foo.bar.qux; +LL + foo.bar.qux += 1; + | error: aborting due to 2 previous errors