swiftlang / swift

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

Unhandled coercion crash when operating on protocol with associated type #74314

Closed rnapier closed 5 months ago

rnapier commented 5 months ago

Description

The following code crashes the 6.0.0.3.300 compiler, but is successfully compiled by the 5.10.0.13 compiler.

Reproduction

struct ActionHolder {
    let action: (any ModelHolder) -> Void
}

protocol ModelHolder: AnyObject {
    associatedtype Model
    var model: Model { get set }
}

func run()  {
    _ = ActionHolder { _ = $0.model }
}

Stack dump

error: compile command failed due to signal 6 (use -v to see invocation)
Unhandled coercion:
(opened_archetype_type address=0x139061dd8 conforms_to="Swift.(file).Copyable" conforms_to="Swift.(file).Escapable" opened_existential_id="63C0B36A-2840-11EF-8D9E-9E81B9574525"
  (interface_type=dependent_member_type assoc_type="main.(file).ModelHolder.Model@main.swift:6:20"
    (base=generic_type_param_type depth=0 index=0))
  (opened_existential=existential_type
    (protocol_type decl="main.(file).ModelHolder@main.swift:5:10")))
(lvalue_type
  (existential_type
    (protocol_composition_type)))
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.  Program arguments: /Applications/Xcode-16.0.0-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file main.swift -target arm64-apple-macosx14.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode-16.0.0-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk -color-diagnostics -new-driver-path /Applications/Xcode-16.0.0-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode-16.0.0-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name main -disable-clang-spi -target-sdk-version 15.0 -target-sdk-name macosx15.0 -external-plugin-path /Applications/Xcode-16.0.0-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode-16.0.0-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-16.0.0-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode-16.0.0-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode-16.0.0-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode-16.0.0-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -o /var/folders/v9/v79hbhjd357fb89355jdx5600000gr/T/TemporaryDirectory.11JMMM/main-1.o
1.  Apple Swift version 6.0 (swiftlang-6.0.0.3.300 clang-1600.0.20.10)
2.  Compiling with effective version 5.10
3.  While evaluating request TypeCheckSourceFileRequest(source_file "main.swift")
4.  While evaluating request TypeCheckFunctionBodyRequest(main.(file).run()@main.swift:10:6)
5.  While type-checking statement at [main.swift:10:13 - line:12:1] RangeText="{
    _ = ActionHolder { _ = $0.model }
