Skip to content

interpreter: create an array element when it is referenced - #146

Merged
Alonely0 merged 1 commit into
uutils:mainfrom
jayhemnani9910:fix/reference-creates-element
Oct 26, 2026
Merged

Alonely0 merged 1 commit into
uutils:mainfrom
jayhemnani9910:fix/reference-creates-element

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

POSIX awk: "a reference to a nonexistent element shall create that element", uninitialized. Here reading a[k] for a missing key returns an uninitialized value and doesn't add k:

$ awk 'BEGIN { x[1]; y = a["k"] a["j"]; print length(x), (1 in x), length(a) }'
0 0 0        # gawk 5.3.2: 1 1 2

Scripts that rely on it, such as { seen[$1] } END { for (k in seen) ... } or a guard like if (a[k] == "") followed by length(a), see an empty array. (k in a) still doesn't create anything.

get_array_elem (used by IndexS) now inserts an uninitialized value with entry_ref(..).or_insert_with(Value::new_untyped), the way array_elem_aoa already does for arrays of arrays.

Test test_referencing_an_element_creates_it (fails on main). cargo test --workspace: 219 pass (218 on main), clippy clean. Eight programs around element reads, in, counting and assignment give the same output as gawk 5.3.2.

Reading a[k] for a missing key returned an uninitialized value without
adding k, so 'x[1]; print length(x), (1 in x)' printed '0 0'. POSIX awk
and gawk create the element on reference.
Copilot AI balanced review requested due to automatic review settings October 26, 2026 03:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@github-actions

Copy link
Copy Markdown

GNU awk testsuite comparison:

Test results comparison:
  Current:   TOTAL: 666 / PASSED: 103 / FAILED: 465 / SKIPPED: 98
  Reference: TOTAL: 666 / PASSED: 101 / FAILED: 467 / SKIPPED: 98

Changes from main branch:
  TOTAL: +0
  PASSED: +2
  FAILED: -2

Test improvements (2):
  + aadelete1
  + tailrecurse

@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.

huh, I was under the impression this was already the case. Good catch, thanks for your contribution!

@Alonely0
Alonely0 merged commit 559195b into uutils:main Oct 26, 2026
15 checks passed
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.

3 participants