public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [C++ PATCH] Fix OBJ_TYPE_REF constexpr handling (PR c++/92695)
@ 2019-11-27 23:54 Jakub Jelinek
  2019-11-28  0:13 ` Marek Polacek
  2019-12-02 19:10 ` Jason Merrill
  0 siblings, 2 replies; 3+ messages in thread
From: Jakub Jelinek @ 2019-11-27 23:54 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches

Hi!

On the following testcase the constexpr evaluation of the virtual call
fails, because what cxx_eval_constant_expression returns for
OBJ_TYPE_REF_OBJECT is actually not ADDR_EXPR, but ADDR_EXPR wrapped in
a NOP_EXPR.

Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
trunk?

2019-11-27  Jakub Jelinek  <jakub@redhat.com>

	PR c++/92695
	* constexpr.c (cxx_eval_constant_expression) <case OBJ_TYPE_REF>: Use
	STRIP_NOPS before checking for ADDR_EXPR.

	* g++.dg/cpp2a/constexpr-virtual15.C: New test.

--- gcc/cp/constexpr.c.jj	2019-11-27 17:53:37.477566346 +0100
+++ gcc/cp/constexpr.c	2019-11-27 21:16:51.094188509 +0100
@@ -5566,6 +5566,7 @@ cxx_eval_constant_expression (const cons
 	tree obj = OBJ_TYPE_REF_OBJECT (t);
 	obj = cxx_eval_constant_expression (ctx, obj, lval, non_constant_p,
 					    overflow_p);
+	STRIP_NOPS (obj);
 	/* We expect something in the form of &x.D.2103.D.2094; get x. */
 	if (TREE_CODE (obj) != ADDR_EXPR
 	    || !DECL_P (get_base_address (TREE_OPERAND (obj, 0))))
--- gcc/testsuite/g++.dg/cpp2a/constexpr-virtual15.C.jj	2019-11-27 21:18:15.418895652 +0100
+++ gcc/testsuite/g++.dg/cpp2a/constexpr-virtual15.C	2019-11-27 21:17:48.602306802 +0100
@@ -0,0 +1,7 @@
+// PR c++/92695
+// { dg-do compile { target c++2a } }
+
+struct A { virtual int get() = 0; };
+struct B : A { constexpr int get() override { return 10; } };
+struct D { B b[2]; A* c{&(b[0])}; };
+static_assert(D{}.c->get() == 10);

	Jakub

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

* Re: [C++ PATCH] Fix OBJ_TYPE_REF constexpr handling (PR c++/92695)
  2019-11-27 23:54 [C++ PATCH] Fix OBJ_TYPE_REF constexpr handling (PR c++/92695) Jakub Jelinek
@ 2019-11-28  0:13 ` Marek Polacek
  2019-12-02 19:10 ` Jason Merrill
  1 sibling, 0 replies; 3+ messages in thread
From: Marek Polacek @ 2019-11-28  0:13 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: Jason Merrill, gcc-patches

On Thu, Nov 28, 2019 at 12:44:43AM +0100, Jakub Jelinek wrote:
> Hi!
> 
> On the following testcase the constexpr evaluation of the virtual call
> fails, because what cxx_eval_constant_expression returns for
> OBJ_TYPE_REF_OBJECT is actually not ADDR_EXPR, but ADDR_EXPR wrapped in
> a NOP_EXPR.
> 
> Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
> trunk?

Looks ok.  In fact, when working on constexpr dynamic_cast, I ran into a very
similar problem and also added STRIP_NOPS.

--
Marek Polacek • Red Hat, Inc. • 300 A St, Boston, MA

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

* Re: [C++ PATCH] Fix OBJ_TYPE_REF constexpr handling (PR c++/92695)
  2019-11-27 23:54 [C++ PATCH] Fix OBJ_TYPE_REF constexpr handling (PR c++/92695) Jakub Jelinek
  2019-11-28  0:13 ` Marek Polacek
@ 2019-12-02 19:10 ` Jason Merrill
  1 sibling, 0 replies; 3+ messages in thread
From: Jason Merrill @ 2019-12-02 19:10 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches

On 11/27/19 6:44 PM, Jakub Jelinek wrote:
> Hi!
> 
> On the following testcase the constexpr evaluation of the virtual call
> fails, because what cxx_eval_constant_expression returns for
> OBJ_TYPE_REF_OBJECT is actually not ADDR_EXPR, but ADDR_EXPR wrapped in
> a NOP_EXPR.
> 
> Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
> trunk?

OK.

> 2019-11-27  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c++/92695
> 	* constexpr.c (cxx_eval_constant_expression) <case OBJ_TYPE_REF>: Use
> 	STRIP_NOPS before checking for ADDR_EXPR.
> 
> 	* g++.dg/cpp2a/constexpr-virtual15.C: New test.
> 
> --- gcc/cp/constexpr.c.jj	2019-11-27 17:53:37.477566346 +0100
> +++ gcc/cp/constexpr.c	2019-11-27 21:16:51.094188509 +0100
> @@ -5566,6 +5566,7 @@ cxx_eval_constant_expression (const cons
>   	tree obj = OBJ_TYPE_REF_OBJECT (t);
>   	obj = cxx_eval_constant_expression (ctx, obj, lval, non_constant_p,
>   					    overflow_p);
> +	STRIP_NOPS (obj);
>   	/* We expect something in the form of &x.D.2103.D.2094; get x. */
>   	if (TREE_CODE (obj) != ADDR_EXPR
>   	    || !DECL_P (get_base_address (TREE_OPERAND (obj, 0))))
> --- gcc/testsuite/g++.dg/cpp2a/constexpr-virtual15.C.jj	2019-11-27 21:18:15.418895652 +0100
> +++ gcc/testsuite/g++.dg/cpp2a/constexpr-virtual15.C	2019-11-27 21:17:48.602306802 +0100
> @@ -0,0 +1,7 @@
> +// PR c++/92695
> +// { dg-do compile { target c++2a } }
> +
> +struct A { virtual int get() = 0; };
> +struct B : A { constexpr int get() override { return 10; } };
> +struct D { B b[2]; A* c{&(b[0])}; };
> +static_assert(D{}.c->get() == 10);
> 
> 	Jakub
> 

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

end of thread, other threads:[~2019-12-02 19:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-27 23:54 [C++ PATCH] Fix OBJ_TYPE_REF constexpr handling (PR c++/92695) Jakub Jelinek
2019-11-28  0:13 ` Marek Polacek
2019-12-02 19:10 ` 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).