Skip to content

Commit 424e264

Browse files
committed
CLJS-2230: Double checked arrays
Root cause were macros that called out into the analyzer to determine if further optimization was possible. These analysis calls are now done with c.a/no-warn. Added test case.
1 parent 797e247 commit 424e264

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

src/main/clojure/cljs/core.cljc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@
842842
([x & next]
843843
(core/let [forms (concat [x] next)]
844844
(if (every? #(simple-test-expr? &env %)
845-
(map #(cljs.analyzer/analyze &env %) forms))
845+
(map #(cljs.analyzer/no-warn (cljs.analyzer/analyze &env %)) forms))
846846
(core/let [and-str (core/->> (repeat (count forms) "(~{})")
847847
(interpose " && ")
848848
(apply core/str))]
@@ -860,7 +860,7 @@
860860
([x & next]
861861
(core/let [forms (concat [x] next)]
862862
(if (every? #(simple-test-expr? &env %)
863-
(map #(cljs.analyzer/analyze &env %) forms))
863+
(map #(cljs.analyzer/no-warn (cljs.analyzer/analyze &env %)) forms))
864864
(core/let [or-str (core/->> (repeat (count forms) "(~{})")
865865
(interpose " || ")
866866
(apply core/str))]
@@ -2477,7 +2477,7 @@
24772477
([]
24782478
'(.-EMPTY cljs.core/List))
24792479
([x & xs]
2480-
(if (= :constant (:op (cljs.analyzer/analyze &env x)))
2480+
(if (= :constant (:op (cljs.analyzer/no-warn (cljs.analyzer/analyze &env x))))
24812481
`(-conj (list ~@xs) ~x)
24822482
`(let [x# ~x]
24832483
(-conj (list ~@xs) x#)))))
@@ -2498,7 +2498,7 @@
24982498
([& kvs]
24992499
(core/let [keys (map first (partition 2 kvs))]
25002500
(if (core/and (every? #(= (:op %) :constant)
2501-
(map #(cljs.analyzer/analyze &env %) keys))
2501+
(map #(cljs.analyzer/no-warn (cljs.analyzer/analyze &env %)) keys))
25022502
(= (count (into #{} keys)) (count keys)))
25032503
`(cljs.core/PersistentArrayMap. nil ~(clojure.core// (count kvs) 2) (array ~@kvs) nil)
25042504
`(.createAsIfByAssoc cljs.core/PersistentArrayMap (array ~@kvs))))))
@@ -2518,7 +2518,7 @@
25182518
([& xs]
25192519
(if (core/and (core/<= (count xs) 8)
25202520
(every? #(= (:op %) :constant)
2521-
(map #(cljs.analyzer/analyze &env %) xs))
2521+
(map #(cljs.analyzer/no-warn (cljs.analyzer/analyze &env %)) xs))
25222522
(= (count (into #{} xs)) (count xs)))
25232523
`(cljs.core/PersistentHashSet. nil
25242524
(cljs.core/PersistentArrayMap. nil ~(count xs) (array ~@(interleave xs (repeat nil))) nil)

src/test/clojure/cljs/analyzer_tests.clj

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -745,39 +745,47 @@
745745

746746
(deftest test-cljs-2148
747747
(binding [ana/*checked-arrays* :warn]
748-
(let [ws (atom [])]
748+
(let [ws (atom [])]
749749
(try
750750
(a/with-warning-handlers [(collecting-warning-handler ws)]
751751
(e/with-compiler-env test-cenv
752752
(a/analyze (a/empty-env)
753753
'(aget (js-obj) "a"))))
754754
(catch Exception _))
755755
(is (= ["cljs.core/aget, arguments must be an array followed by numeric indices, got [object string] instead (consider goog.object/get for object access)"] @ws)))
756-
(let [ws (atom [])]
756+
(let [ws (atom [])]
757757
(try
758758
(a/with-warning-handlers [(collecting-warning-handler ws)]
759759
(e/with-compiler-env test-cenv
760760
(a/analyze (a/empty-env)
761761
'(aget (js-obj) "foo" "bar"))))
762762
(catch Exception _))
763763
(is (= ["cljs.core/aget, arguments must be an array followed by numeric indices, got [object string string] instead (consider goog.object/getValueByKeys for object access)"] @ws)))
764-
(let [ws (atom [])]
764+
(let [ws (atom [])]
765765
(try
766766
(a/with-warning-handlers [(collecting-warning-handler ws)]
767767
(e/with-compiler-env test-cenv
768768
(a/analyze (a/empty-env)
769769
'(aset (js-obj) "a" 2))))
770770
(catch Exception _))
771771
(is (= ["cljs.core/aset, arguments must be an array, followed by numeric indices, followed by a value, got [object string number] instead (consider goog.object/set for object access)"] @ws)))
772-
(let [ws (atom [])]
772+
(let [ws (atom [])]
773773
(try
774774
(a/with-warning-handlers [(collecting-warning-handler ws)]
775775
(e/with-compiler-env test-cenv
776776
(a/analyze (a/empty-env)
777777
'(let [^objects arr (into-array [1 2 3])]
778778
(aget arr 0)))))
779779
(catch Exception _))
780-
(is (empty? @ws)))))
780+
(is (empty? @ws)))
781+
(let [ws (atom [])]
782+
(try
783+
(a/with-warning-handlers [(collecting-warning-handler ws)]
784+
(e/with-compiler-env test-cenv
785+
(a/analyze (a/empty-env)
786+
'(and true (or (aget (js-obj "foo" 1) "foo") 2)))))
787+
(catch Exception _))
788+
(is (= 1 (count @ws))))))
781789

782790
(deftest test-cljs-2037
783791
(let [test-env (assoc-in (a/empty-env) [:ns :name] 'cljs.user)]

0 commit comments

Comments
 (0)