]> git.lizzy.rs Git - rust.git/commitdiff
Emit structured suggestions for field accesses too
authorNoah Lev <camelidcamel@gmail.com>
Thu, 17 Feb 2022 22:19:59 +0000 (14:19 -0800)
committerNoah Lev <camelidcamel@gmail.com>
Thu, 24 Mar 2022 05:31:57 +0000 (22:31 -0700)
compiler/rustc_parse/src/parser/diagnostics.rs
src/test/ui/parser/increment-notfixed.stderr

index 38e67790d748e94caea191cb5a12e9e41f4e4e2d..a8a1842f3771dc802b07eede0a120644551c2df0 100644 (file)
@@ -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<Expr>,
+        base_src: String,
         mut err: DiagnosticBuilder<'a>,
         kind: IncDecRecovery,
-        ident: Ident,
         (pre_span, post_span): (Span, Span),
     ) -> PResult<'a, P<Expr>> {
         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<Expr>,
+        base_src: String,
         mut err: DiagnosticBuilder<'a>,
         kind: IncDecRecovery,
-        ident: Ident,
         (pre_span, post_span): (Span, Span),
     ) -> PResult<'a, P<Expr>> {
         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<Expr>,
         mut err: DiagnosticBuilder<'a>,
         kind: IncDecRecovery,
         (pre_span, post_span): (Span, Span),
index cf60075d00cde702b6101d5360b710c674219450..3110e2427123507486f9ab24215d2a7c48be2bcc 100644 (file)
@@ -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