Skip to content

Commit

Permalink
fix: validate function argument names for duplicates (#680)
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhamedMagdi authored Mar 5, 2025
1 parent d4d49f2 commit 56fc243
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/modules/function/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,20 @@ impl SyntaxModule<ParserMetadata> for FunctionDeclaration {
meta.with_context_fn(Context::set_cc_flags, flags, |meta| {
// Get the arguments
token(meta, "(")?;
let mut seen_argument_names = HashSet::new();
loop {
if token(meta, ")").is_ok() {
break
}
let is_ref = token(meta, "ref").is_ok();
let name_token = meta.get_current_token();
let name = variable(meta, variable_name_extensions())?;

// Check for duplicate argument name
if !seen_argument_names.insert(name.clone()) {
return error!(meta, name_token, format!("Argument '{name}' is already defined"));
}

// Optionally parse the argument type
let mut arg_type = Type::Generic;
match token(meta, ":") {
Expand Down
6 changes: 6 additions & 0 deletions src/tests/erroring/duplicate_argument.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Output
// Argument 'a' is already defined

fun foo(a, a) {
return a
}

0 comments on commit 56fc243

Please sign in to comment.