Skip to content

Commit d4dae80

Browse files
authored
Merge pull request #38 from BuddhiLW/t300/cljw-nan-entry-walk
t300/cljw-nan-entry-walk
2 parents 3068163 + a3c1212 commit d4dae80

3 files changed

Lines changed: 59 additions & 13 deletions

File tree

‎src/lang/clj/clojure/core.clj‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,18 +1149,23 @@
11491149
;; is interned, so `identical?` detects "no impl" exactly.
11501150
(let* [r (cljw.internal/__kv-reduce-or m f init :clojure.core/kv-reduce-none)]
11511151
(if (identical? r :clojure.core/kv-reduce-none)
1152-
(reduce (fn* [acc k] (f acc k (get m k))) init (keys m))
1152+
;; Walk ENTRIES, never keys-then-get: a key that is not `=` to
1153+
;; itself (##NaN, or a list holding it) misses its own lookup, so
1154+
;; `(get m k)` would hand f nil instead of the stored value.
1155+
(reduce (fn* [acc e] (f acc (nth e 0) (nth e 1))) init m)
11531156
r)))))
11541157

11551158
;; `(update-keys m f)` — new map with `(f k)` for each key, same vals.
1159+
;; Entry walk, not keys-then-get, for the same NaN-key reason as reduce-kv.
11561160
(def update-keys
11571161
(fn* [m f]
1158-
(reduce (fn* [acc k] (assoc acc (f k) (get m k))) {} (keys m))))
1162+
(reduce (fn* [acc e] (assoc acc (f (nth e 0)) (nth e 1))) {} m)))
11591163

11601164
;; `(update-vals m f)` — new map with `(f v)` for each val, same keys.
1165+
;; Entry walk, not keys-then-get, for the same NaN-key reason as reduce-kv.
11611166
(def update-vals
11621167
(fn* [m f]
1163-
(reduce (fn* [acc k] (assoc acc k (f (get m k)))) {} (keys m))))
1168+
(reduce (fn* [acc e] (assoc acc (nth e 0) (f (nth e 1)))) {} m)))
11641169

11651170
;; `(seq-to-map-for-destructuring s)` — clojure 1.11. Builds a map from the rest
11661171
;; args of a `& {:keys […]}` call: kwargs pairs, a single trailing map, or a mix

