public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/115522] New: std::to_array no longer works for struct which is trivial but not default constructible
@ 2024-06-17 12:13 daniel.klauer at gin dot de
  2024-06-17 15:11 ` [Bug libstdc++/115522] [13/14/15 Regression] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: daniel.klauer at gin dot de @ 2024-06-17 12:13 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115522
           Summary: std::to_array no longer works for struct which is
                    trivial but not default constructible
           Product: gcc
           Version: 13.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: daniel.klauer at gin dot de
  Target Milestone: ---

Hi,

The following example used to compile successfully with gcc 13.2 or older, but
it no longer compiles in gcc 13.3 and 14.1:

#include <array>
#include <type_traits>
struct range {
    const int min, max;
};
static_assert(std::is_trivial_v<range>);
static_assert(!std::is_default_constructible_v<range>);
static_assert(std::is_copy_constructible_v<range>);
auto my_array = std::to_array<range>({{0, 0}});

Output from g++ 13.3.0 on x86_64-pc-linux-gnu, built with
"--enable-languages=c,c++ --prefix=/opt/gcc-13.3":

$ /opt/gcc-13.3/bin/g++ -std=c++20 e.cxx -c
In file included from e.cxx:1:
/opt/gcc-13.3/include/c++/13.3.0/array: In instantiation of ‘constexpr
std::array<typename std::remove_cv< <template-parameter-1-1> >::type, _Nm>
std::to_array(_Tp (&&)[_Nm]) [with _Tp = range; long unsigned int _Nm = 1;
typename remove_cv< <template-parameter-1-1> >::type = range]’:
e.cxx:9:37:   required from here
/opt/gcc-13.3/include/c++/13.3.0/array:460:44: error: use of deleted function
‘std::array<range, 1>::array()’
  460 |               array<remove_cv_t<_Tp>, _Nm> __arr;
      |                                            ^~~~~
/opt/gcc-13.3/include/c++/13.3.0/array:94:12: note: ‘std::array<range,
1>::array()’ is implicitly deleted because the default definition would be
ill-formed:
   94 |     struct array
      |            ^~~~~
/opt/gcc-13.3/include/c++/13.3.0/array:94:12: error: use of deleted function
‘range::range()’
e.cxx:3:8: note: ‘range::range()’ is implicitly deleted because the default
definition would be ill-formed:
    3 | struct range {
      |        ^~~~~
e.cxx:3:8: error: uninitialized const member in ‘struct range’
e.cxx:4:15: note: ‘const int range::min’ should be initialized
    4 |     const int min, max;
      |               ^~~
e.cxx:3:8: error: uninitialized const member in ‘struct range’
    3 | struct range {
      |        ^~~~~
e.cxx:4:20: note: ‘const int range::max’ should be initialized
    4 |     const int min, max;
      |                    ^~~
/opt/gcc-13.3/include/c++/13.3.0/array:465:39: error: use of deleted function
‘range& range::operator=(const range&)’
  465 |                   __arr._M_elems[__i] = __a[__i];
      |                   ~~~~~~~~~~~~~~~~~~~~^~~~~~
e.cxx:3:8: note: ‘range& range::operator=(const range&)’ is implicitly deleted
because the default definition would be ill-formed:
    3 | struct range {
      |        ^~~~~
e.cxx:3:8: error: non-static const member ‘const int range::min’, cannot use
default assignment operator
e.cxx:3:8: error: non-static const member ‘const int range::max’, cannot use
default assignment operator


It's not clear to me whether it should work in the first place, though it was
my understanding that std::to_array does not necessarily require a default
constructor, since it is closer to a move/copy operation.

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

* [Bug libstdc++/115522] [13/14/15 Regression] std::to_array no longer works for struct which is trivial but not default constructible
  2024-06-17 12:13 [Bug libstdc++/115522] New: std::to_array no longer works for struct which is trivial but not default constructible daniel.klauer at gin dot de
@ 2024-06-17 15:11 ` pinskia at gcc dot gnu.org
  2024-06-17 15:15 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-17 15:11 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pinskia at gcc dot gnu.org
            Summary|std::to_array no longer     |[13/14/15 Regression]
                   |works for struct which is   |std::to_array no longer
                   |trivial but not default     |works for struct which is
                   |constructible               |trivial but not default
                   |                            |constructible
           Keywords|                            |rejects-valid
             Status|UNCONFIRMED                 |NEW
      Known to work|                            |12.3.0, 13.2.0
     Ever confirmed|0                           |1
      Known to fail|                            |13.3.0, 14.1.0
   Target Milestone|---                         |13.4
   Last reconfirmed|                            |2024-06-17

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.

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

* [Bug libstdc++/115522] [13/14/15 Regression] std::to_array no longer works for struct which is trivial but not default constructible
  2024-06-17 12:13 [Bug libstdc++/115522] New: std::to_array no longer works for struct which is trivial but not default constructible daniel.klauer at gin dot de
  2024-06-17 15:11 ` [Bug libstdc++/115522] [13/14/15 Regression] " pinskia at gcc dot gnu.org
@ 2024-06-17 15:15 ` pinskia at gcc dot gnu.org
  2024-06-17 16:17 ` redi 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-17 15:15 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=110167

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Looks like the check (that was added in r13-8421-g4c6bb36e88d5c8 /
r14-1647-g960de5dd886572711ef86fa1e15e30d3810eccb9 ),  constexpr
(is_trivial_v<_Tp>) should be added on to, something like:
constexpr (is_trivial_v<_Tp> && is_default_constructible_v<_Tp>)


Or maybe more, I am not 100% sure.

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

* [Bug libstdc++/115522] [13/14/15 Regression] std::to_array no longer works for struct which is trivial but not default constructible
  2024-06-17 12:13 [Bug libstdc++/115522] New: std::to_array no longer works for struct which is trivial but not default constructible daniel.klauer at gin dot de
  2024-06-17 15:11 ` [Bug libstdc++/115522] [13/14/15 Regression] " pinskia at gcc dot gnu.org
  2024-06-17 15:15 ` pinskia at gcc dot gnu.org
@ 2024-06-17 16:17 ` redi at gcc dot gnu.org
  2024-06-17 19:03 ` redi at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2024-06-17 16:17 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
That shouldn't be needed, because a trivial class has to have a trivial default
constructor.

The problem is that we default-initialize the array, which requires the const
members to be initialized.

We also need to check for assignability, because trivial classes can have
deleted assignment operators (for some reason).


--- a/libstdc++-v3/include/std/array
+++ b/libstdc++-v3/include/std/array
@@ -434,7 +434,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       static_assert(is_constructible_v<_Tp, _Tp&>);
       if constexpr (is_constructible_v<_Tp, _Tp&>)
        {
-         if constexpr (is_trivial_v<_Tp>)
+         if constexpr (is_trivial_v<_Tp> && is_default_constructible_v<_Tp>
+                          && is_copy_assignable_v<_Tp>)
            {
              array<remove_cv_t<_Tp>, _Nm> __arr;
              if (!__is_constant_evaluated() && _Nm != 0)
@@ -463,7 +464,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       static_assert(is_move_constructible_v<_Tp>);
       if constexpr (is_move_constructible_v<_Tp>)
        {
-         if constexpr (is_trivial_v<_Tp>)
+         if constexpr (is_trivial_v<_Tp> && is_default_constructible_v<_Tp>
+                          && is_move_assignable_v<_Tp>)
            {
              array<remove_cv_t<_Tp>, _Nm> __arr;
              if (!__is_constant_evaluated() && _Nm != 0)

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

* [Bug libstdc++/115522] [13/14/15 Regression] std::to_array no longer works for struct which is trivial but not default constructible
  2024-06-17 12:13 [Bug libstdc++/115522] New: std::to_array no longer works for struct which is trivial but not default constructible daniel.klauer at gin dot de
                   ` (2 preceding siblings ...)
  2024-06-17 16:17 ` redi at gcc dot gnu.org
@ 2024-06-17 19:03 ` redi at gcc dot gnu.org
  2024-06-18 12:14 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2024-06-17 19:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #3)
>        static_assert(is_constructible_v<_Tp, _Tp&>);
>        if constexpr (is_constructible_v<_Tp, _Tp&>)
>         {
> -         if constexpr (is_trivial_v<_Tp>)
> +         if constexpr (is_trivial_v<_Tp> && is_default_constructible_v<_Tp>
> +                          && is_copy_assignable_v<_Tp>)

For the testcase above, it would be sufficient to do:

         if constexpr (is_trivial_v<_Tp> && is_copy_assignable_v<_Tp>)

The type with const members isn't assignable.

I'm not sure if this covers all cases. I think combined with is_trivial it
should do, but then I don't understand why the struct ranges type is trivial in
the first place.

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

* [Bug libstdc++/115522] [13/14/15 Regression] std::to_array no longer works for struct which is trivial but not default constructible
  2024-06-17 12:13 [Bug libstdc++/115522] New: std::to_array no longer works for struct which is trivial but not default constructible daniel.klauer at gin dot de
                   ` (3 preceding siblings ...)
  2024-06-17 19:03 ` redi at gcc dot gnu.org
@ 2024-06-18 12:14 ` redi at gcc dot gnu.org
  2024-06-18 12:47 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2024-06-18 12:14 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=85723

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Ah, it looks like is_trivial is just wrong for types with a deleted default
constructor, that's PR 85723

The std::to_array code _should_ work correctly for this case :-\

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

* [Bug libstdc++/115522] [13/14/15 Regression] std::to_array no longer works for struct which is trivial but not default constructible
  2024-06-17 12:13 [Bug libstdc++/115522] New: std::to_array no longer works for struct which is trivial but not default constructible daniel.klauer at gin dot de
                   ` (4 preceding siblings ...)
  2024-06-18 12:14 ` redi at gcc dot gnu.org
@ 2024-06-18 12:47 ` redi at gcc dot gnu.org
  2024-06-19 16:39 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2024-06-18 12:47 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

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

* [Bug libstdc++/115522] [13/14/15 Regression] std::to_array no longer works for struct which is trivial but not default constructible
  2024-06-17 12:13 [Bug libstdc++/115522] New: std::to_array no longer works for struct which is trivial but not default constructible daniel.klauer at gin dot de
                   ` (5 preceding siblings ...)
  2024-06-18 12:47 ` redi at gcc dot gnu.org
@ 2024-06-19 16:39 ` redi at gcc dot gnu.org
  2024-06-20  2:31 ` de34 at live dot cn
  2024-06-21  9:15 ` cvs-commit at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2024-06-19 16:39 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                URL|                            |https://gcc.gnu.org/piperma
                   |                            |il/gcc-patches/2024-June/65
                   |                            |5146.html
           Keywords|                            |patch

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Patch posted: https://gcc.gnu.org/pipermail/gcc-patches/2024-June/655146.html

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

* [Bug libstdc++/115522] [13/14/15 Regression] std::to_array no longer works for struct which is trivial but not default constructible
  2024-06-17 12:13 [Bug libstdc++/115522] New: std::to_array no longer works for struct which is trivial but not default constructible daniel.klauer at gin dot de
                   ` (6 preceding siblings ...)
  2024-06-19 16:39 ` redi at gcc dot gnu.org
@ 2024-06-20  2:31 ` de34 at live dot cn
  2024-06-21  9:15 ` cvs-commit at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: de34 at live dot cn @ 2024-06-20  2:31 UTC (permalink / raw)
  To: gcc-bugs

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

Jiang An <de34 at live dot cn> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |de34 at live dot cn

--- Comment #7 from Jiang An <de34 at live dot cn> ---
(In reply to Jonathan Wakely from comment #5)
> Ah, it looks like is_trivial is just wrong for types with a deleted default
> constructor, that's PR 85723
> 
> The std::to_array code _should_ work correctly for this case :-\

I guess we still need to check is_default_constructible_v even after
__is_trivial gets fixed because the default construct may be protected or
private...

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

* [Bug libstdc++/115522] [13/14/15 Regression] std::to_array no longer works for struct which is trivial but not default constructible
  2024-06-17 12:13 [Bug libstdc++/115522] New: std::to_array no longer works for struct which is trivial but not default constructible daniel.klauer at gin dot de
                   ` (7 preceding siblings ...)
  2024-06-20  2:31 ` de34 at live dot cn
@ 2024-06-21  9:15 ` cvs-commit at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-06-21  9:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:510ce5eed69ee1bea9c2c696fe3b2301e16d1486

commit r15-1533-g510ce5eed69ee1bea9c2c696fe3b2301e16d1486
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Jun 18 13:27:02 2024 +0100

    libstdc++: Fix std::to_array for trivial-ish types [PR115522]

    Due to PR c++/85723 the std::is_trivial trait is true for types with a
    deleted default constructor, so the use of std::is_trivial in
    std::to_array is not sufficient to ensure the type can be trivially
    default constructed then filled using memcpy.

    I also forgot that a type with a deleted assignment operator can still
    be trivial, so we also need to check that it's assignable because the
    is_constant_evaluated() path can't use memcpy.

    Replace the uses of std::is_trivial with std::is_trivially_copyable
    (needed for memcpy), std::is_trivially_default_constructible (needed so
    that the default construction is valid and does no work) and
    std::is_copy_assignable (needed for the constant evaluation case).

    libstdc++-v3/ChangeLog:

            PR libstdc++/115522
            * include/std/array (to_array): Workaround the fact that
            std::is_trivial is not sufficient to check that a type is
            trivially default constructible and assignable.
            * testsuite/23_containers/array/creation/115522.cc: New test.

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

end of thread, other threads:[~2024-06-21  9:15 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-17 12:13 [Bug libstdc++/115522] New: std::to_array no longer works for struct which is trivial but not default constructible daniel.klauer at gin dot de
2024-06-17 15:11 ` [Bug libstdc++/115522] [13/14/15 Regression] " pinskia at gcc dot gnu.org
2024-06-17 15:15 ` pinskia at gcc dot gnu.org
2024-06-17 16:17 ` redi at gcc dot gnu.org
2024-06-17 19:03 ` redi at gcc dot gnu.org
2024-06-18 12:14 ` redi at gcc dot gnu.org
2024-06-18 12:47 ` redi at gcc dot gnu.org
2024-06-19 16:39 ` redi at gcc dot gnu.org
2024-06-20  2:31 ` de34 at live dot cn
2024-06-21  9:15 ` cvs-commit 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).