Skip to content

Commit 7084358

Browse files
committed
Limit core rewrites to core namespace
Apply portable core definition replacements only while rewriting the clojure.core namespace. Prevent user functions with names such as read from being replaced by unrelated core compatibility implementations. Add focused rewrite coverage for a non-core read function.
1 parent 3a68369 commit 7084358

4 files changed

Lines changed: 38 additions & 3 deletions

File tree

‎Makefile‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,21 @@ vet: $(GO)
217217
go vet ./...
218218

219219
# vet is disabled until we fix errors in generated code
220-
test: test-aot-runtime test-aot-codegen test-glj test-repl-wasm test-repl-bsd # vet
220+
test: \
221+
test-rewrite-core \
222+
test-aot-runtime \
223+
test-aot-codegen \
224+
test-glj \
225+
test-repl-wasm \
226+
test-repl-bsd # vet
221227
($(MAKE) test-suite v=1 || $(MAKE) test-suite v=1) || $(MAKE) test-suite v=1
222228

229+
test-rewrite-core: $(CLOJURE)
230+
HOME="$(LOCAL-HOME)" CLJ="$(CLOJURE)" \
231+
scripts/rewrite-core/run.sh \
232+
scripts/rewrite-core/test/non-core.clj | \
233+
cmp - scripts/rewrite-core/test/non-core.glj
234+
223235
TEST-COMPARE-LOAD := $(shell \
224236
processors=$$(getconf _NPROCESSORS_ONLN 2>/dev/null || echo 2); \
225237
awk -v processors="$$processors" \

‎scripts/rewrite-core/rewrite.clj‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55

66
(def zloc (z/of-string (slurp (first *command-line-args*))))
77

8+
(defn source-namespace [zloc]
9+
(loop [zloc zloc]
10+
(if (z/end? zloc)
11+
nil
12+
(let [form (when (z/sexpr-able? zloc) (z/sexpr zloc))]
13+
(if (and (list? form) (= 'ns (first form)))
14+
(second form)
15+
(recur (z/next zloc)))))))
16+
17+
(def core-source?
18+
(= 'clojure.core (source-namespace zloc)))
19+
820
;; remove until we're at the end of all forms
921
(defn skip-n [zloc n]
1022
;; apply z/right n times
@@ -509,8 +521,9 @@
509521

510522
(def replacements
511523
(concat
512-
(map (fn [[name form]] (definition-replace name form))
513-
portable-core-definitions)
524+
(when core-source?
525+
(map (fn [[name form]] (definition-replace name form))
526+
portable-core-definitions))
514527
;; Simple mappings from data structures
515528
(create-simple-replacements namespace-mappings)
516529
(create-simple-replacements type-mappings)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(ns rewrite-test.non-core
2+
(:refer-clojure :exclude [read]))
3+
4+
(defn read [path]
5+
(os.ReadFile path))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(ns rewrite-test.non-core
2+
(:refer-clojure :exclude [read]))
3+
4+
(defn read [path]
5+
(os.ReadFile path))

0 commit comments

Comments
 (0)