8000 Name resolution: names in unrelated modules collide (records, methods, fields, function references) · Issue #476 · kexhq/kex · GitHub
[go: up one dir, main page]

Skip to content

Name resolution: names in unrelated modules collide (records, methods, fields, function references) #476

Open

Description

@akoskovacs

Building a router in Metro and moving an application (Slick) onto it, almost every hour lost went to names in one module colliding with names in an unrelated one. Each case below type-checked or parsed, and most failed only when run. They look like one family: a bare or even a qualified name is resolved by its last segment against everything linked into the program, not against what the module imported.

Related: #448, #451, #475.

1. Two records with the same name, in different modules

Metro.Connection.Connection (new) and Net.HTTP.WebSocket.Connection.

  • Compiled: socket.send(Text(...)) on the WebSocket connection, in a module that imports only the WebSocket one, raised runtime error: Undefined method: send for Tuple. Renaming the new record made it work again; nothing else changed.

Metro.Request.Request and Net.HTTP.Request.

  • Inside Metro.Request, a field annotated Net.HTTP.Request<Binary> — fully qualified — was resolved to the local record: error: `Request.incoming` expects Metro.Request.Request<Binary>, but got Net.HTTP.Request<Binary>, and @incoming.method was Undefined method methodforMetro.Request.Request``.
  • A type alias in a third module (type Incoming = Request<Binary>, importing only Net.HTTP's) did not help: the alias was expanded and resolved again at the use site.
  • Compiled, the program then worked. Under tey test it did not: a spec with using Metro.Request, only: [Request] got Undefined method codeforRequest``, and method calls on the record dispatched to nothing (a router's request.verb == … was never true).

2. A method name breaks other modules that use the name

  • A method let empty(status: Integer) on a record broke the standard library: Kex.Net.HTTP.HTTP: undefined function empty/0 in delete/1 (also head/1, options/1, patch/2, post/2, put/2) — Headers.empty / Binary.empty inside Net.HTTP no longer resolved.
  • A method let text -> String on a record: Kex.Metro.Connection: undefined function text/5 in field/2, where text/5 is Harmat's column builder, in a module that does not import Harmat.
  • A method let route -> String on a record made context.route — a field of Net.HTTP.Context — resolve to the method, and the closure's parameter was then inferred as the record.
  • A function target(incoming) in a module made incoming.target.string (a record field) resolve to the function: error: `string` expects argument 1 to be Atom, but got String.

3. Field access in templates and blocks

  • entry.flag in a .ket template resolved to OptionParser.flag: error: `flag` expects 4 argument(s), got 1.
  • robot.belonging in a template stopped compiling when an unrelated module gained a function belonging/3.
  • opening.id in a block over [Message] resolved to a schema module's id column: expects argument 2 to be String, but got TextColumn.

4. A function with the name and arity of a method called in the same module

foul founded(database, user, tab, title) beside a call ConversationRepository { db: database }.founded(organizationId, title, by): the method call was compiled without its last (context) argument — 'founded'/5 exported, called with 4 — and failed as :undef at run time. The same through a variable (conversations.founded(…)).

Also: IR lower: `served/2` names both a free function and a receiver method in this compilation unit — rename one of them for a function in the application's main.kex and a method of a record in a dependency.

5. Function references

  • ~App.Controllers.Chat.show, and ~Chat.show through using …, as: Chat, type-check as WebRequest -> WebRequest and raise when called: runtime error: Undefined method: show for …. using …, only: [show] and ~show works. (~Things.index for a module declared in the same file also works.)
  • A two-argument reference, ~joined for joined(request, socket), raised Undefined method: joined for … even when imported by name.

What would have helped

  • Resolve a name against the module's own declarations and its using lines first, and only those; a qualified name never against anything but what it names.
  • Dispatch methods on the record's qualified name.
  • When two candidates exist, say so at compile time instead of picking one.
  • A run-time undef / Undefined method that names the module and function it looked for.

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