public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/103480] New: [10,11,12]Werror=useless-cast with constexpr destructor
@ 2021-11-29 22:51 peeceseven at gmail dot com
  2021-11-29 23:06 ` [Bug c++/103480] -Werror=useless-cast " mpolacek at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: peeceseven at gmail dot com @ 2021-11-29 22:51 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103480
           Summary: [10,11,12]Werror=useless-cast with constexpr
                    destructor
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: peeceseven at gmail dot com
  Target Milestone: ---

Created attachment 51901
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51901&action=edit
sample that causes the bug.

https://godbolt.org/z/vsdd6M6cc

Issue: useless cast error triggers somehow when we have a constexpr destructor.

GCC 9 doesn't support c++20's constexpr destructor. But this problem exists in
10,11,12, and trunk.

Code compiles on clang and msvc.



//error output
<source>: In instantiation of 'static constexpr auto scope_guard::array(void
(*)()) [with long unsigned int count = 2]':
<source>:82:45:   required from here
<source>:69:12:   in 'constexpr' expansion of 'std::array<scope_guard, 2>(r)'
<source>:69:12: error: useless cast to type 'struct scope_guard&&'
[-Werror=useless-cast]
   69 |     return r;
      |            ^
<source>: In function 'int main()':
<source>:82:51:   in 'constexpr' expansion of 'scope_guard::array(void (*)())
[with long unsigned int count = 2]()'
<source>:69:12:   in 'constexpr' expansion of 'std::array<scope_guard, 2>(r)'
<source>:82:51: error: useless cast to type 'struct scope_guard&&'
[-Werror=useless-cast]
   82 |     constexpr auto t = scope_guard::array<2>(&test);
      |                                                   ^
<source>:82:51:   in 'constexpr' expansion of 'scope_guard::array(void (*)())
[with long unsigned int count = 2]()'
<source>:69:12:   in 'constexpr' expansion of 'std::array<scope_guard, 2>(r)'
<source>:82:51: error: useless cast to type 'struct scope_guard&&'
[-Werror=useless-cast]
cc1plus: some warnings being treated as errors
Compiler returned: 1

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

* [Bug c++/103480] -Werror=useless-cast with constexpr destructor
  2021-11-29 22:51 [Bug c++/103480] New: [10,11,12]Werror=useless-cast with constexpr destructor peeceseven at gmail dot com
@ 2021-11-29 23:06 ` mpolacek at gcc dot gnu.org
  2021-11-30  0:11 ` mpolacek at gcc dot gnu.org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-11-29 23:06 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-11-29
                 CC|                            |mpolacek at gcc dot gnu.org
           Keywords|                            |diagnostic
             Status|UNCONFIRMED                 |NEW
            Summary|[10,11,12]Werror=useless-ca |-Werror=useless-cast with
                   |st with constexpr           |constexpr destructor
                   |destructor                  |

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

* [Bug c++/103480] -Werror=useless-cast with constexpr destructor
  2021-11-29 22:51 [Bug c++/103480] New: [10,11,12]Werror=useless-cast with constexpr destructor peeceseven at gmail dot com
  2021-11-29 23:06 ` [Bug c++/103480] -Werror=useless-cast " mpolacek at gcc dot gnu.org
@ 2021-11-30  0:11 ` mpolacek at gcc dot gnu.org
  2021-11-30  0:46 ` [Bug c++/103480] [10/11/12 Regression] " mpolacek at gcc dot gnu.org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-11-30  0:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
namespace std {
template <typename _Tp, int _Nm> struct __array_traits {
  typedef _Tp _Type[_Nm];
};
template <typename _Tp, int _Nm> struct array {
  __array_traits<_Tp, _Nm>::_Type _M_elems;
};
} // namespace std
struct scope_guard {
  constexpr scope_guard(scope_guard &&) {}
  template <int count> static auto array(void()) {
    std::array<scope_guard, count> r;
    return r;
  }
  scope_guard() = default;
};
void test() { scope_guard::array<2>(test); }

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

