swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.66k stars 10.38k forks source link

[SR-14559] Compiler Crash Combining Result Builder with Scoped Type Alias #56911

Open swift-ci opened 3 years ago

swift-ci commented 3 years ago
Previous ID SR-14559
Radar rdar://problem/77465733
Original Reporter juliand665 (JIRA User)
Type Bug

Attachment: Download

Environment Xcode 12.5, macOS 11.3 Also tested with Swift 5.5 Development Snapshot from 2021-04-20.
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: abcfd4bd21989fb758c8323471ae9957

Issue Description:

When a result builder is applied to a protocol requirement, it is automatically applied for any conformers. However, this automatic application fails if the builder involves a typealias scoped to the protocol in question:

@resultBuilder
struct Builder<T> {
    static func buildExpression(_ expression: T) -> [T] {
        [expression]
    }

    static func buildBlock(_ components: [T]...) -> [T] {
        Array(components.joined())
    }
}

typealias OutsideString = String
typealias OutsideStringBuilder = Builder<String>

protocol Buildable {
    typealias InsideString = String
    typealias InsideStringBuilder = Builder<String>

    // these crash:
    //@Builder<InsideString>
    //@InsideStringBuilder

    // these work fine:
    //@OutsideStringBuilder
    //@Builder<OutsideString>
    //@Builder<String>

    func build() -> [String]
}

struct Building: Buildable {
    // at this site they all work; just using this as an example
    @InsideStringBuilder
    func build() -> [String] {
        "foo"
        "bar"
    }
}

There are comments in the code snippet for more information. The crash occurs when any type alias is involved in the annotation, whether as a generic parameter (@Builder<InsideString>) or as the type itself (@InsideStringBuilder).

The crash does not occur if you don't declare a type implementing that member or if you override the builder (explicitly specifying it) for that member in the conforming type.

I've attached a build log exhibiting the crash.

typesanitizer commented 3 years ago

@swift-ci create