Skip to content

ICE using _openExistential with PAT #82255

Open
@KeithBauerANZ

Description

@KeithBauerANZ

Description

No response

Reproduction

protocol P<F> {
    associatedtype F: Error
}

struct MapError<Inner: P, F: Error>: P {
    var inner: Inner
    var mapping: (Inner.F) -> F
}

extension P {
    func mapError<F2: Error>(_ mapping: @escaping (F) -> F2) -> MapError<Self, F2> {
        MapError(inner: self, mapping: mapping)
    }
}

struct AnyP<F: Error> {
    var inner: any P<F>
    init(_ inner: some P<F>) {
        self.inner = inner
    }
    func mapError<F2: Error>(_ mapping: @escaping (F) -> F2) -> AnyP<F2> {
#if false
        // this doesn't compile:
        //  member 'mapError' cannot be used on value of type 'any P<F>'; consider using a generic constraint instead
        AnyP<F2>(inner.mapError(mapping))
#elseif true
        // this crashes the compiler
        _openExistential(inner) {
            AnyP<F2>($0.mapError(mapping))
        }
#else
        // this does work:
        _mapError(inner: inner, mapping: mapping)
#endif
    }
}

func _mapError<T: P, F: Error>(inner: T, mapping: @escaping (T.F) -> F) -> AnyP<F> {
    AnyP(inner.mapError(mapping))
}

Stack dump

