Uploaded image for project: 'Clojure'
  1. CLJ-1039

Using 'def with metadata {:type :anything} throws ClassCastException during printing

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Completed
    • Affects versions: Release 1.4
    • Fix versions: Release 1.7
    • Labels:
    • Environment:

      Ubuntu, lein 1.7.1 - lein repl

    • Approval:
      Ok
    • Patch:
      Code and Test

      Description

      Specific to setting :type meta on a var:

      user=> (def ^{:type :anything} mydef 1)
      #<main$repl clojure.main$repl@6193b845>
      CompilerException java.lang.ClassNotFoundException: main.clj:257, compiling:(NO_SOURCE_PATH:13:20)
      ClassCastException clojure.lang.Var cannot be cast to clojure.lang.IObj  clojure.core/with-meta (core.clj:214)
      user=> (pst *e)
      ClassCastException clojure.lang.Var cannot be cast to clojure.lang.IObj
      	clojure.core/with-meta (core.clj:214)
      	clojure.core/vary-meta (core.clj:640)
      	clojure.core/fn--5420 (core_print.clj:76)
      	clojure.lang.MultiFn.invoke (MultiFn.java:232)
      	clojure.core/pr-on (core.clj:3392)
      	clojure.core/pr (core.clj:3404)
      	clojure.core/apply (core.clj:624)
      	clojure.core/prn (core.clj:3437)
      	clojure.main/repl/read-eval-print--6627 (main.clj:241)
      	clojure.main/repl/fn--6636 (main.clj:258)
      	clojure.main/repl (main.clj:258)
      	clojure.main/repl-opt (main.clj:324)
      

      If it is intended to forbid setting the :type metadata, then there should be an appropriate error message instead of the ClassCastException.

      Cause: This is caused by the printer dispatch function

      (defmulti print-method (fn [x writer]
                               (let [t (get (meta x) :type)]
                                 (if (keyword? t) t (class x)))))
      

      which ends up calling the default dispatch, which tries to vary-meta.

      Solution: Add a check in the default print-method for (instance? clojure.lang.IObj o) before calling vary-meta and fallback to print-simple.

      Patch: CLJ-1039-tolerate-misleading-type-metadata-on-var-wh.patch

      Screened by: Alex Miller

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              alex+import import
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: