diff mbox

Fix constexpr evaluation of comparisons involving pointer-to-members

Message ID 1450739227-18825-1-git-send-email-patrick@parcs.ath.cx
State New
Headers show

Commit Message

Patrick Palka Dec. 21, 2015, 11:07 p.m. UTC
We are currently failing to fold equality comparisons involving
PTRMEM_CSTs since (I think) r229018 thus making this a GCC 6 regression.
This regression shows up in the boost testsuite.

Fixed in a straightforward way.  OK to commit after bootstrap + regtest?

gcc/cp/ChangeLog:

	* constexpr.c (cxx_eval_binary_expression): Expand
	the PTRMEM_CST operands of an equality comparison.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/constexpr-ptrmem5.C: New test.
---
 gcc/cp/constexpr.c                             |  9 +++++++++
 gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C | 14 ++++++++++++++
 2 files changed, 23 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C

Comments

Patrick Palka Jan. 11, 2016, 3:19 a.m. UTC | #1
On Mon, Dec 21, 2015 at 6:07 PM, Patrick Palka <patrick@parcs.ath.cx> wrote:
> We are currently failing to fold equality comparisons involving
> PTRMEM_CSTs since (I think) r229018 thus making this a GCC 6 regression.
> This regression shows up in the boost testsuite.
>
> Fixed in a straightforward way.  OK to commit after bootstrap + regtest?
>
> gcc/cp/ChangeLog:
>
>         * constexpr.c (cxx_eval_binary_expression): Expand
>         the PTRMEM_CST operands of an equality comparison.
>
> gcc/testsuite/ChangeLog:
>
>         * g++.dg/cpp0x/constexpr-ptrmem5.C: New test.
> ---
>  gcc/cp/constexpr.c                             |  9 +++++++++
>  gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C | 14 ++++++++++++++
>  2 files changed, 23 insertions(+)
>  create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C
>
> diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
> index 208f43b..1ad87aa 100644
> --- a/gcc/cp/constexpr.c
> +++ b/gcc/cp/constexpr.c
> @@ -1630,6 +1630,15 @@ cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
>    location_t loc = EXPR_LOCATION (t);
>    enum tree_code code = TREE_CODE (t);
>    tree type = TREE_TYPE (t);
> +
> +  if (code == EQ_EXPR || code == NE_EXPR)
> +    {
> +      if (TREE_CODE (lhs) == PTRMEM_CST && CONSTANT_CLASS_P (rhs))
> +       lhs = cplus_expand_constant (lhs);
> +      if (TREE_CODE (rhs) == PTRMEM_CST && CONSTANT_CLASS_P (lhs))
> +       rhs = cplus_expand_constant (rhs);
> +    }
> +
>    r = fold_binary_loc (loc, code, type, lhs, rhs);
>    if (r == NULL_TREE)
>      {
> diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C
> new file mode 100644
> index 0000000..e28c917
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C
> @@ -0,0 +1,14 @@
> +// { dg-do compile { target c++11 } }
> +
> +#define SA(x) static_assert ((x), #x)
> +
> +struct X { int a, b; };
> +
> +void
> +foo ()
> +{
> +  SA (&X::a);
> +  SA (&X::a == &X::a);
> +  SA (&X::a != &X::b);
> +  SA ((!&X::b) == 0);
> +}
> --
> 2.7.0.rc0.50.g1470d8f.dirty
>

Ping.
Patrick Palka Jan. 25, 2016, 12:12 a.m. UTC | #2
On Sun, Jan 10, 2016 at 10:19 PM, Patrick Palka <patrick@parcs.ath.cx> wrote:
> On Mon, Dec 21, 2015 at 6:07 PM, Patrick Palka <patrick@parcs.ath.cx> wrote:
>> We are currently failing to fold equality comparisons involving
>> PTRMEM_CSTs since (I think) r229018 thus making this a GCC 6 regression.
>> This regression shows up in the boost testsuite.
>>
>> Fixed in a straightforward way.  OK to commit after bootstrap + regtest?
>>
>> gcc/cp/ChangeLog:
>>
>>         * constexpr.c (cxx_eval_binary_expression): Expand
>>         the PTRMEM_CST operands of an equality comparison.
>>
>> gcc/testsuite/ChangeLog:
>>
>>         * g++.dg/cpp0x/constexpr-ptrmem5.C: New test.
>> ---
>>  gcc/cp/constexpr.c                             |  9 +++++++++
>>  gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C | 14 ++++++++++++++
>>  2 files changed, 23 insertions(+)
>>  create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C
>>
>> diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
>> index 208f43b..1ad87aa 100644
>> --- a/gcc/cp/constexpr.c
>> +++ b/gcc/cp/constexpr.c
>> @@ -1630,6 +1630,15 @@ cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
>>    location_t loc = EXPR_LOCATION (t);
>>    enum tree_code code = TREE_CODE (t);
>>    tree type = TREE_TYPE (t);
>> +
>> +  if (code == EQ_EXPR || code == NE_EXPR)
>> +    {
>> +      if (TREE_CODE (lhs) == PTRMEM_CST && CONSTANT_CLASS_P (rhs))
>> +       lhs = cplus_expand_constant (lhs);
>> +      if (TREE_CODE (rhs) == PTRMEM_CST && CONSTANT_CLASS_P (lhs))
>> +       rhs = cplus_expand_constant (rhs);
>> +    }
>> +
>>    r = fold_binary_loc (loc, code, type, lhs, rhs);
>>    if (r == NULL_TREE)
>>      {
>> diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C
>> new file mode 100644
>> index 0000000..e28c917
>> --- /dev/null
>> +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C
>> @@ -0,0 +1,14 @@
>> +// { dg-do compile { target c++11 } }
>> +
>> +#define SA(x) static_assert ((x), #x)
>> +
>> +struct X { int a, b; };
>> +
>> +void
>> +foo ()
>> +{
>> +  SA (&X::a);
>> +  SA (&X::a == &X::a);
>> +  SA (&X::a != &X::b);
>> +  SA ((!&X::b) == 0);
>> +}
>> --
>> 2.7.0.rc0.50.g1470d8f.dirty
>>
>
> Ping.

Ping.
Jason Merrill Feb. 2, 2016, 6:51 p.m. UTC | #3
On 12/22/2015 12:07 AM, Patrick Palka wrote:
> +  if (code == EQ_EXPR || code == NE_EXPR)
> +    {
> +      if (TREE_CODE (lhs) == PTRMEM_CST && CONSTANT_CLASS_P (rhs))
> +	lhs = cplus_expand_constant (lhs);
> +      if (TREE_CODE (rhs) == PTRMEM_CST && CONSTANT_CLASS_P (lhs))
> +	rhs = cplus_expand_constant (rhs);
> +    }

If both sides are PTRMEM_CST, we should be able to compare them without 
expanding, using cp_tree_equal.

Jason
diff mbox

Patch

diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 208f43b..1ad87aa 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -1630,6 +1630,15 @@  cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
   location_t loc = EXPR_LOCATION (t);
   enum tree_code code = TREE_CODE (t);
   tree type = TREE_TYPE (t);
+
+  if (code == EQ_EXPR || code == NE_EXPR)
+    {
+      if (TREE_CODE (lhs) == PTRMEM_CST && CONSTANT_CLASS_P (rhs))
+	lhs = cplus_expand_constant (lhs);
+      if (TREE_CODE (rhs) == PTRMEM_CST && CONSTANT_CLASS_P (lhs))
+	rhs = cplus_expand_constant (rhs);
+    }
+
   r = fold_binary_loc (loc, code, type, lhs, rhs);
   if (r == NULL_TREE)
     {
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C
new file mode 100644
index 0000000..e28c917
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-ptrmem5.C
@@ -0,0 +1,14 @@ 
+// { dg-do compile { target c++11 } }
+
+#define SA(x) static_assert ((x), #x)
+
+struct X { int a, b; };
+
+void
+foo ()
+{
+  SA (&X::a);
+  SA (&X::a == &X::a);
+  SA (&X::a != &X::b);
+  SA ((!&X::b) == 0);
+}