swiftlang / swift

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

compiler crash when cpp class conforms to protocol with static function #77577

Open mikewoodworth opened 3 days ago

mikewoodworth commented 3 days ago

Description

When a cpp class is used from swift, and is conformed to a protocol defining a static func, it will fail.

Reproduction

I have a class in cpp:

class Test {
public:
    static void hellostatic() {
        printf("hello from hellostatic\n");
    };

    void hello() {
        printf("hello from cpp\n");
    };

    Test() = default;
    Test(Test &other) = default;
    ~Test() = default;
};

I can call both the hello() func and hellostatic() funcs from swift:

var t = Test()
t.hello()
Test.hellostatic()

But if I create a protocol in swift, and conform the cpp object to it with an extension, the compilation will fail.

protocol Friendly {
    mutating func hello()
   // static func hellostatic() <- uncommenting this will cause compilation to fail
}

extension Test: Friendly {}
var t = Test()
t.hello()

Test.hellostatic()

Stack dump

0.  Program arguments: /Applications/Xcode-16.1.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -emit-module -experimental-skip-non-inlinable-function-bodies-without-types "/Users/mike/Desktop/swift regression/Sources/swift-main/main.swift" -target arm64-apple-macos10.13 -Xllvm -aarch64-use-tbi -enable-objc-interop -cxx-interoperability-mode=default -sdk /Applications/Xcode-16.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.1.sdk -I /Users/mike/Library/Developer/Xcode/DerivedData/swift_regression-brxgrucrclhhoegetqdzfwxtuhob/Build/Products/Debug -I /Applications/Xcode-16.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Users/mike/Library/Developer/Xcode/DerivedData/swift_regression-brxgrucrclhhoegetqdzfwxtuhob/Build/Products/Debug/PackageFrameworks -F /Users/mike/Library/Developer/Xcode/DerivedData/swift_regression-brxgrucrclhhoegetqdzfwxtuhob/Build/Products/Debug -F /Applications/Xcode-16.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -no-color-diagnostics -enable-testing -g -debug-info-format=dwarf -dwarf-version=4 -module-cache-path /Users/mike/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 6 -enforce-exclusivity=checked -Onone -D SWIFT_PACKAGE -D DEBUG -D Xcode -serialize-debugging-options -const-gather-protocols-file "/Users/mike/Library/Developer/Xcode/DerivedData/swift_regression-brxgrucrclhhoegetqdzfwxtuhob/Build/Intermediates.noindex/swift regression.build/Debug/swift-main.build/Objects-normal/arm64/swift-main_const_extract_protocols.json" -enable-experimental-feature DebugDescriptionMacro -enable-experimental-feature OpaqueTypeErasure -empty-abi-descriptor -plugin-path /Applications/Xcode-16.1.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins/testing -validate-clang-modules-once -clang-build-session-file /Users/mike/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc "/Users/mike/Desktop/swift regression/.swiftpm/xcode" -resource-dir /Applications/Xcode-16.1.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir "/Users/mike/Desktop/swift regression/.swiftpm/xcode" -Xcc -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG -Xcc -ivfsstatcache -Xcc /Users/mike/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/macosx15.1-24B75-44d23fded74b714c58e69ec4af40d824.sdkstatcache -Xcc "-I/Users/mike/Desktop/swift regression/Sources/cpp/include" -Xcc -I/Users/mike/Library/Developer/Xcode/DerivedData/swift_regression-brxgrucrclhhoegetqdzfwxtuhob/Build/Products/Debug/include -Xcc "-I/Users/mike/Library/Developer/Xcode/DerivedData/swift_regression-brxgrucrclhhoegetqdzfwxtuhob/Build/Intermediates.noindex/swift regression.build/Debug/swift-main.build/DerivedSources-normal/arm64" -Xcc "-I/Users/mike/Library/Developer/Xcode/DerivedData/swift_regression-brxgrucrclhhoegetqdzfwxtuhob/Build/Intermediates.noindex/swift regression.build/Debug/swift-main.build/DerivedSources/arm64" -Xcc "-I/Users/mike/Library/Developer/Xcode/DerivedData/swift_regression-brxgrucrclhhoegetqdzfwxtuhob/Build/Intermediates.noindex/swift regression.build/Debug/swift-main.build/DerivedSources" -Xcc -DSWIFT_PACKAGE -Xcc -DDEBUG=1 -module-name swift_main -package-name swift_regression -frontend-parseable-output -disable-clang-spi -target-sdk-version 15.1 -target-sdk-name macosx15.1 -external-plugin-path /Applications/Xcode-16.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode-16.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-16.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode-16.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode-16.1.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode-16.1.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -emit-module-doc-path "/Users/mike/Library/Developer/Xcode/DerivedData/swift_regression-brxgrucrclhhoegetqdzfwxtuhob/Build/Intermediates.noindex/swift regression.build/Debug/swift-main.build/Objects-normal/arm64/swift_main.swiftdoc" -emit-module-source-info-path "/Users/mike/Library/Developer/Xcode/DerivedData/swift_regression-brxgrucrclhhoegetqdzfwxtuhob/Build/Intermediates.noindex/swift regression.build/Debug/swift-main.build/Objects-normal/arm64/swift_main.swiftsourceinfo" -serialize-diagnostics-path "/Users/mike/Library/Developer/Xcode/DerivedData/swift_regression-brxgrucrclhhoegetqdzfwxtuhob/Build/Intermediates.noindex/swift regression.build/Debug/swift-main.build/Objects-normal/arm64/swift-main-master-emit-module.dia" -emit-dependencies-path "/Users/mike/Library/Developer/Xcode/DerivedData/swift_regression-brxgrucrclhhoegetqdzfwxtuhob/Build/Intermediates.noindex/swift regression.build/Debug/swift-main.build/Objects-normal/arm64/swift-main-master-emit-module.d" -o "/Users/mike/Library/Developer/Xcode/DerivedData/swift_regression-brxgrucrclhhoegetqdzfwxtuhob/Build/Intermediates.noindex/swift regression.build/Debug/swift-main.build/Objects-normal/arm64/swift_main.swiftmodule" -emit-abi-descriptor-path "/Users/mike/Library/Developer/Xcode/DerivedData/swift_regression-brxgrucrclhhoegetqdzfwxtuhob/Build/Intermediates.noindex/swift regression.build/Debug/swift-main.build/Objects-normal/arm64/swift_main.abi.json"
1.  Apple Swift version 6.0.2 (swiftlang-6.0.2.1.2 clang-1600.0.26.4)
2.  Compiling with the current language version
3.  While evaluating request ASTLoweringRequest(Lowering AST to SIL for module swift_main)
4.  While generating SIL witness table protocol conformance Test: Friendly at extension of Test (at /Users/mike/Desktop/swift regression/Sources/swift-main/main.swift:10:1)
5.  While generating protocol witness thunk SIL function "@$sSo4TestV10swift_main8FriendlyA2cDP11hellostaticyyFZTW".
 for 'hellostatic()' (in module 'cpp')
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           0x0000000109b2a9ac llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000109b28c00 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000109b2af78 SignalHandler(int) + 292
3  libsystem_platform.dylib 0x0000000195432e04 _sigtramp + 56
4  swift-frontend           0x0000000105002388 forwardFunctionArguments(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::CanTypeWrapper<swift::SILFunctionType>, llvm::ArrayRef<swift::Lowering::ManagedValue>, llvm::SmallVectorImpl<swift::SILValue>&) + 412
5  swift-frontend           0x0000000105002388 forwardFunctionArguments(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::CanTypeWrapper<swift::SILFunctionType>, llvm::ArrayRef<swift::Lowering::ManagedValue>, llvm::SmallVectorImpl<swift::SILValue>&) + 412
6  swift-frontend           0x000000010500acc0 swift::Lowering::SILGenFunction::emitProtocolWitness(swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::SILDeclRef, swift::SubstitutionMap, swift::SILDeclRef, swift::SubstitutionMap, swift::Lowering::IsFreeFunctionWitness_t, bool, bool, std::__1::optional<swift::ActorIsolation>) + 5432
7  swift-frontend           0x000000010503f590 swift::Lowering::SILGenModule::emitProtocolWitness(swift::ProtocolConformanceRef, swift::SILLinkage, swift::SerializedKind_t, swift::SILDeclRef, swift::SILDeclRef, swift::Lowering::IsFreeFunctionWitness_t, swift::Witness) + 4344
8  swift-frontend           0x0000000105042468 (anonymous namespace)::SILGenWitnessTable<(anonymous namespace)::SILGenConformance>::addMethodImplementation(swift::SILDeclRef, swift::SILDeclRef, swift::Witness) + 404
9  swift-frontend           0x0000000105041e50 (anonymous namespace)::SILGenWitnessTable<(anonymous namespace)::SILGenConformance>::addMethod(swift::SILDeclRef) + 780
10 swift-frontend           0x0000000105041954 swift::SILWitnessVisitor<(anonymous namespace)::SILGenConformance>::visitProtocolDecl(swift::ProtocolDecl*) + 1956
11 swift-frontend           0x000000010503e0a0 swift::Lowering::SILGenModule::getWitnessTable(swift::NormalProtocolConformance*) + 548
12 swift-frontend           0x00000001050410c4 SILGenExtension::emitExtension(swift::ExtensionDecl*) + 880
13 swift-frontend           0x0000000104eed4d8 swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 264
14 swift-frontend           0x0000000104ef3e4c swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 1804
15 swift-frontend           0x000000010502a740 swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 196
16 swift-frontend           0x0000000104ef8d28 swift::ASTLoweringRequest::OutputType swift::Evaluator::getResultUncached<swift::ASTLoweringRequest, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()>(swift::ASTLoweringRequest const&, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()) + 528
17 swift-frontend           0x00000001044d35f0 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 2828
18 swift-frontend           0x00000001044d6174 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1684
19 swift-frontend           0x00000001044d4ea0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3572
20 swift-frontend           0x000000010445bf64 swift::mainEntry(int, char const**) + 3680
21 dyld                     0x000000019507c274 start + 2840

Expected behavior

compiles

Environment

swift-driver version: 1.115 Apple Swift version 6.0.2 (swiftlang-6.0.2.1.2 clang-1600.0.26.4) Target: arm64-apple-macosx15.0

Additional information

No response