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).
Alonely0
left a comment
There was a problem hiding this comment.
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.
|
|
||
| /// 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> { |
There was a problem hiding this comment.
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?
| // 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 |
There was a problem hiding this comment.
so the parameter error points at whatever span comes first in loc_metadata?
please report it in parse_signature with lex.span(), like DuplicatedArgument
| //! 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 |
There was a problem hiding this comment.
this is not true, the parameter check is in validate() below
| self.check_body(body) | ||
| } | ||
| Statement::ForEach { variable, array, body, .. } => { | ||
| // These carry no dedicated metadata; a zero span is acceptable |
There was a problem hiding this comment.
for (f in a) with f a function is rejected by gawk too, and ForEach has a metadata field. Why not use it?
| } | ||
|
|
||
| #[test] | ||
| fn test_function_name_cannot_be_used_as_variable() { |
There was a problem hiding this comment.
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).
|
Reworked per the review, pushed to the same branch:
|
|
sorry but it needs to be rebased |
| // 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) { |
There was a problem hiding this comment.
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?
| } | ||
|
|
||
| #[test] | ||
| fn function_name_as_parameter_exits_one() { |
There was a problem hiding this comment.
#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 |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
f = f + 1 will push two conflicts for the same statement, so the error is printed twice, no?
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.