Skip to content

Commit a600651

Browse files
anmonteirodnolen
authored andcommitted
CLJS-1536: REPL def symbol init collision
1 parent eb16440 commit a600651

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

src/main/clojure/cljs/analyzer.cljc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,10 @@
10221022

10231023
(defn- var-ast
10241024
[env sym]
1025-
(let [var (resolve-var env sym (confirm-var-exists-throw))
1025+
;; we need to dissoc locals for the `(let [x 1] (def x x))` case, because we
1026+
;; want the var's AST and `resolve-var` will check locals first. - António Monteiro
1027+
(let [env (dissoc env :locals)
1028+
var (resolve-var env sym (confirm-var-exists-throw))
10261029
expr-env (assoc env :context :expr)]
10271030
(if-let [var-ns (:ns var)]
10281031
{:var (analyze expr-env sym)
@@ -1384,9 +1387,10 @@
13841387

13851388
(defmethod parse 'fn*
13861389
[op env [_ & args :as form] name _]
1387-
(let [[name meths] (if (symbol? (first args))
1388-
[(first args) (next args)]
1389-
[name (seq args)])
1390+
(let [named-fn? (symbol? (first args))
1391+
[name meths] (if named-fn?
1392+
[(first args) (next args)]
1393+
[name (seq args)])
13901394
;; turn (fn [] ...) into (fn ([]...))
13911395
meths (if (vector? (first meths))
13921396
(list meths)
@@ -1397,7 +1401,7 @@
13971401
(update-in env [:fn-scope] conj name-var)
13981402
env)
13991403
locals (if (and (not (nil? locals))
1400-
(not (nil? name)))
1404+
named-fn?)
14011405
(assoc locals name name-var)
14021406
locals)
14031407
form-meta (meta form)
@@ -1413,7 +1417,7 @@
14131417
methods (map #(disallowing-ns* (analyze-fn-method menv locals % type)) meths)
14141418
mfa (apply max (map :max-fixed-arity methods))
14151419
variadic (boolean (some :variadic methods))
1416-
locals (if-not (nil? name)
1420+
locals (if named-fn?
14171421
(update-in locals [name] assoc
14181422
;; TODO: can we simplify? - David
14191423
:fn-var true

src/main/clojure/cljs/compiler.cljc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -609,9 +609,9 @@
609609
(when (or init (:def-emits-var env))
610610
(let [mname (munge name)]
611611
(emit-comment env doc (concat jsdoc (:jsdoc init)))
612-
(when (:def-emits-var env)
613-
(when (= :return (:context env))
612+
(when (= :return (:context env))
614613
(emitln "return ("))
614+
(when (:def-emits-var env)
615615
(emitln "(function (){"))
616616
(emits var)
617617
(when init
@@ -625,9 +625,9 @@
625625
{:op :var-special
626626
:env (assoc env :context :expr)}
627627
var-ast))
628-
(emitln ");})()")
629-
(when (= :return (:context env))
630-
(emitln ")")))
628+
(emitln ");})()"))
629+
(when (= :return (:context env))
630+
(emitln ")"))
631631
;; NOTE: JavaScriptCore does not like this under advanced compilation
632632
;; this change was primarily for REPL interactions - David
633633
;(emits " = (typeof " mname " != 'undefined') ? " mname " : undefined")

src/test/cljs/cljs/core_test.cljs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,20 @@
11351135
(is (= (nth "012" 3 :not-found) :not-found))
11361136
(is (= (nth "012" -1 :not-found) :not-found)))
11371137

1138+
(let [foo-1536 2]
1139+
(def foo-1536 foo-1536))
1140+
1141+
(let [foo-1536-2 1]
1142+
(defn foo-1536-2 []
1143+
foo-1536-2))
1144+
1145+
(deftest test-cljs-1536
1146+
(is (= foo-1536 2))
1147+
(is (= (foo-1536-2) 1))
1148+
;; these two lines generate a `:redef-in-file` warning, which is caused by `cljs.test/is`
1149+
(is (= ((let [z 1] (defn z [] z))) 1))
1150+
(is (= (let [w 1] ((defn w [] w))) 1)))
1151+
11381152
(comment
11391153
;; ObjMap
11401154
;; (let [ks (map (partial str "foo") (range 500))

src/test/clojure/cljs/analyzer_tests.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,13 @@
591591
nsb (str (a/gen-user-ns b))]
592592
(is (not= (.substring nsa (- (count nsa) 7)) (.substring nsb (- (count nsb) 7))))
593593
(is (= (.substring nsa 0 (- (count nsa) 7)) (.substring nsb 0 (- (count nsb) 7)))))))
594+
595+
(deftest test-cljs-1536
596+
(let [parsed (e/with-compiler-env test-cenv
597+
(a/analyze (assoc test-env :def-emits-var true)
598+
'(def x 1)))]
599+
(is (some? (:var-ast parsed))))
600+
(let [parsed (e/with-compiler-env test-cenv
601+
(a/analyze (assoc test-env :def-emits-var true)
602+
'(let [y 1] (def y 2))))]
603+
(is (some? (-> parsed :expr :ret :var-ast)))))

0 commit comments

Comments
 (0)