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

Reference type has struct mutability rules applied to it

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Duplicate
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      Xcode 9.3.

      Description

      In a class C<T> where T is constrained to be a reference type and conform to a protocol which is not, Swift does not recognize that T should be treated as a reference type and mutations should be allowed even on let}}s of type {{T.
      {{
      class ReferenceType {

      }

      protocol Protocol {
      var settable: Bool

      { get set }

      }

      class C<T> where T: ReferenceType, T: Protocol {

      func f(t: T)

      { t.settable = false // cannot assign to property: 't' is a 'let' constant }

      }
      }}

      Making Protocol inherit from AnyObject or NSObjectProtocol fixes the problem in this case.

      In the real-world case that led me to report this, my equivalent of Protocol actually did conform to NSObjectProtocol through UITextInput, but Swift still didn't realize it was a reference type. Only be changing all of the other protocols that Protocol inherited from was I able to convince the compiler that t was a reference type. As t was a commonly used property of C in my case, being forced to make it a var was quite inconvenient. I wasn't able to make a small test case of this, however.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              benpious Ben Pious
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: