[PATCH] D132398: Allow static consteval member functions to be used in const expressions.

Utkarsh Saxena via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 22 10:36:33 PDT 2022


usaxena95 created this revision.
Herald added a project: All.
usaxena95 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132398

Files:
  clang/lib/AST/ExprConstant.cpp
  clang/test/SemaCXX/constant-expression-cxx11.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
@@ -908,3 +908,15 @@
   return testDefaultArgForParam() + testDefaultArgForParam((E)1);
 }
 }
+
+namespace GH56246 {
+struct S {
+    static consteval int hello() { return 1;}
+    consteval int baz() const { return this->hello();}
+    // Previously these two failed because of the use of `this` in the constant expressions.
+    int foo() const { return this->hello(); }
+    constexpr int bar() const { return this->hello();}
+} s;
+static_assert(s.bar() == 1);
+static_assert(s.baz() == 1);
+}
\ No newline at end of file
Index: clang/test/SemaCXX/constant-expression-cxx11.cpp
===================================================================
--- clang/test/SemaCXX/constant-expression-cxx11.cpp
+++ clang/test/SemaCXX/constant-expression-cxx11.cpp
@@ -2356,15 +2356,19 @@
   template<typename T> void f1(T t) {
     constexpr int k = t.size();
   }
-  template<typename T> void f2(const T &t) { // expected-note 2{{declared here}}
-    constexpr int k = t.size(); // expected-error 2{{constant}} expected-note 2{{function parameter 't' with unknown value cannot be used in a constant expression}}
+  template<typename T> void f2_for_non_static(const T &t) { // expected-note {{declared here}}
+    constexpr int k = t.size(); // expected-error {{constant}} expected-note {{function parameter 't' with unknown value cannot be used in a constant expression}}
+  }
+  template<typename T> void f2_for_static(const T &t) {
+    constexpr int k = t.size();
+    static_assert(k==3, "");
   }
   template<typename T> void f3(const T &t) {
     constexpr int k = T::size();
   }
   void g(array<3> a) {
     f1(a);
-    f2(a); // expected-note {{instantiation of}}
+    f2_for_static(a);
     f3(a);
   }
 
@@ -2373,7 +2377,7 @@
   };
   void h(array_nonstatic<3> a) {
     f1(a);
-    f2(a); // expected-note {{instantiation of}}
+    f2_for_non_static(a); // expected-note {{instantiation of}}
   }
 }
 
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -43,7 +43,9 @@
 #include "clang/AST/CXXInheritance.h"
 #include "clang/AST/CharUnits.h"
 #include "clang/AST/CurrentSourceLocExprScope.h"
+#include "clang/AST/DeclCXX.h"
 #include "clang/AST/Expr.h"
+#include "clang/AST/ExprCXX.h"
 #include "clang/AST/OSLog.h"
 #include "clang/AST/OptionalDiagnostic.h"
 #include "clang/AST/RecordLayout.h"
@@ -8449,7 +8451,8 @@
   // Handle static member functions.
   if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) {
     if (MD->isStatic()) {
-      VisitIgnoredBaseExpression(E->getBase());
+      if(Info.checkingPotentialConstantExpression())
+        VisitIgnoredBaseExpression(E->getBase());
       return Success(MD);
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132398.454563.patch
Type: text/x-patch
Size: 2987 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220822/45989837/attachment.bin>


More information about the cfe-commits mailing list