Skip to content

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

Open

Description

@sylvestre

Function names and variable names share one name space in awk. GNU 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 accepts all
of these and runs the program.

Reproduction

Rust (incorrect)

$ ./target/release/awk 'function f() { return 1 } BEGIN { f = 2; print f }'
2
$ ./target/release/awk 'function f(f) { return f } BEGIN { print f(3) }'
3

GNU (correct)

$ gawk 'function f() { return 1 } BEGIN { f = 2; print f }'
gawk: cmd. line:1: error: function `f' called with space between name and `(',
or used as a variable or an array
# Exit code: 1
$ gawk 'function f(f) { return f } BEGIN { print f(3) }'
gawk: cmd. line:1: error: function `f': cannot use function name as parameter name
# Exit code: 1

Test

#[test]
fn function_name_as_variable_is_an_error() {
    ucmd()
        .arg("BEGIN { g[1] = 1 } function g() { }")
        .fails_with_code(1);
    ucmd()
        .arg("function h(h) { } BEGIN { }")
        .fails_with_code(1);
}

Exposed by the GNU awk test suite: delfunc, fnamedat, fnarray, fnarray2,
fnasgnm, funsmnam, paramasfunc1, paramasfunc2.

Activity

  1. added
    bug
    good first issue
    gnu-compat
    on Nov 17, 2026
  2. Alonely0 commented on Nov 26, 2026

    @Alonely0
    Collaborator

    Note: this must be addressed during the lowering pass, which is where we resolve functions to the function table before other user code. This probably requires a slight logic rework to first populate the function names, and then register their bytecode location. Lastly, the checks can be added to TypedPlace::new_user, since that resolves all user variables and already handles edge cases like function locals.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggnu-compatgood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions