public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] libstdc++: Fix compare_three_way for constexpr and Clang
@ 2021-08-20 20:17 Paul Keir
  2021-09-03 10:31 ` Paul Keir
  2021-10-11 19:48 ` Jonathan Wakely
  0 siblings, 2 replies; 6+ messages in thread
From: Paul Keir @ 2021-08-20 20:17 UTC (permalink / raw)
  To: gcc-patches; +Cc: libstdc++

[-- Attachment #1: Type: text/plain, Size: 1364 bytes --]

Hi,

The current compare_three_way implementation makes provision for constant evaluation contexts (avoiding reinterpret_cast etc.), but the approach fails with Clang; when it compares two const volatile void pointers: "comparison between unequal pointers to void has unspecified result". I include a fix and test.

Could someone commit the attached patch for me?

Thanks,
Paul


Please consider the environment and think before you print.

The University of the West of Scotland is a registered Scottish charity. Charity number SC002520.

This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender.

Please note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of the University of the West of Scotland.

As a public body, the University of the West of Scotland may be required to make available emails as well as other written forms of information as a result of a request made under the Freedom of Information (Scotland) Act 2002.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: compare_three_way_constexpr.patch --]
[-- Type: text/x-patch; name="compare_three_way_constexpr.patch", Size: 2947 bytes --]

diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index 07cc83d98f4..638f00716c8 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,3 +1,10 @@
+2021-08-20  Paul Keir  <paul.keir@uws.ac.uk>
+
+	* libsupc++/compare: Avoid constexpr pointer comparison failure
+	in std::compare_three_way with Clang.
+	* testsuite/18_support/comparisons/pointers/constexpr.cc:
+	New test.
+
 2021-08-19  Jonathan Wakely  <jwakely@redhat.com>
 
 	* doc/xml/manual/status_cxx2020.xml: Move row  earlier in table.
