Skip to content

Commit

Permalink
gccrs: [E0061] Refactored argument mismatch error function
Browse files Browse the repository at this point in the history
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/unexpected_arguments.rs: New test.

Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
  • Loading branch information
MahadMuhammad committed Jul 5, 2023
1 parent 5406b63 commit ebeda72
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 34 deletions.
84 changes: 52 additions & 32 deletions gcc/rust/typecheck/rust-tyty-call.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 ());
}

Expand Down Expand Up @@ -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 ());
}

Expand Down
2 changes: 1 addition & 1 deletion gcc/testsuite/rust/compile/func2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
}
2 changes: 1 addition & 1 deletion gcc/testsuite/rust/compile/tuple_struct2.rs
Original file line number Diff line number Diff line change
@@ -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" }
}
9 changes: 9 additions & 0 deletions gcc/testsuite/rust/compile/unexpected_arguments.rs
Original file line number Diff line number Diff line change
@@ -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" }
}

0 comments on commit ebeda72

Please sign in to comment.