about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRob Pilling <robpilling@gmail.com>2022-02-02 22:18:36 +0000
committerRob Pilling <robpilling@gmail.com>2022-02-06 20:58:24 +0000
commit344ea6e0e5d5fca3fbfb065a9fd03eff55f76471 (patch)
tree81551c31afbe2dbb041e922343a9af9f66c296c2
parent72d3b45af055276b07e73611278268282848a139 (diff)
downloadrust-344ea6e0e5d5fca3fbfb065a9fd03eff55f76471.tar.gz
rust-344ea6e0e5d5fca3fbfb065a9fd03eff55f76471.zip
Factor out emit_tuple_wrap_err, improve Applicability
-rw-r--r--compiler/rustc_infer/src/infer/error_reporting/mod.rs68
1 files changed, 39 insertions, 29 deletions
diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs
index c16181aab04..3b7e861e44e 100644
--- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs
+++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs
@@ -2045,35 +2045,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
                         // parentheses around it, perhaps the user meant to write `(expr,)` to
                         // build a tuple (issue #86100)
                         (ty::Tuple(_), _) => {
-                            if let [expected_tup_elem] =
-                                expected.tuple_fields().collect::<Vec<_>>()[..]
-                            {
-                                if same_type_modulo_infer(expected_tup_elem, found) {
-                                    if let Ok(code) =
-                                        self.tcx.sess().source_map().span_to_snippet(span)
-                                    {
-                                        if code.starts_with('(') && code.ends_with(')') {
-                                            let before_close = span.hi() - BytePos::from_u32(1);
-
-                                            err.span_suggestion(
-                                                span.with_hi(before_close).shrink_to_hi(),
-                                                "use a trailing comma to create a tuple with one element",
-                                                ",".into(),
-                                                Applicability::MaybeIncorrect,
-                                            );
-                                        } else {
-                                            err.multipart_suggestion(
-                                                "use a trailing comma to create a tuple with one element",
-                                                vec![
-                                                    (span.shrink_to_lo(), "(".into()),
-                                                    (span.shrink_to_hi(), ",)".into()),
-                                                ],
-                                                Applicability::MaybeIncorrect,
-                                            );
-                                        }
-                                    }
-                                }
-                            }
+                            self.emit_tuple_wrap_err(&mut err, span, found, expected)
                         }
                         // If a character was expected and the found expression is a string literal
                         // containing a single character, perhaps the user meant to write `'c'` to
@@ -2136,6 +2108,44 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
         diag
     }
 
+    fn emit_tuple_wrap_err(
+        &self,
+        err: &mut DiagnosticBuilder<'tcx>,
+        span: Span,
+        found: Ty<'tcx>,
+        expected: Ty<'tcx>,
+    ) {
+        let [expected_tup_elem] = &expected.tuple_fields().collect::<Vec<_>>()[..]
+            else { return };
+
+        if !same_type_modulo_infer(expected_tup_elem, found) {
+            return;
+        }
+
+        let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span)
+            else { return };
+
+        if code.starts_with('(') && code.ends_with(')') {
+            let before_close = span.hi() - BytePos::from_u32(1);
+
+            err.span_suggestion(
+                span.with_hi(before_close).shrink_to_hi(),
+                "use a trailing comma to create a tuple with one element",
+                ",".into(),
+                Applicability::MachineApplicable,
+            );
+        } else {
+            err.multipart_suggestion(
+                "use a trailing comma to create a tuple with one element",
+                vec![
+                    (span.shrink_to_lo(), "(".into()),
+                    (span.shrink_to_hi(), ",)".into()),
+                ],
+                Applicability::MachineApplicable,
+            );
+        }
+    }
+
     fn values_str(
         &self,
         values: ValuePairs<'tcx>,