error: compile command failed due to signal 6 (use -v to see invocation)
Fell off the end:
(generic_type_param_type depth=1 index=0 param_kind=type)
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: /Applications/Xcode-16.4.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file TypedThrowsOpenExistential.swift -target arm64-apple-macosx15.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode-16.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.5.sdk -color-diagnostics -swift-version 6 -new-driver-path /Applications/Xcode-16.4.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode-16.4.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name TypedThrowsOpenExistential -disable-clang-spi -target-sdk-version 15.5 -target-sdk-name macosx15.5 -external-plugin-path /Applications/Xcode-16.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode-16.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-16.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode-16.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -in-process-plugin-server-path /Applications/Xcode-16.4.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/libSwiftInProcPluginServer.dylib -plugin-path /Applications/Xcode-16.4.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode-16.4.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -o /var/folders/jg/3hxr5hxx7hd3kcfjs1c7ylqr0000gp/T/TemporaryDirectory.Wx1x6M/TypedThrowsOpenExistential-1.o
1.	Apple Swift version 6.1.2 (swiftlang-6.1.2.1.2 clang-1700.0.13.5)
2.	Compiling with the current language version
3.	While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "TypedThrowsOpenExistential.swift")
4.	While silgen emitFunction SIL function "@$s26TypedThrowsOpenExistential4AnyPV8mapErroryACyqd__Gqd__xcs0G0Rd__lF".
 for 'mapError(_:)' (at TypedThrowsOpenExistential.swift:21:5)
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           0x000000010898ae24 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000108988c5c llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x000000010898b460 SignalHandler(int) + 360
3  libsystem_platform.dylib 0x0000000195c3c624 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000195c0288c pthread_kill + 296
5  libsystem_c.dylib        0x0000000195b0bc60 abort + 124
6  swift-frontend           0x000000010417ebd4 swift::MapLocalArchetypesOutOfContext::getInterfaceType(swift::Type, swift::GenericEnvironment*) const + 444
7  swift-frontend           0x00000001042c4354 swift::InFlightSubstitution::substType(swift::SubstitutableType*, unsigned int) + 48
8  swift-frontend           0x00000001042c9e24 (anonymous namespace)::TypeSubstituter::transformLocalArchetypeType(swift::LocalArchetypeType*, swift::TypePosition) + 64
9  swift-frontend           0x00000001042c5428 swift::TypeTransform<(anonymous namespace)::TypeSubstituter>::doIt(swift::Type, swift::TypePosition) + 540
10 swift-frontend           0x00000001042c5e28 swift::TypeTransform<(anonymous namespace)::TypeSubstituter>::doIt(swift::Type, swift::TypePosition) + 3100
11 swift-frontend           0x00000001042c4d1c swift::Type::subst(swift::InFlightSubstitution&) const + 976
12 swift-frontend           0x00000001042c42c8 swift::Type::subst(llvm::function_ref<swift::Type (swift::SubstitutableType*)>, llvm::function_ref<swift::ProtocolConformanceRef (swift::CanType, swift::Type, swift::ProtocolDecl*)>, swift::SubstOptions) const + 392
13 swift-frontend           0x0000000103f2e610 swift::Mangle::ASTMangler::appendClosureComponents(swift::CanType, unsigned int, bool, swift::DeclContext const*, llvm::ArrayRef<swift::GenericEnvironment*>) + 272
14 swift-frontend           0x0000000103f1afb8 swift::Mangle::ASTMangler::mangleClosureEntity(swift::AbstractClosureExpr const*, swift::Mangle::ASTMangler::SymbolKind) + 236
15 swift-frontend           0x000000010305d920 swift::SILDeclRef::mangle(swift::SILDeclRef::ManglingKind) const + 1684
16 swift-frontend           0x000000010307c064 swift::SILFunctionBuilder::getOrCreateFunction(swift::SILLocation, swift::SILDeclRef, swift::ForDefinition_t, llvm::function_ref<swift::SILFunction* (swift::SILLocation, swift::SILDeclRef)>, swift::ProfileCounter) + 76
17 swift-frontend           0x000000010324267c swift::Lowering::SILGenModule::getFunction(swift::SILDeclRef, swift::ForDefinition_t) + 428
18 swift-frontend           0x000000010324ddc4 void llvm::function_ref<void ()>::callback_fn<swift::Lowering::SILGenModule::emitClosure(swift::AbstractClosureExpr*, swift::Lowering::FunctionTypeInfo const&)::$_0>(long) + 92
19 swift-frontend           0x0000000103272620 (anonymous namespace)::SILGenApply::visitAbstractClosureExpr(swift::AbstractClosureExpr*) + 220
20 swift-frontend           0x0000000103259ad4 swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 532
21 swift-frontend           0x00000001032f6a8c void llvm::function_ref<void (swift::Expr*)>::callback_fn<swift::Lowering::RValue swift::Lowering::SILGenFunction::emitOpenExistentialExpr<swift::Lowering::RValue, (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext)::$_0>(swift::OpenExistentialExpr*, (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext)::$_0)::'lambda'(swift::Expr*)>(long, swift::Expr*) + 36
22 swift-frontend           0x00000001032d946c swift::Lowering::SILGenFunction::emitOpenExistentialExprImpl(swift::OpenExistentialExpr*, llvm::function_ref<void (swift::Expr*)>) + 1024
23 swift-frontend           0x00000001032e611c (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext) + 280
24 swift-frontend           0x00000001032cc61c swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, std::__1::optional<swift::SILLocation>) + 132
25 swift-frontend           0x00000001033888f4 swift::Lowering::SILGenFunction::emitReturnExpr(swift::SILLocation, swift::Expr*) + 404
26 swift-frontend           0x0000000103385238 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 6544
27 swift-frontend           0x0000000103300b84 swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 496
28 swift-frontend           0x000000010324524c swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 7596
29 swift-frontend           0x0000000103246278 swift::Lowering::SILGenModule::emitOrDelayFunction(swift::SILDeclRef) + 232
30 swift-frontend           0x0000000103243454 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 344
31 swift-frontend           0x000000010339dfd0 (anonymous namespace)::SILGenType::visitFuncDecl(swift::FuncDecl*) + 32
32 swift-frontend           0x000000010339a7c8 (anonymous namespace)::SILGenType::emitType() + 364
33 swift-frontend           0x0000000103243098 swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 100
34 swift-frontend           0x0000000103249ce0 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 1568
35 swift-frontend           0x0000000103382f50 swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)17>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 208
36 swift-frontend           0x000000010324e86c 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'()) + 728
37 swift-frontend           0x00000001027dc064 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 968
38 swift-frontend           0x00000001027df654 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1764
39 swift-frontend           0x00000001027ddfd8 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3716
40 swift-frontend           0x00000001027620bc swift::mainEntry(int, char const**) + 5428
41 dyld                     0x0000000195862b98 start + 6076

Expected behavior

not... crash?

Environment

swift-driver version: 1.120.5 Apple Swift version 6.1.2 (swiftlang-6.1.2.1.2 clang-1700.0.13.5)
Target: arm64-apple-macosx15.0

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 softwaretriage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions