Skip to content

Fix Clojure protocols on records (a #27 regression), and an unloaded ClassCastException - #36

Merged
atgreen merged 2 commits into
masterfrom
loader-sibling-refs
Oct 21, 2026
Merged

atgreen merged 2 commits into
masterfrom
loader-sibling-refs

Conversation

@atgreen

@atgreen atgreen commented Oct 21, 2026

Copy link
Copy Markdown
Owner

Two bugs found while building a Clojure warm image for #4.

1. A class implementing an interface from a sibling loader (regression from #27)

Clojure makes a DynamicClassLoader per evaluation, so a defprotocol's interface and a defrecord implementing it come from sibling loaders. On master:

(defprotocol Area (area [s]))
(defrecord Sq [w] Area (area [_] (* w w)))
(area (->Sq 5))                ;; JVM: 25   master: No implementation of method: :area ... for class: user.Sq
(instance? user.Area (->Sq 5)) ;; JVM: true master: false

emit-<class> names a class's superclass and interfaces with class-symbol-for-reference, which only searched the defining loader's parent chain. The interface's loader isn't in that chain, so the lookup missed, interned a fresh symbol in the new class's package, and the find-class filter then dropped the interface from the class's supers. Before #27 all user loaders shared one package, so the fresh symbol happened to be the interface's own; this came in when each loader got its own package.

Fix: class-symbol-for-reference falls back to %resolve-class, which asks the defining loader's loadClass (Clojure's DynamicClassLoader finds the interface in its class cache), and names the class in its own package.

2. ClassCastException before anything has loaded it

static Integer f(Object o) { return (Integer) o; }
... try { f("x"); } catch (RuntimeException e) { ... }

On master this failed with There is no class named OPENLDK::|java/lang/ClassCastException|: %make-throwable instantiated the exception class without loading it. A catch (ClassCastException e) loads it, which is why this rarely showed. Fix: %make-throwable loads the class if needed.

Tests

  • New aaa/crossloader.java: an interface from one loader, implemented by a class a sibling loader defines, whose loadClass asks the first. Checks isInstance, isAssignableFrom, and a cast to the interface. JVM OK; master crashes (on bug 2, after bug 1's failed cast); this branch OK.
  • New aaa/castfail.java. JVM OK; master fails as above; this branch OK.
  • Clojure cold, with the protocol forms above: 25, true, matching the JVM.
  • siblingloaders, childfirst and lazyloader still pass.

make check-regression runs in CI.

🤖 Generated with Claude Code

atgreen and others added 2 commits October 21, 2026 05:48
emit-<class> names a class's superclass and interfaces with
class-symbol-for-reference, which only searched the defining loader's
parent chain.  A class from a loader outside that chain -- Clojure makes
a DynamicClassLoader per evaluation, so a defprotocol's interface and a
defrecord implementing it come from sibling loaders -- was missed, a
fresh symbol was interned in the new class's package, and the interface
was then dropped from its supers.  The record was no instance of the
protocol's interface: (instance? user.Area (->Sq 5)) was false and
calling the protocol method failed.  This came in with #27, when user
loaders stopped sharing a package.

class-symbol-for-reference now falls back to %resolve-class, which asks
the defining loader's loadClass, and names the class in its own package.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A ClassCastException from a checkcast in a method with no handler for it,
caught further up as RuntimeException, failed with "There is no class
named OPENLDK::|java/lang/ClassCastException|" when nothing had loaded
that class yet.  %make-throwable now loads the class first.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@atgreen
atgreen merged commit 452dc60 into master Oct 21, 2026
2 checks passed
@atgreen
atgreen deleted the loader-sibling-refs branch October 21, 2026 10:04
@atgreen atgreen mentioned this pull request Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant