Skip to content

Swift 6 runtime/Compile Crash with AsyncIteratorProcotol TypedThrow #74292

Open
@pbk20191

Description

@pbk20191

Description

AsyncIteratorProtocol has inconsistence behavior with protocol witness table. which cause Runtime crash.

Sometime EXC_BAD_ACCESS

Reproduction

declare own AsyncIteratorProtocol

public protocol TypedAsyncIteratorProtocol<Element, Failure> {
    
    associatedtype Element
    associatedtype Failure: Error

    
    @inlinable
    mutating func next(isolation actor: isolated (any Actor)?) async throws(Failure) -> Element?

}

public protocol TypedAsyncSequence<Element, Failure> {
    
    associatedtype Element where AsyncIterator.Element == Element
    associatedtype Failure:Error where Failure == AsyncIterator.Failure
    associatedtype AsyncIterator:TypedAsyncIteratorProtocol
    
    func makeAsyncIterator() -> AsyncIterator
    
    
}
// this do cause runtime crash
func boom<T:AsyncSequence>(base:T) async throws where T.AsyncIterator:TypedAsyncIteratorProtocol{
    var iter = base.makeAsyncIterator()
    while let value = try await iter.next(isolation: nil) {
        
    }
}

//  this does not cause runtime crash
func noBoom<T:AsyncSequence&TypedAsyncSequence>(base:T) async where T.AsyncIterator:TypedAsyncIteratorProtocol{
    var iter = base.makeAsyncIterator()
    do {
        while let value = try await iter.next(isolation: nil) {
            
        }
    } catch {
        
    }
}

extension AsyncStream.Iterator: TypedAsyncIteratorProtocol {
    
    @_implements(TypedAsyncIteratorProtocol, next(isolation:))
    mutating public func tetraNext(isolation actor: isolated (any Actor)?) async throws(Never) -> Self.Element? {
        return await next()
    }
    
}
extension AsyncStream: TypedAsyncSequence {
    
}
func test() async throws {
// no crash and do compile
       try await noBoom(base: AsyncStream<Int>{
            $0.yield(0)
            $0.finish()
        })
// runtime crash and do compile
        try await boom(base: AsyncStream<Int>{
            $0.yield(0)
            $0.finish()
        })
}

Stack dump

#0	0x00000001adde4c40 in swift_getAssociatedTypeWitness ()
#1	0x000000010373620c in boom<τ_0_0>(base:) ()
#2	0x00000001037357b4 in TetraTests.testExample() at /Users/bagbyeong-gwan/IDE/XcodeWorkSpace/Tetra/Tests/TetraTests/TetraTests.swift:34
#10	0x0000000103706364 in partial apply for specialized thunk for @escaping @isolated(any) @callee_guaranteed @Sendable @async () -> (@out τ_0_0) ()

Expected behavior

Not crash in runtime and should run normally.

Environment

Xcode 16.0 beta swift 6

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.crashBug: A crash, i.e., an abnormal termination of softwaretyped throwsFeature → error handling → throws & rethrows: Typed throws

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions