Skip to content

Commit f826c31

Browse files
anmonteirodnolen
authored andcommitted
CLJS-1817: Strange result when assoc'ing 0 to persistent hash map
there was a bug in the port of Clojure's PersistentHashMap. When looking up a node in a BitmapIndexedNode, we need to increment the found index to compare the value. The relevant line in Clojure's codebase is: https://github.com/clojure/clojure/blob/e547d/src/jvm/clojure/lang/PersistentHashMap.java#L940
1 parent 6da4d77 commit f826c31

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6888,7 +6888,7 @@ reduces them without incurring seq initialization"
68886888
(aset new-arr (inc len) val)
68896889
(set! (.-val added-leaf?) true)
68906890
(HashCollisionNode. nil collision-hash (inc cnt) new-arr))
6891-
(if (= (aget arr idx) val)
6891+
(if (= (aget arr (inc idx)) val)
68926892
inode
68936893
(HashCollisionNode. nil collision-hash cnt (clone-and-set arr (inc idx) val)))))
68946894
(.inode-assoc (BitmapIndexedNode. nil (bitpos collision-hash shift) (array nil inode))

src/test/cljs/cljs/hash_map_test.cljs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,11 @@
2929
(-> (hash-map :a 1 :b 2 :c 3 :d 4 nil 5)
3030
(-iterator)
3131
(iter->set)))))
32+
33+
(deftest test-cljs-1817
34+
(let [cljscore-hash hash]
35+
(with-redefs [hash (fn [x]
36+
(if (or (#{:a :b} x) 0)
37+
cljscore-hash))]
38+
(let [x (hash-map :a :a :b -1)]
39+
(is (= (assoc x :b :b) {:a :a :b :b}))))))

0 commit comments

Comments
 (0)