Skip to content

[cxx-interop] Fix friend operators that come from class template specializations. #61181

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 2 commits into from
Oct 17, 2022
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
13 changes: 12 additions & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2712,6 +2712,11 @@ static bool isVisibleFromModule(const ClangModuleUnit *ModuleFilter,
if (OwningClangModule == ModuleFilter->getClangModule())
return true;

// Friends from class templates don't have an owning module. Just return true.
if (isa<clang::FunctionDecl>(D) &&
cast<clang::FunctionDecl>(D)->isThisDeclarationInstantiatedFromAFriendDefinition())
return true;

// Handle redeclarable Clang decls by checking each redeclaration.
bool IsTagDecl = isa<clang::TagDecl>(D);
if (!(IsTagDecl || isa<clang::FunctionDecl>(D) || isa<clang::VarDecl>(D) ||
Expand Down Expand Up @@ -4124,7 +4129,13 @@ bool ClangImporter::Implementation::lookupValue(SwiftLookupTable &table,

// If the name matched, report this result.
bool anyMatching = false;
if (decl->getName().matchesRef(name) &&

// Use the base name for operators; they likely won't have parameters.
auto foundDeclName = decl->getName();
if (foundDeclName.isOperator())
foundDeclName = foundDeclName.getBaseName();

if (foundDeclName.matchesRef(name) &&
decl->getDeclContext()->isModuleScopeContext()) {
consumer.foundDecl(decl, DeclVisibilityKind::VisibleAtTopLevel);
anyMatching = true;
Expand Down
20 changes: 19 additions & 1 deletion lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2133,7 +2133,16 @@ namespace {
if (auto friendDecl = dyn_cast<clang::FriendDecl>(m)) {
if (friendDecl->getFriendDecl()) {
m = friendDecl->getFriendDecl();
auto lookupTable = Impl.findLookupTable(m->getOwningModule());

// Find the owning module of the class template. Members of class
// template specializations don't have an owning module.
clang::Module *owningModule = nullptr;
if (auto spec = dyn_cast<clang::ClassTemplateSpecializationDecl>(decl))
owningModule = spec->getSpecializedTemplate()->getOwningModule();
else
owningModule = decl->getOwningModule();

auto lookupTable = Impl.findLookupTable(owningModule);
addEntryToLookupTable(*lookupTable, friendDecl->getFriendDecl(),
Impl.getNameImporter());
}
Expand Down Expand Up @@ -8230,6 +8239,15 @@ ClangImporter::Implementation::importDeclContextOf(
if (dc->getDeclKind() == clang::Decl::LinkageSpec)
dc = dc->getParent();

// Treat friend decls like top-level decls.
if (auto functionDecl = dyn_cast<clang::FunctionDecl>(decl)) {
if (functionDecl->getFriendObjectKind()) {
// Find the top-level decl context.
while (isa<clang::NamedDecl>(dc))
dc = dc->getParent();
}
}

if (dc->isTranslationUnit()) {
if (auto *module = getClangModuleForDecl(decl))
return module;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
// CHECK: struct ForwardDeclaredFriend {
// CHECK: init()
// CHECK: }
// CHECK: static func takesFriend(_ f: NestedDeclIsAFirstForwardDeclaration.ForwardDeclaredFriend)
// CHECK: func takesFriend(_ f: NestedDeclIsAFirstForwardDeclaration.ForwardDeclaredFriend)
// CHECK: struct HasNestedForwardDeclaration {
// CHECK: init()
// CHECK: struct IsNestedForwardDeclaration {
Expand Down
10 changes: 10 additions & 0 deletions test/Interop/Cxx/operators/Inputs/member-inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ struct LoadableBoolWrapper {
}
};

template<class T>
struct TemplatedWithFriendOperator {
friend bool operator==(const TemplatedWithFriendOperator &lhs,
const TemplatedWithFriendOperator &rhs) {
return true;
}
};

using TemplatedWithFriendOperatorSpec = TemplatedWithFriendOperator<int>;

struct __attribute__((swift_attr("import_owned"))) AddressOnlyIntWrapper {
int value;

Expand Down
9 changes: 9 additions & 0 deletions test/Interop/Cxx/operators/member-inline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ OperatorsTestSuite.test("LoadableIntWrapper.successor() (inline)") {
expectEqual(42, wrapper.value)
}

OperatorsTestSuite.test("TemplatedWithFriendOperator.equal (inline)") {
let lhs = TemplatedWithFriendOperatorSpec()
let rhs = TemplatedWithFriendOperatorSpec()

let result = lhs == rhs

expectTrue(result)
}

#if !os(Windows) // https://github.com/apple/swift/issues/55575
OperatorsTestSuite.test("LoadableBoolWrapper.exclaim (inline)") {
var wrapper = LoadableBoolWrapper(value: true)
Expand Down
15 changes: 14 additions & 1 deletion test/Interop/Cxx/stdlib/use-std-map.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xfrontend -validate-tbd-against-ir=none)
//
// REQUIRES: executable_test
//
Expand All @@ -7,6 +7,8 @@

import StdlibUnittest
import StdMap
import std
import Cxx

var StdMapTestSuite = TestSuite("StdMap")

Expand All @@ -26,4 +28,15 @@ StdMapTestSuite.test("subscript") {
expectEqual(m[3], 3)
}

extension Map.const_iterator : UnsafeCxxInputIterator { }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extension Map : CxxSequence { }

StdMapTestSuite.test("first(where:)") {
let m = initMap()
let found = m.first(where: { $0.first > 1 })

expectEqual(found!.first, 2)
expectEqual(found!.second, 2)
}

runAllTests()