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

instantiating a generic in such a way that it references itself breaks compiler

    XMLWordPrintable

    Details

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

      swift-4.1-DEVELOPMENT-SNAPSHOT-2017-11-23-a.xctoolchain

      also happens in XCode 9.1 as shipped by Apple

      Description

      Even in one source file (i.e. not across compilation boundaries,) inheriting from a generic and using the derived class as a generic parameter results in a runtime crash.  See included test case.  This looks like a contrived example but comes up in frameworks code where the generic needs to know the type of the final class for type safety.

       

      import XCTest
      
      // swiftlint:disable private_over_fileprivate
      
      fileprivate class G<T> {
          func foo(test: T) {
              print("\(test)")
          }
      }
      
      fileprivate class C: G<C> {
      
      }
      
      class TestSwiftSR6469: XCTestCase {
          func testSelfReferentialGeneric() {
              // this test breaks with locking loop at runtime
              _ = C()
          }
      }

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                derammo Ammo Goettsch
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: