[PATCH] D111817: Fix a rejects-valid with consteval on overloaded operators

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 14 09:45:40 PDT 2021


aaron.ballman created this revision.
aaron.ballman added reviewers: rsmith, rjmccall, erichkeane.
aaron.ballman requested review of this revision.
Herald added a project: clang.

It seems that Clang 11 regressed functionality that was working in Clang 10 regarding calling a few overloaded operators in an immediate context. Specifically, we were not checking for immediate invocations of array subscripting and the arrow operators, but we properly handle the other overloaded operators.

This fixes the two problematic operators and adds some test coverage to show they're equivalent to calling the operator directly. This addresses PR50779.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D111817

Files:
  clang/lib/Sema/SemaOverload.cpp
  clang/test/SemaCXX/cxx2a-consteval.cpp


Index: clang/test/SemaCXX/cxx2a-consteval.cpp
===================================================================
--- clang/test/SemaCXX/cxx2a-consteval.cpp
+++ clang/test/SemaCXX/cxx2a-consteval.cpp
@@ -612,3 +612,27 @@
 static_assert(is_same<long, T>::value);
 
 } // namespace unevaluated
+
+namespace PR50779 {
+struct derp {
+  int b = 0;
+};
+
+constexpr derp d;
+
+struct test {
+  consteval int operator[](int i) const { return {}; }
+  consteval const derp * operator->() const { return &d; }
+};
+
+constexpr test a;
+
+// We previously rejected both of these overloaded operators as taking the
+// address of a consteval function outside of an immediate context, but we
+// accepted direct calls to the overloaded operator. Now we show that we accept
+// both forms.
+constexpr int s = a.operator[](1);
+constexpr int t = a[1];
+constexpr int u = a.operator->()->b;
+constexpr int v = a->b;
+}
Index: clang/lib/Sema/SemaOverload.cpp
===================================================================
--- clang/lib/Sema/SemaOverload.cpp
+++ clang/lib/Sema/SemaOverload.cpp
@@ -14161,7 +14161,8 @@
                               Method->getType()->castAs<FunctionProtoType>()))
           return ExprError();
 
-        return MaybeBindToTemporary(TheCall);
+        return CheckForImmediateInvocation(MaybeBindToTemporary(TheCall),
+                                           FnDecl);
       } else {
         // We matched a built-in operator. Convert the arguments, then
         // break out so that we will build the appropriate built-in
@@ -14916,7 +14917,7 @@
                         Method->getType()->castAs<FunctionProtoType>()))
     return ExprError();
 
-  return MaybeBindToTemporary(TheCall);
+  return CheckForImmediateInvocation(MaybeBindToTemporary(TheCall), Method);
 }
 
 /// BuildLiteralOperatorCall - Build a UserDefinedLiteral by creating a call to


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D111817.379751.patch
Type: text/x-patch
Size: 1883 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211014/894b58bb/attachment-0001.bin>


More information about the cfe-commits mailing list