public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/115664] New: -Wnonnull-compare breaks templated methods
@ 2024-06-26 11:30 ossman at cendio dot se
  2024-06-26 14:11 ` [Bug c++/115664] " mpolacek at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: ossman at cendio dot se @ 2024-06-26 11:30 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115664
           Summary: -Wnonnull-compare breaks templated methods
           Product: gcc
           Version: 13.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ossman at cendio dot se
  Target Milestone: ---

A templated method has this protection in it to check that a given argument is
a subclass:

template<class T>
Object::method(void (*callback)(T*))
{
  if (dynamic_cast<T*>(this) == nullptr)
    throw Exception("Bad callback");
  registerCallback(callback);
}

This works fine as long as T is a subclass of Object. However, if T is Object,
then the gcc is clever enough to realise that dynamic_cast<>() does nothing and
the comparison becomes just "if (this == nullptr)", which triggers
-Wnonnull-compare.

The problem is that this is a templated method, so that if statement is
sometimes useful and cannot be removed.

Presently, we have to work around the issue by having a specialised version of
the affected methods, which adds unnecessary duplication.

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

* [Bug c++/115664] -Wnonnull-compare breaks templated methods
  2024-06-26 11:30 [Bug c++/115664] New: -Wnonnull-compare breaks templated methods ossman at cendio dot se
@ 2024-06-26 14:11 ` mpolacek at gcc dot gnu.org
  2024-06-26 16:01 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-06-26 14:11 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |WAITING
   Last reconfirmed|                            |2024-06-26
                 CC|                            |mpolacek at gcc dot gnu.org
     Ever confirmed|0                           |1

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Please include full preprocessed source.

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

* [Bug c++/115664] -Wnonnull-compare breaks templated methods
  2024-06-26 11:30 [Bug c++/115664] New: -Wnonnull-compare breaks templated methods ossman at cendio dot se
  2024-06-26 14:11 ` [Bug c++/115664] " mpolacek at gcc dot gnu.org
@ 2024-06-26 16:01 ` pinskia at gcc dot gnu.org
  2024-06-26 16:05 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-26 16:01 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|WAITING                     |NEW

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Testcase:
```

struct Object
{
template<class T>
void method(void (*callback)(T*));
virtual void g();
};

template<class T>
void Object::method(void (*callback)(T*))
{
  if (dynamic_cast<T*>(this) == nullptr)
    throw 1;
}

struct g{};

void c(g*);

void f(Object &a)
{
  a.method(c);
}
```

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

* [Bug c++/115664] -Wnonnull-compare breaks templated methods
  2024-06-26 11:30 [Bug c++/115664] New: -Wnonnull-compare breaks templated methods ossman at cendio dot se
  2024-06-26 14:11 ` [Bug c++/115664] " mpolacek at gcc dot gnu.org
  2024-06-26 16:01 ` pinskia at gcc dot gnu.org
@ 2024-06-26 16:05 ` pinskia at gcc dot gnu.org
  2024-06-26 16:33 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-26 16:05 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |WAITING

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
But I can't produce the warning ...

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

* [Bug c++/115664] -Wnonnull-compare breaks templated methods
  2024-06-26 11:30 [Bug c++/115664] New: -Wnonnull-compare breaks templated methods ossman at cendio dot se
                   ` (2 preceding siblings ...)
  2024-06-26 16:05 ` pinskia at gcc dot gnu.org
@ 2024-06-26 16:33 ` pinskia at gcc dot gnu.org
  2024-06-26 16:34 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-26 16:33 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|WAITING                     |NEW

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Oh I take that back here is the testcase which shows the warning:
```
struct Object
{
  template<class T>
    void method(void (*callback)(T*));
  virtual void g();
};

template<class T>
void Object::method(void (*)(T*))
{
  if (dynamic_cast<T*>(this) == nullptr)
    throw 1;
}

void c(Object*);

void f(Object *a)
{
  a->method(c);
}
```

I didn't read the original description correctly.

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

* [Bug c++/115664] -Wnonnull-compare breaks templated methods
  2024-06-26 11:30 [Bug c++/115664] New: -Wnonnull-compare breaks templated methods ossman at cendio dot se
                   ` (3 preceding siblings ...)
  2024-06-26 16:33 ` pinskia at gcc dot gnu.org
@ 2024-06-26 16:34 ` pinskia at gcc dot gnu.org
  2024-06-27 10:07 ` ossman at cendio dot se
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-26 16:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Basically `dynamic_cast<T*>(this)` becomes just this if `T == Object` here and
then we warn because this is compared with nullptr.

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

* [Bug c++/115664] -Wnonnull-compare breaks templated methods
  2024-06-26 11:30 [Bug c++/115664] New: -Wnonnull-compare breaks templated methods ossman at cendio dot se
                   ` (4 preceding siblings ...)
  2024-06-26 16:34 ` pinskia at gcc dot gnu.org
@ 2024-06-27 10:07 ` ossman at cendio dot se
  2024-06-27 10:55 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: ossman at cendio dot se @ 2024-06-27 10:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Pierre Ossman <ossman at cendio dot se> ---
Is there a cleaner way to can work around this than duplicating the affected
methods? I tried adding a #pragma, but that breaks older versions of gcc that
don't have -Wnonnull-compare.

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

* [Bug c++/115664] -Wnonnull-compare breaks templated methods
  2024-06-26 11:30 [Bug c++/115664] New: -Wnonnull-compare breaks templated methods ossman at cendio dot se
                   ` (5 preceding siblings ...)
  2024-06-27 10:07 ` ossman at cendio dot se
@ 2024-06-27 10:55 ` redi at gcc dot gnu.org
  2024-06-27 10:58 ` redi at gcc dot gnu.org
  2024-07-01  8:56 ` ossman at cendio dot se
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2024-06-27 10:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
if constexpr (not is_same_v<T, Object>)
  if (dynamic_cast<T*>(this) == nullptr)
    throw Exception("Bad callback");

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

* [Bug c++/115664] -Wnonnull-compare breaks templated methods
  2024-06-26 11:30 [Bug c++/115664] New: -Wnonnull-compare breaks templated methods ossman at cendio dot se
                   ` (6 preceding siblings ...)
  2024-06-27 10:55 ` redi at gcc dot gnu.org
@ 2024-06-27 10:58 ` redi at gcc dot gnu.org
  2024-07-01  8:56 ` ossman at cendio dot se
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2024-06-27 10:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Even if you can't use C++17, it still works like this:

  if (not std::is_same<T, Object>::value)
    if (dynamic_cast<T*>(this) == nullptr)

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

* [Bug c++/115664] -Wnonnull-compare breaks templated methods
  2024-06-26 11:30 [Bug c++/115664] New: -Wnonnull-compare breaks templated methods ossman at cendio dot se
                   ` (7 preceding siblings ...)
  2024-06-27 10:58 ` redi at gcc dot gnu.org
@ 2024-07-01  8:56 ` ossman at cendio dot se
  8 siblings, 0 replies; 10+ messages in thread
From: ossman at cendio dot se @ 2024-07-01  8:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Pierre Ossman <ossman at cendio dot se> ---
Thank you! That worked nicely!

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

end of thread, other threads:[~2024-07-01  8:56 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-26 11:30 [Bug c++/115664] New: -Wnonnull-compare breaks templated methods ossman at cendio dot se
2024-06-26 14:11 ` [Bug c++/115664] " mpolacek at gcc dot gnu.org
2024-06-26 16:01 ` pinskia at gcc dot gnu.org
2024-06-26 16:05 ` pinskia at gcc dot gnu.org
2024-06-26 16:33 ` pinskia at gcc dot gnu.org
2024-06-26 16:34 ` pinskia at gcc dot gnu.org
2024-06-27 10:07 ` ossman at cendio dot se
2024-06-27 10:55 ` redi at gcc dot gnu.org
2024-06-27 10:58 ` redi at gcc dot gnu.org
2024-07-01  8:56 ` ossman at cendio dot se

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