8000 Two records in one module sharing a field name can corrupt the other's accessor at runtime · Issue #348 · kexhq/kex · GitHub
[go: up one dir, main page]

Skip to content

Two records in one module sharing a field name can corrupt the other's accessor at runtime #348

Closed as not planned

Description

@akoskovacs

Two records declared in the same module with an identical field name corrupted each other's accessor at runtime in a real program. kex -C reported nothing. I could not reduce it to a small always-reproducing case — the simplest 2-record version I tried actually works fine, so this needs a real reporter to help narrow what extra ingredient triggers it — but I want it on record since it cost real debugging time and the workaround (rename the fields) was non-obvious.

What happened

Building kexhq/rodolfo's router (a scope-tree-flattening step: docs/kex-issues.md#18), two records in one module shared field names:

record Definition do
  method : String
  path : String
  handler : Handler
end

record Flat do
  method : String   # same names as Definition's
  path : String
  plugs : [Plug]
  handler : Handler
end

Constructing a Flat from within a flatMap over a recursive function, then reading .method/.path back on it inside a different function further down the call chain, returned either:

  • Undefined method: flatMethod for Tuple — as if the value were an untagged tuple missing its record tag, or
  • a bare BEAM :badarg

depending on the exact shape of the surrounding code (I changed a few things while narrowing it down and got both symptoms at different points). Renaming Flat's fields to be unique (flatMethod, flatPath, ...), and later replacing the whole record with an equivalent plain tuple, fixed it immediately both times.

A minimal 2-record version with the same field-name collision, tried afterward, does not reproduce — it prints correctly. So something about the real case (recursion, a flatMap/match-arm construction site, or the accessor being read from a different function than the one that built the record) is a necessary ingredient I haven't isolated.

A likely-related sibling bug: a top-level function and a record field sharing a name (let plug(...) = ... alongside record PlugEntry do plug : Plug end, in the same module) crashed erlc outright at compile time with {key_exists,{b_local,{b_literal,plug},1}} from beam_ssa_throw — same "name collision across declarations in one module" territory as this, just caught earlier and more loudly.

Expected

Either a compile-time diagnostic for the field-name collision, or — since two records legitimately sharing a field name is normal, common Kex style — accessors that don't collide regardless of how the record is constructed or where it's later read.

Environment

kex 0.4.0-beta.3, 293a0b5 — both the locally built dev toolchain and a clean rebuild of origin/main at the same commit. Full write-up with the exact code: docs/kex-issues.md#18 in kexhq/rodolfo.

Activity

  1. added this to the 0.4.0 milestone on Sep 15, 2026
  2. akoskovacs commented on Sep 16, 2026

    @akoskovacs
    CollaboratorAuthor

    Spent a good amount of time trying to reproduce this — against the current fix-ufcs-typeof branch (past cc5c195) and directly against 293a0b5 (the commit this was originally reported on) — and couldn't get the corruption to show up.

    Tried, in increasing fidelity:

    • The 2-record minimal snippet quoted in this issue — doesn't reproduce (as the issue itself already notes).
    • Synthetic variants with recursion, flatMap over a mutually-recursive ADT, cross-function field reads, and the shared field name placed at a different tuple position in each record (specifically to probe a blind first-match fallback I found in src/ir/lower.cxx's field-accessor codegen). All read correctly.
    • A faithful reconstruction of the actual rodolfo router-flattening code that reportedly triggered this: reverted src/rodolfo.kex's Flat from its current tuple workaround back to a record Flat sharing method/path (and handler, at a different tuple position than Definition's) with Definition, then built a real router through Rodolfo's own DSL (get/scope, nested scopes, the Entry union), compiled it, started it as a live BEAM Net.HTTP.Server, and curl'd every route. Correct method/path/body on every route, on both the tree-walk interpreter and BEAM, at both commits.

    While reading the accessor codegen (addFieldAccessor/fieldAccess/fieldAccessIn/localFieldAccessor in src/ir/lower.cxx) I found two spots that look like the right shape of risk for this bug class: fieldAccess() blindly takes the first registered tuple position for a field name rather than the receiver's actual record (only reachable from untyped record-pattern destructuring), and localFieldAccessor discards an already-correctly-resolved call target whenever a method name collides with any field name anywhere in the program. Neither triggered in any of my attempts, but they're worth keeping in mind if a reliable repro turns up.

    Given it doesn't reproduce even against a reconstruction of the real triggering code at the exact reported commit, whatever the "necessary ingredient" is, it's something more specific than recursion + flatMap + a cross-function read + mismatched field positions — all of which are present in my reconstruction. Leaving this for whoever can pin down the missing piece; happy to try more variants if there's a narrower lead.

  3. added a commit that references this issue on Sep 19, 2026
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

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0