public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* C++ PATCH to fix rejects-valid with constexpr ctor in C++17 (PR c++/83692)
@ 2018-01-25 21:58 Marek Polacek
  2018-01-25 22:37 ` Marek Polacek
  2018-02-02 19:11 ` Jason Merrill
  0 siblings, 2 replies; 8+ messages in thread
From: Marek Polacek @ 2018-01-25 21:58 UTC (permalink / raw)
  To: GCC Patches, Jason Merrill

This is a similar problem to 83116: we'd cached a constexpr call, but after a
store the result had become invalid, yet we used the wrong result again when
encountering the same call later.  This resulted in evaluating a THROW_EXPR
which doesn't work.  Details in
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83692#c5

The fix for 83116 didn't work here, because when evaluating the body of the
ctor via store_init_value -> cxx_constant_value we are in STRICT, so we do
cache.

It seems that we may no longer rely on the constexpr call table when we
do cxx_eval_store_expression, because that just rewrites *valp, i.e. the
value of an object.  Might be too big a hammer again, but I couldn't think
of how I could guard the caching of a constexpr call.

This doesn't manifest in C++14 because build_special_member_call in C++17 is
more aggressive with copy elisions (as required by P0135 which changed how we
view prvalues).  In C++14 build_special_member_call produces a CALL_EXPR, so
expand_default_init calls maybe_constant_init, for which STRICT is false, so
we avoid caching as per 83116.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2018-01-25  Marek Polacek  <polacek@redhat.com>

	PR c++/83692
	* constexpr.c (cxx_eval_store_expression): Clear constexpr_call_table.

	* g++.dg/cpp1y/constexpr-83692.C: New test.

diff --git gcc/cp/constexpr.c gcc/cp/constexpr.c
index 4d2ee4a28fc..0202d22f320 100644
--- gcc/cp/constexpr.c
+++ gcc/cp/constexpr.c
@@ -3663,6 +3663,10 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
   else
     *valp = init;
 
+  /* We've rewritten a value of a temporary in this constexpr
+     context which might invalide a cached call.  */
+  constexpr_call_table = NULL;
+
   /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
      CONSTRUCTORs, if any.  */
   tree elt;
diff --git gcc/testsuite/g++.dg/cpp1y/constexpr-83692.C gcc/testsuite/g++.dg/cpp1y/constexpr-83692.C
index e69de29bb2d..292ba7c22e9 100644
--- gcc/testsuite/g++.dg/cpp1y/constexpr-83692.C
+++ gcc/testsuite/g++.dg/cpp1y/constexpr-83692.C
@@ -0,0 +1,21 @@
+// PR c++/83692
+// { dg-do compile { target c++14 } }
+
+struct integer {
+  constexpr int value() const { return m_value;	}
+  int m_value;
+};
+
+struct outer {
+  integer m_x{0};
+  constexpr outer()
+    {
+      if (m_x.value() != 0)
+	throw 0;
+      m_x.m_value = integer{1}.value();
+      if (m_x.value() != 1)
+	throw 0;
+    }
+};
+
+constexpr outer o{};

	Marek

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2018-02-24  1:55 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-25 21:58 C++ PATCH to fix rejects-valid with constexpr ctor in C++17 (PR c++/83692) Marek Polacek
2018-01-25 22:37 ` Marek Polacek
2018-02-02 19:11 ` Jason Merrill
2018-02-05 13:38   ` Marek Polacek
2018-02-05 18:45     ` Jason Merrill
2018-02-16 21:10       ` Jason Merrill
2018-02-23 14:30         ` Marek Polacek
2018-02-24  1:55           ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).