Repository navigation
Description
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.
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, raisedruntime 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 annotatedNet.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.methodwasUndefined methodmethodforMetro.Request.Request``. - A type alias in a third module (
type Incoming = Request<Binary>, importing onlyNet.HTTP's) did not help: the alias was expanded and resolved again at the use site. - Compiled, the program then worked. Under
tey testit did not: a spec withusing Metro.Request, only: [Request]gotUndefined methodcodeforRequest``, and method calls on the record dispatched to nothing (a router'srequest.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(alsohead/1,options/1,patch/2,post/2,put/2) —Headers.empty/Binary.emptyinsideNet.HTTPno longer resolved. - A method
let text -> Stringon a record:Kex.Metro.Connection: undefined function text/5 in field/2, wheretext/5is Harmat's column builder, in a module that does not import Harmat. - A method
let route -> Stringon a record madecontext.route— a field ofNet.HTTP.Context— resolve to the method, and the closure's parameter was then inferred as the record. - A function
target(incoming)in a module madeincoming.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.flagin a.kettemplate resolved toOptionParser.flag:error: `flag` expects 4 argument(s), got 1.robot.belongingin a template stopped compiling when an unrelated module gained a functionbelonging/3.opening.idin a block over[Message]resolved to a schema module'sidcolumn: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.showthroughusing …, as: Chat, type-check asWebRequest -> WebRequestand raise when called:runtime error: Undefined method: show for ….using …, only: [show]and~showworks. (~Things.indexfor a module declared in the same file also works.)- A two-argument reference,
~joinedforjoined(request, socket), raisedUndefined method: joined for …even when imported by name.
What would have helped
- Resolve a name against the module's own declarations and its
usinglines 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 methodthat names the module and function it looked for.
Activity