Skip to content

interpreter: compare an uninitialized value as 0 against a number - #149

Merged
Alonely0 merged 1 commit into
uutils:mainfrom
jayhemnani9910:fix/uninit-compare
Oct 27, 2026
Merged

Alonely0 merged 1 commit into
uutils:mainfrom
jayhemnani9910:fix/uninit-compare

Conversation

@jayhemnani9910

@jayhemnani9910 jayhemnani9910 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

An unset variable compared with a number uses string comparison, so if (n == 0) is false for an unset n:

$ awk 'BEGIN { print (x == 0), (x > -1), (x >= 0), (x != 0) }'
0 0 0 1
$ gawk 'BEGIN { print (x == 0), (x > -1), (x >= 0), (x != 0) }'    # gawk 5.3.2
1 1 1 0

POSIX gives an uninitialized value both the string value "" and the numeric value 0, and compares numerically when the other side is a number. == and the ordered comparisons now do that for Untyped/Unassigned against Float, Int and Bool. Against a string it is still a string comparison (x < "a" is 1, x == "0" is 0). While there: the (_, Untyped | Unassigned) arm in eq read other.as_str() instead of self.as_str(), so "" == x was 0.

Known gap: a field past NF is also Unassigned, so $9 == 0 now prints 1 (gawk and mawk print 0, main printed 0). Telling the two apart is left for the type rework.

Tests: uninitialized_value_compares_as_zero_with_a_number fails without the change. cargo test --workspace passes (test_awk 100), clippy and cargo fmt --check are clean.

Copilot AI balanced review requested due to automatic review settings October 27, 2026 06:05

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.

@Alonely0

Copy link
Copy Markdown
Collaborator

This PR does two jobs at once, which is not bad, but I'm afraid we shouldn't merge the field-related changes at the moment; let's just keep the different type comparisons for unassigned values for now. I think first we should refactor the internal type implementation to use tagged pointers or other form of algebraic property management; the current disjoint representation is hard to manage and starts to suffer from combinatorial explosion.

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

See my previous comment.

An uninitialized variable or array element is both "" and 0, so a
comparison with a number is numeric: x == 0 and x > -1 are true for an
unset x, as in gawk, mawk and nawk.
@jayhemnani9910

Copy link
Copy Markdown
Contributor Author

Dropped the field part in eb7ebc6 and rebased on main. One side effect: fields past NF are Unassigned too, so $9 == 0 now prints 1 (gawk prints 0). I noted it in the description; it should go away with the type rework.

Copilot AI balanced review requested due to automatic review settings October 27, 2026 10:29

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.

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

Nice! Merging this once CI passes.

@Alonely0
Alonely0 merged commit 67e0816 into uutils:main Oct 27, 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