2026-11-26 15:37 UTC

Lean-assisted testing reports 17 Clojure core bugs

state: seedheat: mediumuncertainty: mediumrelevance: mediumlean-formal-verification clojure software-bugsNaoya YamashitaClojure

What is this?

Naoya Yamashita (conao3) reports testing Clojure 1.12.7, released November 18, 2026, with Lean models for small integer and bit-arithmetic functions, differential tests against Java collections, and manual reproduction checks. He says he reproduced 17 bugs not found in his searches of Clojure JIRA or ask.clojure.org. Examples include a `vector-of` reduction that loops on large inputs, a HAMT collision-node deletion that can make a remaining key inaccessible, and Java functional-interface behavior that changes with a type hint. These are the author’s findings; the supplied sources do not show maintainer confirmation, fixes, or independent reproduction.

Why it matters

This is a useful demonstration of how formal methods can complement conventional testing on mature software: Lean produced counterexamples for tractable arithmetic logic, while differential testing and manual checks helped investigate larger structures and interop. The article reports concrete, reproducible-looking defects, but does not establish their prevalence or downstream impact. The broader possibility is practical: combining proof tools with ordinary tests can expose edge cases in widely used libraries without formally verifying an entire system.

Current assessment

The originating article, published November 24, 2026, is by Naoya Yamashita; the Hacker News item is a pointer to it, not independent corroboration. Yamashita says he tested Clojure 1.12.7 on JDK 21 and reproduced 17 issues he could not locate in the named issue channels. His method has distinct limits: Lean modeled small functions reducible to integer and bit arithmetic; larger persistent collections were checked with differential tests; Java interop was compared with and without type hints, followed by manual reproduction. He reports no mismatch in 3 million HAMT operations and no vector mismatch at tested tree boundaries, so the findings do not imply those structures broadly fail. The most consequential examples described include nonterminating `reduce` on sufficiently large `vector-of` inputs after `drop`, a HAMT collision-node hash error that can hide a remaining key after particular `dissoc`/`assoc` operations, and inconsistent `removeIf` results with versus without a type hint. He also reports smaller collection, numeric-conversion, and interop edge cases. The author says he tested a patch for the HAMT example, but the sources provide no official issue acknowledgments, upstream fixes, independent validation, or evidence of real-world incidence. Track for maintainer response and fixes; treat the 17-count and “unreported” status as the author’s claims based on his searches.

Measured heat

now 0 pts/hpeak 39 pts/hcomments 0/hpeers p21momentum: steady2 platformsage 74h
points/hour across evidence · reading as of 2026-11-27 02:23:58.509736+11:00 · deterministic, not a model opinion

How the heat travelled

11-23 13:00⭐ origin echo-reconstructedThis is the originating article for the Clojure findings, not a repost of the linked Zenn article. It says: “I found 17 bugs that I could no
conao3 (Naoya Yamashita) on blog (echo) · attributed from hn.story.50642939
—
11-25 14:56first on hacker news · published · +50.0hI checked Clojure's core with Lean and found unreported bugs
encyclopedism
—
11-25 14:56amplified on hacker news 👑hn.story.50642939
encyclopedism
peak 220 · 78 comments · 100% of case engagement
11-25 17:21our radar first saw it · +52.4hdiscovery anchor: hn.story.50642939—
pace: p90 vs 2088 stories at the 72h mark (now 74h old) — ahead of axion-1-open-agent-model-release (1.0x), behind claude-pxl-pixel-art-dsl (1.0x)

Evidence (2) — ⭐ canonical anchor

sourceobjectauthorscorecomments
🟧 hnI checked Clojure's core with Lean and found unreported bugs
Retrieved article excerpt

Open article · Retrieved 2026-11-25T17:22:13.981648+00:00

# I checked Clojure's core with Lean and differential testing and found unreported bugs in HAMT and vector-of

2026-11-24[ポスト](https://x.com/intent/post?text=I%20checked%20Clojure%27s%20core%20with%20Lean%20and%20differential%20testing%20and%20found%20unreported%20bugs%20in%20HAMT%20and%20vector-of&url=https%3A%2F%2Fs.conao3.com%2Fblog%2F2026%2Fe4f733)

[.md](https://s.conao3.com/blog/2026/e4f733.md?preview=true)[.org](https://s.conao3.com/blog/2026/e4f733.org?preview=true)[GitHub](https://github.com/conao3/blog-stasis-src/blob/5a75e3b87b8fdf345f091e7adbb1414fcf23848f/contents.org?plain=1#L23227-L23460)

- [#clojure](https://s.conao3.com/tags/clojure.html)
- [#lean](https://s.conao3.com/tags/lean.html)
- [#formal-verification](https://s.conao3.com/tags/formal-verification.html)
- [#testing](https://s.conao3.com/tags/testing.html)
- [#differential-testing](https://s.conao3.com/tags/differential-testing.html)

I read Koukyosyumei's article "[著名なRust製OSSをLean 4で形式検証してみたら、めちゃめちゃバグが見つかった話](https://zenn.dev/koukyosyumei/articles/31c8fcf4da45b7)" (in Japanese; the title means "I formally verified well-known Rust OSS with Lean 4 and found a lot of bugs").
The author ported well-known Rust projects into a form they could prove in Lean 4, and where a proof failed, they found a bug in the original code.

Many people have used Clojure's persistent data structures since around 1.0, so I expected no bugs to remain.
Still, I wanted to see whether Lean would find something there too.

[hirosassa/lean-verify](https://github.com/hirosassa/lean-verify) describes a procedure for modeling a small function in Lean 4 and checking it for all inputs, so I applied it to the part of Clojure's core written in Java.

I modeled integer and bit arithmetic in Lean and searched for counterexamples.
I checked the collections with differential testing against the `java.util` collections, and for Java interop I compared the same call with and without a type hint.

I found 17 bugs that I could not find in Clojure's JIRA or on ask.clojure.org.
They include `reduce` on `vector-of`, the hash collision node of the HAMT, and calls to functional interfaces without a type hint.

The HAMT itself showed no mismatch in 3 million operations of differential testing, and the vector tree showed none around its boundaries.
The bugs sat in separate code outside the trees, and in places where two mechanisms meet, such as `=` and hashing.

I used Clojure 1.12.7, released on 2026-11-18, and JDK 21.
The links to the source point to the [clojure-1.12.7](https://github.com/clojure/clojure/tree/clojure-1.12.7) tag.

## How I checked

The lean-verify procedure picks one function and models it as a Lean definition.
You write a property that compares it with a naive specification computed at a wider width, and `bv_decide` searches for a counterexample.

If you model Java's `long` and `int` as `BitVec 64` and `BitVec 32`, the wraparound of addition and multiplication maps onto `BitVec` operations.
So do the rounding of `/` toward zero and the sign of `%`.

When Lean found a counterexample, I turned the value into a Clojure input and reproduced it in the original code.
I then modeled the fixed code in Lean and proved the same property.
Lean can only take small functions that reduce to integer and bit arithmetic, so I checked the large persistent data structures with differential testing.

Each test applies the same random sequence of operations to `PersistentHashMap` and to `java.util.HashMap` and compares the results.
For the HAMT, the keys mix partial collisions at each 5-bit level with fully identical hashes, and I ran 1,500 operations for each of 2,000 seeds.

For vectors, I compared against `ArrayList` around 32, 1056, 32800, and 1048608, where the tree gains a level.
The operations were `conj`, `pop`, `assoc`, and their transient versions.

I checked whether anyone had reported each bug before.
I searched Clojure's JIRA through its REST API and ask.clojure.org through web search and the site's own search, and found no matching report.

## reduce on vector-of never finishes

`vector-of` is a vector that stores its values in primitive arrays.
Clojure implements it in `gvec.clj`, written in Clojure and separate from `PersistentVector`.
If you `drop` from one and then `reduce` it, the call never returns once the vector has 65505 or more elements.

```
(reduce + (drop 1 (into (vector-of :long) (range 65504))))
;; => 2145354256
(reduce + (drop 1 (into (vector-of :long) (range 65505))))
;; => never returns
(reduce + (drop 1 (vec (range 65505))))
;; => 2145419760
```

The cause is [line 79 of gvec.clj](https://github.com/clojure/clojure/blob/clojure-1.12.7/src/clj/clojure/gvec.clj#L79), where the seq's `reduce` computes the start of the next 32-element chunk.

```
(recur result (bit-and 0xffe0 (+ aidx 32)))
```

The code uses `0xffe0` to clear the low 5 bits and round down to a multiple of 32, but the mask also clears bit 16 and everything above it.
With 65505 elements, the last chunk starts at index 65504.
After that chunk, the start of the next one should be 65536, but the mask turns it into 0, so `reduce` goes back to the start and reads the vector again and again.

[img](https://s.conao3.com/img/e4f733-mask.svg)

The `0xffe0` mask has stayed in place since the [2010 commit](https://github.com/clojure/clojure/commit/5b281880) that added `internal-reduce`.
In Lean, I wrote the property that from a position `a` that is a multiple of 32, the next chunk starts at `a + 32`.

```
import Std.Tactic.BVDecide

def stepImpl (a : BitVec 64) : BitVec 64 := 0xffe0 &&& (a + 32)
def stepFix (a : BitVec 64) : BitVec 64 := (~~~0x1f) &&& (a + 32)

theorem impl_ok (a : BitVec 64) (h32 : a &&& 0x1f = 0) (hlt : a < 0x7fffffe0) :
    stepImpl a = a + 32 := by
  unfold stepImpl; bv_decide
theorem fix_ok (a : BitVec 64) (h32 : a &&& 0x1f = 0) (hlt : a < 0x7fffffe0) :
    stepFix a = a + 32 := by
  unfold stepFix; bv_decide
```

`impl_ok` fails with the counterexample `a = 1073741792`, and `#eval stepImpl 65504` returns 0 as well.
`fix_ok` uses `(bit-not 0x1f)` as the mask, and its proof goes through under the assumption that `a` is a multiple of 32 below `0x7fffffe0`.

## dissoc on a HAMT collision node hides another key

Clojure's hash-map is a HAMT: it splits the key's hash into 5-bit pieces and uses them to walk down the tree.
Keys whose 32-bit hashes are identical share a `HashCollisionNode` at the bottom of the tree, and the node finds a key among them with `=`.

[HashCollisionNode.without](https://github.com/clojure/clojure/blob/clojure-1.12.7/src/jvm/clojure/lang/PersistentHashMap.java#L964-L971) builds the new node with one key removed.
It should give the new node its own hash, `this.hash`, but it passes the parameter `hash` instead.

```
return new HashCollisionNode(null, hash, count - 1, removePair(array, idx/2));
```

The parameter `hash` is the hash of the key you pass to `dissoc`.
If you `dissoc` with a key that is `=` to the stored key but has a different hash, the node records the wrong hash.

In Clojure, a vector is `=` to a Java `ArrayList` with the same elements, but the two hash differently.
[CLJ-1372](https://clojure.atlassian.net/browse/CLJ-1372) tracks this mismatch.

In the example below, I chose `a` and `b` so that both `ArrayList` keys hash to 1003.

```
(def a (java.util.ArrayList. [0 42]))
(def b (java.util.ArrayList. [1 11]))
[(hash a) (hash b) (hash [0 42]) (= a [0 42])]
;; => [1003 1003 1867155179 true]

(def m3 (-> (hash-map a :A b :B)
            (dissoc [0 42])
            (assoc a :A2)))
[m3 (count m3) (get m3 b)]
;; => [{[1 11] :B, [0 42] :A2} 2 nil]
```

When you print `m3`, you can see the key `b`, but `(get m3 b)` returns `nil`.
After the `dissoc`, the node records 1867155179, the hash of `[0 42]`.
The next `assoc` then concludes that `a`, with hash 1003, does not collide with the node, and splits the two into separate branches.

A lookup of `b` follows the branch for 1003 and finds only `a` there, so it never reaches `b`.
In this example the collision node sits on the first level of the tree, so the key passed to `dissoc` needs the same low 5 bits as 1003, and 1867155179 has them.

[img](https://s.conao3.com/img/e4f733-hamt.svg)

`disj` on a hash-set has the same bug, while the path through the transient `dissoc!` does not.

I changed that line in the 1.12.7 `PersistentHashMap.java` to `this.hash` and compiled it.
With the patched class ahead of the jar on the classpath, the same example returned `:B` for `(get m3 b)`.

## removeIf gives different results with and without a type hint

Since Clojure 1.12, you can pass a Clojure function directly where a Java method takes a functional interface.
[CLJ-2864](https://clojure.atlassian.net/browse/CLJ-2864) removed the logic that read the function's return value as a Clojure truthy value.

> In prior alphas, the FI adapter returns inserted Clojure truthy return logic when adapting a Clojure function to an FI with boolean return.

The compiler follows this for calls where it knows the type.
A call without a type hint goes through reflection and uses [Reflector.coerceAdapterReturn](https://github.com/clojure/clojure/blob/clojure-1.12.7/src/jvm/clojure/lang/Reflector.java#L579-L591).
That method still applies `RT.booleanCast` to a `boolean` return value, so the same `.removeIf` call gives a different result depending on the type hint.

```
(let [l (java.util.ArrayList. [1 2 3])]
  (.removeIf l #{1 2}) (vec l))
;; => ClassCastException
(let [l (identity (java.util.ArrayList. [1 2 3]))]
  (.removeIf l #{1 2}) (vec l))
;; => [3]
```

Wrapping the list in `identity` hides the type of `l`, so the call goes through reflection.

A set used as a function returns the element itself.
In the call with a known type, the adapter cannot turn the `Long` into a `Boolean` and throws.
The reflective call reads the return value as true and removes the elements.

[img](https://s.conao3.com/img/e4f733-removeif.svg)

## int overflow and a negative subvec index

Clojure 1.12 added `IDrop` implementations that make `drop` faster.
The ones for string seqs and vector seqs add the current position and `n` as ints and then compare the sum with the length.
[StringSeq.drop](https://github.com/clojure/clojure/blob/clojure-1.12.7/src/jvm/clojure/lang/StringSeq.java#L61-L68) for string seqs is one of them.

```
(drop Integer/MAX_VALUE (next (seq "abc")))
;; => StringIndexOutOfBoundsException Index -2147483648
(drop Integer/MAX_VALUE (seq "abc"))
;; => ()
```

The Lean model gave the counterexample `i = 2147483646` and `n = 2147483647`.
I changed the check to compare `n` with the remaining length `s.length() - i`.
Under the assumptions `0 <` i < s.length()= and `0 < n`, Lean proved that the fixed check matches the naive specification for every 32-bit input.

[img](https://s.conao3.com/img/e4f733-drop.svg)

For `assoc` on the result of `subvec`, [SubVector.assocN](https://github.com/clojure/clojure/blob/clojure-1.12.7/src/jvm/clojure/lang/APersistentVector.java#L661-L667) does not reject a negative index.

```
(assoc [2 3] -1 :x)
;; => IndexOutOfBoundsException
(let [s (assoc (subvec [0 1 2 3 4] 2 4) -1 :x)]
  [s (.-v s)])
;; => [[2 3] [0 :x 2 3 4]]
```

The call overwrites an element of the underlying vector outside the subvec, and returns a subvec that looks unchanged.
[CLJ-962](https://clojure.atlassian.net/browse/CLJ-962) fixed the same bug in `nth`, but `assoc` still has it.

[img](https://s.conao3.com/img/e4f733-subvec.svg)

## Other findings

The other 12 bugs have a smaller impact, and I reproduced each of them on Clojure 1.12.7.

| Behavior | Example |
| --- | --- |
| `reduce-kv` throws on `vector-of` | `(reduce-kv f [] (vector-of :long 5 6))` |
| With a functional interface of three or more arguments, a call with a known type truncates an `int` return value without an error | `(fn [a b c] 4294967297)` returns 1, and `(fn [a b] 4294967297)` throws `ArithmeticException` |
| `next` on a seq of a string, a
encyclopedism22078
🟧 echo.blog ⭐This is the originating article for the Clojure findings, not a repost of the linked Zenn article. It says: “I found 17 bugs that I could noconao3 (Naoya Yamashita)——

Interpretation history

Decision trace