[C++] Fix OBJ_TYPE_REF constexpr handling (PR c++/92695)

Message ID 20191127234443.GF10088@tucnak
State New
Headers show
Series
  • [C++] Fix OBJ_TYPE_REF constexpr handling (PR c++/92695)
Related show

Commit Message

Jakub Jelinek Nov. 27, 2019, 11:44 p.m.
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.


	Jakub

Comments

Marek Polacek Nov. 27, 2019, 11:54 p.m. | #1
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
Jason Merrill Dec. 2, 2019, 7:10 p.m. | #2
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

>

Patch

--- 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);