Clojure

Fix confusing macroexpand1 ArityException handling

Details

  • Type: Defect Defect
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: Release 1.6
  • Fix Version/s: None
  • Component/s: None
  • Patch:
    Code and Test

Description

macros can give very confusing error messages when they execute a form which generates an ArityException. clojure.lang.Compiler.macroexpand1 assumes that any ArityException comes from the call to the macro itself, which need not be the case. For instance:

user> (do (defmacro f [] (assoc)) (f))
ArityException Wrong number of args (-2) passed to: core$assoc clojure.lang.Compiler.macroexpand1 (Compiler.java:6488)
user> (use 'clojure.repl) (pst)
nil
ArityException Wrong number of args (-2) passed to: core$assoc
clojure.lang.Compiler.macroexpand1 (Compiler.java:6488)
clojure.lang.Compiler.macroexpand (Compiler.java:6544)
clojure.lang.Compiler.eval (Compiler.java:6618)
clojure.lang.Compiler.eval (Compiler.java:6624)
clojure.lang.Compiler.eval (Compiler.java:6597)
clojure.core/eval (core.clj:2864)
clojure.main/repl/read-eval-print-6596/fn-6599 (main.clj:260)
clojure.main/repl/read-eval-print--6596 (main.clj:260)
clojure.main/repl/fn--6605 (main.clj:278)
clojure.main/repl (main.clj:278)
clojure.tools.nrepl.middleware.interruptible-eval/evaluate/fn--1251 (interruptible_eval.clj:56)
clojure.core/apply (core.clj:617)
nil

Easy enough to see the source of the problem in this case, but because both the number of arguments actually passed is off by two, and the stacktrace element for the call to assoc has been dropped, this shortcut by macroexpand1 can get super confusing.

The attached patch corrects this behavior. E.g.

user=> (do (defmacro f [] (assoc)) (f))
ArityException Wrong number of args (0) passed to: core$assoc clojure.lang.AFn.throwArity (AFn.java:437)
user=> (use 'clojure.repl) (pst)
nil
ArityException Wrong number of args (0) passed to: core$assoc
user/f (NO_SOURCE_FILE:1)
clojure.lang.Var.invoke (Var.java:419)
clojure.lang.Var.applyTo (Var.java:532)
clojure.lang.Compiler.macroexpand1 (Compiler.java:6507)
clojure.lang.Compiler.macroexpand (Compiler.java:6580)
clojure.lang.Compiler.eval (Compiler.java:6654)
clojure.lang.Compiler.eval (Compiler.java:6660)
clojure.lang.Compiler.eval (Compiler.java:6633)
clojure.core/eval (core.clj:2864)
clojure.main/repl/read-eval-print-6594/fn-6597 (main.clj:260)
clojure.main/repl/read-eval-print--6594 (main.clj:260)
clojure.main/repl/fn--6603 (main.clj:278)
nil

Activity

Alex Miller made changes -
Field Original Value New Value
Labels Compiler bug enhancement errormsgs patch, Compiler errormsgs
Alex Coventry made changes -
Attachment 0003-Test-which-examines-stacktrace-for-desired-element.patch [ 12330 ]
Alex Coventry made changes -
Attachment 0001-Fix-macroexpand1-s-handling-of-ArityException.patch [ 12329 ]
Alex Coventry made changes -
Attachment 0003-Test-which-examines-stacktrace-for-desired-element.patch [ 12330 ]
Alex Coventry made changes -
Attachment 0001-Fix-macroexpand1-s-handling-of-ArityException.patch [ 12331 ]
Alex Coventry made changes -
Comment [ Adding a test which checks that the relevent stack trace elements are in the thrown exception. ]
Alex Coventry made changes -
Attachment 0001-Fix-macroexpand1-s-handling-of-ArityException.patch [ 12383 ]
Alex Coventry made changes -
Attachment 0001-Fix-macroexpand1-s-handling-of-ArityException.patch [ 12331 ]
Alex Coventry made changes -
Comment [ Amended patch to deal more gracefully with unexpected stack trace structure. ]
Alex Coventry made changes -
Alex Coventry made changes -
Attachment 0001-Fix-macroexpand1-s-handling-of-ArityException.patch [ 12383 ]
Alex Coventry made changes -
Alex Miller made changes -
Labels Compiler errormsgs Compiler errormsgs macro

People

Vote (1)
Watch (1)

Dates

  • Created:
    Updated: