Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-6558] Compiler crashes while type checking possibly related to recursive constraints on associated type #49108

Closed
jepers opened this issue Dec 7, 2017 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software

Comments

@jepers
Copy link

jepers commented Dec 7, 2017

Previous ID SR-6558
Radar rdar://problem/36068194
Original Reporter @jepers
Type Bug
Status Closed
Resolution Done

Attachment: Download

Environment

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

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, CompilerCrash
Assignee None
Priority Medium

md5: e18abd46bc32fad921bd003c0499c367

Issue 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)
@belkadan
Copy link
Contributor

belkadan commented Dec 8, 2017

cc @DougGregor

@DougGregor
Copy link
Member

@swift-ci create

@DougGregor
Copy link
Member

We're getting an awful interleaving between associated type inference (for V1: Vector, to determine Element) and the type checking of the constrained extension of VectorRange where Bound == V1<Int>. The actual problem is... annoyingly... checkRedeclaration's way-too-early call to validateDecl (see https://github.com/apple/swift/blob/master/lib/Sema/TypeCheckDecl.cpp#L918) is causing the interleaving.

@DougGregor
Copy link
Member

#13643

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software
Projects
None yet
Development

No branches or pull requests

4 participants