Skip to content

Commit 2be7e26

Browse files
committed
Move mismatched-arg-count diagnostic to inference
1 parent 837901a commit 2be7e26

File tree

4 files changed

+67
-83
lines changed

4 files changed

+67
-83
lines changed

crates/hir/src/lib.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,19 @@ impl DefWithBody {
11831183
.expect("break outside of loop in synthetic syntax");
11841184
acc.push(BreakOutsideOfLoop { expr }.into())
11851185
}
1186+
hir_ty::InferenceDiagnostic::MismatchedArgCount { call_expr, expected, found } => {
1187+
match source_map.expr_syntax(*call_expr) {
1188+
Ok(source_ptr) => acc.push(
1189+
MismatchedArgCount {
1190+
call_expr: source_ptr,
1191+
expected: *expected,
1192+
found: *found,
1193+
}
1194+
.into(),
1195+
),
1196+
Err(SyntheticSyntax) => (),
1197+
}
1198+
}
11861199
}
11871200
}
11881201
for (expr, mismatch) in infer.expr_type_mismatches() {
@@ -1297,14 +1310,6 @@ impl DefWithBody {
12971310
);
12981311
}
12991312
}
1300-
BodyValidationDiagnostic::MismatchedArgCount { call_expr, expected, found } => {
1301-
match source_map.expr_syntax(call_expr) {
1302-
Ok(source_ptr) => acc.push(
1303-
MismatchedArgCount { call_expr: source_ptr, expected, found }.into(),
1304-
),
1305-
Err(SyntheticSyntax) => (),
1306-
}
1307-
}
13081313
BodyValidationDiagnostic::MissingMatchArms { match_expr } => {
13091314
match source_map.expr_syntax(match_expr) {
13101315
Ok(source_ptr) => {

crates/hir_ty/src/diagnostics/expr.rs

+4-62
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
deconstruct_pat::DeconstructedPat,
1818
usefulness::{compute_match_usefulness, MatchCheckCtx},
1919
},
20-
InferenceResult, Interner, TyExt,
20+
InferenceResult, TyExt,
2121
};
2222

2323
pub(crate) use hir_def::{
@@ -35,11 +35,6 @@ pub enum BodyValidationDiagnostic {
3535
ReplaceFilterMapNextWithFindMap {
3636
method_call_expr: ExprId,
3737
},
38-
MismatchedArgCount {
39-
call_expr: ExprId,
40-
expected: usize,
41-
found: usize,
42-
},
4338
MissingMatchArms {
4439
match_expr: ExprId,
4540
},
@@ -119,18 +114,9 @@ impl ExprValidator {
119114
return;
120115
}
121116

122-
let is_method_call = matches!(expr, Expr::MethodCall { .. });
123-
let (sig, mut arg_count) = match expr {
124-
Expr::Call { callee, args } => {
125-
let callee = &self.infer.type_of_expr[*callee];
126-
let sig = match callee.callable_sig(db) {
127-
Some(sig) => sig,
128-
None => return,
129-
};
130-
(sig, args.len())
131-
}
132-
Expr::MethodCall { receiver, args, .. } => {
133-
let (callee, subst) = match self.infer.method_resolution(call_id) {
117+
match expr {
118+
Expr::MethodCall { receiver, .. } => {
119+
let (callee, _) = match self.infer.method_resolution(call_id) {
134120
Some(it) => it,
135121
None => return,
136122
};
@@ -148,53 +134,9 @@ impl ExprValidator {
148134
},
149135
);
150136
}
151-
let receiver = &self.infer.type_of_expr[*receiver];
152-
if receiver.strip_references().is_unknown() {
153-
// if the receiver is of unknown type, it's very likely we
154-
// don't know enough to correctly resolve the method call.
155-
// This is kind of a band-aid for #6975.
156-
return;
157-
}
158-
159-
let sig = db.callable_item_signature(callee.into()).substitute(Interner, &subst);
160-
161-
(sig, args.len() + 1)
162137
}
163138
_ => return,
164139
};
165-
166-
if sig.is_varargs {
167-
return;
168-
}
169-
170-
if sig.legacy_const_generics_indices.is_empty() {
171-
let mut param_count = sig.params().len();
172-
173-
if arg_count != param_count {
174-
if is_method_call {
175-
param_count -= 1;
176-
arg_count -= 1;
177-
}
178-
self.diagnostics.push(BodyValidationDiagnostic::MismatchedArgCount {
179-
call_expr: call_id,
180-
expected: param_count,
181-
found: arg_count,
182-
});
183-
}
184-
} else {
185-
// With `#[rustc_legacy_const_generics]` there are basically two parameter counts that
186-
// are allowed.
187-
let count_non_legacy = sig.params().len();
188-
let count_legacy = sig.params().len() + sig.legacy_const_generics_indices.len();
189-
if arg_count != count_non_legacy && arg_count != count_legacy {
190-
self.diagnostics.push(BodyValidationDiagnostic::MismatchedArgCount {
191-
call_expr: call_id,
192-
// Since most users will use the legacy way to call them, report against that.
193-
expected: count_legacy,
194-
found: arg_count,
195-
});
196-
}
197-
}
198140
}
199141

200142
fn validate_match(

crates/hir_ty/src/infer.rs

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ pub(crate) type InferResult<T> = Result<InferOk<T>, TypeError>;
143143
pub enum InferenceDiagnostic {
144144
NoSuchField { expr: ExprId },
145145
BreakOutsideOfLoop { expr: ExprId },
146+
MismatchedArgCount { call_expr: ExprId, expected: usize, found: usize },
146147
}
147148

148149
/// A mismatch between an expected and an inferred type.

crates/hir_ty/src/infer/expr.rs

+49-13
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,18 @@ impl<'a> InferenceContext<'a> {
296296
break;
297297
}
298298
}
299+
// if the function is unresolved, we use is_varargs=true to
300+
// suppress the arg count diagnostic here
301+
let is_varargs =
302+
derefed_callee.callable_sig(self.db).map_or(false, |sig| sig.is_varargs)
303+
|| res.is_none();
299304
let (param_tys, ret_ty) = match res {
300305
Some(res) => {
301306
let adjustments = auto_deref_adjust_steps(&derefs);
302307
self.write_expr_adj(*callee, adjustments);
303308
res
304309
}
305-
None => (Vec::new(), self.err_ty()),
310+
None => (Vec::new(), self.err_ty()), // FIXME diagnostic
306311
};
307312
let indices_to_skip = self.check_legacy_const_generics(derefed_callee, args);
308313
self.register_obligations_for_call(&callee_ty);
@@ -313,7 +318,14 @@ impl<'a> InferenceContext<'a> {
313318
param_tys.clone(),
314319
);
315320

316-
self.check_call_arguments(args, &expected_inputs, &param_tys, &indices_to_skip);
321+
self.check_call_arguments(
322+
tgt_expr,
323+
args,
324+
&expected_inputs,
325+
&param_tys,
326+
&indices_to_skip,
327+
is_varargs,
328+
);
317329
self.normalize_associated_types_in(ret_ty)
318330
}
319331
Expr::MethodCall { receiver, args, method_name, generic_args } => self
@@ -948,22 +960,28 @@ impl<'a> InferenceContext<'a> {
948960
};
949961
let method_ty = method_ty.substitute(Interner, &substs);
950962
self.register_obligations_for_call(&method_ty);
951-
let (formal_receiver_ty, param_tys, ret_ty) = match method_ty.callable_sig(self.db) {
952-
Some(sig) => {
953-
if !sig.params().is_empty() {
954-
(sig.params()[0].clone(), sig.params()[1..].to_vec(), sig.ret().clone())
955-
} else {
956-
(self.err_ty(), Vec::new(), sig.ret().clone())
963+
let (formal_receiver_ty, param_tys, ret_ty, is_varargs) =
964+
match method_ty.callable_sig(self.db) {
965+
Some(sig) => {
966+
if !sig.params().is_empty() {
967+
(
968+
sig.params()[0].clone(),
969+
sig.params()[1..].to_vec(),
970+
sig.ret().clone(),
971+
sig.is_varargs,
972+
)
973+
} else {
974+
(self.err_ty(), Vec::new(), sig.ret().clone(), sig.is_varargs)
975+
}
957976
}
958-
}
959-
None => (self.err_ty(), Vec::new(), self.err_ty()),
960-
};
977+
None => (self.err_ty(), Vec::new(), self.err_ty(), true),
978+
};
961979
self.unify(&formal_receiver_ty, &receiver_ty);
962980

963981
let expected_inputs =
964982
self.expected_inputs_for_expected_output(expected, ret_ty.clone(), param_tys.clone());
965983

966-
self.check_call_arguments(args, &expected_inputs, &param_tys, &[]);
984+
self.check_call_arguments(tgt_expr, args, &expected_inputs, &param_tys, &[], is_varargs);
967985
self.normalize_associated_types_in(ret_ty)
968986
}
969987

@@ -996,11 +1014,21 @@ impl<'a> InferenceContext<'a> {
9961014

9971015
fn check_call_arguments(
9981016
&mut self,
1017+
expr: ExprId,
9991018
args: &[ExprId],
10001019
expected_inputs: &[Ty],
10011020
param_tys: &[Ty],
10021021
skip_indices: &[u32],
1022+
is_varargs: bool,
10031023
) {
1024+
if args.len() != param_tys.len() + skip_indices.len() && !is_varargs {
1025+
self.push_diagnostic(InferenceDiagnostic::MismatchedArgCount {
1026+
call_expr: expr,
1027+
expected: param_tys.len() + skip_indices.len(),
1028+
found: args.len(),
1029+
});
1030+
}
1031+
10041032
// Quoting https://github.com/rust-lang/rust/blob/6ef275e6c3cb1384ec78128eceeb4963ff788dca/src/librustc_typeck/check/mod.rs#L3325 --
10051033
// We do this in a pretty awful way: first we type-check any arguments
10061034
// that are not closures, then we type-check the closures. This is so
@@ -1188,7 +1216,15 @@ impl<'a> InferenceContext<'a> {
11881216

11891217
// only use legacy const generics if the param count matches with them
11901218
if data.params.len() + data.legacy_const_generics_indices.len() != args.len() {
1191-
return Vec::new();
1219+
if args.len() <= data.params.len() {
1220+
return Vec::new();
1221+
} else {
1222+
// there are more parameters than there should be without legacy
1223+
// const params; use them
1224+
let mut indices = data.legacy_const_generics_indices.clone();
1225+
indices.sort();
1226+
return indices;
1227+
}
11921228
}
11931229

11941230
// check legacy const parameters

0 commit comments

Comments
 (0)