Surpress type mismatches in calls with mismatched arg counts

This commit is contained in:
Lukas Wirth 2024-08-05 16:15:28 +02:00
parent 25d9e05c03
commit deddbbfa60
2 changed files with 18 additions and 3 deletions

View file

@ -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
@ -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() },

View file

@ -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
}
"#,
);
}
}