Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

last var wins only for clojure.core, #332

Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information...
commit 095734ac5064329c03eba65ade873993a890201e 1 parent 65ae492
Stuart Halloway stuarthalloway authored
2  src/clj/clojure/main.clj
View
@@ -194,7 +194,7 @@
(catch Throwable e
(caught e)
(set! *e e)))
- (use 'clojure.repl)
+ (use '[clojure.repl :only (source-fn source apropos dir-fn dir)])
(prompt)
(flush)
(loop []
14 src/jvm/clojure/lang/Namespace.java
View
@@ -66,12 +66,10 @@ public Var intern(Symbol sym){
if(o instanceof Var && ((Var) o).ns == this)
return (Var) o;
-// throw new IllegalStateException(sym + " already refers to: " + o + " in namespace: " + name);
-
if(v == null)
v = new Var(this, sym);
- warnOnReplace(sym, o, v);
+ warnOrFailOnReplace(sym, o, v);
while(!mappings.compareAndSet(map, map.assoc(sym, v)))
@@ -80,7 +78,12 @@ public Var intern(Symbol sym){
return v;
}
-private void warnOnReplace(Symbol sym, Object o, Object v){
+private void warnOrFailOnReplace(Symbol sym, Object o, Object v){
+ if (o instanceof Var) {
+ if (((Var)o).ns != RT.CLOJURE_NS) {
+ throw new IllegalStateException(sym + " already refers to: " + o + " in namespace: " + name);
+ }
+ }
RT.errPrintWriter().println("WARNING: " + sym + " already refers to: " + o + " in namespace: " + name
+ ", being replaced by: " + v);
}
@@ -101,8 +104,7 @@ Object reference(Symbol sym, Object val){
if(o == val)
return o;
-// throw new IllegalStateException(sym + " already refers to: " + o + " in namespace: " + name);
- warnOnReplace(sym, o, val);
+ warnOrFailOnReplace(sym, o, val);
while(!mappings.compareAndSet(map, map.assoc(sym, val)))
map = getMappings();
31 test/clojure/test_clojure/rt.clj
View
@@ -30,6 +30,14 @@
~@body
(str s#))))
+(defn temp-ns
+ "Create and return a temporary ns, using clojure.core + uses"
+ [& uses]
+ (binding [*ns* *ns*]
+ (in-ns (gensym))
+ (apply clojure.core/use 'clojure.core uses)
+ *ns*))
+
(defmacro eval-in-temp-ns [form]
`(binding [*ns* *ns*]
(in-ns (gensym))
@@ -45,7 +53,7 @@
(is (re-matches msg-re (with-err-print-writer (eval-in-temp-ns form))))))
(deftest error-messages
- (testing "binding a var that already refers to something"
+ (testing "binding a core var that already refers to something"
(should-print-err-message
#"WARNING: prefers already refers to: #'clojure.core/prefers in namespace: .*\n"
(defn prefers [] (throw (RuntimeException. "rebound!")))))
@@ -72,3 +80,24 @@
(is (contains? (meta #'example-var) :macro))
(.bindRoot #'example-var 0)
(is (not (contains? (meta #'example-var) :macro))))
+
+(deftest last-var-wins-for-core
+ (testing "you can replace a core name, with warning"
+ (let [ns (temp-ns 'clojure.set)
+ replacement (gensym)]
+ (with-err-string-writer (intern ns 'prefers replacement))
+ (is (= replacement @('prefers (ns-publics ns))))))
+ (testing "you cannot intern over an existing non-core name"
+ (let [ns (temp-ns 'clojure.set)
+ replacement (gensym)]
+ (is (thrown? IllegalStateException
+ (intern ns 'subset? replacement)))
+ (is (nil? ('subset? (ns-publics ns))))
+ (is (= #'clojure.set/subset? ('subset? (ns-refers ns))))))
+ (testing "you cannot refer over an existing non-core name"
+ (let [ns (temp-ns 'clojure.set)
+ replacement (gensym)]
+ (is (thrown? IllegalStateException
+ (.refer ns 'subset? #'clojure.set/intersection)))
+ (is (nil? ('subset? (ns-publics ns))))
+ (is (= #'clojure.set/subset? ('subset? (ns-refers ns)))))))
Please sign in to comment.
Something went wrong with that request. Please try again.