diff options
author | Mike Hinchey <hincheymg@gmail.com> | 2010-10-18 10:16:54 -0700 |
---|---|---|
committer | Stuart Halloway <stu@thinkrelevance.com> | 2010-11-05 08:46:41 -0700 |
commit | 17a8c909e43aa9813e609d7ed18eb679c9ba1e49 (patch) | |
tree | 5b6946dbfcecd6ad560cc53c8b41b917504b5c51 | |
parent | 38e63c2e967c8cca1465f4bdc7560d9288f42e6b (diff) |
397 better error message when calling macros with arity
Error message for macro arity was +2 for the internal params.
Introduce specific class ArityException to correct it.
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
-rw-r--r-- | src/jvm/clojure/lang/AFn.java | 3 | ||||
-rw-r--r-- | src/jvm/clojure/lang/ArityException.java | 32 | ||||
-rw-r--r-- | src/jvm/clojure/lang/Compiler.java | 10 | ||||
-rw-r--r-- | test/clojure/test_clojure.clj | 1 | ||||
-rw-r--r-- | test/clojure/test_clojure/errors.clj | 32 |
5 files changed, 75 insertions, 3 deletions
diff --git a/src/jvm/clojure/lang/AFn.java b/src/jvm/clojure/lang/AFn.java index a93cd7ed..26d2a56f 100644 --- a/src/jvm/clojure/lang/AFn.java +++ b/src/jvm/clojure/lang/AFn.java @@ -436,7 +436,6 @@ static public Object applyToHelper(IFn ifn, ISeq arglist) throws Exception{ public Object throwArity(int n){ String name = getClass().getSimpleName(); int suffix = name.lastIndexOf("__"); - throw new IllegalArgumentException("Wrong number of args (" + n + ") passed to: " - + (suffix == -1 ? name : name.substring(0, suffix)).replace('_', '-')); + throw new ArityException(n, (suffix == -1 ? name : name.substring(0, suffix)).replace('_', '-')); } } diff --git a/src/jvm/clojure/lang/ArityException.java b/src/jvm/clojure/lang/ArityException.java new file mode 100644 index 00000000..71041ee3 --- /dev/null +++ b/src/jvm/clojure/lang/ArityException.java @@ -0,0 +1,32 @@ +/** + * Copyright (c) Rich Hickey. All rights reserved. + * The use and distribution terms for this software are covered by the + * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) + * which can be found in the file epl-v10.html at the root of this distribution. + * By using this software in any fashion, you are agreeing to be bound by + * the terms of this license. + * You must not remove this notice, or any other, from this software. + **/ + +package clojure.lang; + +/** + * @since 1.3 + */ +public class ArityException extends IllegalArgumentException { + + final public int actual; + + final public String name; + + public ArityException(int actual, String name) { + this(actual, name, null); + } + + public ArityException(int actual, String name, Throwable cause) { + super("Wrong number of args (" + actual + ") passed to: " + name, cause); + this.actual = actual; + this.name = name; + } + +} diff --git a/src/jvm/clojure/lang/Compiler.java b/src/jvm/clojure/lang/Compiler.java index d38d25e8..34d46783 100644 --- a/src/jvm/clojure/lang/Compiler.java +++ b/src/jvm/clojure/lang/Compiler.java @@ -6027,7 +6027,15 @@ public static Object macroexpand1(Object x) throws Exception{ Var v = isMacro(op); if(v != null) { - return v.applyTo(RT.cons(form,RT.cons(LOCAL_ENV.get(),form.next()))); + try + { + return v.applyTo(RT.cons(form,RT.cons(LOCAL_ENV.get(),form.next()))); + } + catch(ArityException e) + { + // hide the 2 extra params for a macro + throw new ArityException(e.actual - 2, e.name, e); + } } else { diff --git a/test/clojure/test_clojure.clj b/test/clojure/test_clojure.clj index 2f3fc4f8..d66ecf21 100644 --- a/test/clojure/test_clojure.clj +++ b/test/clojure/test_clojure.clj @@ -66,6 +66,7 @@ :keywords :data :reflect + :errors ]) (def test-namespaces diff --git a/test/clojure/test_clojure/errors.clj b/test/clojure/test_clojure/errors.clj new file mode 100644 index 00000000..3b849870 --- /dev/null +++ b/test/clojure/test_clojure/errors.clj @@ -0,0 +1,32 @@ +; Copyright (c) Rich Hickey. All rights reserved. +; The use and distribution terms for this software are covered by the +; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +; which can be found in the file epl-v10.html at the root of this distribution. +; By using this software in any fashion, you are agreeing to be bound by +; the terms of this license. +; You must not remove this notice, or any other, from this software. + +;; Tests for error handling and messages + +(ns clojure.test-clojure.errors + (:use clojure.test) + (:import clojure.lang.ArityException)) + +(defn f0 [] 0) + +(defn f1 [a] a) + +(defmacro m0 [] `(identity 0)) + +(defmacro m1 [a] `(inc ~a)) + +(deftest arity-exception + ;; IllegalArgumentException is pre-1.3 + (is (thrown-with-msg? IllegalArgumentException #"Wrong number of args \(1\) passed to" + (f0 1))) + (is (thrown-with-msg? ArityException #"Wrong number of args \(0\) passed to" + (f1))) + (is (thrown-with-msg? ArityException #"Wrong number of args \(1\) passed to" + (macroexpand `(m0 1)))) + (is (thrown-with-msg? ArityException #"Wrong number of args \(2\) passed to" + (macroexpand `(m1 1 2))))) |