Uploaded image for project: 'Swift'
  1. Swift
  2. SR-7257

Property observers being called on a nil struct instance upon attempt to mutate a member property

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      Xcode Version 9.2 (9C40b)

      Description

      struct A { 
          var foo: Int { 
              didSet { print("fooDidSet") } // not called
          }
      }
      
      var a: A? {
          didSet { print("didSet") } // called
      }
      
      a?.foo = 5
      

       Although apparently nothing is mutated, the observers of a are always called.

      Refer to this thread for the discussion.

      The SIL seems to be copying the entire composite variable (Int -> A -> Optional<A>) onto the stack, the didSet invocation simply being a side-effect of running the variable’s setter to copy back the contents from the stack. 

      "So no, evidently the compiler is not recognizing that it’s dealing with one big composite of value types and simply figuring that it can treat them all as one lump of data. It’s repeating the same unpacking process at every layer of nesting." 

      Might be related to SR-7220

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            anthonylatsis Anthony Latsis
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Dates

              Created:
              Updated: