Skip to content

Commit

Permalink
Auto merge of #46633 - estebank:arg-mismatch, r=arielb1
Browse files Browse the repository at this point in the history
Point at whole method call instead of args

To avoid confusion in cases where the code is

```rust
fn foo() {}
/ foo(
|     bar()
|     ^^^ current diagnostics point here for arg count mismatch
| );
|_^ new diagnostic span points here
```

as this leads to confusion making people think that the diagnostic is
talking about `bar`'s arg count, not `foo`'s.

Point at `fn`s definition on arg mismatch, just like we do for closures.

Re #42855, Fix #45633.
  • Loading branch information
bors committed Dec 14, 2017
2 parents 8954b16 + 92da913 commit 75a02a9
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 84 deletions.
4 changes: 2 additions & 2 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
let found_did = found_trait_ty.ty_to_def_id();
let found_span = found_did.and_then(|did| {
self.tcx.hir.span_if_local(did)
});
}).map(|sp| self.tcx.sess.codemap().def_span(sp)); // the sp could be an fn def

let found_ty_count =
match found_trait_ref.skip_binder().substs.type_at(1).sty {
Expand Down Expand Up @@ -751,7 +751,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
//
// ```
// [1i32, 2, 3].sort_by(|(a, b)| ..)
// // ^^^^^^^^
// // ^^^^^^^ --------
// // expected_trait_ref: std::ops::FnMut<(&i32, &i32)>
// // found_trait_ref: std::ops::FnMut<(&i32,)>
// ```
Expand Down
1 change: 1 addition & 0 deletions src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
TyAdt(def, _) => Some(def.did),
TyForeign(did) => Some(did),
TyClosure(id, _) => Some(id),
TyFnDef(id, _) => Some(id),
_ => None,
}
}
Expand Down
23 changes: 4 additions & 19 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2432,21 +2432,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let mut expected_arg_tys = expected_arg_tys;
let expected_arg_count = fn_inputs.len();

let sp_args = if args.len() > 0 {
let (first, args) = args.split_at(1);
let mut sp_tmp = first[0].span;
for arg in args {
let sp_opt = self.sess().codemap().merge_spans(sp_tmp, arg.span);
if ! sp_opt.is_some() {
break;
}
sp_tmp = sp_opt.unwrap();
};
sp_tmp
} else {
sp
};

fn parameter_count_error<'tcx>(sess: &Session,
sp: Span,
expr_sp: Span,
Expand All @@ -2465,7 +2450,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if arg_count == 1 {" was"} else {"s were"}),
DiagnosticId::Error(error_code.to_owned()));

