diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp --- a/clang/lib/AST/ExprConstant.cpp +++ b/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(E->getMemberDecl())) { if (MD->isStatic()) { - VisitIgnoredBaseExpression(E->getBase()); + if(Info.checkingPotentialConstantExpression()) + VisitIgnoredBaseExpression(E->getBase()); return Success(MD); } } diff --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp --- a/clang/test/SemaCXX/constant-expression-cxx11.cpp +++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp @@ -2356,15 +2356,19 @@ template void f1(T t) { constexpr int k = t.size(); } - template 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 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 void f2_for_static(const T &t) { + constexpr int k = t.size(); + static_assert(k==3, ""); } template 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}} } } diff --git a/clang/test/SemaCXX/cxx2a-consteval.cpp b/clang/test/SemaCXX/cxx2a-consteval.cpp --- a/clang/test/SemaCXX/cxx2a-consteval.cpp +++ b/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