Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler crash with optional existential #71019

Closed
GeorgeElsham opened this issue Jan 19, 2024 · 2 comments
Closed

Compiler crash with optional existential #71019

GeorgeElsham opened this issue Jan 19, 2024 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values type checker Area → compiler: Semantic analysis

Comments

@GeorgeElsham
Copy link

Description

Another related bug to my recent reports (#70988, #71000, #71012). It could be a duplicate, but I'd think the more reports the better—all occurring in different scenarios. All these reports would make good test cases.

In this case, it appears the compiler crashes when trying to infer how to implicitly cast an optional existential.

Reproduction

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

@available(iOS 16, *)
func convert(from input: (any D<Int>)?) -> (any C)? {
    input
}

Stack dump

error: compile command failed due to signal 5 (use -v to see invocation)
fromType->getCanonicalType() = (bound_generic_enum_type decl=Swift.(file).Optional
  (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))))
toType->getCanonicalType() = (bound_generic_enum_type decl=Swift.(file).Optional
  (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))))
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.q9TKyD/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).convert(from:)@bugreport.swift:9:6)
5.	While type-checking statement at [bugreport.swift:9:53 - line:11:1] RangeText="{
    input
"
6.	While type-checking statement at [bugreport.swift:10:5 - line:10:5] RangeText=""
7.	While type-checking expression at [bugreport.swift:10:5 - line:10:5] RangeText=""
8.	While type-checking-target starting at bugreport.swift:10: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           0x0000000107089abc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000109cefcb0 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000109a59054 SignalHandler(int) + 352
3  libsystem_platform.dylib 0x0000000187b5da24 _sigtramp + 56
4  swift-frontend           0x000000010871e4e8 (anonymous namespace)::ExprRewriter::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder) + 9152
5  swift-frontend           0x0000000108717668 (anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SyntacticElementTarget) + 6236
6  swift-frontend           0x000000010661179c swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget) + 6956
7  swift-frontend           0x0000000106c55b1c swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 756
8  swift-frontend           0x00000001093eee4c swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 436
9  swift-frontend           0x0000000106c557c8 swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::constraints::ContextualTypeInfo, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 84
10 swift-frontend           0x0000000106f0e1d0 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) (.llvm.7275418743163107908) + 6840
11 swift-frontend           0x0000000109a30e70 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::Stmt>(swift::Stmt*&) + 316
12 swift-frontend           0x0000000109a1533c (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) (.llvm.7275418743163107908) + 104
13 swift-frontend           0x0000000106f0c840 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) (.llvm.7275418743163107908) + 296
14 swift-frontend           0x0000000109a209d8 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) (.llvm.7275418743163107908) + 316
15 swift-frontend           0x0000000109a1d614 swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 2200
16 swift-frontend           0x0000000107608e58 swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType) + 884
17 swift-frontend           0x0000000106fb69a8 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 608
18 swift-frontend           0x0000000106fbc658 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest>(swift::TypeCheckSourceFileRequest const&) + 664
19 swift-frontend           0x0000000106f9aa4c swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType) + 228
20 swift-frontend           0x0000000107671a8c swift::CompilerInstance::performSema() + 160
21 swift-frontend           0x00000001095cc504 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 464
22 swift-frontend           0x00000001095d0854 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4568
23 swift-frontend           0x0000000109637d44 swift::mainEntry(int, char const**) + 4408
24 dyld                     0x00000001877ad0e0 start + 2360
error: fatalError

Expected behavior

The compiler should not crash. It should also work out how to implicitly cast this optional existential.

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

Manually use a let-binding with the optional.

@available(iOS 16, *)
func convert(from input: (any D<Int>)?) -> (any C)? {
    guard let input else {
        return nil
    }
    return input
}
@GeorgeElsham GeorgeElsham added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Jan 19, 2024
@hborla hborla added type checker Area → compiler: Semantic analysis existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values and removed triage needed This issue needs more specific labels labels Jul 14, 2024
@slavapestov
Copy link
Contributor

slavapestov commented Aug 19, 2024

typealias D<T> = A<T> & B

A composition containing a constrained protocol type is not supported

@slavapestov
Copy link
Contributor

Duplicate of #63877

@slavapestov slavapestov marked this as a duplicate of #63877 Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants