Repository navigation
fix(parser): reject function names used as variables or parameters #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,6 +93,15 @@ impl TypedPlace { | |
| } | ||
|
|
||
| pub fn new_user(code: &mut CodeGen, ident: Identifier) -> Self { | ||
| // Function names and variable names share a single namespace, so a | ||
| // declared function name may not be used as a variable or array (like | ||
| // gawk's "function `f' used as variable name"). All function names are | ||
| // registered up-front in `CodeGen::lower_ast`, so this catches uses in | ||
| // every rule and function body without another AST walk; the conflict | ||
| // is reported once lowering finishes. | ||
| if code.symbols.is_user_fun(ident) { | ||
| code.name_conflicts.push((ident, code.current_metadata)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| let usr = code.symbols.register_user_var(ident); | ||
| if let Some(reg) = code.get_local_arg(usr) { | ||
| Self(Arg { reg }, PlaceTy::Reg) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -542,8 +542,20 @@ impl<'a> Parser<'a> { | |
| { | ||
| let node = Box::into_inner(node); | ||
| if let ExprNode::InArray(array, expr) = node | ||
| && let [Expr::Leaf(Atom::Variable(var), _)] = expr.as_slice() | ||
| && let [Expr::Leaf(Atom::Variable(var), meta)] = expr.as_slice() | ||
| { | ||
| // `for (var in array)` may not reuse a function name as | ||
| // the loop variable (gawk rejects it too). Report it at | ||
| // the span stored for the variable. | ||
| if let Variable::User(ident) = var | ||
| && self.ast.functions.contains_key(ident) | ||
| { | ||
| let AriadneSpan(_, span) = &self.ast.loc_metadata[*meta]; | ||
| return Err(ParsingError::FunctionNameAsVariable( | ||
| *span, | ||
| format!("{ident:?}"), | ||
| )); | ||
| } | ||
| (*var, array) | ||
| } else { | ||
| return Err(ParsingError::InvalidForLoop(lex.span())); | ||
|
|
@@ -778,6 +790,16 @@ impl<'a> Parser<'a> { | |
| format!("{name:?}"), | ||
| )); | ||
| } | ||
| // Function names and variable names share a single namespace, so a | ||
| // function name is not a valid parameter name (like gawk's | ||
| // "cannot use function name as parameter name"). | ||
| if arg == name || self.ast.functions.contains_key(&arg) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this only sees the functions declared before, so |
||
| return Err(ParsingError::FunctionNameAsParameter( | ||
| lex.span(), | ||
| format!("{arg:?}"), | ||
| format!("{name:?}"), | ||
| )); | ||
| } | ||
| args.push(arg); | ||
|
|
||
| if !lex.consume(&Token::Comma) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1386,6 +1386,47 @@ fn syntax_error_does_not_run_the_program() { | |
| .no_stdout(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn function_name_as_variable_exits_one() { | ||
| // Function and variable names share one namespace; gawk rejects these. | ||
| ucmd() | ||
| .arg("function f() { return 1 } BEGIN { f = 2; print f }") | ||
| .fails_with_code(1) | ||
| .no_stdout() | ||
| .stderr_contains("cannot be used as a variable"); | ||
| ucmd() | ||
| .arg("BEGIN { g[1] = 1 } function g() { }") | ||
| .fails_with_code(1) | ||
| .no_stdout() | ||
| .stderr_contains("cannot be used as a variable"); | ||
| ucmd() | ||
| .arg("function h() { } BEGIN { h += 1 }") | ||
| .fails_with_code(1) | ||
| .no_stdout(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn function_name_as_parameter_exits_one() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #169 also lists calling a parameter as a function (paramasfunc1/2), e.g. |
||
| ucmd() | ||
| .arg("function f(f) { return f } BEGIN { print f(3) }") | ||
| .fails_with_code(1) | ||
| .no_stdout() | ||
| .stderr_contains("cannot be used as a parameter"); | ||
| ucmd() | ||
| .arg("function h(h) { } BEGIN { }") | ||
| .fails_with_code(1) | ||
| .no_stdout(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn function_name_as_foreach_variable_exits_one() { | ||
| ucmd() | ||
| .arg("function f() { } BEGIN { for (f in a) { print } }") | ||
| .fails_with_code(1) | ||
| .no_stdout() | ||
| .stderr_contains("cannot be used as a variable"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn variables_not_clobbered_by_side_effects() { | ||
| ucmd() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6 lines of comment, please make it shorter :)
same for the ones in
lower_astand onis_user_fun