Skip to content

[cxx-interop] Propagate CONFORMS_TO attribute to derived classes #70304

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

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2812,13 +2812,24 @@ namespace {
}

if (auto *ntd = dyn_cast<NominalTypeDecl>(result))
addExplicitProtocolConformances(ntd);
addExplicitProtocolConformances(ntd, decl);

return result;
}

void addExplicitProtocolConformances(NominalTypeDecl *decl) {
auto clangDecl = decl->getClangDecl();
void
addExplicitProtocolConformances(NominalTypeDecl *decl,
const clang::CXXRecordDecl *clangDecl) {
if (Impl.isCxxInteropCompatVersionAtLeast(
version::getUpcomingCxxInteropCompatVersion())) {
// Propagate conforms_to attribute from public base classes.
for (auto base : clangDecl->bases()) {
if (base.getAccessSpecifier() != clang::AccessSpecifier::AS_public)
continue;
if (auto *baseClangDecl = base.getType()->getAsCXXRecordDecl())
addExplicitProtocolConformances(decl, baseClangDecl);
}
}

if (!clangDecl->hasAttrs())
return;
Expand Down
15 changes: 15 additions & 0 deletions test/Interop/Cxx/class/Inputs/conforms-to.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,19 @@ struct
void testImported() const;
};

struct DerivedFromHasTest : HasTest {};
struct DerivedFromDerivedFromHasTest : HasTest {};

struct __attribute__((swift_attr("conforms_to:SwiftTest.Testable")))
DerivedFromDerivedFromHasTestWithDuplicateArg : HasTest {};

struct DerivedFromHasPlay : HasPlay {};
struct DerivedFromDerivedFromHasPlay : HasPlay {};

struct HasTestAndPlay : HasPlay, HasTest {};
struct DerivedFromHasTestAndPlay : HasPlay, HasTest {};

struct DerivedFromHasImportedConf : HasImportedConf {};
struct DerivedFromDerivedFromHasImportedConf : HasImportedConf {};

#endif // TEST_INTEROP_CXX_CLASS_INPUTS_DESTRUCTORS_H
23 changes: 23 additions & 0 deletions test/Interop/Cxx/class/conforms-to.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// RUN: %target-swift-frontend %S/Inputs/conforms-to-imported.swift -module-name ImportedModule -emit-module -emit-module-path %t/ImportedModule.swiftmodule

// RUN: %target-typecheck-verify-swift -verify-ignore-unknown -I %t -I %S/Inputs -module-name SwiftTest -enable-experimental-cxx-interop
// RUN: %target-typecheck-verify-swift -verify-ignore-unknown -I %t -I %S/Inputs -module-name SwiftTest -cxx-interoperability-mode=upcoming-swift -D UPCOMING_SWIFT

import ConformsTo
import ImportedModule
Expand All @@ -21,6 +22,11 @@ func callee(_ _: Testable) {
func caller(_ x: HasTest) {
callee(x)
}
#if UPCOMING_SWIFT
func caller(_ x: DerivedFromHasTest) { callee(x) }
func caller(_ x: DerivedFromDerivedFromHasTest) { callee(x) }
func caller(_ x: DerivedFromDerivedFromHasTestWithDuplicateArg) { callee(x) }
#endif

func callee(_ _: Playable) {

Expand All @@ -29,10 +35,27 @@ func callee(_ _: Playable) {
func caller(_ x: Playable) {
callee(x)
}
#if UPCOMING_SWIFT
func caller(_ x: DerivedFromHasPlay) { callee(x) }
func caller(_ x: DerivedFromDerivedFromHasPlay) { callee(x) }

func caller(_ x: HasTestAndPlay) {
callee(x as Testable)
callee(x as Playable)
}
func caller(_ x: DerivedFromHasTestAndPlay) {
callee(x as Testable)
callee(x as Playable)
}
#endif

func callee(_ _: ProtocolFromImportedModule) {
}

func caller(_ x: HasImportedConf) {
callee(x)
}
#if UPCOMING_SWIFT
func caller(_ x: DerivedFromHasImportedConf) { callee(x) }
func caller(_ x: DerivedFromDerivedFromHasImportedConf) { callee(x) }
#endif