* [Bug c++/103480] [10/11/12 Regression] -Werror=useless-cast with constexpr destructor
  2021-11-29 22:51 [Bug c++/103480] New: [10,11,12]Werror=useless-cast with constexpr destructor peeceseven at gmail dot com
  2021-11-29 23:06 ` [Bug c++/103480] -Werror=useless-cast " mpolacek at gcc dot gnu.org
  2021-11-30  0:11 ` mpolacek at gcc dot gnu.org
@ 2021-11-30  0:46 ` mpolacek at gcc dot gnu.org
  2021-11-30  0:52 ` mpolacek at gcc dot gnu.org
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-11-30  0:46 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|-Werror=useless-cast with   |[10/11/12 Regression]
                   |constexpr destructor        |-Werror=useless-cast with
                   |                            |constexpr destructor
   Target Milestone|---                         |10.4
           Priority|P3                          |P2

--- Comment #2 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Started with r10-6527:

commit aaa26bf496a646778ac861aed124d960b5bf549f
Author: Jason Merrill <jason@redhat.com>
Date:   Sun Jan 26 22:58:32 2020 -0500

    c++: Use constexpr to avoid wrong -Wsign-compare (PR90691).

    We would like to do constexpr evaluation to avoid false positives on
    warnings, but constexpr evaluation can involve function body copying that
    changes DECL_UID, which breaks -fcompare-debug.  So let's remember
    that we need to avoid that.

            PR c++/90691
            * expr.c (fold_for_warn): Call maybe_constant_value.
            * constexpr.c (struct constexpr_ctx): Add uid_sensitive field.
            (maybe_constant_value): Add uid_sensitive parm.
            (get_fundef_copy): Don't copy if it's true.
            (cxx_eval_call_expression): Don't instantiate if it's true.
            (cxx_eval_outermost_constant_expr): Likewise.

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

* [Bug c++/103480] [10/11/12 Regression] -Werror=useless-cast with constexpr destructor
  2021-11-29 22:51 [Bug c++/103480] New: [10,11,12]Werror=useless-cast with constexpr destructor peeceseven at gmail dot com
                   ` (2 preceding siblings ...)
  2021-11-30  0:46 ` [Bug c++/103480] [10/11/12 Regression] " mpolacek at gcc dot gnu.org
