about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-01-21 22:03:16 +0100
committerGitHub <noreply@github.com>2022-01-21 22:03:16 +0100
commit1f3a2dd0b1990bbb2e225f3b341a225e171f6c91 (patch)
tree04e7cd3acc9aa96527bffa13b0bcc900ee2f04c3 /compiler
parent430673f26590f5f40bb6a6587d43136d0b5604dc (diff)
parentef46e38c2bc9c86dcc48b511885630ed9f842a55 (diff)
downloadrust-1f3a2dd0b1990bbb2e225f3b341a225e171f6c91.tar.gz
rust-1f3a2dd0b1990bbb2e225f3b341a225e171f6c91.zip
Rollup merge of #92963 - terrarier2111:tuple-diagnostic, r=davidtwco
Implement tuple array diagnostic

Fixes https://github.com/rust-lang/rust/issues/92089
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_typeck/src/check/callee.rs34
1 files changed, 33 insertions, 1 deletions
diff --git a/compiler/rustc_typeck/src/check/callee.rs b/compiler/rustc_typeck/src/check/callee.rs
index eea8f40635d..0fea0afb572 100644
--- a/compiler/rustc_typeck/src/check/callee.rs
+++ b/compiler/rustc_typeck/src/check/callee.rs
@@ -307,6 +307,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
         }
     }
 
+    /// Give appropriate suggestion when encountering `[("a", 0) ("b", 1)]`, where the
+    /// likely intention is to create an array containing tuples.
+    fn maybe_suggest_bad_array_definition(
+        &self,
+        err: &mut DiagnosticBuilder<'a>,
+        call_expr: &'tcx hir::Expr<'tcx>,
+        callee_expr: &'tcx hir::Expr<'tcx>,
+    ) -> bool {
+        let hir_id = self.tcx.hir().get_parent_node(call_expr.hir_id);
+        let parent_node = self.tcx.hir().get(hir_id);
+        if let (
+            hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Array(_), .. }),
+            hir::ExprKind::Tup(exp),
+            hir::ExprKind::Call(_, args),
+        ) = (parent_node, &callee_expr.kind, &call_expr.kind)
+        {
+            if args.len() == exp.len() {
+                let start = callee_expr.span.shrink_to_hi();
+                err.span_suggestion(
+                    start,
+                    "consider separating array elements with a comma",
+                    ",".to_string(),
+                    Applicability::MaybeIncorrect,
+                );
+                return true;
+            }
+        }
+        false
+    }
+
     fn confirm_builtin_call(
         &self,
         call_expr: &'tcx hir::Expr<'tcx>,
@@ -422,7 +452,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                     _ => Res::Err,
                 };
 
-                err.span_label(call_expr.span, "call expression requires function");
+                if !self.maybe_suggest_bad_array_definition(&mut err, call_expr, callee_expr) {
+                    err.span_label(call_expr.span, "call expression requires function");
+                }
 
                 if let Some(span) = self.tcx.hir().res_span(def) {
                     let callee_ty = callee_ty.to_string();