Skip to content

Commit e159c1e

Browse files
committed
Skip method calls with arity mismatch
1 parent 08cc628 commit e159c1e

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
298298
// call's arguments and we can provide a more explicit span.
299299
let sig = self.tcx.fn_sig(def_id).subst_identity();
300300
let def_self_ty = sig.input(0).skip_binder();
301+
let param_tys = sig.inputs().skip_binder().iter().skip(1);
302+
// If there's an arity mismatch, pointing out the call as the source of an inference
303+
// can be misleading, so we skip it.
304+
if param_tys.len() != args.len() {
305+
continue;
306+
}
301307
let rcvr_ty = self.node_ty(rcvr.hir_id);
302308
// Get the evaluated type *after* calling the method call, so that the influence
303309
// of the arguments can be reflected in the receiver type. The receiver
@@ -323,7 +329,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
323329
let mut param_found = FxHashMap::default();
324330
if self.can_eq(self.param_env, ty, found).is_ok() {
325331
// We only point at the first place where the found type was inferred.
326-
for (param_ty, arg) in sig.inputs().skip_binder().iter().skip(1).zip(args) {
332+
for (param_ty, arg) in param_tys.zip(args) {
327333
if def_self_ty.contains(*param_ty) && let ty::Param(_) = param_ty.kind() {
328334
// We found an argument that references a type parameter in `Self`,
329335
// so we assume that this is the argument that caused the found

tests/ui/type/type-check/point-at-inference-4.rs

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ fn main() {
1313
//~^ ERROR this method takes 2 arguments but 1 argument was supplied
1414
//~| NOTE an argument is missing
1515
//~| HELP provide the argument
16-
//~| NOTE this is of type `i32`, which causes `s` to be inferred as `S<i32, _>`
17-
//~| HELP change the type of the numeric literal from `i32` to `u32`
1816
let t: S<u32, _> = s;
1917
//~^ ERROR mismatched types
2018
//~| NOTE expected `S<u32, _>`, found `S<i32, _>`

tests/ui/type/type-check/point-at-inference-4.stderr

+1-8
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,15 @@ LL | s.infer(0i32, /* b */);
1515
| ~~~~~~~~~~~~~~~
1616

1717
error[E0308]: mismatched types
18-
--> $DIR/point-at-inference-4.rs:18:24
18+
--> $DIR/point-at-inference-4.rs:16:24
1919
|
20-
LL | s.infer(0i32);
21-
| ---- this is of type `i32`, which causes `s` to be inferred as `S<i32, _>`
22-
...
2320
LL | let t: S<u32, _> = s;
2421
| --------- ^ expected `S<u32, _>`, found `S<i32, _>`
2522
| |
2623
| expected due to this
2724
|
2825
= note: expected struct `S<u32, _>`
2926
found struct `S<i32, _>`
30-
help: change the type of the numeric literal from `i32` to `u32`
31-
|
32-
LL | s.infer(0u32);
33-
| ~~~
3427

3528
error: aborting due to 2 previous errors
3629

0 commit comments

Comments
 (0)