Skip to content

Commit 44c35a2

Browse files
committed
[cxx-interop] Fix lookup of member operators
Calling `StructDecl::lookupDirect` with an operator identifier (e.g. `==`) previously returned no results. This happened because the underlying C++ operator function was added to the lookup table with an underscored name (e.g. `__operatorEqualEqual`), and the synthesized function was not added to the lookup table at all. Lookup should find the synthesized decl, since that is what Swift code will call. This fixes a typechecker error when trying to conform a C++ struct that defines an operator to a Swift protocol with an operator requirement (e.g. `Equatable`).
1 parent 2028931 commit 44c35a2

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,6 +2234,9 @@ namespace {
22342234

22352235
// Make the actual member operator private.
22362236
MD->overwriteAccess(AccessLevel::Private);
2237+
2238+
// Make sure the synthesized decl can be found by lookupDirect.
2239+
result->addMemberToLookupTable(opFuncDecl);
22372240
}
22382241

22392242
if (cxxMethod->getDeclName().isIdentifier()) {

test/Interop/Cxx/class/Inputs/protocol-conformance.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,18 @@ struct ReturnsNonNullValue {
3838
}
3939
};
4040

41+
struct HasOperatorExclaim {
42+
int value;
43+
44+
HasOperatorExclaim operator!() const { return {-value}; }
45+
};
46+
47+
struct HasOperatorEqualEqual {
48+
int value;
49+
50+
bool operator==(const HasOperatorEqualEqual &other) const {
51+
return value == other.value;
52+
}
53+
};
54+
4155
#endif // TEST_INTEROP_CXX_CLASS_INPUTS_PROTOCOL_CONFORMANCE_H

test/Interop/Cxx/class/protocol-conformance-typechecker.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,12 @@ protocol HasReturnNonNull {
2626
}
2727

2828
extension ReturnsNonNullValue: HasReturnNonNull {}
29+
30+
31+
protocol Invertable {
32+
static prefix func !(obj: Self) -> Self
33+
}
34+
35+
extension HasOperatorExclaim: Invertable {}
36+
37+
extension HasOperatorEqualEqual: Equatable {}

0 commit comments

Comments
 (0)