You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Field/method name collision across sibling types breaks field access on BEAM #234
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 @itemsinside 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).
Activity