diff mbox series

[C++,2/3] PR c++/91369 - constexpr destructor and member initializer.

Message ID 20200108202506.27411-2-jason@redhat.com
State New
Headers show
Series [C++,1/3] Remove constexpr support for DECL_BY_REFERENCE. | expand

Commit Message

Jason Merrill Jan. 8, 2020, 8:25 p.m. UTC
Previously it didn't matter whether we looked through a TARGET_EXPR in
constexpr evaluation, but now that we have constexpr destructors it does.
On IRC I mentioned the idea of clearing TARGET_EXPR_CLEANUP in
digest_nsdmi_init, but since this initialization is expressed by an
INIT_EXPR, it's better to handle all INIT_EXPR, not just those for a member
initializer.

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

	* constexpr.c (cxx_eval_store_expression): Look through TARGET_EXPR
	when not preevaluating.
---
 gcc/cp/constexpr.c                           |  6 ++++++
 gcc/testsuite/g++.dg/cpp2a/constexpr-new10.C | 19 +++++++++++++++++++
 2 files changed, 25 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-new10.C
diff mbox series

Patch

diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 806d3ab2cff..5fe6d0277b6 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -4577,6 +4577,12 @@  cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
 	}
       new_ctx.ctor = *valp;
       new_ctx.object = target;
+      /* Avoid temporary materialization when initializing from a TARGET_EXPR.
+	 We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
+	 expansion of those trees uses ctx instead.  */
+      if (TREE_CODE (init) == TARGET_EXPR)
+	if (tree tinit = TARGET_EXPR_INITIAL (init))
+	  init = tinit;
       init = cxx_eval_constant_expression (&new_ctx, init, false,
 					   non_constant_p, overflow_p);
       if (ctors->is_empty())
diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-new10.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-new10.C
new file mode 100644
index 00000000000..500a3240c8f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-new10.C
@@ -0,0 +1,19 @@ 
+// PR c++/91369
+// { dg-do compile { target c++2a } }
+
+struct S {
+  constexpr S (int* i) : s{i} {}
+  constexpr ~S () { delete s; }
+  int *s;
+};
+
+struct T { S t = { new int }; };
+
+constexpr auto
+foo ()
+{
+  T b;
+  return true;
+}
+
+static_assert (foo ());