Skip to content

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

Open
bfs2021 wants to merge 1 commit into
uutils:mainfrom
bfs2021:fix/function-name-namespace
Open

bfs2021 wants to merge 1 commit 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).
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

1 participant