8000 Field/method name collision across sibling types breaks field access on BEAM · Issue #234 · kexhq/kex · GitHub
[go: up one dir, main page]

Skip to content

Field/method name collision across sibling types breaks field access on BEAM #234

Closed

Description

@akoskovacs

Summary

Discovered while fixing #233: once bare-module-alias resolution actually lets Set.from(...) reach Data.Set's constructor on BEAM, a deeper, separate lowering bug surfaces for @items (a plain record field on Data.Set) because Data.UnorderedSet — its sibling under the same module Data header — happens to define a method also named items.

Repro

using Data.Set

main do
  let s = Set.from([3, 1, 2])
  IO.printLine("${s.count}")   # prints 2, should print 3
  IO.printLine("${s.items}")   # prints [Data.Set, [1, 2, 3]], should print [1, 2, 3]
end

Run with ./build/kex -R.

Root cause

src/stdlib/data/set.kex declares record Set<A> do items : [A] = [] end (a field) and separately record UnorderedSet<A> do slots : {A: Bool} = {} end with make UnorderedSet<A> do items :> [A]; let items = @slots.keys end (a method). Both are named items.

In src/ir/lower.cxx's makeAccessors, the field-accessor generator for items bails out entirely once it sees ANY function named items exists elsewhere in the program:

if (definedFns.count(field) && definedFnArities.count(field + "/1"))
    continue;

The comment above it says the dispatcher is supposed to carry the field-read clauses too, via appendFieldClauses (called from the generic per-name dispatcher builder around the makeArgumentDispatcher/lowerMakeGroup machinery) — but the ACTUAL emission of @items inside Set's own count method body doesn't go through that dispatcher at all. It builds its own inline 2-branch case (is_record(This, 'Data.UnorderedSet', 2) -> Kex.Data:items(This); true -> kex_prelude:items(This)), which has no clause for Data.Set itself and falls through to the prelude's generic (list/tuple-oriented) items, misreading the whole record tuple as if it were the field's own value.

Impact

Any two types sharing a module X header (or more generally, coexisting in one compilation unit) where one has a field and the other a method of the same name will silently misread the field on BEAM. The tree-walk interpreter is unaffected — this only reproduces under -c/-R.

Affects spec/stdlib/data_set.spec.kex (33 of 59 cases) and spec/stdlib/data_units_precedence.spec.kex under -R; spec-stdlib (interpreter) passes cleanly for the same files.

Related: #229, #233 (the module-alias fix that exposed this).

🤖 Generated with Claude Code

https://claude.ai/code/session_01SbX7B795iGL8Tc66wngUxC

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0