Skip to content

Commit 7339eca

Browse files
authored
Auto merge of #34000 - estebank:missingargs, r=jseyfried
Show types of all args when missing args When there're missing arguments in a function call, present a list of all the expected types: ```rust fn main() { t(""); } fn t(a: &str, x: String) {} ``` ```bash % rustc file.rs file.rs:3:5: 2:8 error: this function takes 2 parameters but 0 parameters were supplied [E0061] file.rs:3 t(); ^~~ file.rs:3:5: 2:8 help: run `rustc --explain E0061` to see a detailed explanation file.rs:3:5: 2:8 note: the following parameter types were expected: &str, std::string::String error: aborting due to previous error ``` Fixes #33649
2 parents 58adb07 + 1020e30 commit 7339eca

File tree

8 files changed

+55
-33
lines changed

8 files changed

+55
-33
lines changed

src/librustc_typeck/check/mod.rs

+39-32
Original file line numberDiff line numberDiff line change
@@ -2402,29 +2402,45 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
24022402

24032403
let mut expected_arg_tys = expected_arg_tys;
24042404
let expected_arg_count = fn_inputs.len();
2405+
2406+
fn parameter_count_error<'tcx>(sess: &Session, sp: Span, fn_inputs: &[Ty<'tcx>],
2407+
expected_count: usize, arg_count: usize, error_code: &str,
2408+
variadic: bool) {
2409+
let mut err = sess.struct_span_err_with_code(sp,
2410+
&format!("this function takes {}{} parameter{} but {} parameter{} supplied",
2411+
if variadic {"at least "} else {""},
2412+
expected_count,
2413+
if expected_count == 1 {""} else {"s"},
2414+
arg_count,
2415+
if arg_count == 1 {" was"} else {"s were"}),
2416+
error_code);
2417+
let input_types = fn_inputs.iter().map(|i| format!("{:?}", i)).collect::<Vec<String>>();
2418+
if input_types.len() > 0 {
2419+
err.note(&format!("the following parameter type{} expected: {}",
2420+
if expected_count == 1 {" was"} else {"s were"},
2421+
input_types.join(", ")));
2422+
}
2423+
err.emit();
2424+
}
2425+
24052426
let formal_tys = if tuple_arguments == TupleArguments {
24062427
let tuple_type = self.structurally_resolved_type(sp, fn_inputs[0]);
24072428
match tuple_type.sty {
2429+
ty::TyTuple(arg_types) if arg_types.len() != args.len() => {
2430+
parameter_count_error(tcx.sess, sp, fn_inputs, arg_types.len(), args.len(),
2431+
"E0057", false);
2432+
expected_arg_tys = &[];
2433+
self.err_args(args.len())
2434+
}
24082435
ty::TyTuple(arg_types) => {
2409-
if arg_types.len() != args.len() {
2410-
span_err!(tcx.sess, sp, E0057,
2411-
"this function takes {} parameter{} but {} parameter{} supplied",
2412-
arg_types.len(),
2413-
if arg_types.len() == 1 {""} else {"s"},
2414-
args.len(),
2415-
if args.len() == 1 {" was"} else {"s were"});
2416-
expected_arg_tys = &[];
2417-
self.err_args(args.len())
2418-
} else {
2419-
expected_arg_tys = match expected_arg_tys.get(0) {
2420-
Some(&ty) => match ty.sty {
2421-
ty::TyTuple(ref tys) => &tys,
2422-
_ => &[]
2423-
},
2424-
None => &[]
2425-
};
2426-
arg_types.to_vec()
2427-
}
2436+
expected_arg_tys = match expected_arg_tys.get(0) {
2437+
Some(&ty) => match ty.sty {
2438+
ty::TyTuple(ref tys) => &tys,
2439+
_ => &[]
2440+
},
2441+
None => &[]
2442+
};
2443+
arg_types.to_vec()
24282444
}
24292445
_ => {
24302446
span_err!(tcx.sess, sp, E0059,
@@ -2440,23 +2456,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
24402456
if supplied_arg_count >= expected_arg_count {
24412457
fn_inputs.to_vec()
24422458
} else {
2443-
span_err!(tcx.sess, sp, E0060,
2444-
"this function takes at least {} parameter{} \
2445-
but {} parameter{} supplied",
2446-
expected_arg_count,
2447-
if expected_arg_count == 1 {""} else {"s"},
2448-
supplied_arg_count,
2449-
if supplied_arg_count == 1 {" was"} else {"s were"});
2459+
parameter_count_error(tcx.sess, sp, fn_inputs, expected_arg_count,
2460+
supplied_arg_count, "E0060", true);
24502461
expected_arg_tys = &[];
24512462
self.err_args(supplied_arg_count)
24522463
}
24532464
} else {
2454-
span_err!(tcx.sess, sp, E0061,
2455-
"this function takes {} parameter{} but {} parameter{} supplied",
2456-
expected_arg_count,
2457-
if expected_arg_count == 1 {""} else {"s"},
2458-
supplied_arg_count,
2459-
if supplied_arg_count == 1 {" was"} else {"s were"});
2465+
parameter_count_error(tcx.sess, sp, fn_inputs, expected_arg_count, supplied_arg_count,
2466+
"E0061", false);
24602467
expected_arg_tys = &[];
24612468
self.err_args(supplied_arg_count)
24622469
};

src/test/compile-fail/issue-18819.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ fn print_x(_: &Foo<Item=bool>, extra: &str) {
2424

2525
fn main() {
2626
print_x(X); //~error this function takes 2 parameters but 1 parameter was supplied
27+
//~^ NOTE the following parameter types were expected: &Foo<Item=bool>, &str
2728
}

src/test/compile-fail/issue-3044.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fn main() {
1414
needlesArr.iter().fold(|x, y| {
1515
});
1616
//~^^ ERROR this function takes 2 parameters but 1 parameter was supplied
17+
//~^^^ NOTE the following parameter types were expected
1718
//
1819
// the first error is, um, non-ideal.
1920
}

src/test/compile-fail/issue-4935.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212

1313
fn foo(a: usize) {}
1414
fn main() { foo(5, 6) } //~ ERROR this function takes 1 parameter but 2 parameters were supplied
15+
//~^ NOTE the following parameter type was expected

src/test/compile-fail/method-call-err-msg.rs

+3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ fn main() {
2121
let x = Foo;
2222
x.zero(0) //~ ERROR this function takes 0 parameters but 1 parameter was supplied
2323
.one() //~ ERROR this function takes 1 parameter but 0 parameters were supplied
24+
//~^ NOTE the following parameter type was expected
2425
.two(0); //~ ERROR this function takes 2 parameters but 1 parameter was supplied
26+
//~^ NOTE the following parameter types were expected
2527

2628
let y = Foo;
2729
y.zero()
2830
.take() //~ ERROR no method named `take` found for type `Foo` in the current scope
31+
//~^ NOTE the method `take` exists but the following trait bounds were not satisfied
2932
.one(0);
3033
}

src/test/compile-fail/not-enough-arguments.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ fn foo(a: isize, b: isize, c: isize, d:isize) {
1919
fn main() {
2020
foo(1, 2, 3);
2121
//~^ ERROR this function takes 4 parameters but 3
22+
//~^^ NOTE the following parameter types were expected
2223
}

src/test/compile-fail/overloaded-calls-bad.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ fn main() {
3636
y: 3,
3737
};
3838
let ans = s("what"); //~ ERROR mismatched types
39-
let ans = s(); //~ ERROR this function takes 1 parameter but 0 parameters were supplied
39+
//~^ NOTE expected isize, found &-ptr
40+
//~| NOTE expected type
41+
//~| NOTE found type
42+
let ans = s();
43+
//~^ ERROR this function takes 1 parameter but 0 parameters were supplied
44+
//~| NOTE the following parameter type was expected
4045
let ans = s("burma", "shave");
4146
//~^ ERROR this function takes 1 parameter but 2 parameters were supplied
47+
//~| NOTE the following parameter type was expected
4248
}

src/test/compile-fail/variadic-ffi-3.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ extern "C" fn bar(f: isize, x: u8) {}
1717
fn main() {
1818
unsafe {
1919
foo(); //~ ERROR: this function takes at least 2 parameters but 0 parameters were supplied
20+
//~^ NOTE the following parameter types were expected
2021
foo(1); //~ ERROR: this function takes at least 2 parameters but 1 parameter was supplied
22+
//~^ NOTE the following parameter types were expected
2123

2224
let x: unsafe extern "C" fn(f: isize, x: u8) = foo;
2325
//~^ ERROR: mismatched types

0 commit comments

Comments
 (0)