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

The dynamic nature of a protocol's Self type isn't checked when equated with another associated type

    XMLWordPrintable

    Details

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

      Swift version 5.1-dev (LLVM 082dec2e22, Swift 397965d3fc)
      Target: x86_64-apple-darwin18.5.0

      Description

      The following unsound code is allowed to compile, which lets us turn a subclass instance into a superclass instance:

      protocol P {
        associatedtype X where X == Self
        func foo() -> X
      }
      
      class C : P {
        typealias X = C
        func foo() -> X {
          return C()
        }
      }
      class D : C {}
      
      func foo<T : P>(_ x: inout T) {
        x = x.foo()
      }
      
      var d = D()
      foo(&d)
      print(type(of: d)) // C
      

      When checking the constraint X == Self, we should treat Self as being the dynamic self and therefore reject the conformance.

        Attachments

          Activity

            People

            Assignee:
            slavapestov Slava Pestov
            Reporter:
            originaluser2 Hamish Knight
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated: