Repository navigation
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 }'printed2instead of failing, andfunction f(f) { return f } BEGIN { print f(3) }printed3.Root cause
The parser had no check enforcing the shared namespace. Functions are stored in
Ast::functionsand 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.rsthat 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 newParsingErrorvariants report the offending identifier with its source span. The pass runs at the end ofparse_top, after all function definitions are known.Testing
cargo test --workspace: 32 + 52 + 47 + 132 tests passed, 0 failed.cargo fmt --checkandcargo clippy --workspaceclean.