diff --git a/libstdc++-v3/libsupc++/compare b/libstdc++-v3/libsupc++/compare
index 5aee89e3a6e..4081a3f2315 100644
--- a/libstdc++-v3/libsupc++/compare
+++ b/libstdc++-v3/libsupc++/compare
@@ -553,10 +553,10 @@ namespace std
       {
 	if constexpr (__detail::__3way_builtin_ptr_cmp<_Tp, _Up>)
 	  {
+	    if (__builtin_is_constant_evaluated())
+	      return static_cast<_Tp&&>(__t) <=> static_cast<_Up&&>(__u);
 	    auto __pt = static_cast<const volatile void*>(__t);
 	    auto __pu = static_cast<const volatile void*>(__u);
-	    if (__builtin_is_constant_evaluated())
-	      return __pt <=> __pu;
 	    auto __it = reinterpret_cast<__UINTPTR_TYPE__>(__pt);
 	    auto __iu = reinterpret_cast<__UINTPTR_TYPE__>(__pu);
 	    return __it <=> __iu;
diff --git a/libstdc++-v3/testsuite/18_support/comparisons/pointers/constexpr.cc b/libstdc++-v3/testsuite/18_support/comparisons/pointers/constexpr.cc
new file mode 100644
index 00000000000..8e1dc2ed6d1
--- /dev/null
+++ b/libstdc++-v3/testsuite/18_support/comparisons/pointers/constexpr.cc
@@ -0,0 +1,43 @@
+// Copyright (C) 2021 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+#include <functional>
+
+constexpr bool check01()
+{
+  int arr[2];
+  bool b1 = &arr[0] < &arr[1];
+  bool b2 = std::less{}(&arr[0], &arr[1]);
+  bool b3 = std::compare_three_way{}(&arr[0],&arr[1]) < 0;
+  return b1 && b2 && b3;
+}
+
+constexpr bool check02()
+{
+  int *p = new int[2];
+  bool b1 = &p[0] < &p[1];
+  bool b2 = std::less{}(&p[0], &p[1]);
+  bool b3 = std::compare_three_way{}(&p[0],&p[1]) < 0;
+  delete [] p;
+  return b1 && b2 && b3;
+}
+
+static_assert(check01());
+static_assert(check02());

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

* Re: [PATCH] libstdc++: Fix compare_three_way for constexpr and Clang
  2021-08-20 20:17 [PATCH] libstdc++: Fix compare_three_way for constexpr and Clang Paul Keir
@ 2021-09-03 10:31 ` Paul Keir
  2021-10-11 14:33   ` Paul Keir
  2021-10-11 19:48 ` Jonathan Wakely
  1 sibling, 1 reply; 6+ messages in thread
From: Paul Keir @ 2021-09-03 10:31 UTC (permalink / raw)
  To: gcc-patches; +Cc: libstdc++

*ping*

________________________________________
From: Paul Keir <Paul.Keir@uws.ac.uk>
Sent: 20 August 2021 21:17
To: gcc-patches@gcc.gnu.org
Cc: libstdc++@gcc.gnu.org
Subject: [PATCH] libstdc++: Fix compare_three_way for constexpr and Clang

Hi,

The current compare_three_way implementation makes provision for constant evaluation contexts (avoiding reinterpret_cast etc.), but the approach fails with Clang; when it compares two const volatile void pointers: "comparison between unequal pointers to void has unspecified result". I include a fix and test.

Could someone commit the attached patch for me?

Thanks,
Paul


Please consider the environment and think before you print.

The University of the West of Scotland is a registered Scottish charity. Charity number SC002520.

This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender.

Please note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of the University of the West of Scotland.

As a public body, the University of the West of Scotland may be required to make available emails as well as other written forms of information as a result of a request made under the Freedom of Information (Scotland) Act 2002.

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

* Re: [PATCH] libstdc++: Fix compare_three_way for constexpr and Clang
  2021-09-03 10:31 ` Paul Keir
@ 2021-10-11 14:33   ` Paul Keir
  0 siblings, 0 replies; 6+ messages in thread
From: Paul Keir @ 2021-10-11 14:33 UTC (permalink / raw)
  To: gcc-patches; +Cc: libstdc++

*ping*

________________________________________
From: Paul Keir <Paul.Keir@uws.ac.uk>
Sent: 03 September 2021 11:31
To: gcc-patches@gcc.gnu.org
Cc: libstdc++@gcc.gnu.org
Subject: Re: [PATCH] libstdc++: Fix compare_three_way for constexpr and Clang

*ping*

________________________________________
From: Paul Keir <Paul.Keir@uws.ac.uk>
Sent: 20 August 2021 21:17
To: gcc-patches@gcc.gnu.org
Cc: libstdc++@gcc.gnu.org
Subject: [PATCH] libstdc++: Fix compare_three_way for constexpr and Clang

Hi,

The current compare_three_way implementation makes provision for constant evaluation contexts (avoiding reinterpret_cast etc.), but the approach fails with Clang; when it compares two const volatile void pointers: "comparison between unequal pointers to void has unspecified result". I include a fix and test.

Could someone commit the attached patch for me?

Thanks,
Paul


Please consider the environment and think before you print.

The University of the West of Scotland is a registered Scottish charity. Charity number SC002520.

This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender.

Please note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of the University of the West of Scotland.

As a public body, the University of the West of Scotland may be required to make available emails as well as other written forms of information as a result of a request made under the Freedom of Information (Scotland) Act 2002.

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

* Re: [PATCH] libstdc++: Fix compare_three_way for constexpr and Clang
  2021-08-20 20:17 [PATCH] libstdc++: Fix compare_three_way for constexpr and Clang Paul Keir
  2021-09-03 10:31 ` Paul Keir
@ 2021-10-11 19:48 ` Jonathan Wakely
  2021-10-11 21:04   ` Jonathan Wakely
  1 sibling, 1 reply; 6+ messages in thread
From: Jonathan Wakely @ 2021-10-11 19:48 UTC (permalink / raw)
  To: Paul Keir; +Cc: gcc-patches, libstdc++

On Fri, 20 Aug 2021 at 21:19, Paul Keir wrote:
>
> Hi,
>
> The current compare_three_way implementation makes provision for constant evaluation contexts (avoiding reinterpret_cast etc.), but the approach fails with Clang; when it compares two const volatile void pointers: "comparison between unequal pointers to void has unspecified result". I include a fix and test.
>
> Could someone commit the attached patch for me?

Sorry for dropping the ball on this again. I've applied the patch
locally and I'm testing it now. Unless I'm mistaken, you do not have a
copyright assignment on file with the FSF, is that right? Are you able
to certify that you have the right to submit this to GCC, as described
at https://gcc.gnu.org/dco.html ?

Also, if GCC is failing to diagnose the invalid comparisons here then
that should be reported to bugzilla as a c++ "accepts-invalid" bug.


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

* Re: [PATCH] libstdc++: Fix compare_three_way for constexpr and Clang
  2021-10-11 19:48 ` Jonathan Wakely
@ 2021-10-11 21:04   ` Jonathan Wakely
  2021-10-13 21:37     ` Paul Keir
  0 siblings, 1 reply; 6+ messages in thread
From: Jonathan Wakely @ 2021-10-11 21:04 UTC (permalink / raw)
  To: Paul Keir; +Cc: gcc-patches, libstdc++

On Mon, 11 Oct 2021 at 20:48, Jonathan Wakely <jwakely@redhat.com> wrote:
>
> On Fri, 20 Aug 2021 at 21:19, Paul Keir wrote:
> >
> > Hi,
> >
> > The current compare_three_way implementation makes provision for constant evaluation contexts (avoiding reinterpret_cast etc.), but the approach fails with Clang; when it compares two const volatile void pointers: "comparison between unequal pointers to void has unspecified result". I include a fix and test.
> >
> > Could someone commit the attached patch for me?
>
> Sorry for dropping the ball on this again. I've applied the patch
> locally and I'm testing it now. Unless I'm mistaken, you do not have a
> copyright assignment on file with the FSF, is that right? Are you able
> to certify that you have the right to submit this to GCC, as described
> at https://gcc.gnu.org/dco.html ?

P.S. patches should not touch the ChangeLog file. It was always wrong,
because it usually makes the patch fail to apply. Since we moved to
Git the ChangeLog files are automatically generated from the Git
commits anyway, so are never touched as part of the commit. The
changelog entry is still needed, but should be in the Git commit
message not as a patch against the actual ChangeLog file.

>
> Also, if GCC is failing to diagnose the invalid comparisons here then
> that should be reported to bugzilla as a c++ "accepts-invalid" bug.


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

* Re: [PATCH] libstdc++: Fix compare_three_way for constexpr and Clang
  2021-10-11 21:04   ` Jonathan Wakely
@ 2021-10-13 21:37     ` Paul Keir
  0 siblings, 0 replies; 6+ messages in thread
From: Paul Keir @ 2021-10-13 21:37 UTC (permalink / raw)
  Cc: gcc-patches, libstdc++

I'd like to cancel the request to apply that patch.

At the time I had actually assumed that Clang was at fault, but your comment made me pause. I'll submit a bug report as you suggest. We can reconsider the patch in future once that bug is resolved.

________________________________________
From: Jonathan Wakely <jwakely@redhat.com>
Sent: 11 October 2021 22:04
To: Paul Keir
Cc: gcc-patches@gcc.gnu.org; libstdc++@gcc.gnu.org
Subject: Re: [PATCH] libstdc++: Fix compare_three_way for constexpr and Clang

The source of this email is EXTERNAL to UWS

On Mon, 11 Oct 2021 at 20:48, Jonathan Wakely <jwakely@redhat.com> wrote:
>
> On Fri, 20 Aug 2021 at 21:19, Paul Keir wrote:
> >
> > Hi,
> >
> > The current compare_three_way implementation makes provision for constant evaluation contexts (avoiding reinterpret_cast etc.), but the approach fails with Clang; when it compares two const volatile void pointers: "comparison between unequal pointers to void has unspecified result". I include a fix and test.
> >
> > Could someone commit the attached patch for me?
>
> Sorry for dropping the ball on this again. I've applied the patch
> locally and I'm testing it now. Unless I'm mistaken, you do not have a
> copyright assignment on file with the FSF, is that right? Are you able
> to certify that you have the right to submit this to GCC, as described
> at https://eu-west-1.protection.sophos.com?d=gnu.org&u=aHR0cHM6Ly9nY2MuZ251Lm9yZy9kY28uaHRtbA==&i=NWY2MGNhZjMzZTA5NzkwZGZlNmJhMzUy&t=SlR2SzN4czZueWZRZGdubVA0Z2M4M2FGbC9YLzIrWEVZaUpTMEhaZUJLND0=&h=929febb5b144486493bd3fc3c8522cfe ?

P.S. patches should not touch the ChangeLog file. It was always wrong,
because it usually makes the patch fail to apply. Since we moved to
Git the ChangeLog files are automatically generated from the Git
commits anyway, so are never touched as part of the commit. The
changelog entry is still needed, but should be in the Git commit
message not as a patch against the actual ChangeLog file.

>
> Also, if GCC is failing to diagnose the invalid comparisons here then
> that should be reported to bugzilla as a c++ "accepts-invalid" bug.



Please consider the environment and think before you print.

The University of the West of Scotland is a registered Scottish charity. Charity number SC002520.

This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender.

Please note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of the University of the West of Scotland.

As a public body, the University of the West of Scotland may be required to make available emails as well as other written forms of information as a result of a request made under the Freedom of Information (Scotland) Act 2002.

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

end of thread, other threads:[~2021-10-13 21:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-20 20:17 [PATCH] libstdc++: Fix compare_three_way for constexpr and Clang Paul Keir
2021-09-03 10:31 ` Paul Keir
2021-10-11 14:33   ` Paul Keir
2021-10-11 19:48 ` Jonathan Wakely
2021-10-11 21:04   ` Jonathan Wakely
2021-10-13 21:37     ` Paul Keir

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