[clang] [Clang] fix static operator()/[] call not evaluating object (PR #78356)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 16 13:58:47 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Mital Ashok (MitalAshok)

<details>
<summary>Changes</summary>

Fixes #<!-- -->78355

---
Full diff: https://github.com/llvm/llvm-project/pull/78356.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaOverload.cpp (+13-7) 
- (modified) clang/test/SemaCXX/overloaded-operator.cpp (+27-1) 


``````````diff
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 37c62b306b3cd3..cde0f3c13e4d9c 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -15089,10 +15089,12 @@ ExprResult Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc,
           TheCall = CXXOperatorCallExpr::Create(
               Context, OO_Subscript, FnExpr.get(), MethodArgs, ResultTy, VK,
               RLoc, CurFPFeatureOverrides());
-        else
-          TheCall =
-              CallExpr::Create(Context, FnExpr.get(), MethodArgs, ResultTy, VK,
-                               RLoc, CurFPFeatureOverrides());
+        else {
+          if (Base->isPRValue())
+            Base = CreateMaterializeTemporaryExpr(Base->getType().getNonReferenceType(), Base, /*BoundToLvalueReference=*/false);
+          ExprResult OperatorAccess = MemberExpr::Create(Context, Base, /*IsArrow=*/false, LLoc, NestedNameSpecifierLoc(), SourceLocation(), FnDecl, Best->FoundDecl, OpLocInfo, nullptr, FnDecl->getType(), ExprValueKind::VK_LValue, ExprObjectKind::OK_Ordinary, NonOdrUseReason::NOUR_None);
+          TheCall = CallExpr::Create(Context, CallExprUnaryConversions(OperatorAccess.get()).get(), MethodArgs, ResultTy, VK, RLoc, CurFPFeatureOverrides());
+        }
 
         if (CheckCallReturnType(FnDecl->getReturnType(), LLoc, TheCall, FnDecl))
           return ExprError();
@@ -15767,9 +15769,13 @@ Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj,
     TheCall = CXXOperatorCallExpr::Create(Context, OO_Call, NewFn.get(),
                                           MethodArgs, ResultTy, VK, RParenLoc,
                                           CurFPFeatureOverrides());
-  else
-    TheCall = CallExpr::Create(Context, NewFn.get(), MethodArgs, ResultTy, VK,
-                               RParenLoc, CurFPFeatureOverrides());
+  else {
+    Expr *Base = Object.get();
+    if (Base->isPRValue())
+      Base = CreateMaterializeTemporaryExpr(Base->getType().getNonReferenceType(), Base, /*BoundToLvalueReference=*/false);
+    ExprResult OperatorAccess = MemberExpr::Create(Context, Base, /*IsArrow=*/false, LParenLoc, NestedNameSpecifierLoc(), SourceLocation(), Method, Best->FoundDecl, OpLocInfo, nullptr, Method->getType(), ExprValueKind::VK_LValue, ExprObjectKind::OK_Ordinary, NonOdrUseReason::NOUR_None);
+    TheCall = CallExpr::Create(Context, CallExprUnaryConversions(OperatorAccess.get()).get(), MethodArgs, ResultTy, VK, RParenLoc, CurFPFeatureOverrides());
+  }
 
   if (CheckCallReturnType(Method->getReturnType(), LParenLoc, TheCall, Method))
     return true;
diff --git a/clang/test/SemaCXX/overloaded-operator.cpp b/clang/test/SemaCXX/overloaded-operator.cpp
index 83a7e65b43dd01..361adda58e48b6 100644
--- a/clang/test/SemaCXX/overloaded-operator.cpp
+++ b/clang/test/SemaCXX/overloaded-operator.cpp
@@ -1,4 +1,6 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s 
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
+
 class X { };
 
 X operator+(X, X);
@@ -598,3 +600,27 @@ namespace B {
 }
 void g(B::X x) { A::f(x); }
 }
+
+namespace static_operator {
+#if __cplusplus >= 201703L
+struct X {
+  static constexpr int operator()() { return 0; }
+  // expected-warning at -1 {{declaring overloaded 'operator()' as 'static' is a C++23 extension}}
+  static constexpr int operator[](int) { return 0; }
+  // expected-warning at -1 {{declaring overloaded 'operator[]' as 'static' is a C++23 extension}}
+};
+
+constexpr int f(int x) {
+  return (++x, X())(), (++x, X())[1], x;
+}
+
+static_assert(f(0) == 2, "");
+
+constexpr int g(int x) {
+  return (++x, []() static { return 0; })(), x;
+  // expected-warning at -1 {{static lambdas are a C++23 extension}}
+}
+
+static_assert(g(0) == 1, "");
+#endif
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/78356


More information about the cfe-commits mailing list