Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
CLJ-2094 - Eagerly evaluated protocols should refer to latest versions
By referencing the `:var` on protocols when passed to protocol functions, any
protocols that are eagerly evaluated (in functions such as `partial`) will
properly use the most up-to-date set of `:impls` on the protocol instead of
those that exist at partial-application time.
  • Loading branch information
NoahTheDuke committed Aug 9, 2023
commit 73b02bce783f5a63cbbbedbdd26560e5a7a22d31
10 changes: 8 additions & 2 deletions src/clj/clojure/core_deftype.clj
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,16 @@
([^Class a ^Class b]
(if (.isAssignableFrom a b) b a)))

(defn- latest-p
"Get the latest version of the protocol before use."
[protocol]
(deref (:var protocol)))

(defn find-protocol-impl [protocol x]
(if (instance? (:on-interface protocol) x)
x
(let [c (class x)
protocol (latest-p protocol)
impl #(get (:impls protocol) %)]
(or (impl c)
(and c (or (first (remove nil? (map impl (butlast (super-chain c)))))
Expand All @@ -559,13 +565,13 @@
{:added "1.2"}
[protocol atype]
(boolean (or (implements? protocol atype)
(get (:impls protocol) atype))))
(get (:impls (latest-p protocol)) atype))))

(defn extenders
"Returns a collection of the types explicitly extending protocol"
{:added "1.2"}
[protocol]
(keys (:impls protocol)))
(keys (:impls (latest-p protocol))))

(defn satisfies?
"Returns true if x satisfies the protocol"
Expand Down
27 changes: 27 additions & 0 deletions test/clojure/test_clojure/protocols.clj
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,33 @@
(reify LongsHintedProto
(longs-hinted [_] (long-array [1])))))))

;; CLJ-2094 - Eagerly evaluated protocols should refer to latest versions

(require '[clojure.spec.alpha :as s])

(defprotocol CLJ-2094
(execute [_]))

(def partial-extends-clj-2094 (partial extends? CLJ-2094))
(def partial-extenders-clj-2094 (partial extenders CLJ-2094))
(def partial-find-protocol-method-clj-204
(partial find-protocol-method CLJ-2094 :execute))
(def partial-satisfies-clj-2094 (partial satisfies? CLJ-2094))
(s/def ::clj-2094 (partial satisfies? CLJ-2094))

(defrecord Foo-2094 [])

(extend-type Foo-2094
CLJ-2094
(execute [_]))

(deftest test-satisfies-uses-latest
(is (partial-extends-clj-2094 Foo-2094))
(is (= [Foo-2094] (partial-extenders-clj-2094)))
(is (partial-find-protocol-method-clj-204 (->Foo-2094)))
(is (partial-satisfies-clj-2094 (->Foo-2094)))
(is (s/valid? ::clj-2094 (->Foo-2094))))

;; CLJ-1180 - resolve type hints in protocol methods

(import 'clojure.lang.ISeq)
Expand Down