Skip to content

fix(parser): reject function names used as variables or parameters - #202

Open
bfs2021 wants to merge 2 commits into
uutils:mainfrom
bfs2021:fix/function-name-namespace
Open

bfs2021 wants to merge 2 commits into
uutils:mainfrom
bfs2021:fix/function-name-namespace

Conversation

@bfs2021

@bfs2021 bfs2021 commented Nov 26, 2026

Copy link
Copy Markdown

Problem

In awk, function names and variable names share a single namespace. GNU awk rejects assigning to a function name, using it as an array, naming a parameter after a function, or calling a parameter as a function. uutils awk accepted all of these and silently ran the program (Fixes #169).

For example, awk 'function f() { return 1 } BEGIN { f = 2; print f }' printed 2 instead of failing, and function f(f) { return f } BEGIN { print f(3) } printed 3.

Root cause

The parser had no check enforcing the shared namespace. Functions are stored in Ast::functions and variables are interned independently, so a function name used as an lvalue or a function parameter reusing a function name both parsed cleanly and reached the interpreter as ordinary variables. Because functions may be defined after their use (forward references), the check cannot be done at parse time for a single statement.

Fix

Added a post-parse validation pass in parser/src/validate.rs that walks the whole AST and rejects any function name used as a variable or array. It also rejects a function parameter that reuses any function name. Two new ParsingError variants report the offending identifier with its source span. The pass runs at the end of parse_top, after all function definitions are known.

Testing

  • Added parser regression tests covering assignment to a function name, use as an array, compound assignment, and a parameter named after a function; plus valid cases that must remain accepted.
  • Confirmed all three reported programs now exit with code 1 and a precise diagnostic, while normal function calls and variables still work.
  • cargo test --workspace: 32 + 52 + 47 + 132 tests passed, 0 failed.
  • cargo fmt --check and cargo clippy --workspace clean.

In awk, function names and variable names share a single namespace. GNU awk
rejects assigning to a function name, using it as an array, naming a parameter
after its own function, or calling a parameter as a function. uutils awk
accepted all of these and ran the program.

Add a post-parse validation pass that walks the AST and rejects any function
name used as a variable or array, plus a check that a function parameter does
not reuse any function name. This uses the whole AST because functions may be
defined after their use (forward references).

@Alonely0 Alonely0 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that adding another pass is necessary. I commented on the issue on how to move the name resolution checks to the lowering pass, but that makes it fallible and, after a night's rest, I'm not comfortable with that. Let me take a stab at this by moving name resolution to the parsing pass, yanking it outta the lowerer.

Comment thread parser/src/validate.rs Outdated

/// Validate that no function name is used as a variable or array anywhere in
/// the parsed program. Returns the first violation found.
pub fn validate(ast: &Ast<'_>) -> Result<(), ParsingError> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alonely0 asked on #169 for this to be done during lowering, in TypedPlace::new_user, after the function names are registered.
That way we don't need a second full AST walk. Could you please move it there?

Comment thread parser/src/validate.rs Outdated
// A parameter that reuses a function name is an error in the same shared
// namespace. Function names may be defined after the function in question,
// so this also runs as a whole-AST pass.
let fallback = ast

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so the parameter error points at whatever span comes first in loc_metadata?
please report it in parse_signature with lex.span(), like DuplicatedArgument

Comment thread parser/src/validate.rs Outdated
//! A function name may not be used as a variable or an array. Functions may be
//! defined after their use, so this check runs over the whole AST instead of
//! while parsing a single statement. (A function parameter reusing a function
//! name is rejected at parse time, in `Parser::parse_signature`, where the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not true, the parameter check is in validate() below

Comment thread parser/src/validate.rs Outdated
self.check_body(body)
}
Statement::ForEach { variable, array, body, .. } => {
// These carry no dedicated metadata; a zero span is acceptable

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (f in a) with f a function is rejected by gawk too, and ForEach has a metadata field. Why not use it?

Comment thread parser/src/tests.rs Outdated
}

#[test]
fn test_function_name_cannot_be_used_as_variable() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a test in tests/by-util/test_awk.rs too (the one from the issue checks the exit code)

Address the review on uutils#202 by moving the function-name/variable-name
namespace checks into the lowering pass, as asked on uutils#169:

- Register all function names up-front in `CodeGen::lower_ast` (names
  first, bytecode locations after), so `TypedPlace::new_user` rejects
  any use of a declared function name as a variable or array while
  lowering, without a second full AST walk. Conflicts are reported as
  diagnostics on the AST and the program exits 1 before it can run.
- Report a function name used as a parameter in `parse_signature` with
  `lex.span()`, like `DuplicatedArgument`, instead of a whole-AST pass
  with a fallback span.
- Reject `for (f in a)` when `f` is a declared function name, reported
  at the span stored for the loop variable.
- Delete the now-dead `validate` pass; move parser-level tests that
  duplicated lowering coverage and add coverage in `interpreter::ir`
  and `tests/by-util/test_awk.rs` (exit code 1, like the issue).
@bfs2021

bfs2021 commented Nov 27, 2026

Copy link
Copy Markdown
Author

Reworked per the review, pushed to the same branch:

  1. Moved the whole check into lowering, as asked on awk: a function name used as a variable or parameter is accepted #169: lower_ast now registers all function names up-front (names first, bytecode locations after), so TypedPlace::new_user rejects every use of a declared function name as a variable or array — no second full AST walk. Conflicts are collected as (name, metadata) while lowering and reported as diagnostics on the AST; the CLI flushes them and exits 1 before the program runs. Spans point at the offending statement (rule patterns now carry their own metadata too).
  2. The parameter error is now reported directly in parse_signature with lex.span(), same style as DuplicatedArgument (plus a matching help hint), instead of a whole-AST pass with a fallback span.
  3. The wrong doc comment went away with parser/src/validate.rs, which is deleted along with the whole validation walk (diff is net -104 lines).
  4. for (f in a) with f a function name is rejected too, reported at the span stored for the loop variable. Since ForEach lowering is still todo!(), this check happens where the node is built in the parser; once for-in lowering lands, the variable resolves through new_user and inherits the check automatically.
  5. Tests: parser-level tests now cover the parameter and for-in cases; the variable/array checks are covered in interpreter::ir::tests and, new, in tests/by-util/test_awk.rs with the issue's repro (exit code 1). cargo test --workspace is green (268 passed, was 263), cargo fmt --check and cargo clippy --workspace --all-targets are clean.

@sylvestre

Copy link
Copy Markdown
Contributor

sorry but it needs to be rebased

Comment thread parser/src/lib.rs
// 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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this only sees the functions declared before, so function f(g) { } function g() { } is still accepted.
now that all the names are registered up-front in lower_ast, could you please do the check in lower_fun_body and keep only arg == name here?

Comment thread tests/by-util/test_awk.rs
}

#[test]
fn function_name_as_parameter_exits_one() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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. function f(a) { a() } BEGIN { f(1) }. Is it handled? please add a test

}

pub fn new_user(code: &mut CodeGen, ident: Identifier) -> Self {
// Function names and variable names share a single namespace, so a

Copy link
Copy Markdown
Contributor

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_ast and on is_user_fun

// 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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f = f + 1 will push two conflicts for the same statement, so the error is printed twice, no?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

awk: a function name used as a variable or parameter is accepted

3 participants