Compiler crash with result-builders containing for-loops

just a heads up, if your result builders have been crashing the compiler, it may have been because you used a for loop in the result builder body:

func foo<T>(repeating value:T) -> [T]
{
    Builder<T>.array
    {
        for _:Int in 0 ..< 1
        {
            value
        }
    }
}
$ 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
        }
    }
"
...
@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 }
    }
}

this problem seems to have been around since at least mid-october.

i have already filed this as SR-15736. reposting this here because the compiler tends to crash and dump the entire source file whenever you make a programming mistake in a result builder, so it took me a long time to realize this was a problem in the compiler, so maybe this might help someone else…

1 Like

No more crash in Swift 5.6

it is still crashing for me with the DEVELOPMENT-SNAPSHOT-2022-02-03-a toolchain

I'm sorry. I made a mistake. It still crashes the compiler in Xcode Version 13.3 beta (13E5086k)

1 Like