swift icon indicating copy to clipboard operation
swift copied to clipboard

Runtime crash when calling functions with opaque return types, stored as closures

Open cristik opened this issue 1 year ago • 2 comments

Description The following code crashes at runtime:

protocol Worker { }

struct DummyWorker: Worker {
    init(_: Int) { }
}

let workerFactory: (Int) -> some Worker = DummyWorker.init

let worker = workerFactory(15)
print(worker)

It crashes when calling workerFactory with

Thread 1: EXC_BAD_ACCESS (code=1, address=0xf)

The address from the crash message is influenced by the value of the parameter passed to workerFactory, for example workerFactory(4) will crash with address 0x4.

If I change the parameter type to [Int] (or any kind of array), the code no longer crashes, however as soon as I add another Int/Double/String/etc parameter it will again crash, but if I add a second array parameter it will not crash.

I think this is a compiler bug, but unsure where the bug is:

  • either the compiler should not allow declaring a type that consist of a function that returns an opaque value (if one tries to write a closure, the compiler won't allow it to use the some keyword)
  • or the bug is when the compiler wraps up the opaque value

Steps to reproduce Run the above code snippet.

Expected behavior Either it should not crash, or the compiler should now allow using some in this scenario.

Environment

  • Swift compiler version info:

    swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1) Target: arm64-apple-macosx13.0

  • Xcode version info:

    Xcode 15.0 Build version 15A240d

  • Deployment target: iOS 17, macOS 13

cristik avatar Oct 09 '23 05:10 cristik