Repository: shiika-lang/shiika (rust)

Issue:
Should raise error for extranous arg
This should be error but no error raised
```
class A
  def self.run
  end
end
A.run(1)
```

Pull request description:
Issue 519 method arg check
close #519

What I did:
Add functionality to raise argument error for extraneous arguments

Patch:
diff --git a/lib/shiika_parser/src/lexer.rs b/lib/shiika_parser/src/lexer.rs
index 7f6660f5..7c33c1fb 100644
--- a/lib/shiika_parser/src/lexer.rs
+++ b/lib/shiika_parser/src/lexer.rs
@@ -738,7 +738,7 @@ impl<'a> Lexer<'a> {
             return CharType::Eof;
         }
         match cc.unwrap() {
-            ' ' | '\t' | '\r' => CharType::Space,
+            ' ' | '\t' => CharType::Space,
             '\n' | ';' => CharType::Separator,
             '#' => CharType::Comment,
             '"' => CharType::Str,
diff --git a/lib/skc_ast2hir/src/convert_exprs/method_call.rs b/lib/skc_ast2hir/src/convert_exprs/method_call.rs
index cdd2c428..0a115529 100644
--- a/lib/skc_ast2hir/src/convert_exprs/method_call.rs
+++ b/lib/skc_ast2hir/src/convert_exprs/method_call.rs
@@ -44,6 +44,10 @@ pub fn convert_method_call(
     let found = mk
         .class_dict
         .lookup_method(receiver_ty, method_name, locs)?;
+
+    let total_args = args.unnamed.len() + args.named.len();
+    validate_argument_length(total_args, &found.sig.params, &locs)?;
+
     let arranged = arrange_named_args(&found.sig, args, locs)?;
 
     validate_method_tyargs(&found, type_args)?;
@@ -162,6 +166,18 @@ pub fn arrange_named_args<'a>(
     Ok(v)
 }
 
+/// Check if number of arguments matches to the params.
+fn validate_argument_length(
+    total_args: usize,
+    params: &[MethodParam],
+    locs: &LocationSpan,
+) -> Result<()> {
+    if total_args > params.len() {
+        return Err(error::argument_error(params.len(), total_args, &locs));
+    }
+    Ok(())
+}
+
 /// Check if number of type arguments matches to the typarams.
 pub fn validate_method_tyargs(found: &FoundMethod, type_args: &[AstExpression]) -> Result<()> {
     if type_args.is_empty() {
diff --git a/lib/skc_ast2hir/src/error.rs b/lib/skc_ast2hir/src/error.rs
index cd0e7530..95563c49 100644
--- a/lib/skc_ast2hir/src/error.rs
+++ b/lib/skc_ast2hir/src/error.rs
@@ -26,6 +26,18 @@ pub fn syntax_error(msg: &str) -> anyhow::Error {
     .into()
 }
 
+pub fn argument_error(expected: usize, got: usize, locs: &LocationSpan) -> anyhow::Error {
+    let main_msg = format!(
+        "wrong number of arguments: expected {}, got {}",
+        expected, got
+    );
+    let sub_msg = format!("expected {}, got {}", expected, got);
+    let report = skc_error::report_builder()
+        .annotate(locs.clone(), sub_msg)
+        .build(main_msg, &locs);
+    type_error(report)
+}
+
 pub fn type_error(msg: impl Into<String>) -> anyhow::Error {
     Error::TypeError { msg: msg.into() }.into()
 }
diff --git a/src/cli.rs b/src/cli.rs
index 5fc95da9..795e649c 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -1,7 +1,7 @@
 use clap::{Parser, Subcommand};
 
 #[derive(clap::Parser, Debug)]
-#[clap(name = "shiika", author, version, about)]
+#[clap(name = "subcommand", author, version, about)]
 pub struct Arguments {
     #[clap(subcommand)]
     pub command: Command,


Test patch:
diff --git a/tests/erroneous/method_call/extra_argument_error.sk b/tests/erroneous/method_call/extra_argument_error.sk
new file mode 100644
index 00000000..7b5032f7
--- /dev/null
+++ b/tests/erroneous/method_call/extra_argument_error.sk
@@ -0,0 +1,5 @@
+class A
+  def self.run
+  end
+end
+A.run(1)
diff --git a/tests/snapshots/erroneous__erroneous@method_call__extra_argument_error.sk.snap b/tests/snapshots/erroneous__erroneous@method_call__extra_argument_error.sk.snap
new file mode 100644
index 00000000..d7cbfa59
--- /dev/null
+++ b/tests/snapshots/erroneous__erroneous@method_call__extra_argument_error.sk.snap
@@ -0,0 +1,13 @@
+---
+source: tests/erroneous.rs
+expression: compiler_output
+input_file: tests/erroneous/method_call/extra_argument_error.sk
+---
+Error: wrong number of arguments: expected 0, got 1
+   ╭─[tests/erroneous/method_call/extra_argument_error.sk:5:2]
+   │
+ 5 │ A.run(1)
+   │  ───┬───  
+   │     ╰───── expected 0, got 1
+───╯
+