Skip to content

Crash using key path to map an array of existentials #84744

@lawrence-forooghian

Description

@lawrence-forooghian

Description

Passing a key path to map to extract a value of primary associated type from an array of existentials crashes the compiler.

Reproduction

protocol Bar<Content> {
    associatedtype Content

    var content: Content { get }
}

struct BarImplementation: Bar {
    var content: String {
        "hello"
    }
}

struct Foo {
    var content: some Bar<String> {
        BarImplementation()
    }
}

let foo = Foo()
let bars: [any Bar<String>] = [foo.content]

// This causes a compiler crash
let barContents: [String] = bars.map(\.content)

Stack dump

Assertion failed: (!componentTy->hasTypeParameter()), function emitKeyPathComponentForDecl at SILGenExpr.cpp:4688.
(to display assertion configuration options: -Xllvm -assert-help)
Stack dump:
0.	Program arguments: /Applications/Xcode-26.0.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -interpret example.swift -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode-26.0.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk -no-color-diagnostics -Xcc -fno-color-diagnostics -new-driver-path /Applications/Xcode-26.0.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -no-auto-bridging-header-chaining -module-name example -disable-clang-spi -target-sdk-version 26.0 -target-sdk-name macosx26.0 -external-plugin-path /Applications/Xcode-26.0.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode-26.0.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-26.0.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode-26.0.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -in-process-plugin-server-path /Applications/Xcode-26.0.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/libSwiftInProcPluginServer.dylib -plugin-path /Applications/Xcode-26.0.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode-26.0.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins
1.	Apple Swift version 6.2 (swiftlang-6.2.0.19.9 clang-1700.3.19.1)
2.	Compiling with effective version 5.10
3.	While evaluating request ASTLoweringRequest(Lowering AST to SIL for module example)
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           0x0000000106961bcc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x000000010695f55c llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x00000001069621f8 SignalHandler(int, __siginfo*, void*) + 344
3  libsystem_platform.dylib 0x00000001824fe744 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001824f4888 pthread_kill + 296
5  libsystem_c.dylib        0x00000001823fa808 abort + 124
6  swift-frontend           0x00000001022263c0 ASSERT_failure(char const*, char const*, int, char const*) + 1720
7  swift-frontend           0x0000000101121510 swift::Lowering::SILGenModule::emitKeyPathComponentForDecl(swift::SILLocation, swift::GenericEnvironment*, swift::ResilienceExpansion, unsigned int&, bool&, swift::SubstitutionMap, swift::ValueDecl*, llvm::ArrayRef<swift::ProtocolConformanceRef>, swift::CanType, swift::DeclContext*, bool, bool) + 11360
8  swift-frontend           0x00000001011431d4 (anonymous namespace)::RValueEmitter::visitKeyPathExpr(swift::KeyPathExpr*, swift::Lowering::SGFContext) + 1180
9  swift-frontend           0x000000010111a920 swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, std::__1::optional<swift::SILLocation>) + 132
10 swift-frontend           0x0000000101104348 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int, bool) + 1728
11 swift-frontend           0x0000000101134e7c (anonymous namespace)::RValueEmitter::visitCaptureListExpr(swift::CaptureListExpr*, swift::Lowering::SGFContext) + 248
12 swift-frontend           0x000000010111dc80 swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 40
13 swift-frontend           0x00000001010fb964 swift::Lowering::SILGenFunction::emitConvertedRValue(swift::SILLocation, swift::Lowering::Conversion const&, swift::Lowering::SGFContext, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::SGFContext)>) + 300
14 swift-frontend           0x00000001010fbb6c swift::Lowering::ConvertingInitialization::tryPeephole(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::Conversion, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::SGFContext)>) + 360
15 swift-frontend           0x00000001010fb8d4 swift::Lowering::SILGenFunction::emitConvertedRValue(swift::SILLocation, swift::Lowering::Conversion const&, swift::Lowering::SGFContext, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::SGFContext)>) + 156
16 swift-frontend           0x0000000101138bfc (anonymous namespace)::RValueEmitter::visitFunctionConversionExpr(swift::FunctionConversionExpr*, swift::Lowering::SGFContext) + 3216
17 swift-frontend           0x000000010111dc80 swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 40
18 swift-frontend           0x00000001010fb964 swift::Lowering::SILGenFunction::emitConvertedRValue(swift::SILLocation, swift::Lowering::Conversion const&, swift::Lowering::SGFContext, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::SGFContext)>) + 300
19 swift-frontend           0x0000000101071d58 swift::Lowering::ArgumentSource::getConverted(swift::Lowering::SILGenFunction&, swift::Lowering::Conversion const&, swift::Lowering::SGFContext) && + 124
20 swift-frontend           0x00000001010b5bfc (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern, bool, std::__1::optional<swift::AnyFunctionType::Param>) + 5964
21 swift-frontend           0x00000001010a24e0 (anonymous namespace)::ArgEmitter::emitSingleArg(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern, bool, std::__1::optional<swift::AnyFunctionType::Param>) + 204
22 swift-frontend           0x00000001010b3af0 (anonymous namespace)::ArgEmitter::emitPreparedArgs(swift::Lowering::PreparedArguments&&, swift::Lowering::AbstractionPattern, llvm::ArrayRef<swift::LifetimeDependenceInfo>) + 304
23 swift-frontend           0x00000001010c0a20 (anonymous namespace)::CallSite::emit(swift::Lowering::SILGenFunction&, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, llvm::ArrayRef<swift::LifetimeDependenceInfo>, (anonymous namespace)::ParamLowering&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::SmallVectorImpl<(anonymous namespace)::DelayedArgument>&, swift::ForeignInfo const&) && + 676
24 swift-frontend           0x00000001010c01d8 (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, llvm::ArrayRef<swift::LifetimeDependenceInfo>, swift::ForeignInfo const&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, std::__1::optional<swift::SILLocation>&) + 1112
25 swift-frontend           0x00000001010a7100 (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 1092
26 swift-frontend           0x00000001010a5a3c swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 3204
27 swift-frontend           0x000000010111a920 swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, std::__1::optional<swift::SILLocation>) + 132
28 swift-frontend           0x0000000101104348 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int, bool) + 1728
29 swift-frontend           0x000000010110b804 swift::ASTVisitor<swift::Lowering::SILGenFunction, void, void, void, void, void, void>::visit(swift::Decl*) + 164
30 swift-frontend           0x00000001011ec85c swift::ASTVisitor<swift::Lowering::SILGenTopLevel, void, void, void, void, void, void>::visit(swift::Decl*) + 536
31 swift-frontend           0x00000001011e9ad4 swift::Lowering::SILGenModule::emitEntryPoint(swift::SourceFile*, swift::SILFunction*) + 1340
32 swift-frontend           0x00000001011ec440 swift::Lowering::SILGenModule::emitEntryPoint(swift::SourceFile*) + 208
33 swift-frontend           0x0000000101093484 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 2184
34 swift-frontend           0x00000001011d961c 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
35 swift-frontend           0x0000000101097fc0 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'()) + 572
36 swift-frontend           0x000000010059bd7c swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 2616
37 swift-frontend           0x000000010059f6dc performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 4932
38 swift-frontend           0x000000010059d68c swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3580
39 swift-frontend           0x000000010051ec6c swift::mainEntry(int, char const**) + 5412
40 dyld                     0x0000000182135d54 start + 7184

Expected behavior

The code should compile, since it compiles fine when you instead write the functionally equivalent

let barContents: [String] = bars.map { $0.content }

Environment

swift-driver version: 1.127.14.1 Apple Swift version 6.2 (swiftlang-6.2.0.19.9 clang-1700.3.19.1)
Target: arm64-apple-macosx26.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