Skip to content

Commit c33d531

Browse files
authored
Rollup merge of rust-lang#68877 - estebank:point-at-params, r=petrochenkov
On mismatched argument count point at arguments
2 parents 8f813b7 + 683ebc2 commit c33d531

35 files changed

+214
-105
lines changed

src/librustc_typeck/check/mod.rs

+47-7
Original file line numberDiff line numberDiff line change
@@ -3844,17 +3844,58 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
38443844
error_code: &str,
38453845
c_variadic: bool,
38463846
sugg_unit: bool| {
3847+
let (span, start_span, args) = match &expr.kind {
3848+
hir::ExprKind::Call(hir::Expr { span, .. }, args) => (*span, *span, &args[..]),
3849+
hir::ExprKind::MethodCall(path_segment, span, args) => (
3850+
*span,
3851+
// `sp` doesn't point at the whole `foo.bar()`, only at `bar`.
3852+
path_segment
3853+
.args
3854+
.and_then(|args| args.args.iter().last())
3855+
// Account for `foo.bar::<T>()`.
3856+
.map(|arg| {
3857+
// Skip the closing `>`.
3858+
tcx.sess
3859+
.source_map()
3860+
.next_point(tcx.sess.source_map().next_point(arg.span()))
3861+
})
3862+
.unwrap_or(*span),
3863+
&args[1..], // Skip the receiver.
3864+
),
3865+
k => span_bug!(sp, "checking argument types on a non-call: `{:?}`", k),
3866+
};
3867+
let arg_spans = if args.is_empty() {
3868+
// foo()
3869+
// ^^^-- supplied 0 arguments
3870+
// |
3871+
// expected 2 arguments
3872+
vec![tcx.sess.source_map().next_point(start_span).with_hi(sp.hi())]
3873+
} else {
3874+
// foo(1, 2, 3)
3875+
// ^^^ - - - supplied 3 arguments
3876+
// |
3877+
// expected 2 arguments
3878+
args.iter().map(|arg| arg.span).collect::<Vec<Span>>()
3879+
};
3880+
38473881
let mut err = tcx.sess.struct_span_err_with_code(
3848-
sp,
3882+
span,
38493883
&format!(
38503884
"this function takes {}{} but {} {} supplied",
38513885
if c_variadic { "at least " } else { "" },
3852-
potentially_plural_count(expected_count, "parameter"),
3853-
potentially_plural_count(arg_count, "parameter"),
3886+
potentially_plural_count(expected_count, "argument"),
3887+
potentially_plural_count(arg_count, "argument"),
38543888
if arg_count == 1 { "was" } else { "were" }
38553889
),
38563890
DiagnosticId::Error(error_code.to_owned()),
38573891
);
3892+
let label = format!("supplied {}", potentially_plural_count(arg_count, "argument"));
3893+
for (i, span) in arg_spans.into_iter().enumerate() {
3894+
err.span_label(
3895+
span,
3896+
if arg_count == 0 || i + 1 == arg_count { &label } else { "" },
3897+
);
3898+
}
38583899

38593900
if let Some(def_s) = def_span.map(|sp| tcx.sess.source_map().def_span(sp)) {
38603901
err.span_label(def_s, "defined here");
@@ -3871,11 +3912,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
38713912
);
38723913
} else {
38733914
err.span_label(
3874-
sp,
3915+
span,
38753916
format!(
38763917
"expected {}{}",
38773918
if c_variadic { "at least " } else { "" },
3878-
potentially_plural_count(expected_count, "parameter")
3919+
potentially_plural_count(expected_count, "argument")
38793920
),
38803921
);
38813922
}
@@ -5577,8 +5618,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
55775618

55785619
self.tcx.sess.span_err(
55795620
span,
5580-
"this function can only be invoked \
5581-
directly, not through a function pointer",
5621+
"this function can only be invoked directly, not through a function pointer",
55825622
);
55835623
}
55845624

src/test/ui/arg-count-mismatch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// error-pattern: parameters were supplied
1+
// error-pattern: arguments were supplied
22

33
fn f(x: isize) { }
44