if let Some(def_s) = def_span {
if let Some(def_s) = def_span.map(|sp| sess.codemap().def_span(sp)) {
err.span_label(def_s, "defined here");
}
if sugg_unit {
Expand All @@ -2489,7 +2474,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let tuple_type = self.structurally_resolved_type(sp, fn_inputs[0]);
match tuple_type.sty {
ty::TyTuple(arg_types, _) if arg_types.len() != args.len() => {
parameter_count_error(tcx.sess, sp_args, expr_sp, arg_types.len(), args.len(),
parameter_count_error(tcx.sess, sp, expr_sp, arg_types.len(), args.len(),
"E0057", false, def_span, false);
expected_arg_tys = &[];
self.err_args(args.len())
Expand Down Expand Up @@ -2518,7 +2503,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if supplied_arg_count >= expected_arg_count {
fn_inputs.to_vec()
} else {
parameter_count_error(tcx.sess, sp_args, expr_sp, expected_arg_count,
parameter_count_error(tcx.sess, sp, expr_sp, expected_arg_count,
supplied_arg_count, "E0060", true, def_span, false);
expected_arg_tys = &[];
self.err_args(supplied_arg_count)
Expand All @@ -2532,7 +2517,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
} else {
false
};
parameter_count_error(tcx.sess, sp_args, expr_sp, expected_arg_count,
parameter_count_error(tcx.sess, sp, expr_sp, expected_arg_count,
supplied_arg_count, "E0061", false, def_span, sugg_unit);
expected_arg_tys = &[];
self.err_args(supplied_arg_count)
Expand Down
22 changes: 11 additions & 11 deletions src/test/ui/anonymous-higher-ranked-lifetime.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:12:5
|
12 | f1(|_: (), _: ()| {}); //~ ERROR type mismatch
| ^^ ----------------- found signature of `fn((), ()) -> _`
| ^^ -------------- found signature of `fn((), ()) -> _`
| |
| expected signature of `for<'r, 's> fn(&'r (), &'s ()) -> _`
|
Expand All @@ -12,7 +12,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:13:5
|
13 | f2(|_: (), _: ()| {}); //~ ERROR type mismatch
| ^^ ----------------- found signature of `fn((), ()) -> _`
| ^^ -------------- found signature of `fn((), ()) -> _`
| |
| expected signature of `for<'a, 'r> fn(&'a (), &'r ()) -> _`
|
Expand All @@ -22,7 +22,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:14:5
|
14 | f3(|_: (), _: ()| {}); //~ ERROR type mismatch
| ^^ ----------------- found signature of `fn((), ()) -> _`
| ^^ -------------- found signature of `fn((), ()) -> _`
| |
| expected signature of `for<'r> fn(&(), &'r ()) -> _`
|
Expand All @@ -32,7 +32,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:15:5
|
15 | f4(|_: (), _: ()| {}); //~ ERROR type mismatch
| ^^ ----------------- found signature of `fn((), ()) -> _`
| ^^ -------------- found signature of `fn((), ()) -> _`
| |
| expected signature of `for<'s, 'r> fn(&'s (), &'r ()) -> _`
|
Expand All @@ -42,7 +42,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:16:5
|
16 | f5(|_: (), _: ()| {}); //~ ERROR type mismatch
| ^^ ----------------- found signature of `fn((), ()) -> _`
| ^^ -------------- found signature of `fn((), ()) -> _`
| |
| expected signature of `for<'r> fn(&'r (), &'r ()) -> _`
|
Expand All @@ -52,7 +52,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:17:5
|
17 | g1(|_: (), _: ()| {}); //~ ERROR type mismatch
| ^^ ----------------- found signature of `fn((), ()) -> _`
| ^^ -------------- found signature of `fn((), ()) -> _`
| |
| expected signature of `for<'r> fn(&'r (), std::boxed::Box<for<'s> std::ops::Fn(&'s ()) + 'static>) -> _`
|
Expand All @@ -62,7 +62,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:18:5
|
18 | g2(|_: (), _: ()| {}); //~ ERROR type mismatch
| ^^ ----------------- found signature of `fn((), ()) -> _`
| ^^ -------------- found signature of `fn((), ()) -> _`
| |
| expected signature of `for<'r> fn(&'r (), for<'s> fn(&'s ())) -> _`
|
Expand All @@ -72,7 +72,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:19:5
|
19 | g3(|_: (), _: ()| {}); //~ ERROR type mismatch
| ^^ ----------------- found signature of `fn((), ()) -> _`
| ^^ -------------- found signature of `fn((), ()) -> _`
| |
| expected signature of `for<'s> fn(&'s (), std::boxed::Box<for<'r> std::ops::Fn(&'r ()) + 'static>) -> _`
|
Expand All @@ -82,7 +82,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:20:5
|
20 | g4(|_: (), _: ()| {}); //~ ERROR type mismatch
| ^^ ----------------- found signature of `fn((), ()) -> _`
| ^^ -------------- found signature of `fn((), ()) -> _`
| |
| expected signature of `for<'s> fn(&'s (), for<'r> fn(&'r ())) -> _`
|
Expand All @@ -92,7 +92,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:21:5
|
21 | h1(|_: (), _: (), _: (), _: ()| {}); //~ ERROR type mismatch
| ^^ ------------------------------- found signature of `fn((), (), (), ()) -> _`
| ^^ ---------------------------- found signature of `fn((), (), (), ()) -> _`
| |
| expected signature of `for<'r, 's> fn(&'r (), std::boxed::Box<for<'t0> std::ops::Fn(&'t0 ()) + 'static>, &'s (), for<'t0, 't1> fn(&'t0 (), &'t1 ())) -> _`
|
Expand All @@ -102,7 +102,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:22:5
|
22 | h2(|_: (), _: (), _: (), _: ()| {}); //~ ERROR type mismatch
| ^^ ------------------------------- found signature of `fn((), (), (), ()) -> _`
| ^^ ---------------------------- found signature of `fn((), (), (), ()) -> _`
| |
| expected signature of `for<'r, 't0> fn(&'r (), std::boxed::Box<for<'s> std::ops::Fn(&'s ()) + 'static>, &'t0 (), for<'s, 't1> fn(&'s (), &'t1 ())) -> _`
|
Expand Down
14 changes: 7 additions & 7 deletions src/test/ui/method-call-err-msg.stderr
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
error[E0061]: this function takes 0 parameters but 1 parameter was supplied
--> $DIR/method-call-err-msg.rs:25:12
--> $DIR/method-call-err-msg.rs:25:7
|
15 | fn zero(self) -> Foo { self }
| ----------------------------- defined here
| -------------------- defined here
...
25 | x.zero(0) //~ ERROR this function takes 0 parameters but 1 parameter was supplied
| ^ expected 0 parameters
| ^^^^ expected 0 parameters

error[E0061]: this function takes 1 parameter but 0 parameters were supplied
--> $DIR/method-call-err-msg.rs:27:7
|
17 | fn one(self, _: isize) -> Foo { self }
| -------------------------------------- defined here
| ----------------------------- defined here
...
27 | .one() //~ ERROR this function takes 1 parameter but 0 parameters were supplied
| ^^^ expected 1 parameter

error[E0061]: this function takes 2 parameters but 1 parameter was supplied
--> $DIR/method-call-err-msg.rs:29:11
--> $DIR/method-call-err-msg.rs:29:7
|
19 | fn two(self, _: isize, _: isize) -> Foo { self }
| ------------------------------------------------ defined here
| --------------------------------------- defined here
...
29 | .two(0); //~ ERROR this function takes 2 parameters but 1 parameter was supplied
| ^ expected 2 parameters
| ^^^ expected 2 parameters

error[E0599]: no method named `take` found for type `Foo` in the current scope
--> $DIR/method-call-err-msg.rs:34:7
Expand Down
20 changes: 10 additions & 10 deletions src/test/ui/mismatched_types/E0631.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/E0631.rs:17:5
|
17 | foo(|_: isize| {}); //~ ERROR type mismatch
| ^^^ ------------- found signature of `fn(isize) -> _`
| ^^^ ---------- found signature of `fn(isize) -> _`
| |
| expected signature of `fn(usize) -> _`
|
Expand All @@ -12,7 +12,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/E0631.rs:18:5
|
18 | bar(|_: isize| {}); //~ ERROR type mismatch
| ^^^ ------------- found signature of `fn(isize) -> _`
| ^^^ ---------- found signature of `fn(isize) -> _`
| |
| expected signature of `fn(usize) -> _`
|
Expand All @@ -21,22 +21,22 @@ error[E0631]: type mismatch in closure arguments
error[E0631]: type mismatch in function arguments
--> $DIR/E0631.rs:19:5
|
16 | fn f(_: u64) {}
| ------------ found signature of `fn(u64) -> _`
...
19 | foo(f); //~ ERROR type mismatch
| ^^^
| |
| expected signature of `fn(usize) -> _`
| found signature of `fn(u64) -> _`
| ^^^ expected signature of `fn(usize) -> _`
|
= note: required by `foo`

error[E0631]: type mismatch in function arguments
--> $DIR/E0631.rs:20:5
|
16 | fn f(_: u64) {}
| ------------ found signature of `fn(u64) -> _`
...
20 | bar(f); //~ ERROR type mismatch
| ^^^
| |
| expected signature of `fn(usize) -> _`
| found signature of `fn(u64) -> _`
| ^^^ expected signature of `fn(usize) -> _`
|
= note: required by `bar`

Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/mismatched_types/closure-arg-count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ fn main() {
//~^ ERROR closure is expected to take
let _it = vec![1, 2, 3].into_iter().enumerate().map(|i, x, y| i);
//~^ ERROR closure is expected to take
let _it = vec![1, 2, 3].into_iter().enumerate().map(foo);
//~^ ERROR function is expected to take
let bar = |i, x, y| i;
let _it = vec![1, 2, 3].into_iter().enumerate().map(bar);
//~^ ERROR closure is expected to take
}

fn foo() {}
19 changes: 18 additions & 1 deletion src/test/ui/mismatched_types/closure-arg-count.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,22 @@ error[E0593]: closure is expected to take a single 2-tuple as argument, but it t
| |
| expected closure that takes a single 2-tuple as argument

error: aborting due to 7 previous errors
error[E0593]: function is expected to take a single 2-tuple as argument, but it takes 0 arguments
--> $DIR/closure-arg-count.rs:30:53
|
30 | let _it = vec![1, 2, 3].into_iter().enumerate().map(foo);
| ^^^ expected function that takes a single 2-tuple as argument
...
37 | fn foo() {}
| -------- takes 0 arguments

error[E0593]: closure is expected to take a single 2-tuple as argument, but it takes 3 distinct arguments
--> $DIR/closure-arg-count.rs:33:53
|
32 | let bar = |i, x, y| i;
| --------- takes 3 distinct arguments
33 | let _it = vec![1, 2, 3].into_iter().enumerate().map(bar);
| ^^^ expected closure that takes a single 2-tuple as argument

error: aborting due to 9 previous errors

4 changes: 2 additions & 2 deletions src/test/ui/mismatched_types/fn-variance-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
// except according to those terms.

fn takes_imm(x: &isize) { }
//~^ NOTE found signature

fn takes_mut(x: &mut isize) { }
//~^ NOTE found signature

fn apply<T, F>(t: T, f: F) where F: FnOnce(T) {
f(t)
Expand All @@ -22,12 +24,10 @@ fn main() {
//~^ ERROR type mismatch
//~| NOTE required by `apply`
//~| NOTE expected signature
//~| NOTE found signature

apply(&mut 3, takes_mut);
apply(&mut 3, takes_imm);
//~^ ERROR type mismatch
//~| NOTE required by `apply`
//~| NOTE expected signature
//~| NOTE found signature
}
24 changes: 12 additions & 12 deletions src/test/ui/mismatched_types/fn-variance-1.stderr
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
error[E0631]: type mismatch in function arguments
--> $DIR/fn-variance-1.rs:21:5
--> $DIR/fn-variance-1.rs:23:5
|
21 | apply(&3, takes_mut);
| ^^^^^
| |
| expected signature of `fn(&{integer}) -> _`
| found signature of `for<'r> fn(&'r mut isize) -> _`
14 | fn takes_mut(x: &mut isize) { }
| --------------------------- found signature of `for<'r> fn(&'r mut isize) -> _`
...
23 | apply(&3, takes_mut);
| ^^^^^ expected signature of `fn(&{integer}) -> _`
|
= note: required by `apply`

error[E0631]: type mismatch in function arguments
--> $DIR/fn-variance-1.rs:28:5
--> $DIR/fn-variance-1.rs:29:5
|
28 | apply(&mut 3, takes_imm);
| ^^^^^
| |
| expected signature of `fn(&mut {integer}) -> _`
| found signature of `for<'r> fn(&'r isize) -> _`
11 | fn takes_imm(x: &isize) { }
| ----------------------- found signature of `for<'r> fn(&'r isize) -> _`
...
29 | apply(&mut 3, takes_imm);
| ^^^^^ expected signature of `fn(&mut {integer}) -> _`
|
= note: required by `apply`

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/mismatched_types/overloaded-calls-bad.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ error[E0057]: this function takes 1 parameter but 0 parameters were supplied
| ^^^ expected 1 parameter

error[E0057]: this function takes 1 parameter but 2 parameters were supplied
--> $DIR/overloaded-calls-bad.rs:44:17
--> $DIR/overloaded-calls-bad.rs:44:15
|
44 | let ans = s("burma", "shave");
| ^^^^^^^^^^^^^^^^ expected 1 parameter
| ^^^^^^^^^^^^^^^^^^^ expected 1 parameter

error: aborting due to 3 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/unboxed-closures-vtable-mismatch.rs:24:13
|
22 | let f = to_fn_mut(|x: usize, y: isize| -> isize { (x as isize) + y });
| -------------------------------------------------- found signature of `fn(usize, isize) -> _`
| ----------------------------- found signature of `fn(usize, isize) -> _`
23 | //~^ NOTE found signature of `fn(usize, isize)
24 | let z = call_it(3, f);
| ^^^^^^^ expected signature of `fn(isize, isize) -> _`
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/span/E0057.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ error[E0057]: this function takes 1 parameter but 0 parameters were supplied
| ^^^ expected 1 parameter

error[E0057]: this function takes 1 parameter but 2 parameters were supplied
--> $DIR/E0057.rs:15:15
--> $DIR/E0057.rs:15:13
|
15 | let c = f(2, 3); //~ ERROR E0057
| ^^^^ expected 1 parameter
| ^^^^^^^ expected 1 parameter

error: aborting due to 2 previous errors

Loading

0 comments on commit 75a02a9

Please sign in to comment.