8000 BEAM: nested module call mis-lowers as UFCS when a make method shares the name · Issue #182 · kexhq/kex · GitHub
[go: up one dir, main page]

Skip to content

BEAM: nested module call mis-lowers as UFCS when a make method shares the name #182

Closed

Description

@akoskovacs

A call to a nested module's function is mis-lowered on BEAM when any make block in the unit defines a method with the same name. The walker is correct; BEAM evaluates the module path as a value.

module Outer do
  module Inner do
    foul greet(name: String) -> String do
      return "hi " + name
    end
  end
end

record Loud do end
make Loud do
  foul greet(name: String) -> String = "HI " + name
end

foul use(n: String) -> String = Outer.Inner.greet(n)

main do
  IO.printLine(use("x"))
end
walker: hi x
BEAM:   runtime error: Undefined function: Outer.Inner

No capabilities are involved. Remove the make block and BEAM is correct again, so the trigger is purely the name collision.

Cause

The emitted Core shows the module path being evaluated as a value and the method applied UFCS-style to the result:

'use'/2 = fun (N, _) ->
  let _ir_T4 = call 'erlang':'error'(<<"... Undefined function: Outer.Inner">>)
  in apply 'greet'/2(_ir_T4, N)

Once greet is a known receiver method, Outer.Inner.greet(n) resolves through the receiver/UFCS machinery instead of the module path, and the dotted receiver Outer.Inner is then lowered as an ordinary expression — which fails, because it is a module, not a value.

A bare receiver survives this: Outer.greet(n) resolves as a module path before UFCS gets a chance. Only a dotted receiver loses.

Why it matters beyond this repro

It is the blocker for nested capabilities in #143/#181. A capability always has both a module member and a same-named make method implementing it, so FS.File.read(path) collides by construction — and FS.File and FS.Directory are exactly the shapes the stdlib conversion needs.

Expected

A dotted path naming a module in the unit should resolve as a module-qualified call regardless of whether a make-block method shares the function's name, matching the walker.

Activity

  1. added this to the 0.4.0 milestone on Aug 22, 2026
  2. akoskovacs commented on Aug 22, 2026

    @akoskovacs
    CollaboratorAuthor

    Fixed. A dotted path naming a module of the compilation unit now wins over the resolved receiver method, so the call reaches the module instead of being lowered as a value.

    The resolver picks the make-block method once one shares the name, and lowering follows that target faithfully — the dotted receiver is then lowered as an ordinary expression, which a module is not. There was already precedent for overriding a resolved target in exactly this way (localTypeShadows, localFieldAccessor), so the fix sits beside those: if modulePath yields two or more segments and moduleFunctions holds <path>.<method>, the module path owns the call.

    Deliberately limited to dotted paths of two or more segments. A bare receiver never had the problem — it resolves as a module path before UFCS gets a chance — so nothing about single-segment behaviour changes.

    Spec: spec/nested_module_method_collision.kex, running on both backends, and covering the reverse direction too (the make method must still reach its own receiver).

    This also unblocked nested capabilities on BEAM (#181): spec/capabilities_nested.kex came off # kex: skip-beam. spec-beam went 259 → 261.

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

    BEAMBugSomething isn't workingConfirmed IssueBug is reproduced on the current development versionModules

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0