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

Compiler crashes while type checking possibly related to recursive constraints on associated type

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:
    • Environment:

      macOS 10.13.1, Xcode 9.2 with recent toolchain (eg snapshot 2017-12-06)

      Description

      Below is a reduced version of my original project (which is now kind of stalled because of this bug  ), the comments include the few clues I managed to find while trying to isolate it.

      Attached is the same code in an Xcode project (in its compiler crashing state).

      NOTE: Only intended for recent snapshots.

       

      main.swift:

      // This (AFAICS valid) program will crash the compiler (while type checking).
      
      // The program consists of these three files:
      // main.swift
      // Vector.swift
      // Table.swift
      
      // The behavior will not be the same if all the code is in a single file.
      
      // Each one of the 5 "workarounds" (see numbered comments) makes the program
      // compile successfully (ie avoids triggering the compiler crash).
      
      func hmm() {
          // 1. Compiler crash not triggered if either of the
          // following two lines is commented out:
          let _ = Table(V1Float1D())
          let _ = VectorRange(V1(123)).points
      }
      hmm()
      
      

       

      Vector.swift:

      protocol VectorIndex {
          // 2. Compiler crash not triggered if commenting out this line:
          associatedtype BV : Vector where BV.Index == Self, BV.Element == UInt8
      }
      struct VectorIndex1 : VectorIndex {
          typealias BV = V1<UInt8>
      }
      
      protocol Vector {
          associatedtype Index: VectorIndex
          associatedtype Element
          // 3. Compiler crash not triggered if commenting out this line:
          init(elementForIndex: (Index) -> Element)
      }
      extension Vector where Index == VectorIndex1 {
          init(_: Element) { fatalError() }
      }
      extension Vector where Element: Comparable {
          // 4. Compiler crash not triggered if commenting out this line:
          init(randomInRange range: VectorRange<Self>) { fatalError() }
      }
      
      struct V1<Element> : Vector {
          typealias Index = VectorIndex1
          init(elementForIndex: (VectorIndex1) -> Element) { }
      }
      
      struct VectorRange<Bound> where Bound: Vector, Bound.Element: Comparable {
          init(_: Bound) { }
      }
      extension VectorRange where Bound == V1<Int> {
          var points: VectorRangePoints<Bound> { fatalError() }
      }
      struct VectorRangePoints<V> where V: Vector, V.Element: FixedWidthInteger {
      }
      
      

       

      Table.swift:

      protocol TableData {
          associatedtype Value
          associatedtype Coordinate: Vector where Coordinate.Element == Int
      }
      
      struct V1Float1D : TableData {
          typealias Value = V1<Float>
          typealias Coordinate = V1<Int>
      }
      
      final class Table<Data: TableData> {
          init(_: Data) { }
      }
      
      // 5. The compiler crash is not triggered if commenting out this line:
      extension Table where Data.Coordinate.Index == VectorIndex1 { }
      
      

       

      This is what the crash looks like:

      $ /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2017-12-06-a.xctoolchain/usr/bin/swiftc main.swift Vector.swift Table.swift 
      Assertion failed: (isCanonicalTypeInContext(result, builder)), function getCanonicalTypeInContext, file /Users/buildnode/jenkins/workspace/oss-swift-package-osx/swift/lib/AST/GenericSignature.cpp, line 862.
      /.../
      1.	While type-checking 'hmm()' at main.swift:14:1
      2.	While type-checking declaration 0x7fdad685af18 at main.swift:18:5
      3.	While type-checking expression at [main.swift:18:13 - line:18:34] RangeText="VectorRange(V1(123)).p"
      <unknown>:0: error: unable to execute command: Abort trap: 6
      <unknown>:0: error: compile command failed due to signal 6 (use -v to see invocation)
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              jens Jens Persson
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: