Skip to content

Compiler crash with cast involving any existentials #71000

@GeorgeElsham

Description

@GeorgeElsham

Description

It appears that casting a wrapped any existential to another one can cause a compiler crash.

Reproduction

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

struct Foo<Value> {
    let value: Value
}

@available(iOS 16, *)
func run(a: Foo<any C>) {
    _ = a as Foo<any D<Int>>
}

Stack dump

error: compile command failed due to signal 5 (use -v to see invocation)
fromType->getCanonicalType() = (bound_generic_struct_type decl=bugreport.(file)[email protected]:8:8
  (existential_type
    (protocol_composition_type
      (protocol_type decl=bugreport.(file)[email protected]:1:10)
      (protocol_type decl=bugreport.(file)[email protected]:4:10))))
toType->getCanonicalType() = (bound_generic_struct_type decl=bugreport.(file)[email protected]:8:8
  (existential_type
    (protocol_composition_type
      (parameterized_protocol_type
        (base=protocol_type decl=bugreport.(file)[email protected]:1:10)
        (struct_type decl=Swift.(file).Int))
      (protocol_type decl=bugreport.(file)[email protected]: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.JTaMoA/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(a:)@bugreport.swift:13:6)
5.	While type-checking statement at [bugreport.swift:13:25 - line:15:1] RangeText="{
    _ = a as Foo<any D<Int>>
"
6.	While type-checking expression at [bugreport.swift:14:5 - line:14:28] RangeText="_ = a as Foo<any D<Int>"
7.	While type-checking-target starting at bugreport.swift:14: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           0x000000010703dabc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000109ca3cb0 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000109a0d054 SignalHandler(int) + 352
3  libsystem_platform.dylib 0x0000000187b5da24 _sigtramp + 56
4  swift-frontend           0x00000001086d24e8 (anonymous namespace)::ExprRewriter::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder) + 9152
5  swift-frontend           0x0000000105561724 swift::constraints::Solution::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocator*) + 176
6  swift-frontend           0x00000001065e5eac (anonymous namespace)::ExprRewriter::walkToExprPost(swift::Expr*) + 17760
7  swift-frontend           0x00000001065ce64c (anonymous namespace)::ExprWalker::walkToExprPost(swift::Expr*) + 24
8  swift-frontend           0x0000000105d04a70 (anonymous namespace)::Traversal::visitAssignExpr(swift::AssignExpr*) + 236
9  swift-frontend           0x00000001048b8020 (anonymous namespace)::Traversal::visit(swift::Expr*) (.llvm.15054325256744354582) + 1052
10 swift-frontend           0x00000001086ca88c (anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SyntacticElementTarget) + 2688
11 swift-frontend           0x00000001065c579c swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget) + 6956
12 swift-frontend           0x0000000106c09b1c swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 756
13 swift-frontend           0x00000001093a2e4c swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 436
14 swift-frontend           0x00000001099c9460 (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) (.llvm.7275418743163107908) + 396
15 swift-frontend           0x0000000106ec0840 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) (.llvm.7275418743163107908) + 296
16 swift-frontend           0x00000001099d49d8 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) (.llvm.7275418743163107908) + 316
17 swift-frontend           0x00000001099d1614 swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 2200
18 swift-frontend           0x00000001075bce58 swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType) + 884
19 swift-frontend           0x0000000106f6a9a8 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 608
20 swift-frontend           0x0000000106f70658 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest>(swift::TypeCheckSourceFileRequest const&) + 664
21 swift-frontend           0x0000000106f4ea4c swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType) + 228
22 swift-frontend           0x0000000107625a8c swift::CompilerInstance::performSema() + 160
23 swift-frontend           0x0000000109580504 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 464
24 swift-frontend           0x0000000109584854 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4568
25 swift-frontend           0x00000001095ebd44 swift::mainEntry(int, char const**) + 4408
26 dyld                     0x00000001877ad0e0 start + 2360
error: fatalError

Expected behavior

The code should either compile or produce a diagnostic, instead of crashing.

Interestingly, if you use a force-cast as! rather than as you get the following warning:

Forced cast from 'Foo<any C>' (aka 'Foo<any A & B>') to 'Foo<any D<Int>>' (aka 'Foo<any A<Int> & B>') always succeeds; did you mean to use 'as'?

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

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.castingFeature: explicit casting (is, as, as? and as!)compilerThe Swift compiler itselfcrashBug: A crash, i.e., an abnormal termination of softwareexistentialsFeature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased valuesexpressionsFeature: expressionsparameterized protocolsFeature → protocol: protocols with primary associated typesprotocol compositionsFeature → types: protocol composition typesswift 6.0type 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