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

Compiler seems to ignore constraint on generic type parameter

    XMLWordPrintable

    Details

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

      Xcode 9.2 using recent toolchain (dev snapshot 2017-12-07)

      Description

      Compiles successfully only with recent snapshots.
      Issue is described in the comments.

      protocol P {
          associatedtype T
          func f() // §
      }
      extension P {
          func f() { print("T is unknown") }
      }
      extension P where T == Int {
          func f() { print("T is Int") }
      }
      struct X<T> : P {}
      
      // NOTE: U.T == Int is key here ...
      struct Y<U> where U: P, U.T == Int {
          // because the compiler/type-checker knows that U.T == Int here so ...
          var a: U
          func g() { a.f() } // ... how/why could this print anything but "T is Int"?
      }
      
      let x = X<Int>()
      x.f() // Prints "T is Int", no matter if line § is commented out or not.
      let y = Y(a: X<Int>())
      y.g() // Prints "T is unknown" unless line § is commented out. Why?
      
      // IMHO it looks like the compiler simply ignores that struct Y<U> has the constraint  U.T == Int.
      

        Attachments

          Activity

            People

            Assignee:
            DougGregor Doug Gregor
            Reporter:
            jens Jens Persson
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: