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

Bug with Class and Subtype existentials, when protocol doesn't have class requirement

    XMLWordPrintable

    Details

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

      macOS 10.13.0

      Xcode 9.0 (App Store version)

      Swift 4 (not in Swift 3 compatibility mode)

      Description

      In order to mutate class-type existentials, protocols used in the existential must have a class requirement. This seems like a bug. Consider the following Swift code with class A, subclass B, and protocol P:

      protocol P {
          var foo: String { get set }
      }
      
      class A {
      }
      
      class B: A, P {
          var foo = ""
      }
      
      typealias AandP = A & P
      
      let c: AandP = B()
      c.foo = ""

       

      In Swift 4, this generates the error:

      test.swift:16:7: error: cannot assign to property: 'c' is a 'let' constant

      c.foo = ""

      ~     ^

      test.swift:14:1: note: change 'let' to 'var' to make it mutable

      let c: AandP = B()

      ^~~

      var

      Since AandP must be a class type, this seems odd. Including AnyObject in the existential doesn't help. The only fix appears to be make declaring P as protocol P: class. This is unfortunate, since in the real project I want to use UIView & UITextInput to represent any text input view. I can't configure the view using configuration available via UITextInput due to this constraint, and I can't update the UITextInput protocol to have a class requirement because the protocol lives in UIKit.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              benasher44 Ben A
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: