Skip to content

[cxx-interop] Fix assertion failure when looking up C++ operators #59345

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
Jun 9, 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
56 changes: 35 additions & 21 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4132,35 +4132,49 @@ bool ClangImporter::Implementation::lookupValue(SwiftLookupTable &table,
bool declFound = false;

if (name.isOperator()) {

auto findAndConsumeBaseNameFromTable = [this, &table, &consumer, &declFound,
&name](DeclBaseName declBaseName) {
for (auto entry : table.lookupMemberOperators(declBaseName)) {
if (isVisibleClangEntry(entry)) {
if (auto decl = dyn_cast_or_null<ValueDecl>(
importDeclReal(entry->getMostRecentDecl(), CurrentVersion))) {
consumer.foundDecl(decl, DeclVisibilityKind::VisibleAtTopLevel);
declFound = true;
for (auto alternate : getAlternateDecls(decl)) {
if (alternate->getName().matchesRef(name)) {
consumer.foundDecl(alternate, DeclVisibilityKind::DynamicLookup,
DynamicLookupInfo::AnyObject);
}
}
}
for (auto entry : table.lookupMemberOperators(name.getBaseName())) {
if (isVisibleClangEntry(entry)) {
if (auto decl = dyn_cast_or_null<ValueDecl>(
importDeclReal(entry->getMostRecentDecl(), CurrentVersion))) {
consumer.foundDecl(decl, DeclVisibilityKind::VisibleAtTopLevel);
declFound = true;
}
}
};

findAndConsumeBaseNameFromTable(name.getBaseName());
}

// If CXXInterop is enabled we need to check the modified operator name as
// well
if (SwiftContext.LangOpts.EnableCXXInterop) {
auto declBaseName = DeclBaseName(SwiftContext.getIdentifier(
auto funcBaseName = DeclBaseName(SwiftContext.getIdentifier(
"__operator" + getOperatorNameForToken(
name.getBaseName().getIdentifier().str().str())));
findAndConsumeBaseNameFromTable(declBaseName);
for (auto entry : table.lookupMemberOperators(funcBaseName)) {
if (isVisibleClangEntry(entry)) {
if (auto func = dyn_cast_or_null<ValueDecl>(
importDeclReal(entry->getMostRecentDecl(), CurrentVersion))) {
// `func` is not an operator, it is a regular function which has a
// name that starts with `__operator`. We were asked for a
// corresponding synthesized Swift operator, so let's retrieve it.

// The synthesized Swift operator was added as an alternative decl
// for `func`.
auto alternateDecls = getAlternateDecls(func);
// Did we actually synthesize an operator for `func`?
if (alternateDecls.empty())
continue;
// If we did, then we should have only synthesized one.
assert(alternateDecls.size() == 1 &&
"expected only the synthesized operator as an alternative");

auto synthesizedOperator = alternateDecls.front();
assert(synthesizedOperator->isOperator() &&
"expected the alternative to be a synthesized operator");

consumer.foundDecl(synthesizedOperator,
DeclVisibilityKind::VisibleAtTopLevel);
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ let resultExclaimEqual = lhs != rhs
let resultLessEqual = lhs <= rhs
let resultGreaterEqual = lhs >= rhs

public func ==(ptr: UnsafePointer<UInt8>, count: Int) -> Bool {
let lhs = UnsafeBufferPointer<UInt8>(start: ptr, count: count)
let rhs = UnsafeBufferPointer<UInt8>(start: ptr, count: count)
return lhs.elementsEqual(rhs, by: ==)
}


var lhsBool = LoadableBoolWrapper(value: true)
var rhsBool = LoadableBoolWrapper(value: false)

Expand Down