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: Unhandled coercion #65189

Closed
jasonbobier opened this issue Apr 14, 2023 · 8 comments
Closed

Compiler crash: Unhandled coercion #65189

jasonbobier opened this issue Apr 14, 2023 · 8 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software duplicate Resolution: Duplicates another issue existential member accesses Feature → existentials: existential member accesses existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values type checker Area → compiler: Semantic analysis

Comments

@jasonbobier
Copy link

jasonbobier commented Apr 14, 2023

Description

Attempting to access a member of an existential crashes.

Steps to reproduce

Please run this code:

protocol U {}

protocol CU<VT>: U {
	associatedtype VT
}

protocol TN: Numeric {}

extension Double: TN { }

struct EU<VT: TN & Hashable>: CU {}

protocol MP {
	associatedtype VT: TN
	associatedtype UT: U

	var value: VT { get set }
	var unit: UT { get set }
}


struct M<VT: TN, UT: U>: MP {
	var value: VT
	var unit: UT
}

let m = M(value: 1.0, unit: EU<Double>())

var mp: any MP = m

let v = mp.value

This produces the following crash in a playground:

Thread 15 Crashed:: RPC packet thread for client tid 009e689b (10381467)
0   libsystem_kernel.dylib        	       0x187b28724 __pthread_kill + 8
1   libsystem_pthread.dylib       	       0x187b5fc28 pthread_kill + 288
2   libsystem_c.dylib             	       0x187a6dae8 abort + 180
3   LLDB                          	       0x11a07843c (anonymous namespace)::ExprRewriter::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder, llvm::Optional<swift::Pattern*>) (.cold.1) + 64
4   LLDB                          	       0x116b2abb0 (anonymous namespace)::ExprRewriter::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder, llvm::Optional<swift::Pattern*>) + 7580
5   LLDB                          	       0x116b2a374 (anonymous namespace)::ExprRewriter::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder, llvm::Optional<swift::Pattern*>) + 5472
6   LLDB                          	       0x116b40bb8 (anonymous namespace)::ExprRewriter::closeExistential(swift::Expr*&, swift::constraints::ConstraintLocatorBuilder, bool) + 328
7   LLDB                          	       0x116b3bef0 (anonymous namespace)::ExprRewriter::buildMemberRef(swift::Expr*, swift::SourceLoc, swift::constraints::SelectedOverload, swift::DeclNameLoc, swift::constraints::ConstraintLocatorBuilder, swift::constraints::ConstraintLocatorBuilder, bool, swift::AccessSemantics) + 6160
8   LLDB                          	       0x116b3228c (anonymous namespace)::ExprRewriter::walkToExprPost(swift::Expr*) + 15612
9   LLDB                          	       0x116b2c090 (anonymous namespace)::ExprWalker::walkToExprPost(swift::Expr*) + 24
10  LLDB                          	       0x116faf768 swift::Expr::walk(swift::ASTWalker&) + 116
11  LLDB                          	       0x116b27278 (anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SolutionApplicationTarget) + 284
12  LLDB                          	       0x116b26d50 swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SolutionApplicationTarget) + 568
13  LLDB                          	       0x116cf66d0 swift::TypeChecker::typeCheckTarget(swift::constraints::SolutionApplicationTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 512
14  LLDB                          	       0x116cf64a8 swift::TypeChecker::typeCheckExpression(swift::constraints::SolutionApplicationTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 176
15  LLDB                          	       0x116cf7584 swift::TypeChecker::typeCheckBinding(swift::Pattern*&, swift::Expr*&, swift::DeclContext*, swift::Type, swift::PatternBindingDecl*, unsigned int, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 192
16  LLDB                          	       0x116cf7778 swift::TypeChecker::typeCheckPatternBinding(swift::PatternBindingDecl*, unsigned int, swift::Type, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 288
17  LLDB                          	       0x116db03c0 swift::PatternBindingEntryRequest::evaluate(swift::Evaluator&, swift::PatternBindingDecl*, unsigned int, bool) const + 2036
18  LLDB                          	       0x116d370ac llvm::Expected<swift::PatternBindingEntryRequest::OutputType> swift::Evaluator::getResultUncached<swift::PatternBindingEntryRequest>(swift::PatternBindingEntryRequest const&) + 268
19  LLDB                          	       0x116d36f40 swift::PatternBindingEntryRequest::OutputType swift::evaluateOrDefault<swift::PatternBindingEntryRequest>(swift::Evaluator&, swift::PatternBindingEntryRequest, swift::PatternBindingEntryRequest::OutputType) + 108
20  LLDB                          	       0x116d2a710 (anonymous namespace)::DeclChecker::visit(swift::Decl*) + 3256
21  LLDB                          	       0x116d29a40 swift::TypeChecker::typeCheckDecl(swift::Decl*, bool) + 120
22  LLDB                          	       0x116dabf1c swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 292
23  LLDB                          	       0x116dab1b8 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 136
24  LLDB                          	       0x116dab234 swift::TypeChecker::typeCheckTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 52
25  LLDB                          	       0x116ddb7b4 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 204
26  LLDB                          	       0x116ddd14c llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest>(swift::TypeCheckSourceFileRequest const&) + 268
27  LLDB                          	       0x116ddcfb0 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultCached<swift::TypeCheckSourceFileRequest, (void*)0>(swift::TypeCheckSourceFileRequest const&) + 68
28  LLDB                          	       0x116ddb67c swift::performTypeChecking(swift::SourceFile&) + 48
29  LLDB                          	       0x1159f1060 lldb_private::SwiftExpressionParser::Parse(lldb_private::DiagnosticManager&, unsigned int, unsigned int) + 7548
30  LLDB                          	       0x115a0169c lldb_private::SwiftUserExpression::GetTextAndSetExpressionParser(lldb_private::DiagnosticManager&, std::__1::unique_ptr<lldb_private::SwiftExpressionSourceCode, std::__1::default_delete<lldb_private::SwiftExpressionSourceCode>>&, lldb_private::ExecutionContext&, lldb_private::ExecutionContextScope*) + 4640
31  LLDB                          	       0x115a02020 lldb_private::SwiftUserExpression::Parse(lldb_private::DiagnosticManager&, lldb_private::ExecutionContext&, lldb_private::ExecutionPolicy, bool, bool) + 1272
32  LLDB                          	       0x11583cb68 lldb_private::UserExpression::Evaluate(lldb_private::ExecutionContext&, lldb_private::EvaluateExpressionOptions const&, llvm::StringRef, llvm::StringRef, std::__1::shared_ptr<lldb_private::ValueObject>&, lldb_private::Status&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>*, lldb_private::ValueObject*) + 2048
33  LLDB                          	       0x11593c6bc lldb_private::Target::EvaluateExpression(llvm::StringRef, lldb_private::ExecutionContextScope*, std::__1::shared_ptr<lldb_private::ValueObject>&, lldb_private::EvaluateExpressionOptions const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>*, lldb_private::ValueObject*) + 832
34  LLDB                          	       0x115618a44 lldb::SBFrame::EvaluateExpression(char const*, lldb::SBExpressionOptions const&) + 444
35  lldb-rpc-server               	       0x1045451dc rpc_server::_ZN4lldb7SBFrame18EvaluateExpressionEPKcRKNS_19SBExpressionOptionsE::HandleRPCCall(rpc_common::Connection&, rpc_common::RPCStream&, rpc_common::RPCStream&) + 208
36  lldb-rpc-server               	       0x10455a7a4 rpc_common::Connection::PrivateHandleRPCPacket(rpc_common::RPCPacket&, rpc_common::RPCPacket&, bool&) + 628
37  lldb-rpc-server               	       0x10455b6e8 rpc_common::Connection::HandleRPCPacket(rpc_common::RPCPacket&) + 72
38  lldb-rpc-server               	       0x10455e58c Packets::ProcessPackets() + 660
39  lldb-rpc-server               	       0x10455e26c Packets::ReadThread() + 276
40  lldb-rpc-server               	       0x10455e14c Packets::RunReadThread(void*) + 12
41  libsystem_pthread.dylib       	       0x187b5ffa8 _pthread_start + 148
42  libsystem_pthread.dylib       	       0x187b5ada0 thread_start + 8

Environment
swift-driver version: 1.75.2 Apple Swift version 5.8 (swiftlang-5.8.0.124.2 clang-1403.0.22.11.100)
Xcode 14.3
Build version 14E222b

Also note, filed with more information here: FB12117149

@jasonbobier jasonbobier added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Apr 14, 2023
@xedin
Copy link
Contributor

xedin commented Apr 14, 2023

@angela-laar Another instance of l-value existential type being opened.

@xedin xedin added type checker Area → compiler: Semantic analysis compiler crash and removed triage needed This issue needs more specific labels labels Apr 14, 2023
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler itself 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 existential member accesses Feature → existentials: existential member accesses duplicate Resolution: Duplicates another issue labels Apr 14, 2023
@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Apr 14, 2023

Reduced:

protocol TN: Numeric {}
protocol MP {
  associatedtype VT: TN

  var value: VT { get set }
}

var mp: any MP

let v = mp.value

@jasonbobier FYI currently you are not supposed to be allowed to access a {get set} requirement on an existential if it has a type parameter in its type (only if any such type parameter is known to be concrete).

@AnthonyLatsis AnthonyLatsis closed this as not planned Won't fix, can't repro, duplicate, stale Apr 14, 2023
@AnthonyLatsis
Copy link
Collaborator

Duplicate of #62219

@AnthonyLatsis AnthonyLatsis marked this as a duplicate of #62219 Apr 14, 2023
@jasonbobier
Copy link
Author

OK, thanks. So I can do something like this? (seems to work)

func unbox(m: some MP) -> some MP {
	return m
}

let v = unbox(m: mp).value

@AnthonyLatsis
Copy link
Collaborator

Sure thing, these limitations are exclusive to values of any type.

A hand-written getter will do the job as well:

extension MP {
  func getValue() -> VT { value }
}

@jasonbobier
Copy link
Author

With the solution being that simple, is there something missing about why it isn't implemented in the compiler itself?

@xedin
Copy link
Contributor

xedin commented Apr 15, 2023

It’s a just bug we need to fix.

@jasonbobier
Copy link
Author

Awesome! Thanks for the help and explanation all!

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. compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software duplicate Resolution: Duplicates another issue existential member accesses Feature → existentials: existential member accesses 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