public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/111087] New: -Wnonnull issued for std::array of zero size when under C++20
@ 2023-08-21  6:29 adam.f.badura at gmail dot com
  2023-08-21  6:33 ` [Bug c++/111087] " pinskia at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: adam.f.badura at gmail dot com @ 2023-08-21  6:29 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111087
           Summary: -Wnonnull issued for std::array of zero size when
                    under C++20
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: adam.f.badura at gmail dot com
  Target Milestone: ---

At least for GCC 10.2 and 12.2, when using -std=c++20 I'm getting unexpected
-Wnonnull in the following (simplified) case:

  #include <algorithm>
  #include <array>
  #include <string_view>

  const std::array<std::string_view, 0> tags{};

  bool hasTag(const char* const tag)
  {
    const auto result = std::lower_bound(tags.begin(), tags. End(), tag);
    return result != tags.end() && result->compare(tag) == 0;
  }

See it on Compiler Explorer: https://godbolt.org/z/rGenGef7n.

It seems to be associated with the 0 size of the array since as soon as I
change it to 1 (or more) the warning goes away. Same if I drop the -std=c++20
(for 12.2 - with 10.2 it will not work due to <string_view>).

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

* [Bug c++/111087] -Wnonnull issued for std::array of zero size when under C++20
  2023-08-21  6:29 [Bug c++/111087] New: -Wnonnull issued for std::array of zero size when under C++20 adam.f.badura at gmail dot com
@ 2023-08-21  6:33 ` pinskia at gcc dot gnu.org
  2023-08-21  6:53 ` adam.f.badura at gmail dot com
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-21  6:33 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Zero sized arrays are a gcc extension.

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

* [Bug c++/111087] -Wnonnull issued for std::array of zero size when under C++20
  2023-08-21  6:29 [Bug c++/111087] New: -Wnonnull issued for std::array of zero size when under C++20 adam.f.badura at gmail dot com
  2023-08-21  6:33 ` [Bug c++/111087] " pinskia at gcc dot gnu.org
@ 2023-08-21  6:53 ` adam.f.badura at gmail dot com
  2023-08-21  7:26 ` pinskia at gcc dot gnu.org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: adam.f.badura at gmail dot com @ 2023-08-21  6:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Adam Badura <adam.f.badura at gmail dot com> ---
(In reply to Andrew Pinski from comment #1)
> Zero sized arrays are a gcc extension.

The problem is with `std::array` rather than C-array. It seems to me
`std::array` can have `0` size - it is not an extension. The sample builds with
Clang fine without warning...

Anyway, even if this would be GCC extension, it still shouldn't result in
false-positives, right? (Unless, of course, this is not a false-positive...)

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

* [Bug c++/111087] -Wnonnull issued for std::array of zero size when under C++20
  2023-08-21  6:29 [Bug c++/111087] New: -Wnonnull issued for std::array of zero size when under C++20 adam.f.badura at gmail dot com
  2023-08-21  6:33 ` [Bug c++/111087] " pinskia at gcc dot gnu.org
  2023-08-21  6:53 ` adam.f.badura at gmail dot com
@ 2023-08-21  7:26 ` pinskia at gcc dot gnu.org
  2023-08-21  7:27 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-21  7:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The warning is correct as tags.end() and tags.begin() will always be a nullptr
for std::array<T, 0>

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

* [Bug c++/111087] -Wnonnull issued for std::array of zero size when under C++20
  2023-08-21  6:29 [Bug c++/111087] New: -Wnonnull issued for std::array of zero size when under C++20 adam.f.badura at gmail dot com
                   ` (2 preceding siblings ...)
  2023-08-21  7:26 ` pinskia at gcc dot gnu.org
@ 2023-08-21  7:27 ` pinskia at gcc dot gnu.org
  2023-08-21  7:31 ` adam.f.badura at gmail dot com
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-21  7:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
just since GCC decides to act const in some cases like constexpr especially
with -std=c++20, you get the warning.

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

* [Bug c++/111087] -Wnonnull issued for std::array of zero size when under C++20
  2023-08-21  6:29 [Bug c++/111087] New: -Wnonnull issued for std::array of zero size when under C++20 adam.f.badura at gmail dot com
                   ` (3 preceding siblings ...)
  2023-08-21  7:27 ` pinskia at gcc dot gnu.org
@ 2023-08-21  7:31 ` adam.f.badura at gmail dot com
  2023-08-21  7:39 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: adam.f.badura at gmail dot com @ 2023-08-21  7:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Adam Badura <adam.f.badura at gmail dot com> ---
(In reply to Andrew Pinski from comment #3)
> The warning is correct as tags.end() and tags.begin() will always be a
> nullptr for std::array<T, 0>

Yes. But what does it change?

`std::lower_bound` gets an empty range (begin == end) and hence will return the
`end` value. Then we check `result != tags.end()` which should cut off since it
will be false and hence we will never reach the place where we use
`result->`...

Let me add, that the issue is not about short-circuiting since it also shows in
the elaborate form:

  if (result != tags.end())
  {
    return result->compare(tag) == 0;
  }
  else
  {
    return false;
  }

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

* [Bug c++/111087] -Wnonnull issued for std::array of zero size when under C++20
  2023-08-21  6:29 [Bug c++/111087] New: -Wnonnull issued for std::array of zero size when under C++20 adam.f.badura at gmail dot com
                   ` (4 preceding siblings ...)
  2023-08-21  7:31 ` adam.f.badura at gmail dot com
@ 2023-08-21  7:39 ` pinskia at gcc dot gnu.org
  2023-08-21  7:47 ` adam.f.badura at gmail dot com
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-21  7:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Because the warning is not flow dependent.
So even things like:
const class1 *a = nullptr;
a != nullptr && a->method1();

Will also warn.

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

* [Bug c++/111087] -Wnonnull issued for std::array of zero size when under C++20
  2023-08-21  6:29 [Bug c++/111087] New: -Wnonnull issued for std::array of zero size when under C++20 adam.f.badura at gmail dot com
                   ` (5 preceding siblings ...)
  2023-08-21  7:39 ` pinskia at gcc dot gnu.org
@ 2023-08-21  7:47 ` adam.f.badura at gmail dot com
  2023-08-21  7:54 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: adam.f.badura at gmail dot com @ 2023-08-21  7:47 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Adam Badura <adam.f.badura at gmail dot com> ---
So, the cases I described here _is_ a false-positive, however, an expected one.
Do I understand this correctly?

However, consider the code:

  class class1
  {
  public:
    bool method1() const;
  };

  bool foo()
  {
    const class1* a = nullptr;
    return a != nullptr && a->method1();
  }

See it also on Compiler Explorer: https://godbolt.org/z/7GKT98Y5b.

It _does not_ generate the warning. So, what prevents generating the warning
here? How do I formulate the code to avoid the warning?

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

* [Bug c++/111087] -Wnonnull issued for std::array of zero size when under C++20
  2023-08-21  6:29 [Bug c++/111087] New: -Wnonnull issued for std::array of zero size when under C++20 adam.f.badura at gmail dot com
                   ` (6 preceding siblings ...)
  2023-08-21  7:47 ` adam.f.badura at gmail dot com
@ 2023-08-21  7:54 ` pinskia at gcc dot gnu.org
  2023-08-21  8:03 ` adam.f.badura at gmail dot com
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-21  7:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Adam Badura from comment #7)
> So, the cases I described here _is_ a false-positive, however, an expected
> one. Do I understand this correctly?
> It _does not_ generate the warning. So, what prevents generating the warning
> here? How do I formulate the code to avoid the warning?

This does though:
  bool foo()
  {
    const auto a = (class1*)nullptr;
    return a != nullptr && a->method1();
  }

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

* [Bug c++/111087] -Wnonnull issued for std::array of zero size when under C++20
  2023-08-21  6:29 [Bug c++/111087] New: -Wnonnull issued for std::array of zero size when under C++20 adam.f.badura at gmail dot com
                   ` (7 preceding siblings ...)
  2023-08-21  7:54 ` pinskia at gcc dot gnu.org
@ 2023-08-21  8:03 ` adam.f.badura at gmail dot com
  2023-08-21 10:31 ` redi at gcc dot gnu.org
  2023-08-21 10:36 ` redi at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: adam.f.badura at gmail dot com @ 2023-08-21  8:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Adam Badura <adam.f.badura at gmail dot com> ---
Is there any option to improve the warning so that it is not generated in cases
that are valid. I mean, to make it flow-dependent?

How it works now? What it takes into account and how to phrase the code to
avoid the false-positive?

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

* [Bug c++/111087] -Wnonnull issued for std::array of zero size when under C++20
  2023-08-21  6:29 [Bug c++/111087] New: -Wnonnull issued for std::array of zero size when under C++20 adam.f.badura at gmail dot com
                   ` (8 preceding siblings ...)
  2023-08-21  8:03 ` adam.f.badura at gmail dot com
@ 2023-08-21 10:31 ` redi at gcc dot gnu.org
  2023-08-21 10:36 ` redi at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: redi at gcc dot gnu.org @ 2023-08-21 10:31 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2023-08-21
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

--- Comment #10 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The warning in this case is idiotic:

#include <algorithm>
#include <array>
#include <string_view>

const std::array<std::string_view, 0> tags{};

bool hasTag(const char* const tag) {
  const auto result = std::lower_bound(tags.begin(), tags.end(), tag);
  if (result == tags.end())
    return false;
  if (!result)
    return false;
  if (result)
    return result->compare(tag) == 0;
}

null.cc: In function 'bool hasTag(const char*)':
null.cc:14:12: warning: 'this' pointer is null [-Wnonnull]
   14 |     return result->compare(tag) == 0;
      |            ^~~~~~
In file included from null.cc:3:
/home/jwakely/gcc/14/include/c++/14.0.0/string_view:364:7: note: in a call to
non-static member function 'constexpr int std::basic_string_view<_CharT,
_Traits>::compare(const _CharT*) const [with _CharT = char; _Traits =
std::char_traits<char>]'
  364 |       compare(const _CharT* __str) const noexcept
      |       ^~~~~~~


Confirmed as a bug.

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

* [Bug c++/111087] -Wnonnull issued for std::array of zero size when under C++20
  2023-08-21  6:29 [Bug c++/111087] New: -Wnonnull issued for std::array of zero size when under C++20 adam.f.badura at gmail dot com
                   ` (9 preceding siblings ...)
  2023-08-21 10:31 ` redi at gcc dot gnu.org
@ 2023-08-21 10:36 ` redi at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: redi at gcc dot gnu.org @ 2023-08-21 10:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #11 from Jonathan Wakely <redi at gcc dot gnu.org> ---
For GCC 10 it was arguably even worse:

null.cc: In function 'bool hasTag(const char*)':
null.cc:14:31: warning: null argument where non-null required (argument 1)
[-Wnonnull]
   14 |     return result->compare(tag) == 0;
      |                               ^

This makes it look like GCC is complaining about the 'tag' argument.

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

end of thread, other threads:[~2023-08-21 10:36 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-21  6:29 [Bug c++/111087] New: -Wnonnull issued for std::array of zero size when under C++20 adam.f.badura at gmail dot com
2023-08-21  6:33 ` [Bug c++/111087] " pinskia at gcc dot gnu.org
2023-08-21  6:53 ` adam.f.badura at gmail dot com
2023-08-21  7:26 ` pinskia at gcc dot gnu.org
2023-08-21  7:27 ` pinskia at gcc dot gnu.org
2023-08-21  7:31 ` adam.f.badura at gmail dot com
2023-08-21  7:39 ` pinskia at gcc dot gnu.org
2023-08-21  7:47 ` adam.f.badura at gmail dot com
2023-08-21  7:54 ` pinskia at gcc dot gnu.org
2023-08-21  8:03 ` adam.f.badura at gmail dot com
2023-08-21 10:31 ` redi at gcc dot gnu.org
2023-08-21 10:36 ` redi 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).