Skip to content

Commit 3e6f978

Browse files
committed
Resolve imported vars at runtime, not read time
If a var has tag metadata attached to the var symbol (not on the vector, as is the preferred method for return types in functions), it will be resolved when `import-*` is called during read time. This currently doesn't break anything, but if the proposed Clojure patch for [CLJ-1929][CLJ-1929] is accepted, then code that's so tagged will break. Includes a test that will break without the associated change if the Clojure patch is accepted. Closes [clj-commons#72][clj-commons#72] [CLJ-1929]: https://clojure.atlassian.net/browse/CLJ-1929 [clj-commons#72]: clj-commons#72
1 parent 768de32 commit 3e6f978

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

src/potemkin/namespaces.clj

+15-15
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
(throw (IllegalArgumentException.
2727
(str "Calling import-fn on a macro: " sym))))
2828

29-
`(do
30-
(def ~(with-meta n {:protocol protocol}) (deref ~vr))
31-
(alter-meta! (var ~n) merge (dissoc (meta ~vr) :name))
32-
(link-vars ~vr (var ~n))
33-
~vr))))
29+
`(let [vr# (resolve '~sym)]
30+
(def ~(with-meta n {:protocol protocol}) (deref vr#))
31+
(alter-meta! (var ~n) merge (dissoc (meta vr#) :name))
32+
(link-vars vr# (var ~n))
33+
vr#))))
3434

3535
(defmacro import-macro
3636
"Given a macro in another namespace, defines a macro with the same
@@ -48,12 +48,12 @@
4848
(when-not (:macro m)
4949
(throw (IllegalArgumentException.
5050
(str "Calling import-macro on a non-macro: " sym))))
51-
`(do
52-
(def ~n ~(resolve sym))
53-
(alter-meta! (var ~n) merge (dissoc (meta ~vr) :name))
51+
`(let [vr# (resolve '~sym)]
52+
(def ~n (deref vr#))
53+
(alter-meta! (var ~n) merge (dissoc (meta vr#) :name))
5454
(.setMacro (var ~n))
55-
(link-vars ~vr (var ~n))
56-
~vr))))
55+
(link-vars vr# (var ~n))
56+
vr#))))
5757

5858
(defmacro import-def
5959
"Given a regular def'd var from another namespace, defined a new var with the
@@ -68,11 +68,11 @@
6868
nspace (:ns m)]
6969
(when-not vr
7070
(throw (IllegalArgumentException. (str "Don't recognize " sym))))
71-
`(do
72-
(def ~n @~vr)
73-
(alter-meta! (var ~n) merge (dissoc (meta ~vr) :name))
74-
(link-vars ~vr (var ~n))
75-
~vr))))
71+
`(let [vr# (resolve '~sym)]
72+
(def ~n (deref vr#))
73+
(alter-meta! (var ~n) merge (dissoc (meta vr#) :name))
74+
(link-vars vr# (var ~n))
75+
vr#))))
7676

7777
(defmacro import-vars
7878
"Imports a list of vars from other namespaces."

test/potemkin/imports_test.clj

+3
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@
2020
x)
2121

2222
(def some-value 1)
23+
24+
(defn ^clojure.lang.ExceptionInfo ex-info-2 [msg data]
25+
(ex-info msg data))

test/potemkin/namespaces_test.clj

+3
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,6 @@
5050
(is false "`import-vars` should have thrown an exception")
5151
(catch Exception ex
5252
(is "`clojure.set/onion-misspelled` does not exist" (.getMessage ex)))))
53+
54+
;; This is the whole test for CLJ-1929
55+
(import-vars [potemkin.imports-test ex-info-2])

0 commit comments

Comments
 (0)