src/test/ui/arg-count-mismatch.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
error[E0061]: this function takes 1 parameter but 0 parameters were supplied
1+
error[E0061]: this function takes 1 argument but 0 arguments were supplied
22
--> $DIR/arg-count-mismatch.rs:5:28
33
|
44
LL | fn f(x: isize) { }
55
| -------------- defined here
66
LL |
77
LL | fn main() { let i: (); i = f(); }
8-
| ^^^ expected 1 parameter
8+
| ^-- supplied 0 arguments
9+
| |
10+
| expected 1 argument
911

1012
error: aborting due to previous error
1113

src/test/ui/c-variadic/variadic-ffi-1.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ extern "C" fn bar(f: isize, x: u8) {}
1313

1414
fn main() {
1515
unsafe {
16-
foo(); //~ ERROR this function takes at least 2 parameters but 0 parameters were supplied
17-
foo(1); //~ ERROR this function takes at least 2 parameters but 1 parameter was supplied
16+
foo(); //~ ERROR this function takes at least 2 arguments but 0 arguments were supplied
17+
foo(1); //~ ERROR this function takes at least 2 arguments but 1 argument was supplied
1818

1919
let x: unsafe extern "C" fn(f: isize, x: u8) = foo; //~ ERROR mismatched types
2020
let y: extern "C" fn(f: isize, x: u8, ...) = bar; //~ ERROR mismatched types

src/test/ui/c-variadic/variadic-ffi-1.stderr

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@ error[E0045]: C-variadic function must have C or cdecl calling convention
44
LL | fn printf(_: *const u8, ...);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ C-variadics require C or cdecl calling convention
66

7-
error[E0060]: this function takes at least 2 parameters but 0 parameters were supplied
7+
error[E0060]: this function takes at least 2 arguments but 0 arguments were supplied
88
--> $DIR/variadic-ffi-1.rs:16:9
99
|
1010
LL | fn foo(f: isize, x: u8, ...);
1111
| ----------------------------- defined here
1212
...
1313
LL | foo();
14-
| ^^^^^ expected at least 2 parameters
14+
| ^^^-- supplied 0 arguments
15+
| |
16+
| expected at least 2 arguments
1517

16-
error[E0060]: this function takes at least 2 parameters but 1 parameter was supplied
18+
error[E0060]: this function takes at least 2 arguments but 1 argument was supplied
1719
--> $DIR/variadic-ffi-1.rs:17:9
1820
|
1921
LL | fn foo(f: isize, x: u8, ...);
2022
| ----------------------------- defined here
2123
...
2224
LL | foo(1);
23-
| ^^^^^^ expected at least 2 parameters
25+
| ^^^ - supplied 1 argument
26+
| |
27+
| expected at least 2 arguments
2428

2529
error[E0308]: mismatched types
2630
--> $DIR/variadic-ffi-1.rs:19:56

src/test/ui/error-codes/E0057.stderr

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
error[E0057]: this function takes 1 parameter but 0 parameters were supplied
1+
error[E0057]: this function takes 1 argument but 0 arguments were supplied
22
--> $DIR/E0057.rs:3:13
33
|
44
LL | let a = f();
5-
| ^^^ expected 1 parameter
5+
| ^-- supplied 0 arguments
6+
| |
7+
| expected 1 argument
68

7-
error[E0057]: this function takes 1 parameter but 2 parameters were supplied
9+
error[E0057]: this function takes 1 argument but 2 arguments were supplied
810
--> $DIR/E0057.rs:5:13
911
|
1012
LL | let c = f(2, 3);
11-
| ^^^^^^^ expected 1 parameter
13+
| ^ - - supplied 2 arguments
14+
| |
15+
| expected 1 argument
1216

1317
error: aborting due to 2 previous errors
1418

src/test/ui/error-codes/E0060.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ extern "C" {
55
fn main() {
66
unsafe { printf(); }
77
//~^ ERROR E0060
8-
//~| expected at least 1 parameter
8+
//~| expected at least 1 argument
99
}

src/test/ui/error-codes/E0060.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
error[E0060]: this function takes at least 1 parameter but 0 parameters were supplied
1+
error[E0060]: this function takes at least 1 argument but 0 arguments were supplied
22
--> $DIR/E0060.rs:6:14
33
|
44
LL | fn printf(_: *const u8, ...) -> u32;
55
| ------------------------------------ defined here
66
...
77
LL | unsafe { printf(); }
8-
| ^^^^^^^^ expected at least 1 parameter
8+
| ^^^^^^-- supplied 0 arguments
9+
| |
10+
| expected at least 1 argument
911

1012
error: aborting due to previous error
1113

src/test/ui/error-codes/E0061.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ fn f2(a: u16) {}
55
fn main() {
66
f(0);
77
//~^ ERROR E0061
8-
//~| expected 2 parameters
8+
//~| expected 2 arguments
99

1010
f2();
1111
//~^ ERROR E0061
12-
//~| expected 1 parameter
12+
//~| expected 1 argument
1313
}

src/test/ui/error-codes/E0061.stderr

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
error[E0061]: this function takes 2 parameters but 1 parameter was supplied
1+
error[E0061]: this function takes 2 arguments but 1 argument was supplied
22
--> $DIR/E0061.rs:6:5
33
|
44
LL | fn f(a: u16, b: &str) {}
55
| --------------------- defined here
66
...
77
LL | f(0);
8-
| ^^^^ expected 2 parameters
8+
| ^ - supplied 1 argument
9+
| |
10+
| expected 2 arguments
911

10-
error[E0061]: this function takes 1 parameter but 0 parameters were supplied
12+
error[E0061]: this function takes 1 argument but 0 arguments were supplied
1113
--> $DIR/E0061.rs:10:5
1214
|
1315
LL | fn f2(a: u16) {}
1416
| ------------- defined here
1517
...
1618
LL | f2();
17-
| ^^^^ expected 1 parameter
19+
| ^^-- supplied 0 arguments
20+
| |
21+
| expected 1 argument
1822

1923
error: aborting due to 2 previous errors
2024

src/test/ui/hrtb/issue-58451.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ where
99
{}
1010

1111
fn main() {
12-
f(&[f()]); //~ ERROR this function takes 1 parameter
12+
f(&[f()]); //~ ERROR this function takes 1 argument
1313
}

src/test/ui/hrtb/issue-58451.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0061]: this function takes 1 parameter but 0 parameters were supplied
1+
error[E0061]: this function takes 1 argument but 0 arguments were supplied
22
--> $DIR/issue-58451.rs:12:9
33
|
44
LL | / fn f<I>(i: I)
@@ -9,7 +9,9 @@ LL | | {}
99
| |__- defined here
1010
...
1111
LL | f(&[f()]);
12-
| ^^^ expected 1 parameter
12+
| ^-- supplied 0 arguments
13+
| |
14+
| expected 1 argument
1315

1416
error: aborting due to previous error
1517

src/test/ui/issues/issue-16939.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error[E0057]: this function takes 0 parameters but 1 parameter was supplied
1+
error[E0057]: this function takes 0 arguments but 1 argument was supplied
22
--> $DIR/issue-16939.rs:5:9
33
|
44
LL | |t| f(t);
5-
| ^^^^ expected 0 parameters
5+
| ^ - supplied 1 argument
6+
| |
7+
| expected 0 arguments
68

79
error: aborting due to previous error
810

src/test/ui/issues/issue-18819.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
error[E0061]: this function takes 2 parameters but 1 parameter was supplied
1+
error[E0061]: this function takes 2 arguments but 1 argument was supplied
22
--> $DIR/issue-18819.rs:16:5
33
|
44
LL | fn print_x(_: &dyn Foo<Item=bool>, extra: &str) {
55
| ----------------------------------------------- defined here
66
...
77
LL | print_x(X);
8-
| ^^^^^^^^^^ expected 2 parameters
8+
| ^^^^^^^ - supplied 1 argument
9+
| |
10+
| expected 2 arguments
911

1012
error: aborting due to previous error
1113

src/test/ui/issues/issue-26094.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
macro_rules! some_macro {
22
($other: expr) => ({
3-
$other(None)
4-
//~^ this function takes 0 parameters but 1 parameter was supplied
3+
$other(None) //~ NOTE supplied 1 argument
54
})
65
}
76

8-
fn some_function() {}
7+
fn some_function() {} //~ NOTE defined here
98

109
fn main() {
1110
some_macro!(some_function);
12-
//~^ in this expansion of some_macro!
11+
//~^ ERROR this function takes 0 arguments but 1 argument was supplied
12+
//~| NOTE expected 0 arguments
1313
}

src/test/ui/issues/issue-26094.stderr

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
error[E0061]: this function takes 0 parameters but 1 parameter was supplied
2-
--> $DIR/issue-26094.rs:3:9
1+
error[E0061]: this function takes 0 arguments but 1 argument was supplied
2+
--> $DIR/issue-26094.rs:10:17
33
|
44
LL | $other(None)
5-
| ^^^^^^^^^^^^ expected 0 parameters
5+
| ---- supplied 1 argument
66
...
77
LL | fn some_function() {}
88
| ------------------ defined here
99
...
1010
LL | some_macro!(some_function);
11-
| --------------------------- in this macro invocation
12-
|
13-
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
11+
| ^^^^^^^^^^^^^ expected 0 arguments
1412

1513
error: aborting due to previous error
1614

src/test/ui/issues/issue-3044.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ fn main() {
22
let needlesArr: Vec<char> = vec!['a', 'f'];
33
needlesArr.iter().fold(|x, y| {
44
});
5-
//~^^ ERROR this function takes 2 parameters but 1 parameter was supplied
5+
//~^^ ERROR this function takes 2 arguments but 1 argument was supplied
66
}

src/test/ui/issues/issue-3044.stderr

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
error[E0061]: this function takes 2 parameters but 1 parameter was supplied
1+
error[E0061]: this function takes 2 arguments but 1 argument was supplied
22
--> $DIR/issue-3044.rs:3:23
33
|
4-
LL | needlesArr.iter().fold(|x, y| {
5-
| ^^^^ expected 2 parameters
4+
LL | needlesArr.iter().fold(|x, y| {
5+
| _______________________^^^^_-
6+
| | |
7+
| | expected 2 arguments
8+
LL | | });
9+
| |_____- supplied 1 argument
610

711
error: aborting due to previous error
812

src/test/ui/issues/issue-4935.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
fn foo(a: usize) {}
44
//~^ defined here
55
fn main() { foo(5, 6) }
6-
//~^ ERROR this function takes 1 parameter but 2 parameters were supplied
6+
//~^ ERROR this function takes 1 argument but 2 arguments were supplied

src/test/ui/issues/issue-4935.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
error[E0061]: this function takes 1 parameter but 2 parameters were supplied
1+
error[E0061]: this function takes 1 argument but 2 arguments were supplied
22
--> $DIR/issue-4935.rs:5:13
33
|
44
LL | fn foo(a: usize) {}
55
| ---------------- defined here
66
LL |
77
LL | fn main() { foo(5, 6) }
8-
| ^^^^^^^^^ expected 1 parameter
8+
| ^^^ - - supplied 2 arguments
9+
| |
10+
| expected 1 argument
911

1012
error: aborting due to previous error
1113

src/test/ui/methods/method-call-err-msg.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ impl Foo {
55
fn zero(self) -> Foo { self }
66
fn one(self, _: isize) -> Foo { self }
77
fn two(self, _: isize, _: isize) -> Foo { self }
8+
fn three<T>(self, _: T, _: T, _: T) -> Foo { self }
89
}
910

1011
fn main() {
1112
let x = Foo;
12-
x.zero(0) //~ ERROR this function takes 0 parameters but 1 parameter was supplied
13-
.one() //~ ERROR this function takes 1 parameter but 0 parameters were supplied
14-
.two(0); //~ ERROR this function takes 2 parameters but 1 parameter was supplied
13+
x.zero(0) //~ ERROR this function takes 0 arguments but 1 argument was supplied
14+
.one() //~ ERROR this function takes 1 argument but 0 arguments were supplied
15+
.two(0); //~ ERROR this function takes 2 arguments but 1 argument was supplied
1516

1617
let y = Foo;
1718
y.zero()
1819
.take() //~ ERROR no method named `take` found
1920
.one(0);
21+
y.three::<usize>(); //~ ERROR this function takes 3 arguments but 0 arguments were supplied
2022
}

0 commit comments

Comments
 (0)