Skip to content

Commit e1892f6

Browse files
mfikesswannodette
authored andcommitted
CLJS-1516: better error message when calling macros with arity
When expanding a macro with an incorrect number of arguments, emit invalid arity diagnostic reporting the number of arguments decremented by 2 in order to hide the count associated with special variables &env and &form. - The approach taken for regular JVM ClojureScript is to catch the ArityException and throw a new one with the count decremented by two. - The approach taken for bootstrapped ClojureScript ---since a generic js/Error is thrown---is to instead add a check for multi-arity-fns to consult the meta in the invalid arity path to see if the function is actually a bootstrap macroexpander and to decrement by two in that case.
1 parent 9391870 commit e1892f6

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/main/clojure/cljs/analyzer.cljc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
[cljs.reader :as edn]))
3535
#?(:clj (:import [java.io File Reader PushbackReader]
3636
[java.net URL]
37-
[clojure.lang Namespace Var LazySeq]
37+
[clojure.lang Namespace Var LazySeq ArityException]
3838
[cljs.tagged_literals JSValue])))
3939

4040
#?(:clj (set! *warn-on-reflection* true))
@@ -2349,7 +2349,10 @@
23492349
(if-not (nil? mac-var)
23502350
(#?@(:clj [binding [*ns* (create-ns *cljs-ns*)]]
23512351
:cljs [do])
2352-
(let [form' (apply @mac-var form env (rest form))]
2352+
(let [form' (try
2353+
(apply @mac-var form env (rest form))
2354+
#?(:clj (catch ArityException e
2355+
(throw (ArityException. (- (.actual e) 2) (.name e))))))]
23532356
(if #?(:clj (seq? form') :cljs (cljs-seq? form'))
23542357
(let [sym' (first form')
23552358
sym (first form)]

src/main/clojure/cljs/core.cljc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2784,9 +2784,11 @@
27842784
(~'cljs$core$IFn$_invoke$arity$variadic
27852785
~@(dest-args maxfa)
27862786
argseq#)))
2787-
`(throw (js/Error.
2788-
(str "Invalid arity: "
2789-
(alength ~args-sym)))))))))
2787+
(if (:macro meta)
2788+
`(throw (js/Error.
2789+
(str "Invalid arity: " (- (alength ~args-sym) 2))))
2790+
`(throw (js/Error.
2791+
(str "Invalid arity: " (alength ~args-sym))))))))))
27902792
~@(map fn-method fdecl)
27912793
;; optimization properties
27922794
(set! (. ~name ~'-cljs$lang$maxFixedArity) ~maxfa)))))

0 commit comments

Comments
 (0)