swiftlang / swift

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

[C++ interop] Compiler crash compiling ambiguous return value #58458

Closed mikepinkerton closed 2 years ago

mikepinkerton commented 2 years ago

In Xcode 13.3 (Swift 5.6) with C++ interop, the following will crash the compiler:

(in .h)

class Object {
 public:
  Object(int value) : value_(value) {}
  ~Object() {}

  int GetValue() const { return value_; }

 private:
  int value_;
};

(in .swift)

class ObjectPassingTest: XCTestCase {
  func addObjects(one: Object, two: Object) -> Int {
    return one.GetValue() + two.GetValue()
  }

  func testPassToSwift() throws {
    let a = Object(10)
    let b = Object(20)

    XCTAssertEqual(addObjects(one:a, two:b), 30)
  }
}

Note that if you change the result of addObjects to be Int32 it compiles and runs correctly. Having it as Int is the problem.

Here's the output:

<unknown>:0: error: fatal error encountered while reading from module 'Foundation'; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.  Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c ../../ios/chrome/test/swift_interop/classes/inlined_xctest.swift ../../ios/chrome/test/swift_interop/classes/outlined_xctest.swift ../../ios/chrome/test/swift_interop/classes/polymorphism_xctest.swift ../../ios/chrome/test/swift_interop/enum/enum_xctest.swift ../../ios/chrome/test/swift_interop/namespace/namespace_xctest.swift -primary-file ../../ios/chrome/test/swift_interop/pointer/object_passing_xctest.swift ../../ios/chrome/test/swift_interop/pointer/pointer_xctest.swift ../../ios/chrome/test/swift_interop/pointer/unique_ptr_xctest.swift -emit-dependencies-path /var/folders/ws/k9x7k7396dggjyqydcjt9y9r000f6r/T/tmphy0hz2y6/object_passing_xctest.d -target x86_64-apple-ios14.0-simulator -enable-objc-interop -sdk /Users/pinkerton/src/bling/src/out/Debug-iphonesimulator/sdk/xcode_links/iPhoneSimulator15.4.sdk -I ../.. -I gen -I ../../buildtools/third_party/libc++ -I ../../third_party/perfetto/include -I gen/third_party/perfetto/build_config -I gen/third_party/perfetto -I ../../third_party/abseil-cpp -I ../../third_party/boringssl/src/include -I ../../third_party/protobuf/src -I gen/protoc_out -I sdk/xcode_links/iPhoneSimulator.platform/Developer/usr/lib -F sdk/xcode_links/iPhoneSimulator.platform/Developer/Library/Frameworks -F sdk/xcode_links/iPhoneSimulator15.4.sdk/Developer/Library/Frameworks -g -swift-version 5 -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -enable-cxx-interop -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -import-objc-header /Users/pinkerton/src/bling/src/ios/chrome/test/swift_interop/swift_bridge.h -pch-output-dir /Users/pinkerton/src/bling/src/out/Debug-iphonesimulator/obj/ios/chrome/test/swift_interop/ios_chrome_test_swift_interop_swift_interop_tests:pch -pch-disable-validation -module-name ios_chrome_test_swift_interop_swift_interop_tests -target-sdk-version 15.4 -parse-as-library -o obj/ios/chrome/test/swift_interop/swift_interop_tests/object_passing_xctest.o
1.  Apple Swift version 5.6 (swiftlang-5.6.0.323.62 clang-1316.0.20.8)
2.  Compiling with the current language version
3.  While evaluating request TypeCheckSourceFileRequest(source_file "../../ios/chrome/test/swift_interop/pointer/object_passing_xctest.swift")
4.  While evaluating request TypeCheckFunctionBodyRequest(ios_chrome_test_swift_interop_swift_interop_tests.(file).ObjectPassingTest.addObjects(one:two:)@../../ios/chrome/test/swift_interop/pointer/object_passing_xctest.swift:40:8)
5.  While type-checking statement at [../../ios/chrome/test/swift_interop/pointer/object_passing_xctest.swift:40:52 - line:42:3] RangeText="{
    return one.GetValue() + two.GetValue()
  "
6.  While type-checking statement at [../../ios/chrome/test/swift_interop/pointer/object_passing_xctest.swift:41:5 - line:41:42] RangeText="return one.GetValue() + two.GetValue("
7.  While type-checking expression at [../../ios/chrome/test/swift_interop/pointer/object_passing_xctest.swift:41:12 - line:41:42] RangeText="one.GetValue() + two.GetValue("
8.  While evaluating request LookupConformanceInModuleRequest(looking up conformance to Swift.(file).RawRepresentable for IndexPath in ios_chrome_test_swift_interop_swift_interop_tests)
9.  While loading conformances for 'IndexPath' (in module 'Foundation')
10. *** DESERIALIZATION FAILURE ***
module 'Foundation' with full misc version '5.6(5.6)/Apple Swift version 5.6 (swiftlang-5.6.0.323.62 clang-1316.0.20.8)'
Could not deserialize type for 'ReferenceConvertible'
Caused by: result is ambiguous
Cross-reference to module 'ObjectiveC'
... NSObject

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           0x00000001096b5de7 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x00000001096b4e38 llvm::sys::RunSignalHandlers() + 248
2  swift-frontend           0x00000001096b6440 SignalHandler(int) + 288
3  libsystem_platform.dylib 0x00007ff807285dfd _sigtramp + 29
4  libsystem_platform.dylib 0x00007f7ca2856030 _sigtramp + 18446743543740170832
5  libsystem_c.dylib        0x00007ff8071bbd24 abort + 123
6  swift-frontend           0x0000000104a46bf5 swift::ModuleFileSharedCore::fatal(llvm::Error) const + 629
7  swift-frontend           0x00000001049cb026 swift::ModuleFile::fatal(llvm::Error) const + 566
8  swift-frontend           0x00000001049efeb0 swift::ModuleFile::loadAllConformances(swift::Decl const*, unsigned long long, llvm::SmallVectorImpl<swift::ProtocolConformance*>&) + 1248
9  swift-frontend           0x000000010571a181 swift::ConformanceLookupTable::updateLookupTable(swift::NominalTypeDecl*, swift::ConformanceLookupTable::ConformanceStage) + 289
10 swift-frontend           0x000000010571a2c1 swift::ConformanceLookupTable::updateLookupTable(swift::NominalTypeDecl*, swift::ConformanceLookupTable::ConformanceStage) + 609
11 swift-frontend           0x000000010571e82c swift::ConformanceLookupTable::lookupConformance(swift::NominalTypeDecl*, swift::ProtocolDecl*, llvm::SmallVectorImpl<swift::ProtocolConformance*>&) + 44
12 swift-frontend           0x000000010586cff8 swift::LookupConformanceInModuleRequest::evaluate(swift::Evaluator&, swift::LookupConformanceDescriptor) const + 3288
13 swift-frontend           0x00000001058b4bad swift::SimpleRequest<swift::LookupConformanceInModuleRequest, swift::ProtocolConformanceRef (swift::LookupConformanceDescriptor), (swift::RequestFlags)17>::evaluateRequest(swift::LookupConformanceInModuleRequest const&, swift::Evaluator&) + 29
14 swift-frontend           0x000000010586bd4a swift::ModuleDecl::lookupConformance(swift::Type, swift::ProtocolDecl*, bool) + 1146
15 swift-frontend           0x0000000105445b7e swift::TypeChecker::conformsToProtocol(swift::Type, swift::ProtocolDecl*, swift::ModuleDecl*, bool) + 30
16 swift-frontend           0x00000001052997c7 swift::constraints::isRawRepresentable(swift::constraints::ConstraintSystem&, swift::Type) + 103
17 swift-frontend           0x00000001051bfa68 swift::constraints::ConstraintSystem::repairFailures(swift::Type, swift::Type, swift::constraints::ConstraintKind, llvm::SmallVectorImpl<swift::constraints::RestrictionOrFix>&, swift::constraints::ConstraintLocatorBuilder) + 24120
18 swift-frontend           0x00000001051af9c6 swift::constraints::ConstraintSystem::matchTypes(swift::Type, swift::Type, swift::constraints::ConstraintKind, swift::OptionSet<swift::constraints::ConstraintSystem::TypeMatchFlags, unsigned int>, swift::constraints::ConstraintLocatorBuilder) + 14726
19 swift-frontend           0x00000001051df761 swift::constraints::ConstraintSystem::addArgumentConversionConstraintImpl(swift::constraints::ConstraintKind, swift::Type, swift::Type, swift::constraints::ConstraintLocatorBuilder) + 225
20 swift-frontend           0x00000001051aa7d5 swift::constraints::matchCallArguments(swift::constraints::ConstraintSystem&, swift::FunctionType*, llvm::ArrayRef<swift::AnyFunctionType::Param>, llvm::ArrayRef<swift::AnyFunctionType::Param>, swift::constraints::ConstraintKind, swift::constraints::ConstraintLocatorBuilder, llvm::Optional<swift::constraints::TrailingClosureMatching>) + 8005
21 swift-frontend           0x00000001051daafb swift::constraints::ConstraintSystem::simplifyApplicableFnConstraint(swift::Type, swift::Type, llvm::Optional<swift::constraints::TrailingClosureMatching>, swift::OptionSet<swift::constraints::ConstraintSystem::TypeMatchFlags, unsigned int>, swift::constraints::ConstraintLocatorBuilder) + 1771
22 swift-frontend           0x00000001051e1468 swift::constraints::ConstraintSystem::simplifyConstraint(swift::constraints::Constraint const&) + 2856
23 swift-frontend           0x00000001051ee382 swift::constraints::ConstraintSystem::simplify() + 114
24 swift-frontend           0x000000010520599b swift::constraints::DisjunctionStep::attempt(swift::constraints::DisjunctionChoice const&) + 187
25 swift-frontend           0x00000001052048fb swift::constraints::BindingStep<swift::constraints::DisjunctionChoiceProducer>::take(bool) + 555
26 swift-frontend           0x00000001052046b9 swift::constraints::DisjunctionStep::resume(bool) + 969
27 swift-frontend           0x00000001051f144d swift::constraints::ConstraintSystem::solveImpl(llvm::SmallVectorImpl<swift::constraints::Solution>&) + 541
28 swift-frontend           0x0000000105290f4c swift::constraints::ConstraintSystem::salvage() + 172
29 swift-frontend           0x00000001051f3d3d swift::constraints::ConstraintSystem::solve(swift::constraints::SolutionApplicationTarget&, swift::FreeTypeVariableBinding) + 1853
30 swift-frontend           0x00000001053a01a8 swift::TypeChecker::typeCheckExpression(swift::constraints::SolutionApplicationTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 792
31 swift-frontend           0x0000000105481241 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 6433
32 swift-frontend           0x000000010547f898 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::Stmt>(swift::Stmt*&) + 296
33 swift-frontend           0x000000010547c646 (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) + 166
34 swift-frontend           0x000000010547f99c swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 124
35 swift-frontend           0x000000010547e638 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 296
36 swift-frontend           0x000000010547d26e swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 1742
37 swift-frontend           0x00000001057c2e09 llvm::Expected<swift::TypeCheckFunctionBodyRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckFunctionBodyRequest>(swift::TypeCheckFunctionBodyRequest const&) + 665
38 swift-frontend           0x000000010574d674 swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType) + 68
39 swift-frontend           0x00000001054be80b swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 875
40 swift-frontend           0x00000001054c1969 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest>(swift::TypeCheckSourceFileRequest const&) + 633
41 swift-frontend           0x00000001054be452 swift::performTypeChecking(swift::SourceFile&) + 114
42 swift-frontend           0x000000010457f739 swift::CompilerInstance::performSema() + 217
43 swift-frontend           0x00000001043dde7a swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 14234
44 swift-frontend           0x000000010439d5d4 swift::mainEntry(int, char const**) + 1108
45 dyld                     0x000000011169a51e start + 462
ninja: build stopped: subcommand failed.
mikepinkerton commented 2 years ago

Looks like this has been fixed in ToT (Apple Swift version 5.7-dev (LLVM 0111970099b6de6, Swift b6ec1874c96449c)).

It results in a compile error that it cannot convert the return expression of type Int32 to return type Int.

zoecarver commented 2 years ago

If this is resolved, I'm going to close it. Please let me know if you see this issue again in the future. Thank you for the continued, high-quality bug reports. Having small reproducers like this really helps.