swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.49k stars 10.35k forks source link

Compiler crash when referencing key path on existential #76984

Open NachoSoto opened 1 week ago

NachoSoto commented 1 week ago

Reproduction

func f(_ c: [any RandomAccessCollection<Int>]) {
    let _ = c.compactMap(\.last)
}

Stack dump

1.  Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
2.  Compiling with the current language version
3.  While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "/Users/nacho/code/tests/Swift6Tests/Swift6Tests/Test.swift")
4.  While silgen emitFunction SIL function "@$s11Swift6Tests1fyySaySk_pSi7ElementSkRts_XPGF".
 for 'f(_:)' (at /Users/nacho/code/tests/Swift6Tests/Swift6Tests/Test.swift:13:1)
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           0x00000001066ef0fc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001066ed350 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x00000001066ef6c8 SignalHandler(int) + 292
3  libsystem_platform.dylib 0x0000000180b58184 _sigtramp + 56
4  swift-frontend           0x0000000101bcb010 (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::SILType, swift::Lowering::SGFContext) + 8244
5  swift-frontend           0x0000000101bd2eb4 swift::Lowering::ManagedValue llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::SILType, swift::Lowering::SGFContext)>::callback_fn<(anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::SILType, swift::Lowering::SGFContext)::$_1>(long, swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::SILType, swift::Lowering::SGFContext) + 120
6  swift-frontend           0x0000000101b1d7c8 std::__1::__function::__func<swift::Lowering::SILGenFunction::emitOptionalToOptional(swift::SILLocation, swift::Lowering::ManagedValue, swift::SILType, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::SILType, swift::Lowering::SGFContext)>, swift::Lowering::SGFContext)::$_0, std::__1::allocator<swift::Lowering::SILGenFunction::emitOptionalToOptional(swift::SILLocation, swift::Lowering::ManagedValue, swift::SILType, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::SILType, swift::Lowering::SGFContext)>, swift::Lowering::SGFContext)::$_0>, void (swift::Lowering::ManagedValue, swift::Lowering::SwitchCaseFullExpr&&)>::operator()(swift::Lowering::ManagedValue&&, swift::Lowering::SwitchCaseFullExpr&&) + 452
7  swift-frontend           0x0000000101aabdf4 swift::Lowering::SwitchEnumBuilder::emit() && + 2728
8  swift-frontend           0x0000000101b17f48 swift::Lowering::SILGenFunction::emitOptionalToOptional(swift::SILLocation, swift::Lowering::ManagedValue, swift::SILType, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::SILType, swift::Lowering::SGFContext)>, swift::Lowering::SGFContext) + 2236
9  swift-frontend           0x0000000101bc96f0 (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::SILType, swift::Lowering::SGFContext) + 1812
10 swift-frontend           0x0000000101b400ec getOrCreateKeyPathGetter(swift::Lowering::SILGenModule&, swift::AbstractStorageDecl*, swift::SubstitutionMap, swift::GenericEnvironment*, swift::ResilienceExpansion, llvm::ArrayRef<std::__1::pair<swift::CanType, swift::SILType>>, swift::CanType, swift::CanType) + 4836
11 swift-frontend           0x0000000101b3bb24 swift::Lowering::SILGenModule::emitKeyPathComponentForDecl(swift::SILLocation, swift::GenericEnvironment*, swift::ResilienceExpansion, unsigned int&, bool&, swift::SubstitutionMap, swift::AbstractStorageDecl*, llvm::ArrayRef<swift::ProtocolConformanceRef>, swift::CanType, swift::DeclContext*, bool) + 3328
12 swift-frontend           0x0000000101b5b498 (anonymous namespace)::RValueEmitter::visitKeyPathExpr(swift::KeyPathExpr*, swift::Lowering::SGFContext) + 1112
13 swift-frontend           0x0000000101b37100 swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, std::__1::optional<swift::SILLocation>) + 128
14 swift-frontend           0x0000000101b22690 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int, bool) + 2016
15 swift-frontend           0x0000000101b4e3c8 (anonymous namespace)::RValueEmitter::visitCaptureListExpr(swift::CaptureListExpr*, swift::Lowering::SGFContext) + 240
16 swift-frontend           0x0000000101b3a310 swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 40
17 swift-frontend           0x0000000101b19fb8 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)>) + 316
18 swift-frontend           0x0000000101b1a1c0 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
19 swift-frontend           0x0000000101b19f1c 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)>) + 160
20 swift-frontend           0x0000000101b5194c (anonymous namespace)::RValueEmitter::visitFunctionConversionExpr(swift::FunctionConversionExpr*, swift::Lowering::SGFContext) + 2000
21 swift-frontend           0x0000000101b3a310 swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 40
22 swift-frontend           0x0000000101b19fb8 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)>) + 316
23 swift-frontend           0x0000000101a99bf8 swift::Lowering::ArgumentSource::getConverted(swift::Lowering::SILGenFunction&, swift::Lowering::Conversion const&, swift::Lowering::SGFContext) && + 136
24 swift-frontend           0x0000000101ad8fe0 (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern, std::__1::optional<swift::AnyFunctionType::Param>) + 5528
25 swift-frontend           0x0000000101ac6e74 (anonymous namespace)::ArgEmitter::emitSingleArg(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern, std::__1::optional<swift::AnyFunctionType::Param>) + 216
26 swift-frontend           0x0000000101ad7730 (anonymous namespace)::ArgEmitter::emitPreparedArgs(swift::Lowering::PreparedArguments&&, swift::Lowering::AbstractionPattern) + 256
27 swift-frontend           0x0000000101ae35fc (anonymous namespace)::CallSite::emit(swift::Lowering::SILGenFunction&, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, (anonymous namespace)::ParamLowering&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::SmallVectorImpl<(anonymous namespace)::DelayedArgument>&, swift::ForeignInfo const&) && + 664
28 swift-frontend           0x0000000101ae301c (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, swift::ForeignInfo const&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, std::__1::optional<swift::SILLocation>&) + 1000
29 swift-frontend           0x0000000101acbc00 (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 2028
30 swift-frontend           0x0000000101aca18c swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 3116
31 swift-frontend           0x0000000101b37100 swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, std::__1::optional<swift::SILLocation>) + 128
32 swift-frontend           0x0000000101b22690 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int, bool) + 2016
33 swift-frontend           0x0000000101b295c4 swift::ASTVisitor<swift::Lowering::SILGenFunction, void, void, void, void, void, void>::visit(swift::Decl*) + 140
34 swift-frontend           0x0000000101bf20ec swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 5756
35 swift-frontend           0x0000000101b69d4c swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 492
36 swift-frontend           0x0000000101ab58a0 swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 8420
37 swift-frontend           0x0000000101ab6140 swift::Lowering::SILGenModule::emitOrDelayFunction(swift::SILDeclRef) + 216
38 swift-frontend           0x0000000101ab3770 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 364
39 swift-frontend           0x0000000101ab9dbc swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 1804
40 swift-frontend           0x0000000101bf06b0 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
41 swift-frontend           0x0000000101abec98 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'()) + 528
42 swift-frontend           0x0000000101098f34 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 920
43 swift-frontend           0x000000010109c228 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1680
44 swift-frontend           0x000000010109af58 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3572
45 swift-frontend           0x000000010102201c swift::mainEntry(int, char const**) + 3680
46 dyld                     0x00000001807a0274 start + 2840

Expected behavior

Compiles

Environment

swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
Target: arm64-apple-macosx15.0
jamieQ commented 1 week ago

for future triage: note that the issue seems like it may be specific to the interaction with associated types. e.g. this compiles:

protocol P {
    var prop: String? { get }
}

func f(_ c: [any P]) {
    let _ = c.compactMap(\.prop)
}

but these do not:

protocol P {
    associatedtype U
    var prop: U? { get }
}

func f(_ c: [any P]) {
    let _ = c.compactMap(\.prop)
}
protocol P<U> {
    associatedtype U
    var prop: U? { get }
}

func f(_ c: [any P<Int>]) {
    let _ = c.compactMap(\.prop)
}