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-15736] compiler crashes if a generic result-builder contains a for-loop #58013

Open
tayloraswift opened this issue Jan 16, 2022 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@tayloraswift
Copy link
Contributor

Previous ID SR-15736
Radar None
Original Reporter @Kelvin13
Type Bug
Environment

$ swiftenv version
DEVELOPMENT-SNAPSHOT-2022-01-09-a
$ swift --version
Swift version 5.6-dev (LLVM 7b20e61dd04138a, Swift 9438cf6)
Target: x86_64-unknown-linux-gnu

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

md5: a43ed93d3615c72ad24c46a859ca0f4e

Issue Description:

as of DEVELOPMENT-SNAPSHOT-2022-01-09-a, using any result-builder in a generic context will crash the compiler, if the result-builder contains a for-loop.

Minimal reproduction:

func foo<T>(repeating value:T) -> [T]
{
    return Builder<T>.array
    {
        for _:Int in [1, 2, 3] as [Int]
        {
            value
        }
    }
}

@resultBuilder 
enum Builder<Element> 
{
    static 
    func array(@Builder<Element> _ elements:() -> [Element]) -> [Element]
    {
        elements()
    }
    
    static 
    func buildExpression(_ element:Element) -> [Element]
    {
        [element]
    }
    static 
    func buildExpression(_ elements:[Element]) -> [Element]
    {
        elements
    }
    static 
    func buildBlock(_ elements:[Element]...) -> [Element]
    {
        elements.flatMap{ $0 }
    }
    static 
    func buildArray(_ elements:[[Element]]) -> [Element]
    {
        elements.flatMap{ $0 }
    }
}

crashes with:

$ swiftc result-builder-crash.swift 
error: compile command failed due to signal 6 (use -v to see invocation)
swift-frontend: /home/build-user/swift/lib/AST/TypeCheckRequests.cpp:981: void swift::InterfaceTypeRequest::cacheResult(swift::Type) const: Assertion `!type->hasArchetype() && "Archetype in interface type"' failed.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.  Program arguments: .swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-01-09-a/usr/bin/swift-frontend -frontend -c -primary-file result-builder-crash.swift -target x86_64-unknown-linux-gnu -disable-objc-interop -color-diagnostics -new-driver-path /home/klossy/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-01-09-a/usr/bin/swift-driver -resource-dir .swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-01-09-a/usr/lib/swift -module-name main -o /tmp/TemporaryDirectory.YHE35r/result-builder-crash-1.o
1.  Swift version 5.6-dev (LLVM 7b20e61dd04138a, Swift 9438cf6b2e83c5f)
2.  Compiling with the current language version
3.  While evaluating request TypeCheckSourceFileRequest(source_file "result-builder-crash.swift")
4.  While evaluating request TypeCheckFunctionBodyRequest(main.(file).foo(repeating:)@result-builder-crash.swift:1:6)
5.  While type-checking statement at [result-builder-crash.swift:2:1 - line:10:1] RangeText="{
    return Builder<T>.array
    {
        for _:Int in [1, 2, 3] as [Int]
        {
            value
        }
    }
"
6.  While type-checking statement at [result-builder-crash.swift:3:5 - line:9:5] RangeText="return Builder<T>.array
    {
        for _:Int in [1, 2, 3] as [Int]
        {
            value
        }
    "
7.  While type-checking expression at [result-builder-crash.swift:3:12 - line:9:5] RangeText="Builder<T>.array
    {
        for _:Int in [1, 2, 3] as [Int]
        {
            value
        }
    "

Replacing the parameter `T` with a concrete type such as `Int` will compile normally.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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
Projects
None yet
Development

No branches or pull requests

1 participant