‎src/runtime/print.zig‎

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -354,20 +354,42 @@ fn deepRealizeAt(rt: *Runtime, env: *env_mod.Env, v: Value, depth: i64) anyerror
354354
// no-op" — deepRealize returns the same Value when nothing was lazy.
355355
.array_map, .hash_map => {
356356
var out = v;
357-
var ks = try map_collection.keys(rt, v);
358-
while (ks.tag() == .list and list_collection.countOf(ks) > 0) : (ks = list_collection.rest(ks)) {
359-
const k = list_collection.first(ks);
360-
const val = try map_collection.get(v, k);
357+
// Walk ENTRIES, not keys-then-get: a key that is not `=` to itself
358+
// (a list holding ##NaN) misses its own lookup, so `get` returned
359+
// nil, the re-assoc below then ADDED a phantom `k nil` entry and
360+
// the map printed one entry too many.
361+
// A re-KEYED entry rebuilds the map from its realized entries
362+
// instead of dissoc+assoc: `dissoc` of a self-unequal key is a
363+
// no-op, which also left a phantom.
364+
var rekeyed = false;
365+
var es = try map_collection.seq(rt, v);
366+
while (es.tag() == .list and list_collection.countOf(es) > 0) : (es = list_collection.rest(es)) {
367+
const entry = list_collection.first(es);
368+
const k = map_entry_collection.keyOf(entry);
369+
const val = map_entry_collection.valOf(entry);
361370
const rk = try deepRealizeAt(rt, env, k, depth + 1);
362371
const rv = try deepRealizeAt(rt, env, val, depth + 1);
363372
if (@intFromEnum(rk) != @intFromEnum(k)) {
364-
out = try map_collection.dissoc(rt, out, k);
365-
out = try map_collection.assoc(rt, out, rk, rv);
373+
rekeyed = true;
374+
break;
366375
} else if (@intFromEnum(rv) != @intFromEnum(val)) {
367376
out = try map_collection.assoc(rt, out, k, rv);
368377
}
369378
}
370-
return out;
379+
if (!rekeyed) return out;
380+
var rebuilt = map_collection.empty();
381+
var rs = try map_collection.seq(rt, v);
382+
while (rs.tag() == .list and list_collection.countOf(rs) > 0) : (rs = list_collection.rest(rs)) {
383+
const entry = list_collection.first(rs);
384+
rebuilt = try map_collection.assoc(
385+
rt,
386+
rebuilt,
387+
try deepRealizeAt(rt, env, map_entry_collection.keyOf(entry), depth + 1),
388+
try deepRealizeAt(rt, env, map_entry_collection.valOf(entry), depth + 1),
389+
);
390+
}
391+
const m = map_collection.metaOf(v);
392+
return if (m.isNil()) rebuilt else try map_collection.withMeta(rt, rebuilt, m);
371393
},
372394
// Sorted variants ride the sorted module (comparator-aware assoc/
373395
// dissoc need env; a realized KEY is not re-keyed — the comparator
@@ -388,14 +410,22 @@ fn deepRealizeAt(rt: *Runtime, env: *env_mod.Env, v: Value, depth: i64) anyerror
388410
return out;
389411
},
390412
.hash_set => {
391-
var out = v;
413+
const out = v;
392414
var es = try set_collection.seq(rt, v);
393415
while (es.tag() == .list and list_collection.countOf(es) > 0) : (es = list_collection.rest(es)) {
394416
const e = list_collection.first(es);
395417
const re = try deepRealizeAt(rt, env, e, depth + 1);
396418
if (@intFromEnum(re) != @intFromEnum(e)) {
397-
out = try set_collection.disj(rt, out, e);
398-
out = try set_collection.conj(rt, out, re);
419+
// `disj` of a self-unequal element (a list holding
420+
// ##NaN) is a no-op, so disj+conj printed a phantom
421+
// second element. Rebuild from the realized elements.
422+
var rebuilt = set_collection.empty();
423+
var rs = try set_collection.seq(rt, v);
424+
while (rs.tag() == .list and list_collection.countOf(rs) > 0) : (rs = list_collection.rest(rs)) {
425+
rebuilt = try set_collection.conj(rt, rebuilt, try deepRealizeAt(rt, env, list_collection.first(rs), depth + 1));
426+
}
427+
const m = set_collection.metaOf(v);
428+
return if (m.isNil()) rebuilt else try set_collection.withMeta(rt, rebuilt, m);
399429
}
400430
}
401431
return out;

‎test/clj/suites/map_helpers_test.clj‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,14 @@
4242
(is (thrown? Throwable (keys 0)))
4343
(is (= [:a] (keys (list [:a 1]))))
4444
(is (= [1] (vals (list [:a 1])))))
45+
46+
;; A key that is not `=` to itself (##NaN, or a list holding it) misses its
47+
;; own lookup, so a keys-then-get walk lost its value and the print realize
48+
;; pass grew a phantom `k nil` entry. Verified against JVM clj 1.12.
49+
(deftest nan-key-entry-walk
50+
(is (= [1] (reduce-kv (fn* [acc k v] (conj acc v)) [] {##NaN 1})) "reduce_kv_nan_key")
51+
(is (= [2] (vec (vals (update-vals {##NaN 1} inc)))) "update_vals_nan_key")
52+
(is (= [1] (vec (vals (update-keys {##NaN 1} identity)))) "update_keys_nan_key")
53+
(is (= "{(##NaN) 1}" (pr-str {(list ##NaN) 1})) "print_list_nan_key_map")
54+
(is (= "#{(##NaN)}" (pr-str #{(list ##NaN)})) "print_list_nan_set")
55+
(is (= 2 (count (re-seq #"##NaN" (pr-str (hash-map (list ##NaN) 1 (list ##NaN) 2))))) "print_two_nan_keys"))

0 commit comments

Comments
 (0)