r194767 - Don't reject dependent range-based for loops in constexpr functions. The loop

Richard Smith richard-llvm at metafoo.co.uk
Thu Nov 14 18:29:26 PST 2013


Author: rsmith
Date: Thu Nov 14 20:29:26 2013
New Revision: 194767

URL: http://llvm.org/viewvc/llvm-project?rev=194767&view=rev
Log:
Don't reject dependent range-based for loops in constexpr functions. The loop
variable isn't really uninitialized, it's just not initialized yet.

Modified:
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/test/SemaCXX/constant-expression-cxx1y.cpp

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=194767&r1=194766&r2=194767&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Thu Nov 14 20:29:26 2013
@@ -880,7 +880,7 @@ static bool CheckConstexprDeclStmt(Sema
               diag::err_constexpr_local_var_non_literal_type,
               isa<CXXConstructorDecl>(Dcl)))
           return false;
-        if (!VD->hasInit()) {
+        if (!VD->hasInit() && !VD->isCXXForRangeDecl()) {
           SemaRef.Diag(VD->getLocation(),
                        diag::err_constexpr_local_var_no_init)
             << isa<CXXConstructorDecl>(Dcl);

Modified: cfe/trunk/test/SemaCXX/constant-expression-cxx1y.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/constant-expression-cxx1y.cpp?rev=194767&r1=194766&r2=194767&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/constant-expression-cxx1y.cpp (original)
+++ cfe/trunk/test/SemaCXX/constant-expression-cxx1y.cpp Thu Nov 14 20:29:26 2013
@@ -898,3 +898,16 @@ namespace PR17615 {
   };
   constexpr int k = A().r; // expected-error {{constant expression}} expected-note {{in call to}}
 }
+
+namespace PR17331 {
+  template<typename T, unsigned int N>
+  constexpr T sum(const T (&arr)[N]) {
+    T result = 0;
+    for (T i : arr)
+      result += i;
+    return result;
+  }
+
+  constexpr int ARR[] = { 1, 2, 3, 4, 5 };
+  static_assert(sum(ARR) == 15, "");
+}





More information about the cfe-commits mailing list