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

Swift 6 runtime/Compile Crash with AsyncIteratorProcotol TypedThrow #74292

Open
pbk20191 opened this issue Jun 11, 2024 · 2 comments
Open

Swift 6 runtime/Compile Crash with AsyncIteratorProcotol TypedThrow #74292

pbk20191 opened this issue Jun 11, 2024 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software typed throws Feature → error handling → throws & rethrows: Typed throws

Comments

@pbk20191
Copy link

pbk20191 commented Jun 11, 2024

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

@pbk20191 pbk20191 added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Jun 11, 2024
@pbk20191 pbk20191 changed the title Swift 6 runtime Crash with AsyncIteratorProcotol Swift 6 runtime/Compile Crash with AsyncIteratorProcotol TypedThrow Jun 12, 2024
@pbk20191
Copy link
Author

There is compile time crash with rethrow Protocol and TypedFailure.

This happens in Xcode 16.0 beta swift 6

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

    typealias TetraFailure = Failure

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

}

extension AsyncMapSequence.Iterator: TypedAsyncIteratorProtocol  where Base.AsyncIterator: TypedAsyncIteratorProtocol {
    
    @_implements(TypedAsyncIteratorProtocol, next(isolation:))
    mutating public func tetraNext(isolation actor: isolated (any Actor)?) async throws(Base.AsyncIterator.TetraFailure) -> Self.Element? {
        if #available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) {
            return try await nextValue(isolation: actor)
        } else {
            return try await nextMapValue()
        }
    }
    
}


@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
extension AsyncIteratorProtocol {
    
    @usableFromInline
    mutating internal func nextValue(isolation actor: isolated (any Actor)?) async throws(Failure) -> Self.Element? {
        try await next(isolation: actor)
    }
     
}

extension AsyncMapSequence.Iterator {
    
    mutating func nextMapValue() async rethrows -> sending Self.Element? {
        try await next()
    }
}

