diff mbox

C++ PATCH for c++/66387 (error with lambda and constexpr array)

Message ID 55783518.6000106@redhat.com
State New
Headers show

Commit Message

Jason Merrill June 10, 2015, 1:01 p.m. UTC
My change to rationalize the *_constant_value functions broke this 
testcase, because we were no longer replacing the reference to the array 
with its constant value.  Fixed by using maybe_constant_value instead.

Tested x86_64-pc-linux-gnu, applying to trunk.
diff mbox

Patch

commit 00557979b84dabd18eb7cf46cb20bed9cf9bd7f6
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Jun 5 16:19:11 2015 -0400

    	PR c++/66387
    	* semantics.c (process_outer_var_ref): Make sure the value is
    	actually constant before returning it.
    	* typeck.c (cp_build_array_ref): Allow subscripting non-lvalue
    	array.

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 650ef4c..59ec9047 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -3128,7 +3128,11 @@  process_outer_var_ref (tree decl, tsubst_flags_t complain)
 	   form, so wait until instantiation time.  */
 	return decl;
       else if (decl_constant_var_p (decl))
-	return scalar_constant_value (decl);
+	{
+	  tree t = maybe_constant_value (convert_from_reference (decl));
+	  if (TREE_CONSTANT (t))
+	    return t;
+	}
     }
 
   if (parsing_nsdmi ())
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 6c4b038..5b09b73 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -3141,15 +3141,6 @@  cp_build_array_ref (location_t loc, tree array, tree idx,
 	    return error_mark_node;
 	}
 
-      if (!lvalue_p (array))
-	{
-	  if (complain & tf_error)
-	    pedwarn (loc, OPT_Wpedantic, 
-		     "ISO C++ forbids subscripting non-lvalue array");
-	  else
-	    return error_mark_node;
-	}
-
       /* Note in C++ it is valid to subscript a `register' array, since
 	 it is valid to take the address of something with that
 	 storage specification.  */
diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const5.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const5.C
new file mode 100644
index 0000000..cba0370
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const5.C
@@ -0,0 +1,16 @@ 
+// PR c++/66387
+// { dg-do compile { target c++11 } }
+
+template <typename T>
+void
+bar (T x)
+{
+  x ();
+}
+
+void
+foo ()
+{
+  constexpr int a[1] = { 1 };
+  bar ([&]{ return a[0]; });
+}