@ 2021-11-30  0:52 ` mpolacek at gcc dot gnu.org
  2021-12-30 17:49 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-11-30  0:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Add typename to make it C++14:

namespace std {
template <typename _Tp, int _Nm> struct __array_traits {
  typedef _Tp _Type[_Nm];
};
template <typename _Tp, int _Nm> struct array {
  typename __array_traits<_Tp, _Nm>::_Type _M_elems;
};
} // namespace std
struct scope_guard {
  constexpr scope_guard(scope_guard &&) {}
  template <int count> static auto array(void()) {
    std::array<scope_guard, count> r;
    return r;
  }
  scope_guard() = default;
};
void test() { scope_guard::array<2>(test); }

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

* [Bug c++/103480] [10/11/12 Regression] -Werror=useless-cast with constexpr destructor
  2021-11-29 22:51 [Bug c++/103480] New: [10,11,12]Werror=useless-cast with constexpr destructor peeceseven at gmail dot com
                   ` (3 preceding siblings ...)
  2021-11-30  0:52 ` mpolacek at gcc dot gnu.org
@ 2021-12-30 17:49 ` jakub at gcc dot gnu.org
  2022-01-12  8:49 ` cvs-commit at gcc dot gnu.org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-12-30 17:49 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

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

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 52095
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52095&action=edit
gcc12-pr103480.patch

Untested patch to fix that.

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

* [Bug c++/103480] [10/11/12 Regression] -Werror=useless-cast with constexpr destructor
  2021-11-29 22:51 [Bug c++/103480] New: [10,11,12]Werror=useless-cast with constexpr destructor peeceseven at gmail dot com
                   ` (4 preceding siblings ...)
  2021-12-30 17:49 ` jakub at gcc dot gnu.org
@ 2022-01-12  8:49 ` cvs-commit at gcc dot gnu.org
  2022-01-12  8:53 ` [Bug c++/103480] [10/11 " jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-01-12  8:49 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:6bba184ccbf47368eaea27ee2c1e7b850526640b

commit r12-6516-g6bba184ccbf47368eaea27ee2c1e7b850526640b
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Jan 12 09:47:46 2022 +0100

    c++: Silence -Wuseless-cast warnings during move [PR103480]

    This is maybe just a shot in the dark, but IMHO we shouldn't be diagnosing
    -Wuseless-cast on casts the compiler adds on its own when calling its move
    function.  We don't seem to warn when user calls std::move either.
    We call move on elinit (*NON_LVALUE_EXPR <(struct C[2] &&) &D.2497->b>)[0]
    so it is already an xvalue_p and try to static_cast it to struct C &&.
    But we don't warn e.g. on std::move (std::move (whatever)).

    Fixed by not doing the static cast and just returning expr from move
    if expr is already an xvalue.

    2022-01-11  Jakub Jelinek  <jakub@redhat.com>
                Jason Merrill  <jason@redhat.com>

            PR c++/103480
            * tree.c (move): If expr is xvalue_p, just return expr without
            build_static_cast.

            * g++.dg/warn/Wuseless-cast2.C: New test.

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

* [Bug c++/103480] [10/11 Regression] -Werror=useless-cast with constexpr destructor
  2021-11-29 22:51 [Bug c++/103480] New: [10,11,12]Werror=useless-cast with constexpr destructor peeceseven at gmail dot com
                   ` (5 preceding siblings ...)
  2022-01-12  8:49 ` cvs-commit at gcc dot gnu.org
@ 2022-01-12  8:53 ` jakub at gcc dot gnu.org
  2022-01-24  9:21 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-01-12  8:53 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[10/11/12 Regression]       |[10/11 Regression]
                   |-Werror=useless-cast with   |-Werror=useless-cast with
                   |constexpr destructor        |constexpr destructor

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed on the trunk so far.

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

