Skip to content

Commit

Permalink
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.
  • Loading branch information
estebank committed Dec 10, 2017
1 parent 6537fd1 commit 8ee82d0
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 78 deletions.
4 changes: 2 additions & 2 deletions src/librustc/traits/error_reporting.rs
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
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
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
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
@@ -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
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
4 changes: 4 additions & 0 deletions src/test/ui/mismatched_types/closure-arg-count.rs
Expand Up @@ -27,4 +27,8 @@ 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
}

fn foo() {}
11 changes: 10 additions & 1 deletion src/test/ui/mismatched_types/closure-arg-count.stderr
Expand Up @@ -56,5 +56,14 @@ 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
...
34 | fn foo() {}
| -------- takes 0 arguments

error: aborting due to 8 previous errors

16 changes: 8 additions & 8 deletions src/test/ui/mismatched_types/fn-variance-1.stderr
@@ -1,22 +1,22 @@
error[E0631]: type mismatch in function arguments
--> $DIR/fn-variance-1.rs:21:5
|
13 | fn takes_mut(x: &mut isize) { }
| --------------------------- found signature of `for<'r> fn(&'r mut isize) -> _`
...
21 | apply(&3, takes_mut);
| ^^^^^
| |
| expected signature of `fn(&{integer}) -> _`
| found signature of `for<'r> fn(&'r mut isize) -> _`
| ^^^^^ expected signature of `fn(&{integer}) -> _`
|
= note: required by `apply`

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

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
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

12 changes: 6 additions & 6 deletions src/test/ui/span/issue-34264.stderr
Expand Up @@ -17,13 +17,13 @@ error: expected one of `:` or `@`, found `,`
| ^ expected one of `:` or `@` here

error[E0061]: this function takes 2 parameters but 3 parameters were supplied
--> $DIR/issue-34264.rs:17:9
--> $DIR/issue-34264.rs:17:5
|
11 | fn foo(Option<i32>, String) {} //~ ERROR expected one of
| ------------------------------ defined here
| --------------------------- defined here
...
17 | foo(Some(42), 2, ""); //~ ERROR this function takes
| ^^^^^^^^^^^^^^^ expected 2 parameters
| ^^^^^^^^^^^^^^^^^^^^ expected 2 parameters

error[E0308]: mismatched types
--> $DIR/issue-34264.rs:18:13
Expand All @@ -37,13 +37,13 @@ error[E0308]: mismatched types
- .len()

error[E0061]: this function takes 2 parameters but 3 parameters were supplied
--> $DIR/issue-34264.rs:20:9
--> $DIR/issue-34264.rs:20:5
|
13 | fn bar(x, y: usize) {} //~ ERROR expected one of
| ---------------------- defined here
| ------------------- defined here
...
20 | bar(1, 2, 3); //~ ERROR this function takes
| ^^^^^^^ expected 2 parameters
| ^^^^^^^^^^^^ expected 2 parameters

error: aborting due to 6 previous errors

0 comments on commit 8ee82d0

Please sign in to comment.