"
6.  While type-checking expression at [main.swift:11:5 - line:11:37] RangeText="_ = ActionHolder { _ = $0.model "
7.  While type-checking-target starting at main.swift:11: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           0x0000000105c2acd0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000105c28f44 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000105c2b2d8 SignalHandler(int) + 352
3  libsystem_platform.dylib 0x0000000192a9f584 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000192a6ec20 pthread_kill + 288
5  libsystem_c.dylib        0x000000019297ba30 abort + 180
6  swift-frontend           0x000000010168554c (anonymous namespace)::ExprRewriter::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder) + 9980
7  swift-frontend           0x0000000101684698 (anonymous namespace)::ExprRewriter::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder) + 6216
8  swift-frontend           0x00000001016a4738 (anonymous namespace)::ExprRewriter::closeExistential(swift::Expr*&, swift::constraints::ConstraintLocatorBuilder, bool) + 596
9  swift-frontend           0x000000010169d3d4 (anonymous namespace)::ExprRewriter::buildMemberRef(swift::Expr*, swift::SourceLoc, swift::constraints::SelectedOverload, swift::DeclNameLoc, swift::constraints::ConstraintLocatorBuilder, swift::constraints::ConstraintLocatorBuilder, bool, swift::AccessSemantics) + 9324
10 swift-frontend           0x000000010168f294 (anonymous namespace)::ExprRewriter::walkToExprPost(swift::Expr*) + 21080
11 swift-frontend           0x0000000101686aa4 (anonymous namespace)::ExprWalker::walkToExprPost(swift::Expr*) + 24
12 swift-frontend           0x0000000101d166a8 (anonymous namespace)::Traversal::visitAssignExpr(swift::AssignExpr*) + 228
13 swift-frontend           0x0000000101d14c9c (anonymous namespace)::Traversal::visit(swift::Expr*) + 1100
14 swift-frontend           0x0000000101680de8 (anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SyntacticElementTarget) + 344
15 swift-frontend           0x0000000101686d4c std::__1::__function::__func<(anonymous namespace)::ExprWalker::rewriteFunction(swift::AnyFunctionRef)::'lambda'(swift::constraints::SyntacticElementTarget), std::__1::allocator<(anonymous namespace)::ExprWalker::rewriteFunction(swift::AnyFunctionRef)::'lambda'(swift::constraints::SyntacticElementTarget)>, std::__1::optional<swift::constraints::SyntacticElementTarget> (swift::constraints::SyntacticElementTarget)>::operator()(swift::constraints::SyntacticElementTarget&&) + 80
16 swift-frontend           0x00000001016d13dc (anonymous namespace)::SyntacticElementSolutionApplication::visitReturnStmt(swift::ReturnStmt*) + 832
17 swift-frontend           0x00000001016cfba8 (anonymous namespace)::SyntacticElementSolutionApplication::visit(swift::Stmt*, bool) + 540
18 swift-frontend           0x00000001016d0da0 (anonymous namespace)::SyntacticElementSolutionApplication::visit(swift::Stmt*, bool) + 5140
19 swift-frontend           0x00000001016cdc00 (anonymous namespace)::SyntacticElementSolutionApplication::apply() + 184
20 swift-frontend           0x00000001016cda58 swift::constraints::ConstraintSystem::applySolutionToBody(swift::constraints::Solution&, swift::AnyFunctionRef, swift::DeclContext*&, std::__1::function<std::__1::optional<swift::constraints::SyntacticElementTarget> (swift::constraints::SyntacticElementTarget)>) + 372
21 swift-frontend           0x00000001016cd518 swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::AnyFunctionRef, swift::DeclContext*&, std::__1::function<std::__1::optional<swift::constraints::SyntacticElementTarget> (swift::constraints::SyntacticElementTarget)>) + 1396
22 swift-frontend           0x0000000101686b9c (anonymous namespace)::ExprWalker::rewriteFunction(swift::AnyFunctionRef) + 76
23 swift-frontend           0x0000000101685d18 (anonymous namespace)::ExprWalker::walkToExprPre(swift::Expr*) + 76
24 swift-frontend           0x0000000101d1a79c (anonymous namespace)::Traversal::visit(swift::ArgumentList*) + 248
25 swift-frontend           0x0000000101d17384 (anonymous namespace)::Traversal::visitApplyExpr(swift::ApplyExpr*) + 196
26 swift-frontend           0x0000000101d148a8 (anonymous namespace)::Traversal::visit(swift::Expr*) + 88
27 swift-frontend           0x0000000101d1668c (anonymous namespace)::Traversal::visitAssignExpr(swift::AssignExpr*) + 200
28 swift-frontend           0x0000000101d14c9c (anonymous namespace)::Traversal::visit(swift::Expr*) + 1100
29 swift-frontend           0x0000000101680de8 (anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SyntacticElementTarget) + 344
30 swift-frontend           0x00000001016807ec swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget) + 7108
31 swift-frontend           0x000000010193bb28 swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 680
32 swift-frontend           0x000000010193b75c swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 416
33 swift-frontend           0x0000000101a23388 (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) + 332
34 swift-frontend           0x0000000101a28880 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 288
35 swift-frontend           0x0000000101a26294 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 316
36 swift-frontend           0x0000000101a24e30 swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 1512
37 swift-frontend           0x0000000101e2f4dc swift::TypeCheckFunctionBodyRequest::OutputType swift::Evaluator::getResultUncached<swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType)::'lambda'()>(swift::TypeCheckFunctionBodyRequest const&, swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType)::'lambda'()) + 632
38 swift-frontend           0x0000000101da01fc swift::AbstractFunctionDecl::getTypecheckedBody() const + 160
39 swift-frontend           0x0000000101a73ab4 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 868
40 swift-frontend           0x0000000101a7b098 swift::TypeCheckSourceFileRequest::OutputType swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()>(swift::TypeCheckSourceFileRequest const&, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()) + 616
41 swift-frontend           0x0000000101a73734 swift::performTypeChecking(swift::SourceFile&) + 328
42 swift-frontend           0x00000001009cbc60 swift::CompilerInstance::performSema() + 260
43 swift-frontend           0x00000001005f93d0 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1520
44 swift-frontend           0x00000001005f7438 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4752
45 swift-frontend           0x000000010057d2d8 swift::mainEntry(int, char const**) + 2812
46 dyld                     0x00000001926e60e0 start + 2360

Expected behavior

This code should compile.

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4) Target: arm64-apple-macosx14.0

Additional information

No response

hamishknight commented 5 months ago

Minimized a bit more:

protocol P {
  associatedtype X
  var x: X { get set }
}

func foo(_ x: any P)  {
  var x = x
  _ = x.x
}

cc @xedin

Looks like:

rdar://121214563 is tracking fixing this internally

AnthonyLatsis commented 5 months ago

Duplicate of #62219