* [Bug c++/103480] [10/11 Regression] -Werror=useless-cast with constexpr destructor
  2021-11-29 22:51 [Bug c++/103480] New: [10,11,12]Werror=useless-cast with constexpr destructor peeceseven at gmail dot com
                   ` (6 preceding siblings ...)
  2022-01-12  8:53 ` [Bug c++/103480] [10/11 " jakub at gcc dot gnu.org
@ 2022-01-24  9:21 ` cvs-commit at gcc dot gnu.org
  2022-01-24  9:28 ` [Bug c++/103480] [10 " jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-01-24  9:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:cb4998fcdf2ad92fc6323fc4d9bc03299ca8a541

commit r11-9501-gcb4998fcdf2ad92fc6323fc4d9bc03299ca8a541
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Jan 12 09:47:46 2022 +0100

    c++: Silence -Wuseless-cast warnings during move [PR103480]

    This is maybe just a shot in the dark, but IMHO we shouldn't be diagnosing
    -Wuseless-cast on casts the compiler adds on its own when calling its move
    function.  We don't seem to warn when user calls std::move either.
    We call move on elinit (*NON_LVALUE_EXPR <(struct C[2] &&) &D.2497->b>)[0]
    so it is already an xvalue_p and try to static_cast it to struct C &&.
    But we don't warn e.g. on std::move (std::move (whatever)).

    Fixed by not doing the static cast and just returning expr from move
    if expr is already an xvalue.

    2022-01-11  Jakub Jelinek  <jakub@redhat.com>
                Jason Merrill  <jason@redhat.com>

            PR c++/103480
            * tree.c (move): If expr is xvalue_p, just return expr without
            build_static_cast.

            * g++.dg/warn/Wuseless-cast2.C: New test.

    (cherry picked from commit 6bba184ccbf47368eaea27ee2c1e7b850526640b)

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

* [Bug c++/103480] [10 Regression] -Werror=useless-cast with constexpr destructor
  2021-11-29 22:51 [Bug c++/103480] New: [10,11,12]Werror=useless-cast with constexpr destructor peeceseven at gmail dot com
                   ` (7 preceding siblings ...)
  2022-01-24  9:21 ` cvs-commit at gcc dot gnu.org
@ 2022-01-24  9:28 ` jakub at gcc dot gnu.org
  2022-05-10  8:22 ` cvs-commit at gcc dot gnu.org
  2022-05-10 10:14 ` jakub at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-01-24  9:28 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[10/11 Regression]          |[10 Regression]
                   |-Werror=useless-cast with   |-Werror=useless-cast with
                   |constexpr destructor        |constexpr destructor

--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed for 11.3 too.

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

* [Bug c++/103480] [10 Regression] -Werror=useless-cast with constexpr destructor
  2021-11-29 22:51 [Bug c++/103480] New: [10,11,12]Werror=useless-cast with constexpr destructor peeceseven at gmail dot com
                   ` (8 preceding siblings ...)
  2022-01-24  9:28 ` [Bug c++/103480] [10 " jakub at gcc dot gnu.org
@ 2022-05-10  8:22 ` cvs-commit at gcc dot gnu.org
  2022-05-10 10:14 ` jakub at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-05-10  8:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:ac26cfeca99c488213eaf76d767ec4470ba3cee9

commit r10-10667-gac26cfeca99c488213eaf76d767ec4470ba3cee9
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Jan 12 09:47:46 2022 +0100

    c++: Silence -Wuseless-cast warnings during move [PR103480]

    This is maybe just a shot in the dark, but IMHO we shouldn't be diagnosing
    -Wuseless-cast on casts the compiler adds on its own when calling its move
    function.  We don't seem to warn when user calls std::move either.
    We call move on elinit (*NON_LVALUE_EXPR <(struct C[2] &&) &D.2497->b>)[0]
    so it is already an xvalue_p and try to static_cast it to struct C &&.
    But we don't warn e.g. on std::move (std::move (whatever)).

    Fixed by not doing the static cast and just returning expr from move
    if expr is already an xvalue.

    2022-01-11  Jakub Jelinek  <jakub@redhat.com>
                Jason Merrill  <jason@redhat.com>

            PR c++/103480
            * tree.c (move): If expr is xvalue_p, just return expr without
            build_static_cast.

            * g++.dg/warn/Wuseless-cast2.C: New test.

    (cherry picked from commit 6bba184ccbf47368eaea27ee2c1e7b850526640b)

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

* [Bug c++/103480] [10 Regression] -Werror=useless-cast with constexpr destructor
  2021-11-29 22:51 [Bug c++/103480] New: [10,11,12]Werror=useless-cast with constexpr destructor peeceseven at gmail dot com
                   ` (9 preceding siblings ...)
  2022-05-10  8:22 ` cvs-commit at gcc dot gnu.org
@ 2022-05-10 10:14 ` jakub at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-05-10 10:14 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

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

--- Comment #10 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed for 10.4 too.

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

end of thread, other threads:[~2022-05-10 10:14 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-29 22:51 [Bug c++/103480] New: [10,11,12]Werror=useless-cast with constexpr destructor peeceseven at gmail dot com
2021-11-29 23:06 ` [Bug c++/103480] -Werror=useless-cast " mpolacek at gcc dot gnu.org
2021-11-30  0:11 ` mpolacek at gcc dot gnu.org
2021-11-30  0:46 ` [Bug c++/103480] [10/11/12 Regression] " mpolacek at gcc dot gnu.org
2021-11-30  0:52 ` mpolacek at gcc dot gnu.org
2021-12-30 17:49 ` jakub at gcc dot gnu.org
2022-01-12  8:49 ` cvs-commit at gcc dot gnu.org
2022-01-12  8:53 ` [Bug c++/103480] [10/11 " jakub at gcc dot gnu.org
2022-01-24  9:21 ` cvs-commit at gcc dot gnu.org
2022-01-24  9:28 ` [Bug c++/103480] [10 " jakub at gcc dot gnu.org
2022-05-10  8:22 ` cvs-commit at gcc dot gnu.org
2022-05-10 10:14 ` jakub 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).