Skip to content

Commit

Permalink
Merge pull request #9089 from compnerd/53815
Browse files Browse the repository at this point in the history
[Clang] [Sema] Handle placeholders in '.*' expressions (llvm#83103)
  • Loading branch information
compnerd authored Aug 12, 2024
2 parents 8abe125 + 4c0dc61 commit 57564d4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
22 changes: 17 additions & 5 deletions clang/lib/Sema/SemaOverload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13957,6 +13957,23 @@ ExprResult Sema::CreateOverloadedBinOp(SourceLocation OpLoc,
CurFPFeatureOverrides());
}

// If this is the .* operator, which is not overloadable, just
// create a built-in binary operator.
if (Opc == BO_PtrMemD) {
auto CheckPlaceholder = [&](Expr *&Arg) {
ExprResult Res = CheckPlaceholderExpr(Arg);
if (Res.isUsable())
Arg = Res.get();
return !Res.isUsable();
};

// CreateBuiltinBinOp() doesn't like it if we tell it to create a '.*'
// expression that contains placeholders (in either the LHS or RHS).
if (CheckPlaceholder(Args[0]) || CheckPlaceholder(Args[1]))
return ExprError();
return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]);
}

// Always do placeholder-like conversions on the RHS.
if (checkPlaceholderForOverload(*this, Args[1]))
return ExprError();
Expand All @@ -13976,11 +13993,6 @@ ExprResult Sema::CreateOverloadedBinOp(SourceLocation OpLoc,
if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType())
return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]);

// If this is the .* operator, which is not overloadable, just
// create a built-in binary operator.
if (Opc == BO_PtrMemD)
return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]);

// Build the overload set.
OverloadCandidateSet CandidateSet(OpLoc, OverloadCandidateSet::CSK_Operator,
OverloadCandidateSet::OperatorRewriteInfo(
Expand Down
21 changes: 21 additions & 0 deletions clang/test/SemaCXX/gh53815.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
// expected-no-diagnostics

// Check that we don't crash due to forgetting to check for placeholders
// in the RHS of '.*'.

template <typename Fn>
static bool has_explicitly_named_overload() {
return requires { Fn().*&Fn::operator(); };
}

int main() {
has_explicitly_named_overload<decltype([](auto){})>();
}

template <typename Fn>
constexpr bool has_explicitly_named_overload_2() {
return requires { Fn().*&Fn::operator(); };
}

static_assert(!has_explicitly_named_overload_2<decltype([](auto){})>());

0 comments on commit 57564d4

Please sign in to comment.