Skip to content

Commit f6bdfe1

Browse files
mfikesdnolen
authored andcommitted
CLJS-2989: Fast-path issues for predicate-induced inference based on satisfies?
1 parent 70a33f2 commit f6bdfe1

2 files changed

Lines changed: 64 additions & 14 deletions

File tree

src/main/clojure/cljs/analyzer.cljc

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,29 +1477,22 @@
14771477
cljs.core/delay? cljs.core/Delay
14781478
cljs.core/reduced? cljs.core/Reduced
14791479

1480+
;;; Note: For non-marker protocol entries below, we
1481+
;;; omit predicates that are based on satisfies? because
1482+
;;; we cannot safely apply the fast-path optimization
1483+
;;; which is enabled when the protocol type is inferred.
1484+
;;; If adding a non-marker entry here, also add a test to
1485+
;;; cljs.extend-to-native-test/test-extend-to-protocols.
1486+
14801487
;; Protocols
14811488
cljs.core/map-entry? cljs.core/IMapEntry
1482-
cljs.core/reversible? cljs.core/IReversible
14831489
cljs.core/uuid? cljs.core/IUUID
14841490
cljs.core/tagged-literal? cljs.core/ITaggedLiteral
1485-
cljs.core/iterable? cljs.core/IIterable
1486-
cljs.core/cloneable? cljs.core/ICloneable
14871491
cljs.core/inst? cljs.core/Inst
1488-
cljs.core/counted? cljs.core/ICounted
1489-
cljs.core/indexed? cljs.core/IIndexed
1490-
cljs.core/coll? cljs.core/ICollection
1491-
cljs.core/set? cljs.core/ISet
1492-
cljs.core/associative? cljs.core/IAssociative
1493-
cljs.core/ifind? cljs.core/IFind
14941492
cljs.core/sequential? cljs.core/ISequential
1495-
cljs.core/sorted? cljs.core/ISorted
1496-
cljs.core/reduceable cljs.core/IReduce
1497-
cljs.core/map? cljs.core/IMap
14981493
cljs.core/list? cljs.core/IList
14991494
cljs.core/record? cljs.core/IRecord
1500-
cljs.core/vector? cljs.core/IVector
15011495
cljs.core/chunked-seq? cljs.core/IChunkedSeq
1502-
cljs.core/ifn? cljs.core/IFn
15031496

15041497
;; Composites
15051498
cljs.core/seqable? #{cljs.core/ISeqable array string}

src/test/cljs/cljs/extend_to_native_test.cljs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
(is (= "#function[custom-print-cljs-2812]" (pr-str map)))
5454
;; Restore basic native types so that test summary output looks correct
5555
(extend-protocol IPrintWithWriter
56+
object
57+
(-pr-writer [obj writer _]
58+
(write-all writer (str obj)))
5659
boolean
5760
(-pr-writer [obj writer _]
5861
(write-all writer (str obj)))
@@ -70,3 +73,57 @@
7073
(let [empty-array (empty #js [1 2 3])]
7174
(is (and (array? empty-array)
7275
(empty? empty-array)))))
76+
77+
(defn test-map-entry [x] (when (map-entry? x) (-key x)))
78+
(defn test-coll [x] (when (coll? x) (-conj x 1)))
79+
(defn test-set [x] (when (set? x) (-disjoin x 1)))
80+
(defn test-associative [x] (when (associative? x) (-assoc x 1 2)))
81+
(defn test-find [x] (when (ifind? x) (-find x 1)))
82+
(defn test-sorted [x] (when (sorted? x) (-sorted-seq x true)))
83+
(defn test-map [x] (when (map? x) (-dissoc x 1)))
84+
(defn test-vector [x] (when (vector? x) (-assoc-n x 1 2)))
85+
(defn test-chunked-seq [x] (when (chunked-seq? x) (-chunked-first x)))
86+
(defn test-ifn [x] (when (ifn? x) (-invoke x)))
87+
(defn test-reversible [x] (when (reversible? x) (-rseq x)))
88+
(defn test-iterable [x] (when (iterable? x) (-iterator x)))
89+
(defn test-cloneable [x] (when (cloneable? x) (-clone x)))
90+
(defn test-counted [x] (when (counted? x) (-count x)))
91+
(defn test-indexed [x] (when (indexed? x) (-nth x 0)))
92+
(defn test-seqable [x] (when (seqable? x) (-seq x)))
93+
(defn test-reduceable [x] (when (reduceable? x) (-reduce x inc)))
94+
95+
(deftest test-extend-to-protocols
96+
(extend-type string IMapEntry (-key [_] :a))
97+
(is (nil? (test-map-entry "a")))
98+
(extend-type string ICollection (-conj [_ _] :b))
99+
(is (= :b (test-coll "a")))
100+
(extend-type string ISet (-disjoin [_ _] :c))
101+
(is (= :c (test-set "a")))
102+
(extend-type string IAssociative (-assoc [_ _ _] :d))
103+
(is (= :d (test-associative "a")))
104+
(extend-type string IFind (-find [_ _] :e))
105+
(is (= :e (test-find "a")))
106+
(extend-type string ISorted (-sorted-seq [_ _] :f))
107+
(is (= :f (test-sorted "a")))
108+
(extend-type string IMap (-dissoc [_ _] :g))
109+
(is (= :g (test-map "a")))
110+
(extend-type string IVector (-assoc-n [_ _ _] :h))
111+
(is (= :h (test-vector "a")))
112+
(extend-type string IChunkedSeq (-chunked-first [_] :i))
113+
(is (nil? (test-chunked-seq "a")))
114+
(extend-type string IFn (-invoke [_] :j))
115+
(is (= :j (test-ifn "a")))
116+
(extend-type string IReversible (-rseq [_] :k))
117+
(is (= :k (test-reversible "a")))
118+
(extend-type string IIterable (-iterator [_] :l))
119+
(is (= :l (test-iterable "a")))
120+
(extend-type string ICloneable (-clone [_] :m))
121+
(is (= :m (test-cloneable "a")))
122+
(extend-type string ICounted (-count [_] :n))
123+
(is (= :n (test-counted "a")))
124+
(extend-type string IIndexed (-nth [_] :o))
125+
(is (= :o (test-indexed "a")))
126+
(extend-type number ISeqable (-seq [_] :p))
127+
(is (= :p (test-seqable 1)))
128+
(extend-type string IReduce (-reduce [_ _] :q))
129+
(is (= :q (test-reduceable "a"))))

0 commit comments

Comments
 (0)