public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/99251] New: Strange -Wnonnull warning behaviour with dynamic_cast
@ 2021-02-24 16:21 sirl at gcc dot gnu.org
  2021-02-24 17:12 ` [Bug c++/99251] [11 Regression] inconsistent " msebor at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: sirl at gcc dot gnu.org @ 2021-02-24 16:21 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 99251
           Summary: Strange -Wnonnull warning behaviour with dynamic_cast
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sirl at gcc dot gnu.org
  Target Milestone: ---

With this testcase:

class cl1 {
  virtual void m();
};
class cl2 : public cl1 {
public:
  int g();
  int h();
  int i();
};
class cl3 {
  cl1 *p;
  int g();
  int h();
  int i();
};
int cl3::g() {
  if (!p)
    return 0;
  cl2 *x = dynamic_cast<cl2 *>(p);
  return x->g();
}
int cl3::h() {
  if (!p)
    return 0;
  return (dynamic_cast<cl2 *>(p))->h();
}
int cl3::i() {
  if (!p)
    return 0;
  return dynamic_cast<cl2 *>(p)->i();
}

compiled with g++-trunk@r11-7356 -Wnonnull this warning is issued:

testcase.cpp: In member function 'int cl3::i()':
testcase.cpp:30:36: warning: 'this' pointer is null [-Wnonnull]
   30 |   return dynamic_cast<cl2 *>(p)->i();
      |                                    ^
testcase.cpp:8:7: note: in a call to non-static member function 'int cl2::i()'
    8 |   int i();
      |       ^

I believe there should be no warning for cl3::i() like with g++-10.2.

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

* [Bug c++/99251] [11 Regression] inconsistent -Wnonnull warning behaviour with dynamic_cast
  2021-02-24 16:21 [Bug c++/99251] New: Strange -Wnonnull warning behaviour with dynamic_cast sirl at gcc dot gnu.org
@ 2021-02-24 17:12 ` msebor at gcc dot gnu.org
  2021-02-24 17:13 ` msebor at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-02-24 17:12 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |95507
   Last reconfirmed|                            |2021-02-24
           Keywords|                            |diagnostic
            Summary|Strange -Wnonnull warning   |[11 Regression]
                   |behaviour with dynamic_cast |inconsistent -Wnonnull
                   |                            |warning behaviour with
                   |                            |dynamic_cast
     Ever confirmed|0                           |1
                 CC|                            |msebor at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
We discussed this instance of the warning in pr98646 and decided that even
though issuing it for an access to the result of dynamic_cast was strictly a
false positive when the operand was guaranteed to be nonnull by a prior test,
the workaround to cast to a reference rather than a pointer was simple enough
and made the intent clearer:

  return dynamic_cast<cl2 &>(*p).i();

But the inconsistency exhibited in this test case is not a good thing
(enclosing the cast in parentheses certainly shouldn't make a difference) and
suggests the decision should be revisited.  The warning for the dynamic_cast
should either be issued consistently or not at all.  Let me look into it.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95507
[Bug 95507] [meta-bug] bogus/missing -Wnonnull

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

* [Bug c++/99251] [11 Regression] inconsistent -Wnonnull warning behaviour with dynamic_cast
  2021-02-24 16:21 [Bug c++/99251] New: Strange -Wnonnull warning behaviour with dynamic_cast sirl at gcc dot gnu.org
  2021-02-24 17:12 ` [Bug c++/99251] [11 Regression] inconsistent " msebor at gcc dot gnu.org
@ 2021-02-24 17:13 ` msebor at gcc dot gnu.org
  2021-02-24 18:14 ` msebor at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-02-24 17:13 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |msebor at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

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

* [Bug c++/99251] [11 Regression] inconsistent -Wnonnull warning behaviour with dynamic_cast
  2021-02-24 16:21 [Bug c++/99251] New: Strange -Wnonnull warning behaviour with dynamic_cast sirl at gcc dot gnu.org
  2021-02-24 17:12 ` [Bug c++/99251] [11 Regression] inconsistent " msebor at gcc dot gnu.org
  2021-02-24 17:13 ` msebor at gcc dot gnu.org
@ 2021-02-24 18:14 ` msebor at gcc dot gnu.org
  2021-02-24 22:29 ` msebor at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-02-24 18:14 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Depends on|                            |74762

--- Comment #2 from Martin Sebor <msebor at gcc dot gnu.org> ---
The reason why there's no warning for cl3::g() is because the result of the
cast is not dereferenced in the same expression (the -Wnonnull warning is
issued for the call, and the call is in the next statement).

The reason why there's no warning for the parenthesized cast in cl3::h() is due
to pr74762: the C++ front end sets the no-warning bit on the parenthesized
expression.  The warning sees this IL:

  cl2::h (((struct cl3 *) this)->p != 0B ? (struct cl2 *) __dynamic_cast
(this->p, &_ZTI3cl1, &_ZTI3cl2, 0) : 0B)

where both the COND_EXPR (?:) and the NE_EXPR (!=) have the no-warning bit set
and the warning code uses the first bit to suppress it.

The reason why there is a warning for cl3::i() is because the no-warning bit is
set only on the NE_EXPR and not on the COND_EXPR as above, and the warning code
only tests the latter.

Finally, the reason why the warning is not issued for a similar static_cast
(where the argument has to be checked for equality to null in order for the
result to stay null) is because of the fix for pr96003 that set the no-warning
bit even on the COND_EXPR but didn't make the corresponding change in
ifnonnull() in cp/rtti.c.  What a mess.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=74762
[Bug 74762] [8/9/10/11 Regression] missing uninitialized warning (C++,
parenthesized expr, TREE_NO_WARNING)

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

* [Bug c++/99251] [11 Regression] inconsistent -Wnonnull warning behaviour with dynamic_cast
  2021-02-24 16:21 [Bug c++/99251] New: Strange -Wnonnull warning behaviour with dynamic_cast sirl at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2021-02-24 18:14 ` msebor at gcc dot gnu.org
@ 2021-02-24 22:29 ` msebor at gcc dot gnu.org
  2021-02-25  7:59 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-02-24 22:29 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
Patch: https://gcc.gnu.org/pipermail/gcc-patches/2021-February/565824.html

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

* [Bug c++/99251] [11 Regression] inconsistent -Wnonnull warning behaviour with dynamic_cast
  2021-02-24 16:21 [Bug c++/99251] New: Strange -Wnonnull warning behaviour with dynamic_cast sirl at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2021-02-24 22:29 ` msebor at gcc dot gnu.org
@ 2021-02-25  7:59 ` rguenth at gcc dot gnu.org
  2021-03-02 18:17 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-02-25  7:59 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |11.0

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

* [Bug c++/99251] [11 Regression] inconsistent -Wnonnull warning behaviour with dynamic_cast
  2021-02-24 16:21 [Bug c++/99251] New: Strange -Wnonnull warning behaviour with dynamic_cast sirl at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2021-02-25  7:59 ` rguenth at gcc dot gnu.org
@ 2021-03-02 18:17 ` cvs-commit at gcc dot gnu.org
  2021-03-02 18:18 ` msebor at gcc dot gnu.org
  2021-06-28 20:13 ` msebor at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-03-02 18:17 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Martin Sebor <msebor@gcc.gnu.org>:

https://gcc.gnu.org/g:66ecb059c9d77cfcfb06cbdc3cac6a63b9e67f3d

commit r11-7458-g66ecb059c9d77cfcfb06cbdc3cac6a63b9e67f3d
Author: Martin Sebor <msebor@redhat.com>
Date:   Tue Mar 2 11:12:50 2021 -0700

    PR c++/99251 - inconsistent -Wnonnull warning behaviour with dynamic_cast

    gcc/cp/ChangeLog:

            PR c++/99251
            * class.c (build_base_path): Call build_if_nonnull.
            * cp-tree.h (build_if_nonnull): Declare.
            * rtti.c (ifnonnull): Rename...
            (build_if_nonnull): ...to this.  Set no-warning bit on COND_EXPR.
            (build_dynamic_cast_1): Adjust to name change.

    gcc/testsuite/ChangeLog:

            PR c++/99251
            * g++.dg/warn/Wnonnull9.C: Expect no warnings.
            * g++.dg/warn/Wnonnull12.C: New test.

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

* [Bug c++/99251] [11 Regression] inconsistent -Wnonnull warning behaviour with dynamic_cast
  2021-02-24 16:21 [Bug c++/99251] New: Strange -Wnonnull warning behaviour with dynamic_cast sirl at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2021-03-02 18:17 ` cvs-commit at gcc dot gnu.org
@ 2021-03-02 18:18 ` msebor at gcc dot gnu.org
  2021-06-28 20:13 ` msebor at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-03-02 18:18 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

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

--- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> ---
Fixed in r11-7458.

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

* [Bug c++/99251] [11 Regression] inconsistent -Wnonnull warning behaviour with dynamic_cast
  2021-02-24 16:21 [Bug c++/99251] New: Strange -Wnonnull warning behaviour with dynamic_cast sirl at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2021-03-02 18:18 ` msebor at gcc dot gnu.org
@ 2021-06-28 20:13 ` msebor at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-06-28 20:13 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99251
Bug 99251 depends on bug 74762, which changed state.

Bug 74762 Summary: [9/10/11 Regression] missing uninitialized warning (C++, parenthesized expr, TREE_NO_WARNING)
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=74762

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

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

end of thread, other threads:[~2021-06-28 20:13 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-24 16:21 [Bug c++/99251] New: Strange -Wnonnull warning behaviour with dynamic_cast sirl at gcc dot gnu.org
2021-02-24 17:12 ` [Bug c++/99251] [11 Regression] inconsistent " msebor at gcc dot gnu.org
2021-02-24 17:13 ` msebor at gcc dot gnu.org
2021-02-24 18:14 ` msebor at gcc dot gnu.org
2021-02-24 22:29 ` msebor at gcc dot gnu.org
2021-02-25  7:59 ` rguenth at gcc dot gnu.org
2021-03-02 18:17 ` cvs-commit at gcc dot gnu.org
2021-03-02 18:18 ` msebor at gcc dot gnu.org
2021-06-28 20:13 ` msebor 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).