Skip to content

Commit ef46e38

Browse files
terrarier2111David Wood
and
David Wood
committed
Implement tuple array diagnostic
Co-authored-by: David Wood <[email protected]>
1 parent 86f7f78 commit ef46e38

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

compiler/rustc_typeck/src/check/callee.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
307307
}
308308
}
309309

310+
/// Give appropriate suggestion when encountering `[("a", 0) ("b", 1)]`, where the
311+
/// likely intention is to create an array containing tuples.
312+
fn maybe_suggest_bad_array_definition(
313+
&self,
314+
err: &mut DiagnosticBuilder<'a>,
315+
call_expr: &'tcx hir::Expr<'tcx>,
316+
callee_expr: &'tcx hir::Expr<'tcx>,
317+
) -> bool {
318+
let hir_id = self.tcx.hir().get_parent_node(call_expr.hir_id);
319+
let parent_node = self.tcx.hir().get(hir_id);
320+
if let (
321+
hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Array(_), .. }),
322+
hir::ExprKind::Tup(exp),
323+
hir::ExprKind::Call(_, args),
324+
) = (parent_node, &callee_expr.kind, &call_expr.kind)
325+
{
326+
if args.len() == exp.len() {
327+
let start = callee_expr.span.shrink_to_hi();
328+
err.span_suggestion(
329+
start,
330+
"consider separating array elements with a comma",
331+
",".to_string(),
332+
Applicability::MaybeIncorrect,
333+
);
334+
return true;
335+
}
336+
}
337+
false
338+
}
339+
310340
fn confirm_builtin_call(
311341
&self,
312342
call_expr: &'tcx hir::Expr<'tcx>,
@@ -422,7 +452,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
422452
_ => Res::Err,
423453
};
424454

425-
err.span_label(call_expr.span, "call expression requires function");
455+
if !self.maybe_suggest_bad_array_definition(&mut err, call_expr, callee_expr) {
456+
err.span_label(call_expr.span, "call expression requires function");
457+
}
426458

427459
if let Some(span) = self.tcx.hir().res_span(def) {
428460
let callee_ty = callee_ty.to_string();

src/test/ui/issues/issue-5100.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ LL | &(true, false) => ()
5959
error[E0618]: expected function, found `(char, char)`
6060
--> $DIR/issue-5100.rs:48:14
6161
|
62-
LL | let v = [('a', 'b')
63-
| ______________-^^^^^^^^^
64-
LL | | ('c', 'd'),
65-
| |_______________________- call expression requires function
62+
LL | let v = [('a', 'b')
63+
| ^^^^^^^^^^- help: consider separating array elements with a comma: `,`
6664

6765
error[E0308]: mismatched types
6866
--> $DIR/issue-5100.rs:55:19
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let _tmp = [
3+
("C200B40A82", 3),
4+
("C200B40A83", 4) //~ ERROR: expected function, found `(&'static str, {integer})` [E0618]
5+
("C200B40A8537", 5),
6+
];
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0618]: expected function, found `(&'static str, {integer})`
2+
--> $DIR/array-diagnostics.rs:4:9
3+
|
4+
LL | ("C200B40A83", 4)
5+
| ^^^^^^^^^^^^^^^^^- help: consider separating array elements with a comma: `,`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0618`.

0 commit comments

Comments
 (0)