r306327 - Check that the initializer of a non-dependent constexpr variable is constant even within templates.
Richard Smith via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 26 13:33:42 PDT 2017
Author: rsmith
Date: Mon Jun 26 13:33:42 2017
New Revision: 306327
URL: http://llvm.org/viewvc/llvm-project?rev=306327&view=rev
Log:
Check that the initializer of a non-dependent constexpr variable is constant even within templates.
Modified:
cfe/trunk/lib/Sema/SemaDecl.cpp
cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=306327&r1=306326&r2=306327&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Mon Jun 26 13:33:42 2017
@@ -11100,9 +11100,7 @@ void Sema::CheckCompleteVariableDeclarat
bool IsGlobal = GlobalStorage && !var->isStaticLocal();
QualType baseType = Context.getBaseElementType(type);
- if (!var->getDeclContext()->isDependentContext() &&
- Init && !Init->isValueDependent()) {
-
+ if (Init && !Init->isValueDependent()) {
if (var->isConstexpr()) {
SmallVector<PartialDiagnosticAt, 8> Notes;
if (!var->evaluateValue(Notes) || !var->isInitICE()) {
Modified: cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp?rev=306327&r1=306326&r2=306327&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp (original)
+++ cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp Mon Jun 26 13:33:42 2017
@@ -608,7 +608,7 @@ namespace DependentValues {
struct I { int n; typedef I V[10]; };
I::V x, y;
-int g();
+int g(); // expected-note {{declared here}}
template<bool B, typename T> struct S : T {
int k;
void f() {
@@ -616,13 +616,23 @@ template<bool B, typename T> struct S :
I &i = cells[k];
switch (i.n) {}
- // FIXME: We should be able to diagnose this.
- constexpr int n = g();
+ constexpr int n = g(); // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr function 'g'}}
constexpr int m = this->g(); // ok, could be constexpr
}
};
+extern const int n;
+template<typename T> void f() {
+ // This is ill-formed, because a hypothetical instantiation at the point of
+ // template definition would be ill-formed due to a construct that does not
+ // depend on a template parameter.
+ constexpr int k = n; // expected-error {{must be initialized by a constant expression}}
+}
+// It doesn't matter that the instantiation could later become valid:
+constexpr int n = 4;
+template void f<int>();
+
}
namespace Class {
More information about the cfe-commits
mailing list