Skip to content

Commit

Permalink
Auto merge of rust-lang#17802 - Veykril:arg-mismatch-no-ty-mismatch, …
Browse files Browse the repository at this point in the history
…r=Veykril

fix: Surpress type mismatches in calls with mismatched arg counts

These tend to get very noisy, hiding the actual problem.
  • Loading branch information
bors committed Aug 5, 2024
2 parents 64cd3da + 3e23c45 commit 37f7569
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/tools/rust-analyzer/crates/hir-ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1759,13 +1759,14 @@ impl InferenceContext<'_> {
skip_indices: &[u32],
is_varargs: bool,
) {
if args.len() != param_tys.len() + skip_indices.len() && !is_varargs {
let arg_count_mismatch = args.len() != param_tys.len() + skip_indices.len() && !is_varargs;
if arg_count_mismatch {
self.push_diagnostic(InferenceDiagnostic::MismatchedArgCount {
call_expr: expr,
expected: param_tys.len() + skip_indices.len(),
found: args.len(),
});
}
};

// Quoting https://github.com/rust-lang/rust/blob/6ef275e6c3cb1384ec78128eceeb4963ff788dca/src/librustc_typeck/check/mod.rs#L3325 --
// We do this in a pretty awful way: first we type-check any arguments
Expand Down Expand Up @@ -1819,7 +1820,7 @@ impl InferenceContext<'_> {
// The function signature may contain some unknown types, so we need to insert
// type vars here to avoid type mismatch false positive.
let coercion_target = self.insert_type_vars(coercion_target);
if self.coerce(Some(arg), &ty, &coercion_target).is_err() {
if self.coerce(Some(arg), &ty, &coercion_target).is_err() && !arg_count_mismatch {
self.result.type_mismatches.insert(
arg.into(),
TypeMismatch { expected: coercion_target, actual: ty.clone() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,18 @@ fn f(
"#,
)
}

#[test]
fn no_type_mismatches_when_arg_count_mismatch() {
check_diagnostics(
r#"
fn foo((): (), (): ()) {
foo(1, 2, 3);
// ^^ error: expected 2 arguments, found 3
foo(1);
// ^ error: expected 2 arguments, found 1
}
"#,
);
}
}

0 comments on commit 37f7569

Please sign in to comment.