Skip to content

Commit 92c65f5

Browse files
authored
Merge pull request #76252 from swiftlang/egorzhdan/static-call
[cxx-interop] Do not crash for `static operator()`
2 parents a3c0e22 + 7a5154f commit 92c65f5

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

lib/ClangImporter/SwiftDeclSynthesizer.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,14 +2138,19 @@ clang::CXXMethodDecl *SwiftDeclSynthesizer::synthesizeCXXForwardingMethod(
21382138
thisExpr = conv.get();
21392139
}
21402140

2141+
auto memberExprTy =
2142+
(method->isStatic() && method->getOverloadedOperator() ==
2143+
clang::OverloadedOperatorKind::OO_Call)
2144+
? method->getType()
2145+
: clangCtx.BoundMemberTy;
21412146
auto memberExpr = clangSema.BuildMemberExpr(
21422147
thisExpr, /*isArrow=*/true, clang::SourceLocation(),
21432148
clang::NestedNameSpecifierLoc(), clang::SourceLocation(),
21442149
const_cast<clang::CXXMethodDecl *>(method),
21452150
clang::DeclAccessPair::make(const_cast<clang::CXXMethodDecl *>(method),
21462151
clang::AS_public),
21472152
/*HadMultipleCandidates=*/false, method->getNameInfo(),
2148-
clangCtx.BoundMemberTy, clang::VK_PRValue, clang::OK_Ordinary);
2153+
memberExprTy, clang::VK_PRValue, clang::OK_Ordinary);
21492154
llvm::SmallVector<clang::Expr *, 4> args;
21502155
for (size_t i = 0; i < newMethod->getNumParams(); ++i) {
21512156
auto *param = newMethod->getParamDecl(i);
@@ -2156,7 +2161,7 @@ clang::CXXMethodDecl *SwiftDeclSynthesizer::synthesizeCXXForwardingMethod(
21562161
clangCtx, param, false, type, clang::ExprValueKind::VK_LValue,
21572162
clang::SourceLocation()));
21582163
}
2159-
auto memberCall = clangSema.BuildCallToMemberFunction(
2164+
auto memberCall = clangSema.BuildCallExpr(
21602165
nullptr, memberExpr, clang::SourceLocation(), args,
21612166
clang::SourceLocation());
21622167
if (!memberCall.isUsable())

test/Interop/Cxx/operators/Inputs/member-inline.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,15 @@ class HasStaticOperatorCallBase {
482482
static int operator()(int x) { return x + 42; }
483483
};
484484

485+
class HasStaticOperatorCallBaseNonTrivial: public NonTrivial {
486+
public:
487+
HasStaticOperatorCallBaseNonTrivial() {}
488+
HasStaticOperatorCallBaseNonTrivial(const HasStaticOperatorCallBaseNonTrivial &self) : NonTrivial(self) {}
489+
HasStaticOperatorCallBaseNonTrivial(HasStaticOperatorCallBaseNonTrivial &&self) : NonTrivial(self) {}
490+
491+
static int operator()(const NonTrivial &arg) { return arg.f + 42; }
492+
};
493+
485494
class HasStaticOperatorCallDerived : public HasStaticOperatorCallBase {};
486495

487496
class HasStaticOperatorCallWithConstOperator {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,13 @@ OperatorsTestSuite.test("HasStaticOperatorCallBase.call") {
443443
expectEqual(43, res)
444444
}
445445

446+
OperatorsTestSuite.test("HasStaticOperatorCallBase2.call") {
447+
let m = NonTrivial()
448+
let h = HasStaticOperatorCallBaseNonTrivial()
449+
let res = h(m)
450+
expectEqual(48, res)
451+
}
452+
446453
OperatorsTestSuite.test("HasStaticOperatorCallDerived.call") {
447454
let h = HasStaticOperatorCallDerived()
448455
let res = h(0)

0 commit comments

Comments
 (0)