This is the stack trace

Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/bagbyeong-gwan/IDE/XcodeWorkSpace/MyMacro/Sources/MyMacroClient/Untitled.swift /Users/bagbyeong-gwan/IDE/XcodeWorkSpace/MyMacro/Sources/MyMacroClient/main.swift -emit-dependencies-path /Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/MyMacro-fgcldwjivgjnjtexkylrwrjjuhab/Build/Intermediates.noindex/MyMacro.build/Debug/MyMacroClient.build/Objects-normal/arm64/Untitled.d -emit-const-values-path /Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/MyMacro-fgcldwjivgjnjtexkylrwrjjuhab/Build/Intermediates.noindex/MyMacro.build/Debug/MyMacroClient.build/Objects-normal/arm64/Untitled.swiftconstvalues -emit-reference-dependencies-path /Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/MyMacro-fgcldwjivgjnjtexkylrwrjjuhab/Build/Intermediates.noindex/MyMacro.build/Debug/MyMacroClient.build/Objects-normal/arm64/Untitled.swiftdeps -serialize-diagnostics-path /Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/MyMacro-fgcldwjivgjnjtexkylrwrjjuhab/Build/Intermediates.noindex/MyMacro.build/Debug/MyMacroClient.build/Objects-normal/arm64/Untitled.dia -target arm64-apple-macos10.15 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk -I /Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/MyMacro-fgcldwjivgjnjtexkylrwrjjuhab/Build/Products/Debug -I /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/MyMacro-fgcldwjivgjnjtexkylrwrjjuhab/Build/Products/Debug/PackageFrameworks -F /Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/MyMacro-fgcldwjivgjnjtexkylrwrjjuhab/Build/Products/Debug -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -no-color-diagnostics -enable-testing -g -debug-info-format=dwarf -dwarf-version=4 -module-cache-path /Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 6 -enforce-exclusivity=checked -Onone -D SWIFT_PACKAGE -D DEBUG -D Xcode -serialize-debugging-options -enable-experimental-opaque-type-erasure -load-plugin-executable /Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/MyMacro-fgcldwjivgjnjtexkylrwrjjuhab/Build/Products/Debug/MyMacroMacros#MyMacroMacros -const-gather-protocols-file /Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/MyMacro-fgcldwjivgjnjtexkylrwrjjuhab/Build/Intermediates.noindex/MyMacro.build/Debug/MyMacroClient.build/Objects-normal/arm64/MyMacroClient_const_extract_protocols.json -enable-experimental-feature DebugDescriptionMacro -empty-abi-descriptor -plugin-path /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins/testing -validate-clang-modules-once -clang-build-session-file /Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Users/bagbyeong-gwan/IDE/XcodeWorkSpace/MyMacro/.swiftpm/xcode -resource-dir /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /Users/bagbyeong-gwan/IDE/XcodeWorkSpace/MyMacro/.swiftpm/xcode -Xcc -ivfsstatcache -Xcc /Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/macosx15.0-24A5264i-495167fbbdaa9fb73ea1f786c83da506.sdkstatcache -Xcc -I/Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/MyMacro-fgcldwjivgjnjtexkylrwrjjuhab/Build/Products/Debug/include -Xcc -I/Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/MyMacro-fgcldwjivgjnjtexkylrwrjjuhab/Build/Intermediates.noindex/MyMacro.build/Debug/MyMacroClient.build/DerivedSources-normal/arm64 -Xcc -I/Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/MyMacro-fgcldwjivgjnjtexkylrwrjjuhab/Build/Intermediates.noindex/MyMacro.build/Debug/MyMacroClient.build/DerivedSources/arm64 -Xcc -I/Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/MyMacro-fgcldwjivgjnjtexkylrwrjjuhab/Build/Intermediates.noindex/MyMacro.build/Debug/MyMacroClient.build/DerivedSources -Xcc -DSWIFT_PACKAGE -Xcc -DDEBUG=1 -module-name MyMacroClient -package-name mymacro -frontend-parseable-output -disable-clang-spi -target-sdk-version 15.0 -target-sdk-name macosx15.0 -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -o /Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/MyMacro-fgcldwjivgjnjtexkylrwrjjuhab/Build/Intermediates.noindex/MyMacro.build/Debug/MyMacroClient.build/Objects-normal/arm64/Untitled.o -index-unit-output-path /MyMacro.build/Debug/MyMacroClient.build/Objects-normal/arm64/Untitled.o -index-store-path /Users/bagbyeong-gwan/Library/Developer/Xcode/DerivedData/MyMacro-fgcldwjivgjnjtexkylrwrjjuhab/Index.noindex/DataStore -index-system-modules
1.	Apple Swift version 6.0 (swiftlang-6.0.0.3.300 clang-1600.0.20.10)
2.	Compiling with the current language version
3.	While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "/Users/bagbyeong-gwan/IDE/XcodeWorkSpace/MyMacro/Sources/MyMacroClient/Untitled.swift")
4.	While silgen emitFunction SIL function "@$ss16AsyncMapSequenceV8IteratorV13MyMacroClientAE05TypedaD8Protocol0aD0RpzrlE9tetraNext9isolationq_SgScA_pSgYi_tYa7FailureQzYKF".
 for 'tetraNext(isolation:)' (at /Users/bagbyeong-gwan/IDE/XcodeWorkSpace/MyMacro/Sources/MyMacroClient/Untitled.swift:24:21)
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x000000010a0fecd0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x000000010a0fcf44 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x000000010a0ff2d8 SignalHandler(int) + 352
3  libsystem_platform.dylib 0x00000001a01d0184 _sigtramp + 56
4  swift-frontend           0x0000000105540a68 swift::Lowering::SILGenFunction::emitExistentialErasure(swift::SILLocation, swift::CanType, swift::Lowering::TypeLowering const&, swift::Lowering::TypeLowering const&, llvm::ArrayRef<swift::ProtocolConformanceRef>, swift::Lowering::SGFContext, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SGFContext)>, bool) + 300
5  swift-frontend           0x0000000105600c20 swift::Lowering::SILGenFunction::emitThrow(swift::SILLocation, swift::Lowering::ManagedValue, bool) + 2772
6  swift-frontend           0x0000000105600070 swift::Lowering::SILGenFunction::getTryApplyErrorDest(swift::SILLocation, swift::CanTypeWrapper<swift::SILFunctionType>, swift::Lowering::ExecutorBreadcrumb, swift::SILResultInfo, swift::SILValue, bool) + 544
7  swift-frontend           0x00000001054ed310 emitRawApply(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::SubstitutionMap, llvm::ArrayRef<swift::Lowering::ManagedValue>, swift::CanTypeWrapper<swift::SILFunctionType>, swift::optionset::OptionSet<swift::ApplyFlags, unsigned char>, llvm::ArrayRef<swift::SILValue>, swift::SILValue, llvm::SmallVectorImpl<swift::SILValue>&, swift::Lowering::ExecutorBreadcrumb) + 2708
8  swift-frontend           0x00000001054edcc8 swift::Lowering::SILGenFunction::emitApply(std::__1::unique_ptr<swift::Lowering::ResultPlan, std::__1::default_delete<swift::Lowering::ResultPlan>>&&, swift::Lowering::ArgumentScope&&, swift::SILLocation, swift::Lowering::ManagedValue, swift::SubstitutionMap, llvm::ArrayRef<swift::Lowering::ManagedValue>, swift::Lowering::CalleeTypeInfo const&, swift::optionset::OptionSet<swift::ApplyFlags, unsigned char>, swift::Lowering::SGFContext, std::__1::optional<swift::ActorIsolation>) + 2280
9  swift-frontend           0x00000001054f4e18 (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 2244
10 swift-frontend           0x00000001054f32c4 swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 3116
11 swift-frontend           0x000000010555e484 swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, std::__1::optional<swift::SILLocation>) + 128
12 swift-frontend           0x00000001055ffc98 swift::Lowering::SILGenFunction::emitReturnExpr(swift::SILLocation, swift::Expr*) + 1304
13 swift-frontend           0x00000001055fd3e8 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 8640
14 swift-frontend           0x00000001055fc844 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 5660
15 swift-frontend           0x00000001055fda70 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 10312
16 swift-frontend           0x00000001055fc844 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 5660
17 swift-frontend           0x0000000105590aa4 swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 492
18 swift-frontend           0x00000001054df3d0 swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 8252
19 swift-frontend           0x00000001054dfc2c swift::Lowering::SILGenModule::emitOrDelayFunction(swift::SILDeclRef) + 212
20 swift-frontend           0x00000001054dd380 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 172
21 swift-frontend           0x0000000105615858 SILGenExtension::visitFuncDecl(swift::FuncDecl*) + 244
22 swift-frontend           0x0000000105611878 SILGenExtension::emitExtension(swift::ExtensionDecl*) + 424
23 swift-frontend           0x00000001054dd118 swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 264
24 swift-frontend           0x00000001054e3200 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 1804
25 swift-frontend           0x00000001055fae68 swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 196
26 swift-frontend           0x00000001054e80f4 swift::ASTLoweringRequest::OutputType swift::Evaluator::getResultUncached<swift::ASTLoweringRequest, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()>(swift::ASTLoweringRequest const&, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()) + 524
27 swift-frontend           0x0000000104ac8fdc swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 744
28 swift-frontend           0x0000000104acd468 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1672
29 swift-frontend           0x0000000104acb438 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4752
30 swift-frontend           0x0000000104a512d8 swift::mainEntry(int, char const**) + 2812
31 dyld                     0x000000019fe19298 start + 2876

@hborla hborla added typed throws Feature → error handling → throws & rethrows: Typed throws and removed triage needed This issue needs more specific labels labels Jul 14, 2024
@kabiroberai
Copy link
Contributor

Not sure if this is another incarnation of the same issue, or a related-but-different one, but it's also possible to skirt the availability checks more directly:

protocol HasAsyncSequenceTraits {
    associatedtype Base: AsyncSequence
    associatedtype Failure

    @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
    associatedtype _Witness = Never where Base.Failure == Failure
}
enum AsyncSequenceTraits<Base: AsyncSequence>: HasAsyncSequenceTraits {}
typealias FailureOf<S: AsyncSequence> = AsyncSequenceTraits<S>.Failure

// absurd: would allow you to "extract" the Failure associatedtype at runtime
// despite that info not being present in ABI
func extractFailureType(of sequence: any AsyncSequence) -> Error.Type {
    func helper<S: AsyncSequence>(_ sequence: S) -> Error.Type {
        FailureOf<S>.self
    }
    return helper(sequence)
}

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. crash Bug: A crash, i.e., an abnormal termination of software typed throws Feature → error handling → throws & rethrows: Typed throws
Projects
None yet
Development

No branches or pull requests

3 participants