diff --git a/gcc/rust/typecheck/rust-tyty-call.cc b/gcc/rust/typecheck/rust-tyty-call.cc index 5c3006140942..549379890938 100644 --- a/gcc/rust/typecheck/rust-tyty-call.cc +++ b/gcc/rust/typecheck/rust-tyty-call.cc @@ -23,6 +23,35 @@ namespace Rust { namespace TyTy { +void +emit_unexpected_argument_error (Location loc, + unsigned long unexpected_arg_count, + unsigned long expected_arg_count) +{ + // https://doc.rust-lang.org/error_codes/E0061.html + // rustc treats 1 as singular and others as plural + std::string err_msg = "this function takes %lu "; + if (expected_arg_count == 1) + { + err_msg += "argument"; + } + else + { + err_msg += "arguments"; + } + + if (unexpected_arg_count == 1) + { + err_msg += " but %lu argument was supplied"; + } + else + { + err_msg += " but %lu arguments were supplied"; + } + rust_error_at (loc, ErrorCode ("E0061"), err_msg.c_str (), expected_arg_count, + unexpected_arg_count); +} + void TypeCheckCallExpr::visit (ADTType &type) { @@ -38,10 +67,9 @@ TypeCheckCallExpr::visit (ADTType &type) if (call.num_params () != variant.num_fields ()) { - rust_error_at (call.get_locus (), - "unexpected number of arguments %lu expected %lu", - (unsigned long) call.num_params (), - (unsigned long) variant.num_fields ()); + emit_unexpected_argument_error (call.get_locus (), + (unsigned long) call.num_params (), + (unsigned long) variant.num_fields ()); return; } @@ -75,9 +103,8 @@ TypeCheckCallExpr::visit (ADTType &type) if (i != call.num_params ()) { - rust_error_at (call.get_locus (), - "unexpected number of arguments %lu expected %lu", - (unsigned long) i, (unsigned long) call.num_params ()); + emit_unexpected_argument_error (call.get_locus (), (unsigned long) i, + (unsigned long) call.num_params ()); return; } @@ -93,19 +120,17 @@ TypeCheckCallExpr::visit (FnType &type) { if (call.num_params () < type.num_params ()) { - rust_error_at (call.get_locus (), - "unexpected number of arguments %lu expected %lu", - (unsigned long) call.num_params (), - (unsigned long) type.num_params ()); + emit_unexpected_argument_error ( + call.get_locus (), (unsigned long) call.num_params (), + (unsigned long) type.num_params ()); return; } } else { - rust_error_at (call.get_locus (), - "unexpected number of arguments %lu expected %lu", - (unsigned long) call.num_params (), - (unsigned long) type.num_params ()); + emit_unexpected_argument_error (call.get_locus (), + (unsigned long) call.num_params (), + (unsigned long) type.num_params ()); return; } } @@ -207,9 +232,8 @@ TypeCheckCallExpr::visit (FnType &type) if (i < call.num_params ()) { - rust_error_at (call.get_locus (), - "unexpected number of arguments %lu expected %lu", - (unsigned long) i, (unsigned long) call.num_params ()); + emit_unexpected_argument_error (call.get_locus (), (unsigned long) i, + (unsigned long) call.num_params ()); return; } @@ -222,10 +246,9 @@ TypeCheckCallExpr::visit (FnPtr &type) { if (call.num_params () != type.num_params ()) { - rust_error_at (call.get_locus (), - "unexpected number of arguments %lu expected %lu", - (unsigned long) call.num_params (), - (unsigned long) type.num_params ()); + emit_unexpected_argument_error (call.get_locus (), + (unsigned long) call.num_params (), + (unsigned long) type.num_params ()); return; } @@ -257,9 +280,8 @@ TypeCheckCallExpr::visit (FnPtr &type) if (i != call.num_params ()) { - rust_error_at (call.get_locus (), - "unexpected number of arguments %lu expected %lu", - (unsigned long) i, (unsigned long) call.num_params ()); + emit_unexpected_argument_error (call.get_locus (), (unsigned long) i, + (unsigned long) call.num_params ()); return; } @@ -329,10 +351,9 @@ TypeCheckMethodCallExpr::check (FnType &type) size_t num_args_to_call = arguments.size () + 1; if (num_args_to_call != type.num_params ()) { - rust_error_at (call_locus, - "unexpected number of arguments %lu expected %lu", - (unsigned long) num_args_to_call, - (unsigned long) type.num_params ()); + emit_unexpected_argument_error (call_locus, + (unsigned long) num_args_to_call, + (unsigned long) type.num_params ()); return new ErrorType (type.get_ref ()); } @@ -364,9 +385,8 @@ TypeCheckMethodCallExpr::check (FnType &type) if (i != num_args_to_call) { - rust_error_at (call_locus, - "unexpected number of arguments %lu expected %lu", - (unsigned long) i, (unsigned long) arguments.size ()); + emit_unexpected_argument_error (call_locus, (unsigned long) i, + (unsigned long) arguments.size ()); return new ErrorType (type.get_ref ()); } diff --git a/gcc/testsuite/rust/compile/func2.rs b/gcc/testsuite/rust/compile/func2.rs index 0b8d999fec17..2d7e88f2bedb 100644 --- a/gcc/testsuite/rust/compile/func2.rs +++ b/gcc/testsuite/rust/compile/func2.rs @@ -3,5 +3,5 @@ fn test(a: i32, b: i32) -> i32 { } fn main() { - let a = test(1); // { dg-error "unexpected number of arguments 1 expected 2" } + let a = test(1); // { dg-error "this function takes 2 arguments but 1 argument was supplied" } } diff --git a/gcc/testsuite/rust/compile/tuple_struct2.rs b/gcc/testsuite/rust/compile/tuple_struct2.rs index 1fc189685773..6cd52ccbc436 100644 --- a/gcc/testsuite/rust/compile/tuple_struct2.rs +++ b/gcc/testsuite/rust/compile/tuple_struct2.rs @@ -1,5 +1,5 @@ struct Bar(i32, i32, bool); fn main() { - let a = Bar(1, 2); // { dg-error "unexpected number of arguments 2 expected 3" } + let a = Bar(1, 2); // { dg-error "this function takes 3 arguments but 2 arguments were supplied" } } diff --git a/gcc/testsuite/rust/compile/unexpected_arguments.rs b/gcc/testsuite/rust/compile/unexpected_arguments.rs new file mode 100644 index 000000000000..ffca5a78d864 --- /dev/null +++ b/gcc/testsuite/rust/compile/unexpected_arguments.rs @@ -0,0 +1,9 @@ +// https://doc.rust-lang.org/error_codes/E0061.html +fn main() { + fn f(u: i32) {} + fn T(u: i32, v: i32, w: i32, x: i32, y: i32, z: i32) {} + + f(); // { dg-error "this function takes 1 argument but 0 arguments were supplied" } + + T(1, 2, 3, 4, 5, 6, 7, 8, 9); // { dg-error "this function takes 6 arguments but 9 arguments were supplied" } +}