Fix cross-namespace defmethod: Unknown symbol on compile#377
Open
dankinsoid wants to merge 1 commit into
Open
Conversation
…contribution names
expand-method-impl rebuilt the multimethod dispatch table from
(keys contributions) — the bare synthetic class names. A bare name only
resolves when the defmethod lives in the same namespace as its defmulti;
for a cross-namespace defmethod, compiling the multimethod's namespace
fails with:
Unknown symbol: multi$SLASH_kind... (no source location)
emit-contribute* already stores the relocatable ($lib:-qualified) class
name as the contribution value (relocatable-class-name), keyed by
contributing namespace — the same resolution mechanism protocol
extensions use. Build the table from those values instead.
Minimal repro (fails before, compiles and dispatches correctly after —
same-namespace methods keep working, their relocatable names resolve
locally too):
;; repro/multi.cljc
(ns repro.multi)
(defmulti kind :type)
(defmethod kind :same-ns [_] "same-ns")
;; repro/ext.cljc
(ns repro.ext (:require [repro.multi :as multi]))
(defmethod multi/kind :cross-ns [_] "cross-ns")
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
Any
defmethodextending a multimethoddefmulti'd in a different namespace fails to compile:Reproduced on current master (178e4fb) with a fresh (uncached) compile of a plain
:kind :dartproject. Note that a warm.clojuredartcache can mask the failure, which may be why this survives day-to-day incremental use.Root cause
expand-method-implrebuilds the dispatch table from(keys contributions)— the bare synthetic method-class names.emit-contribute*keys contributions by that bare name and stores the relocatable ($lib:-qualified, viarelocatable-class-name) name as the value, keyed by contributing namespace:contributions ; {bare-cname {contributing-ns relocatable-cname}}A bare name only resolves while compiling the namespace that defined the method class. For a same-namespace
defmethodthat happens to be the multimethod's own namespace, so it works; for a cross-namespace one, the multimethod's namespace can't resolve it. The emitted table makes this visible — the same-ns entry is resolved, the cross-ns one isn't:Protocol extensions already handle this correctly by reading the relocatable names out of the contribution values (
(vals exts)in theextensionsexpansion).Fix
Build the table from the contribution values instead:
After the fix the repro compiles and both dispatches work at runtime (
same-ns/cross-nsboth print) — same-namespace methods keep working because relocatable names resolve locally too, same as for protocols.