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

[CRTP] Non-generic class inheriting from a generic class with Self as generic parameter locks execution

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Reopened
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      Xcode 7.3.1 or Xcode 8 beta 6, Swift 2.2 or latest Swift 3.0 dev snapshot

      Description

      I recently spotted this pattern in UIKit

      // Generic superclass
      open class NSLayoutAnchor<AnchorType : AnyObject> : NSObject {}
      
      // Non-generic subclasses with the pattern: `Sub : Super<Sub>`
      open class NSLayoutXAxisAnchor : NSLayoutAnchor<NSLayoutXAxisAnchor> {}
      open class NSLayoutYAxisAnchor : NSLayoutAnchor<NSLayoutYAxisAnchor> {}
      open class NSLayoutDimension : NSLayoutAnchor<NSLayoutDimension> {}
      

      If you try this patter, the compiler won't warn or raise an error:

      print(1)
      
      class Test<T> {}
      
      class Foo : Test<Foo> {
      	
      	override init() { print(2) }
      	func foo() { print(3) }
      }
      
      Foo().foo()
      
      print(4)
      

      The example above will only print "1\n" and lock on `Foo()` without any error message or crash.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                DevAndArtist Adrian Zubarev
              • Votes:
                1 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated: