Skip to content

Commit 60860c2

Browse files
authored
Merge pull request #59345 from apple/egorzhdan/cxx-operator-lookup-fix
[cxx-interop] Fix assertion failure when looking up C++ operators
2 parents c470fd1 + 31381a3 commit 60860c2

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4132,35 +4132,49 @@ bool ClangImporter::Implementation::lookupValue(SwiftLookupTable &table,
41324132
bool declFound = false;
41334133

41344134
if (name.isOperator()) {
4135-
4136-
auto findAndConsumeBaseNameFromTable = [this, &table, &consumer, &declFound,
4137-
&name](DeclBaseName declBaseName) {
4138-
for (auto entry : table.lookupMemberOperators(declBaseName)) {
4139-
if (isVisibleClangEntry(entry)) {
4140-
if (auto decl = dyn_cast_or_null<ValueDecl>(
4141-
importDeclReal(entry->getMostRecentDecl(), CurrentVersion))) {
4142-
consumer.foundDecl(decl, DeclVisibilityKind::VisibleAtTopLevel);
4143-
declFound = true;
4144-
for (auto alternate : getAlternateDecls(decl)) {
4145-
if (alternate->getName().matchesRef(name)) {
4146-
consumer.foundDecl(alternate, DeclVisibilityKind::DynamicLookup,
4147-
DynamicLookupInfo::AnyObject);
4148-
}
4149-
}
4150-
}
4135+
for (auto entry : table.lookupMemberOperators(name.getBaseName())) {
4136+
if (isVisibleClangEntry(entry)) {
4137+
if (auto decl = dyn_cast_or_null<ValueDecl>(
4138+
importDeclReal(entry->getMostRecentDecl(), CurrentVersion))) {
4139+
consumer.foundDecl(decl, DeclVisibilityKind::VisibleAtTopLevel);
4140+
declFound = true;
41514141
}
41524142
}
4153-
};
4154-
4155-
findAndConsumeBaseNameFromTable(name.getBaseName());
4143+
}
41564144

41574145
// If CXXInterop is enabled we need to check the modified operator name as
41584146
// well
41594147
if (SwiftContext.LangOpts.EnableCXXInterop) {
4160-
auto declBaseName = DeclBaseName(SwiftContext.getIdentifier(
4148+
auto funcBaseName = DeclBaseName(SwiftContext.getIdentifier(
41614149
"__operator" + getOperatorNameForToken(
41624150
name.getBaseName().getIdentifier().str().str())));
4163-
findAndConsumeBaseNameFromTable(declBaseName);
4151+
for (auto entry : table.lookupMemberOperators(funcBaseName)) {
4152+
if (isVisibleClangEntry(entry)) {
4153+
if (auto func = dyn_cast_or_null<ValueDecl>(
4154+
importDeclReal(entry->getMostRecentDecl(), CurrentVersion))) {
4155+
// `func` is not an operator, it is a regular function which has a
4156+
// name that starts with `__operator`. We were asked for a
4157+
// corresponding synthesized Swift operator, so let's retrieve it.
4158+
4159+
// The synthesized Swift operator was added as an alternative decl
4160+
// for `func`.
4161+
auto alternateDecls = getAlternateDecls(func);
4162+
// Did we actually synthesize an operator for `func`?
4163+
if (alternateDecls.empty())
4164+
continue;
4165+
// If we did, then we should have only synthesized one.
4166+
assert(alternateDecls.size() == 1 &&
4167+
"expected only the synthesized operator as an alternative");
4168+
4169+
auto synthesizedOperator = alternateDecls.front();
4170+
assert(synthesizedOperator->isOperator() &&
4171+
"expected the alternative to be a synthesized operator");
4172+
4173+
consumer.foundDecl(synthesizedOperator,
4174+
DeclVisibilityKind::VisibleAtTopLevel);
4175+
}
4176+
}
4177+
}
41644178
}
41654179
}
41664180

test/Interop/Cxx/operators/non-member-inline-typechecker.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ let resultExclaimEqual = lhs != rhs
2222
let resultLessEqual = lhs <= rhs
2323
let resultGreaterEqual = lhs >= rhs
2424

25+
public func ==(ptr: UnsafePointer<UInt8>, count: Int) -> Bool {
26+
let lhs = UnsafeBufferPointer<UInt8>(start: ptr, count: count)
27+
let rhs = UnsafeBufferPointer<UInt8>(start: ptr, count: count)
28+
return lhs.elementsEqual(rhs, by: ==)
29+
}
30+
31+
2532
var lhsBool = LoadableBoolWrapper(value: true)
2633
var rhsBool = LoadableBoolWrapper(value: false)
2734

0 commit comments

Comments
 (0)