public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/95242] New: [10 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a
@ 2020-05-20 18:24 gcc at mattwhitlock dot name
  2020-05-22  6:08 ` [Bug c++/95242] [10/11 " rguenth at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: gcc at mattwhitlock dot name @ 2020-05-20 18:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95242

            Bug ID: 95242
           Summary: [10 Regression] spurious "warning: zero as null
                    pointer constant [-Wzero-as-null-pointer-constant]" on
                    comparisons with -std=c++2a
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at mattwhitlock dot name
  Target Milestone: ---
              Host: x86_64-pc-linux-gnu
            Target: x86_64-pc-linux-gnu
             Build: x86_64-pc-linux-gnu

/* BEGIN bug.cpp */

#include <chrono>
#include <string>

bool bug(std::chrono::milliseconds lhs, std::chrono::milliseconds rhs) {
        return lhs < rhs; // spurious "warning: zero as null pointer constant"
}

bool bug(std::string::const_iterator lhs, std::string::const_iterator rhs) {
        return lhs < rhs; // spurious "warning: zero as null pointer constant"
}

/* END bug.cpp */


$ g++ -std=c++2a -Wzero-as-null-pointer-constant -c bug.cpp
bug.cpp: In function 'bool bug(std::chrono::milliseconds,
std::chrono::milliseconds)':
bug.cpp:5:15: warning: zero as null pointer constant
[-Wzero-as-null-pointer-constant]
    5 |  return lhs < rhs; // spurious "warning: zero as null pointer constant"
      |               ^~~
bug.cpp: In function 'bool
bug(std::__cxx11::basic_string<char>::const_iterator,
std::__cxx11::basic_string<char>::const_iterator)':
bug.cpp:9:15: warning: zero as null pointer constant
[-Wzero-as-null-pointer-constant]
    9 |  return lhs < rhs; // spurious "warning: zero as null pointer constant"
      |               ^~~


$ g++ --version | head -n1
g++ (Gentoo 10.1.0 p1) 10.1.0


The bug is not present in G++ 9.3, and it also is not present in G++ 10.1 in
-std=c++17 (or earlier) mode. This leads me to believe it may be related to the
new three-way comparison operator functionality.

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

* [Bug c++/95242] [10/11 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a
  2020-05-20 18:24 [Bug c++/95242] New: [10 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a gcc at mattwhitlock dot name
@ 2020-05-22  6:08 ` rguenth at gcc dot gnu.org
  2020-05-26 21:21 ` jason at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-05-22  6:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95242

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to work|                            |9.3.0
           Keywords|                            |diagnostic
            Summary|[10 Regression] spurious    |[10/11 Regression] spurious
                   |"warning: zero as null      |"warning: zero as null
                   |pointer constant            |pointer constant
                   |[-Wzero-as-null-pointer-con |[-Wzero-as-null-pointer-con
                   |stant]" on comparisons with |stant]" on comparisons with
                   |-std=c++2a                  |-std=c++2a
   Target Milestone|---                         |10.2

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

* [Bug c++/95242] [10/11 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a
  2020-05-20 18:24 [Bug c++/95242] New: [10 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a gcc at mattwhitlock dot name
  2020-05-22  6:08 ` [Bug c++/95242] [10/11 " rguenth at gcc dot gnu.org
@ 2020-05-26 21:21 ` jason at gcc dot gnu.org
  2020-05-26 23:13 ` redi at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jason at gcc dot gnu.org @ 2020-05-26 21:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95242

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
     Ever confirmed|0                           |1
           Assignee|unassigned at gcc dot gnu.org      |jason at gcc dot gnu.org
                 CC|                            |jason at gcc dot gnu.org,
                   |                            |jwakely.gcc at gmail dot com
   Last reconfirmed|                            |2020-05-26

--- Comment #1 from Jason Merrill <jason at gcc dot gnu.org> ---
Yes, in C++20 mode the < comparison uses operator<=> to produce a
std::strong_ordering, and then compares that to literal 0 to produce a boolean
value.  The operator< has an rhs type of 'std::__cmp_cat::__unspec', which has
a single constructor taking a pointer to itself, which is designed to only
accept null pointer constants, but this causes problems with this warning.

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

* [Bug c++/95242] [10/11 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a
  2020-05-20 18:24 [Bug c++/95242] New: [10 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a gcc at mattwhitlock dot name
  2020-05-22  6:08 ` [Bug c++/95242] [10/11 " rguenth at gcc dot gnu.org
  2020-05-26 21:21 ` jason at gcc dot gnu.org
@ 2020-05-26 23:13 ` redi at gcc dot gnu.org
  2020-05-27  5:21 ` daniel.kruegler at googlemail dot com
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2020-05-26 23:13 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95242

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Another way to implement the __unspec constructor would be:

  consteval __unspec(int __n) { if (__n != 0) throw __n; }

But I think I discussed this with Richard Smith in Prague and we realised there
was a problem with it, but I might be misremembering.

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

* [Bug c++/95242] [10/11 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a
  2020-05-20 18:24 [Bug c++/95242] New: [10 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a gcc at mattwhitlock dot name
                   ` (2 preceding siblings ...)
  2020-05-26 23:13 ` redi at gcc dot gnu.org
@ 2020-05-27  5:21 ` daniel.kruegler at googlemail dot com
  2020-05-27  6:59 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2020-05-27  5:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95242

--- Comment #3 from Daniel Krügler <daniel.kruegler at googlemail dot com> ---
(In reply to Jonathan Wakely from comment #2)
> Another way to implement the __unspec constructor would be:
> 
>   consteval __unspec(int __n) { if (__n != 0) throw __n; }
> 
> But I think I discussed this with Richard Smith in Prague and we realised
> there was a problem with it, but I might be misremembering.

Remember that we need to ensure that this __unspec constructor needs to be
noexcept (See bug 94565), so this function could only do a terminate-like
action.

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

* [Bug c++/95242] [10/11 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a
  2020-05-20 18:24 [Bug c++/95242] New: [10 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a gcc at mattwhitlock dot name
                   ` (3 preceding siblings ...)
  2020-05-27  5:21 ` daniel.kruegler at googlemail dot com
@ 2020-05-27  6:59 ` redi at gcc dot gnu.org
  2020-05-27  8:07 ` daniel.kruegler at googlemail dot com
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2020-05-27  6:59 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95242

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
It's consteval, the throw is there to make it not a constant expression and
give an error if anything except 0 is used. i.e. it can never throw, it either
compiles or it doesn't.

But I've remembered the problem with this technique, it allows non-literal
zeros:

constexpr int zero = 0;
auto c = (1 <=> 1) == zero;  // should be ill-formed.

This compiles OK, and isn't *obviously* wrong when reviewing the code.

The advantage of the current definition using a pointer is that nobody can
declare a constant of type __unspec without obviously using a reserved name and
venturing into undefined territory.

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

* [Bug c++/95242] [10/11 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a
  2020-05-20 18:24 [Bug c++/95242] New: [10 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a gcc at mattwhitlock dot name
                   ` (4 preceding siblings ...)
  2020-05-27  6:59 ` redi at gcc dot gnu.org
@ 2020-05-27  8:07 ` daniel.kruegler at googlemail dot com
  2020-05-27  8:20 ` redi at gcc dot gnu.org
  2020-05-27 14:15 ` jason at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2020-05-27  8:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95242

--- Comment #5 from Daniel Krügler <daniel.kruegler at googlemail dot com> ---
(In reply to Jonathan Wakely from comment #4)
> It's consteval, the throw is there to make it not a constant expression and
> give an error if anything except 0 is used. i.e. it can never throw, it
> either compiles or it doesn't.

Sure, I understand that, but that should still result in a failure of a static
assertion testing for noexcept, e.g. consider:

struct __unspec 
{
  consteval __unspec(int __n) { if (__n != 0) throw __n; }
};

static_assert(noexcept(__unspec(0)));

int main()
{
}

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

* [Bug c++/95242] [10/11 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a
  2020-05-20 18:24 [Bug c++/95242] New: [10 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a gcc at mattwhitlock dot name
                   ` (5 preceding siblings ...)
  2020-05-27  8:07 ` daniel.kruegler at googlemail dot com
@ 2020-05-27  8:20 ` redi at gcc dot gnu.org
  2020-05-27 14:15 ` jason at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2020-05-27  8:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95242

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
It was just a sketch to show the idea.

Obviously the real thing would need noexcept, but we have a regression test for
that. How to construct it is what's relevant here.

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

* [Bug c++/95242] [10/11 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a
  2020-05-20 18:24 [Bug c++/95242] New: [10 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a gcc at mattwhitlock dot name
                   ` (6 preceding siblings ...)
  2020-05-27  8:20 ` redi at gcc dot gnu.org
@ 2020-05-27 14:15 ` jason at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jason at gcc dot gnu.org @ 2020-05-27 14:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95242

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #7 from Jason Merrill <jason at gcc dot gnu.org> ---
Fixed for 10.2/11.

r11-663-g6c66c692043d680a5efcd9616b94f9629c417038
r10-8190-gfac77bd88dc450f6bcc8f5945186990c094b5cfc

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

end of thread, other threads:[~2020-05-27 14:15 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-20 18:24 [Bug c++/95242] New: [10 Regression] spurious "warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]" on comparisons with -std=c++2a gcc at mattwhitlock dot name
2020-05-22  6:08 ` [Bug c++/95242] [10/11 " rguenth at gcc dot gnu.org
2020-05-26 21:21 ` jason at gcc dot gnu.org
2020-05-26 23:13 ` redi at gcc dot gnu.org
2020-05-27  5:21 ` daniel.kruegler at googlemail dot com
2020-05-27  6:59 ` redi at gcc dot gnu.org
2020-05-27  8:07 ` daniel.kruegler at googlemail dot com
2020-05-27  8:20 ` redi at gcc dot gnu.org
2020-05-27 14:15 ` jason at gcc dot gnu.org

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