diff mbox

C++ PATCH for c++/67550 (wrong value for reference to const class var)

Message ID 5672E7E3.40900@redhat.com
State New
Headers show

Commit Message

Jason Merrill Dec. 17, 2015, 4:50 p.m. UTC
In my rework of decl_constant_value and kin, I enabled its use in more 
places, which revealed a problem: when it is allowing non-constexpr 
aggregate initializers, we need to double-check that we aren't returning 
something that had initializers stripped out in split_nonconstant_init.

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

Patch

commit 230c77258443c76da9837a078f0326fee3311f02
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Dec 17 09:56:20 2015 -0500

    	PR c++/67550
    	* init.c (constant_value_1): Don't return a CONSTRUCTOR missing
    	non-constant elements.

diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index a08f7d7..b7f10a1 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -2093,6 +2093,11 @@  constant_value_1 (tree decl, bool strict_p, bool return_aggregate_cst_ok_p)
 	      && (TREE_CODE (init) == CONSTRUCTOR
 		  || TREE_CODE (init) == STRING_CST)))
 	break;
+      /* Don't return a CONSTRUCTOR for a variable with partial run-time
+	 initialization, since it doesn't represent the entire value.  */
+      if (TREE_CODE (init) == CONSTRUCTOR
+	  && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
+	break;
       decl = unshare_expr (init);
     }
   return decl;
diff --git a/gcc/testsuite/g++.dg/init/aggr13.C b/gcc/testsuite/g++.dg/init/aggr13.C
new file mode 100644
index 0000000..08248a6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/init/aggr13.C
@@ -0,0 +1,17 @@ 
+// PR c++/67550
+// { dg-do run }
+
+struct S {
+  int x;
+  int y;
+};
+int foo() { return 1; }
+
+int main() {
+  S const data[] = {{0, foo()}};
+
+  S data2[] = {data[0]};
+
+  if (!data2[0].y)
+    __builtin_abort();
+}