Skip to content

Compiler crash with generics involving existentials #71012

Closed as duplicate of#63877
@GeorgeElsham

Description

@GeorgeElsham

Description

I've found another issue with existentials, potentially related to my recent issues #70988 and #71000.

Reproduction

protocol A<T> {
    associatedtype T
}
protocol B {}
typealias C = A & B
typealias D<T> = A<T> & B

struct Foo: C {
    typealias T = Int
}

struct Bar<Value> {
    let value: Value
}

@available(iOS 16, *)
struct Baz<U> {
    let bar: Bar<any D<U>>
}

@available(iOS 16, *)
func run() {
    let foo: any C = Foo()
    let bar = Bar(value: foo)
    _ = Baz<Int>(bar: bar)
}

Stack dump

error: compile command failed due to signal 5 (use -v to see invocation)
fromType->getCanonicalType() = (bound_generic_struct_type decl=bugreport.(file).Bar@bugreport.swift:12:8
  (existential_type
    (protocol_composition_type
      (protocol_type decl=bugreport.(file).A@bugreport.swift:1:10)
      (protocol_type decl=bugreport.(file).B@bugreport.swift:4:10))))
toType->getCanonicalType() = (bound_generic_struct_type decl=bugreport.(file).Bar@bugreport.swift:12:8
  (existential_type
    (protocol_composition_type
      (parameterized_protocol_type
        (base=protocol_type decl=bugreport.(file).A@bugreport.swift:1:10)
        (struct_type decl=Swift.(file).Int))
      (protocol_type decl=bugreport.(file).B@bugreport.swift:4:10))))
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: /Applications/Xcode-15.2.0-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file bugreport.swift -target arm64-apple-macosx14.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode-15.2.0-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -color-diagnostics -new-driver-path /Applications/Xcode-15.2.0-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode-15.2.0-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name bugreport -disable-clang-spi -target-sdk-version 14.2 -target-sdk-name macosx14.2 -external-plugin-path /Applications/Xcode-15.2.0-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/lib/swift/host/plugins#/Applications/Xcode-15.2.0-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-15.2.0-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode-15.2.0-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-15.2.0-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode-15.2.0-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-15.2.0-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode-15.2.0-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode-15.2.0-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode-15.2.0-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -o /var/folders/tz/qd43l62d02n828rbr1rkf4bh0000gn/T/TemporaryDirectory.pAXpov/bugreport-1.o
1.	Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
2.	Compiling with the current language version
3.	While evaluating request TypeCheckSourceFileRequest(source_file "bugreport.swift")
4.	While evaluating request TypeCheckFunctionBodyRequest(bugreport.(file).run()@bugreport.swift:22:6)
5.	While type-checking statement at [bugreport.swift:22:12 - line:26:1] RangeText="{
    let foo: any C = Foo()
    let bar = Bar(value: foo)
    _ = Baz<Int>(bar: bar)
"
6.	While type-checking expression at [bugreport.swift:25:5 - line:25:26] RangeText="_ = Baz<Int>(bar: bar"
7.	While type-checking-target starting at bugreport.swift:25: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           0x0000000104c3dabc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001078a3cb0 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x000000010760d054 SignalHandler(int) + 352
3  libsystem_platform.dylib 0x0000000187b5da24 _sigtramp + 56
4  swift-frontend           0x00000001062d24e8 (anonymous namespace)::ExprRewriter::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder) + 9152
5  swift-frontend           0x000000010631c89c (anonymous namespace)::ExprRewriter::coerceCallArguments(swift::ArgumentList*, swift::AnyFunctionType*, swift::ConcreteDeclRef, swift::ApplyExpr*, swift::constraints::ConstraintLocatorBuilder, llvm::ArrayRef<swift::AppliedPropertyWrapper>) + 6440
6  swift-frontend           0x00000001041fc7dc (anonymous namespace)::ExprRewriter::finishApply(swift::ApplyExpr*, swift::Type, swift::constraints::ConstraintLocatorBuilder, swift::constraints::ConstraintLocatorBuilder) + 6100
7  swift-frontend           0x00000001041fcd74 (anonymous namespace)::ExprRewriter::finishApply(swift::ApplyExpr*, swift::Type, swift::constraints::ConstraintLocatorBuilder, swift::constraints::ConstraintLocatorBuilder) + 7532
8  swift-frontend           0x000000010634bad8 (anonymous namespace)::ExprRewriter::visitApplyExpr(swift::ApplyExpr*) + 508
9  swift-frontend           0x00000001041e19c4 (anonymous namespace)::ExprRewriter::walkToExprPost(swift::Expr*) + 120
10 swift-frontend           0x00000001041ce64c (anonymous namespace)::ExprWalker::walkToExprPost(swift::Expr*) + 24
11 swift-frontend           0x0000000103904a70 (anonymous namespace)::Traversal::visitAssignExpr(swift::AssignExpr*) + 236
12 swift-frontend           0x00000001024b8020 (anonymous namespace)::Traversal::visit(swift::Expr*) (.llvm.15054325256744354582) + 1052
13 swift-frontend           0x00000001062ca88c (anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SyntacticElementTarget) + 2688
14 swift-frontend           0x00000001041c579c swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget) + 6956
15 swift-frontend           0x0000000104809b1c swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 756
16 swift-frontend           0x0000000106fa2e4c swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 436
17 swift-frontend           0x00000001075c9460 (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) (.llvm.7275418743163107908) + 396
18 swift-frontend           0x0000000104ac0840 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) (.llvm.7275418743163107908) + 296
19 swift-frontend           0x00000001075d49d8 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) (.llvm.7275418743163107908) + 316
20 swift-frontend           0x00000001075d1614 swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 2200
21 swift-frontend           0x00000001051bce58 swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType) + 884
22 swift-frontend           0x0000000104b6a9a8 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 608
23 swift-frontend           0x0000000104b70658 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest>(swift::TypeCheckSourceFileRequest const&) + 664
24 swift-frontend           0x0000000104b4ea4c swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType) + 228
25 swift-frontend           0x0000000105225a8c swift::CompilerInstance::performSema() + 160
26 swift-frontend           0x0000000107180504 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 464
27 swift-frontend           0x0000000107184854 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4568
28 swift-frontend           0x00000001071ebd44 swift::mainEntry(int, char const**) + 4408
29 dyld                     0x00000001877ad0e0 start + 2360
error: fatalError

Expected behavior

Not to crash the compiler. The code should either correctly compile, or produce a diagnostic.

Environment

swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx14.0

Additional information

Workaround

Explicitly set type of bar, changing:

let bar = Bar(value: foo)

To:

let bar: Bar<any D<Int>> = Bar(value: foo)

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfconstrained existentialsFeature → existentials: constrained existentials such as 'any Collection<Int>'crashBug: A crash, i.e., an abnormal termination of softwareexistentialsFeature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased valuesgenericsFeature: generic declarations and typesprotocol compositionsFeature → types: protocol composition typesswift 5.9type checkerArea → compiler: Semantic analysistypesFeature: types

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions