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

set! of a deftype field using field-access syntax causes ClassCastException

    Details

    • Approval:
      Ok
    • Patch:
      Code and Test

      Description

      set! can be used to set a public field on an instance with (set! (.field inst) val). This does not work inside a protocol function defined on a deftype with a mutable field for an instance of that type itself.

      user=> (defprotocol p (f [_]))
      p
      user=> (deftype t [^:unsynchronized-mutable x] p (f [this] (set! (.x this) 1)))
      user.t
      user=>  (f (t. 1))   ;; expect: 1
      ClassCastException user.t cannot be cast to compile__stub.user.t  user.t (NO_SOURCE_FILE:1
      

      Cause: The type assigned in the bytecode at this point is the compile_stub type, not the expected class type.

      Approach: Use getType(targetClass) instead of Type.getType(targetClass)

      Patch: 0001-CLJ-1226-fix-set-of-instance-field-expression-that-r-v2.patch
      Screened by: Fogus

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              bronsa Nicola Mometto
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: