]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: [E0061] Refactored argument mismatch error function
authorMuhammad Mahad <mahadtxt@gmail.com>
Thu, 6 Jul 2023 12:08:56 +0000 (17:08 +0500)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:49:35 +0000 (18:49 +0100)
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 <mahadtxt@gmail.com>
gcc/rust/typecheck/rust-tyty-call.cc
gcc/testsuite/rust/compile/func2.rs
gcc/testsuite/rust/compile/tuple_struct2.rs
gcc/testsuite/rust/compile/wrong_no_of_parameters.rs [new file with mode: 0644]

index 0f04d149fc84e60986983395484007892fd4dfed..8469cf825b651eb8bef3e3600254389dfdf6d650 100644 (file)
 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 ());
     }
 
index 0b8d999fec172310a7b14eb6634b43d9348e419d..2d7e88f2bedbf81b93c944c393ceaf3b53183841 100644 (file)
@@ -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" }
 }
index 1fc1896857738fd43ef1c4dad61dc6dc7310eae3..6cd52ccbc436e7a6c9246eecd2ca54ccfcce1d02 100644 (file)
@@ -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 (file)
index 0000000..ffca5a7
--- /dev/null
@@ -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" }
+}