From f7a29f5653ae0787fd57ced935be0be4beb93036 Mon Sep 17 00:00:00 2001 From: Muhammad Mahad Date: Thu, 6 Jul 2023 17:08:56 +0500 Subject: gccrs: [E0061] Refactored argument mismatch error function Added Invalid number of arguments (argument mismatch) was passed when calling a function - unexpected number of arguments `x` expected `y` And Refactored error into one function. gcc/rust/ChangeLog: * typecheck/rust-tyty-call.cc (emit_unexpected_argument_error): Refactored invalid number of argument into one function. (TypeCheckCallExpr::visit): called refactored function. (TypeCheckMethodCallExpr::check): likewise. gcc/testsuite/ChangeLog: * rust/compile/func2.rs: updated comment to pass new test cases. * rust/compile/tuple_struct2.rs: likewise. * rust/compile/wrong_no_of_parameters.rs: New test. Signed-off-by: Muhammad Mahad --- gcc/rust/typecheck/rust-tyty-call.cc | 84 +++++++++++++--------- gcc/testsuite/rust/compile/func2.rs | 2 +- gcc/testsuite/rust/compile/tuple_struct2.rs | 2 +- .../rust/compile/wrong_no_of_parameters.rs | 9 +++ 4 files changed, 63 insertions(+), 34 deletions(-) create mode 100644 gcc/testsuite/rust/compile/wrong_no_of_parameters.rs (limited to 'gcc') diff --git a/gcc/rust/typecheck/rust-tyty-call.cc b/gcc/rust/typecheck/rust-tyty-call.cc index 5c30061..5493798 100644 --- a/gcc/rust/typecheck/rust-tyty-call.cc +++ b/gcc/rust/typecheck/rust-tyty-call.cc @@ -24,6 +24,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) { rust_assert (!variant.is_error ()); @@ -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 0b8d999..2d7e88f 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 1fc1896..6cd52cc 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/wrong_no_of_parameters.rs b/gcc/testsuite/rust/compile/wrong_no_of_parameters.rs new file mode 100644 index 0000000..ffca5a7 --- /dev/null +++ b/gcc/testsuite/rust/compile/wrong_no_of_parameters.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" } +} -- cgit v1.1