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

AnyObject allows invalid as? conversion of combined protocols

    XMLWordPrintable

    Details

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

      Xcode 11.1 GM

      Description

      protocol P {}
      
      
      class C { let v = 1 }
      class SubCP: C, P {}   // Just to show that some C's are P, but not all of them
      class D: P {}
      
      
      typealias AnyP = AnyObject & P  // Removing AnyObject resolves this issue
      typealias CP = C & P            // Not all C are P, but CP is
      
      
      let d = (D() as AnyP) as? CP    // This should fail; D is not C
      d?.v    // Returns garbage value
      
      

       

      See StackOverflow for another form of this issue. 

      This problem persists when P requires AnyObject directly.

      protocol P: AnyObject {}
      
      
      class C { let v = 1 }
      class SubCP: C, P {}
      class D: P {}
      
      
      let d = (D() as P) as? C & P    // This should fail
      d?.v    // Returns garbage value

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            robnapier Rob Napier
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated: