diff mbox series

libstdc++: Ensure active union member is correctly set

Message ID ZQ6UAFyty9z/Vuw+@Thaum.localdomain
State New
Headers show
Series libstdc++: Ensure active union member is correctly set | expand

Commit Message

Nathaniel Shead Sept. 23, 2023, 7:30 a.m. UTC
On Sat, Sep 23, 2023 at 07:40:48AM +0100, Jonathan Wakely wrote:
> On Sat, 23 Sept 2023, 01:39 Nathaniel Shead via Libstdc++, <
> libstdc++@gcc.gnu.org> wrote:
> 
> > Now that bootstrap has finished, I have gotten regressions in the
> > following libstdc++ tests:
> >
> > Running libstdc++:libstdc++-dg/conformance.exp ...
> > FAIL: 20_util/bitset/access/constexpr.cc -std=gnu++23 (test for excess
> > errors)
> > FAIL: 20_util/bitset/access/constexpr.cc -std=gnu++26 (test for excess
> > errors)
> > FAIL: 20_util/variant/constexpr.cc -std=gnu++20 (test for excess errors)
> > FAIL: 20_util/variant/constexpr.cc -std=gnu++26 (test for excess errors)
> > FAIL: 21_strings/basic_string/cons/char/constexpr.cc -std=gnu++20 (test
> > for excess errors)
> > FAIL: 21_strings/basic_string/cons/char/constexpr.cc -std=gnu++26 (test
> > for excess errors)
> > FAIL: 21_strings/basic_string/cons/wchar_t/constexpr.cc -std=gnu++20 (test
> > for excess errors)
> > FAIL: 21_strings/basic_string/cons/wchar_t/constexpr.cc -std=gnu++26 (test
> > for excess errors)
> > FAIL: 21_strings/basic_string/modifiers/swap/constexpr-wchar_t.cc
> > -std=gnu++20 (test for excess errors)
> > FAIL: 21_strings/basic_string/modifiers/swap/constexpr-wchar_t.cc
> > -std=gnu++26 (test for excess errors)
> > FAIL: 21_strings/basic_string/modifiers/swap/constexpr.cc -std=gnu++20
> > (test for excess errors)
> > FAIL: 21_strings/basic_string/modifiers/swap/constexpr.cc -std=gnu++26
> > (test for excess errors)
> > FAIL: std/ranges/adaptors/join_with/1.cc -std=gnu++23 (test for excess
> > errors)
> > UNRESOLVED: std/ranges/adaptors/join_with/1.cc -std=gnu++23 compilation
> > failed to produce executable
> > FAIL: std/ranges/adaptors/join_with/1.cc -std=gnu++26 (test for excess
> > errors)
> > UNRESOLVED: std/ranges/adaptors/join_with/1.cc -std=gnu++26 compilation
> > failed to produce executable
> >
> > On investigation though it looks like the issue might be with libstdc++
> > rather than the patch itself; running the failing tests using clang with
> > libstdc++ also produces similar errors, and my reading of the code
> > suggests that this is correct.
> >
> > What's the way forward here? Should I look at creating a patch to fix
> > the libstdc++ issues before resubmitting this patch for the C++
> > frontend? Or should I submit a version of this patch without the
> > `std::construct_at` changes and wait till libstdc++ gets fixed for that?
> >
> 
> I think we should fix libstdc++. There are probably only a few places that
> need a fix, which cause all those failures.
> 
> I can help with those fixes. I'll look into it after the weekend.
> 

Thanks. I did end up getting a chance to look at it earlier today, and
with the following patch I had no regressions when applying the frontend
changes. Bootstrapped and regtested on x86_64-pc-linux-gnu.

-- >8 --

This patch ensures that the union members for std::string and
std::variant are always properly set when a change occurs.

libstdc++-v3/ChangeLog:

	* include/bits/basic_string.h: (basic_string(basic_string&&)):
	Activate _M_local_buf when needed.
	(basic_string(basic_string&&, const _Alloc&)): Likewise.
	* include/bits/basic_string.tcc: (basic_string::swap): Likewise.
	* include/std/variant: (__detail::__variant::__construct_n): New.
	(__detail::_variant::__emplace): Use __construct_n.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
---
 libstdc++-v3/include/bits/basic_string.h   |  7 +++--
 libstdc++-v3/include/bits/basic_string.tcc |  8 +++---
 libstdc++-v3/include/std/variant           | 32 ++++++++++++++++++++--
 3 files changed, 38 insertions(+), 9 deletions(-)

Comments

Jonathan Wakely Sept. 23, 2023, 10:52 a.m. UTC | #1
On Sat, 23 Sept 2023, 08:30 Nathaniel Shead, <nathanieloshead@gmail.com>
wrote:

> On Sat, Sep 23, 2023 at 07:40:48AM +0100, Jonathan Wakely wrote:
> > On Sat, 23 Sept 2023, 01:39 Nathaniel Shead via Libstdc++, <
> > libstdc++@gcc.gnu.org> wrote:
> >
> > > Now that bootstrap has finished, I have gotten regressions in the
> > > following libstdc++ tests:
> > >
> > > Running libstdc++:libstdc++-dg/conformance.exp ...
> > > FAIL: 20_util/bitset/access/constexpr.cc -std=gnu++23 (test for excess
> > > errors)
> > > FAIL: 20_util/bitset/access/constexpr.cc -std=gnu++26 (test for excess
> > > errors)
> > > FAIL: 20_util/variant/constexpr.cc -std=gnu++20 (test for excess
> errors)
> > > FAIL: 20_util/variant/constexpr.cc -std=gnu++26 (test for excess
> errors)
> > > FAIL: 21_strings/basic_string/cons/char/constexpr.cc -std=gnu++20 (test
> > > for excess errors)
> > > FAIL: 21_strings/basic_string/cons/char/constexpr.cc -std=gnu++26 (test
> > > for excess errors)
> > > FAIL: 21_strings/basic_string/cons/wchar_t/constexpr.cc -std=gnu++20
> (test
> > > for excess errors)
> > > FAIL: 21_strings/basic_string/cons/wchar_t/constexpr.cc -std=gnu++26
> (test
> > > for excess errors)
> > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr-wchar_t.cc
> > > -std=gnu++20 (test for excess errors)
> > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr-wchar_t.cc
> > > -std=gnu++26 (test for excess errors)
> > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr.cc -std=gnu++20
> > > (test for excess errors)
> > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr.cc -std=gnu++26
> > > (test for excess errors)
> > > FAIL: std/ranges/adaptors/join_with/1.cc -std=gnu++23 (test for excess
> > > errors)
> > > UNRESOLVED: std/ranges/adaptors/join_with/1.cc -std=gnu++23 compilation
> > > failed to produce executable
> > > FAIL: std/ranges/adaptors/join_with/1.cc -std=gnu++26 (test for excess
> > > errors)
> > > UNRESOLVED: std/ranges/adaptors/join_with/1.cc -std=gnu++26 compilation
> > > failed to produce executable
> > >
> > > On investigation though it looks like the issue might be with libstdc++
> > > rather than the patch itself; running the failing tests using clang
> with
> > > libstdc++ also produces similar errors, and my reading of the code
> > > suggests that this is correct.
> > >
> > > What's the way forward here? Should I look at creating a patch to fix
> > > the libstdc++ issues before resubmitting this patch for the C++
> > > frontend? Or should I submit a version of this patch without the
> > > `std::construct_at` changes and wait till libstdc++ gets fixed for
> that?
> > >
> >
> > I think we should fix libstdc++. There are probably only a few places
> that
> > need a fix, which cause all those failures.
> >
> > I can help with those fixes. I'll look into it after the weekend.
> >
>
> Thanks. I did end up getting a chance to look at it earlier today, and
> with the following patch I had no regressions when applying the frontend
> changes. Bootstrapped and regtested on x86_64-pc-linux-gnu.
>

Nice, thanks! This looks good at first glance, I'll review it properly ASAP.



> -- >8 --
>
> This patch ensures that the union members for std::string and
> std::variant are always properly set when a change occurs.
>
> libstdc++-v3/ChangeLog:
>
>         * include/bits/basic_string.h: (basic_string(basic_string&&)):
>         Activate _M_local_buf when needed.
>         (basic_string(basic_string&&, const _Alloc&)): Likewise.
>         * include/bits/basic_string.tcc: (basic_string::swap): Likewise.
>         * include/std/variant: (__detail::__variant::__construct_n): New.
>         (__detail::_variant::__emplace): Use __construct_n.
>
> Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
> ---
>  libstdc++-v3/include/bits/basic_string.h   |  7 +++--
>  libstdc++-v3/include/bits/basic_string.tcc |  8 +++---
>  libstdc++-v3/include/std/variant           | 32 ++++++++++++++++++++--
>  3 files changed, 38 insertions(+), 9 deletions(-)
>
> diff --git a/libstdc++-v3/include/bits/basic_string.h
> b/libstdc++-v3/include/bits/basic_string.h
> index 09fd62afa66..7c342879827 100644
> --- a/libstdc++-v3/include/bits/basic_string.h
> +++ b/libstdc++-v3/include/bits/basic_string.h
> @@ -678,7 +678,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>        {
>         if (__str._M_is_local())
>           {
> -           traits_type::copy(_M_local_buf, __str._M_local_buf,
> +           traits_type::copy(_M_use_local_data(), __str._M_local_buf,
>                               __str.length() + 1);
>           }
>         else
> @@ -691,7 +691,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>         // basic_stringbuf relies on writing into unallocated capacity so
>         // we mess up the contents if we put a '\0' in the string.
>         _M_length(__str.length());
> -       __str._M_data(__str._M_local_data());
> +       __str._M_data(__str._M_use_local_data());
>         __str._M_set_length(0);
>        }
>
> @@ -717,6 +717,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>        {
>         if (__str._M_is_local())
>           {
> +           _M_use_local_data();
>             traits_type::copy(_M_local_buf, __str._M_local_buf,
>                               __str.length() + 1);
>             _M_length(__str.length());
> @@ -728,7 +729,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>             _M_data(__str._M_data());
>             _M_length(__str.length());
>             _M_capacity(__str._M_allocated_capacity);
> -           __str._M_data(__str._M_local_buf);
> +           __str._M_data(__str._M_use_local_data());
>             __str._M_set_length(0);
>           }
>         else
> diff --git a/libstdc++-v3/include/bits/basic_string.tcc
> b/libstdc++-v3/include/bits/basic_string.tcc
> index 104a517f794..ee6e57da555 100644
> --- a/libstdc++-v3/include/bits/basic_string.tcc
> +++ b/libstdc++-v3/include/bits/basic_string.tcc
> @@ -79,7 +79,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>               }
>             else if (__s.length())
>               {
> -               traits_type::copy(_M_local_buf, __s._M_local_buf,
> +               traits_type::copy(_M_use_local_data(), __s._M_local_buf,
>                                   __s.length() + 1);
>                 _M_length(__s.length());
>                 __s._M_set_length(0);
> @@ -87,7 +87,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>               }
>             else if (length())
>               {
> -               traits_type::copy(__s._M_local_buf, _M_local_buf,
> +               traits_type::copy(__s._M_use_local_data(), _M_local_buf,
>                                   length() + 1);
>                 __s._M_length(length());
>                 _M_set_length(0);
> @@ -97,7 +97,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>         else
>           {
>             const size_type __tmp_capacity = __s._M_allocated_capacity;
> -           traits_type::copy(__s._M_local_buf, _M_local_buf,
> +           traits_type::copy(__s._M_use_local_data(), _M_local_buf,
>                               length() + 1);
>             _M_data(__s._M_data());
>             __s._M_data(__s._M_local_buf);
> @@ -108,7 +108,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>           const size_type __tmp_capacity = _M_allocated_capacity;
>           if (__s._M_is_local())
>             {
> -             traits_type::copy(_M_local_buf, __s._M_local_buf,
> +             traits_type::copy(_M_use_local_data(), __s._M_local_buf,
>                                 __s.length() + 1);
>               __s._M_data(_M_data());
>               _M_data(_M_local_buf);
> diff --git a/libstdc++-v3/include/std/variant
> b/libstdc++-v3/include/std/variant
> index c0e41740dcf..7f24e760bb1 100644
> --- a/libstdc++-v3/include/std/variant
> +++ b/libstdc++-v3/include/std/variant
> @@ -320,6 +320,33 @@ namespace __variant
>      __get(_Variant&& __v) noexcept
>      { return __variant::__get_n<_Np>(std::forward<_Variant>(__v)._M_u); }
>
> +  // Gets the _Uninitialized to construct into for __u.
> +  template<size_t _Np, typename _Union>
> +    constexpr decltype(auto)
> +    __construct_n(_Union& __u) noexcept
> +    {
> +      if constexpr (_Np == 0)
> +       return &__u._M_first;
> +      else if constexpr (_Np == 1)
> +       {
> +         std::_Construct(&__u._M_rest);
> +         return &__u._M_rest._M_first;
> +       }
> +      else if constexpr (_Np == 2)
> +       {
> +         std::_Construct(&__u._M_rest);
> +         std::_Construct(&__u._M_rest._M_rest);
> +         return &__u._M_rest._M_rest._M_first;
> +       }
> +      else
> +       {
> +         std::_Construct(&__u._M_rest);
> +         std::_Construct(&__u._M_rest._M_rest);
> +         std::_Construct(&__u._M_rest._M_rest._M_rest);
> +         return __variant::__construct_n<_Np -
> 3>(__u._M_rest._M_rest._M_rest);
> +       }
> +    }
> +
>    template<typename... _Types>
>      struct _Traits
>      {
> @@ -536,8 +563,9 @@ namespace __variant
>      __emplace(_Variant_storage<_Triv, _Types...>& __v, _Args&&... __args)
>      {
>        __v._M_reset();
> -      auto* __addr = std::__addressof(__variant::__get_n<_Np>(__v._M_u));
> -      std::_Construct(__addr, std::forward<_Args>(__args)...);
> +      auto* __addr = __variant::__construct_n<_Np>(__v._M_u);
> +      std::_Construct(__addr, in_place_index<0>,
> +                     std::forward<_Args>(__args)...);
>        // Construction didn't throw, so can set the new index now:
>        __v._M_index = _Np;
>      }
> --
> 2.41.0
>
>
Jonathan Wakely Sept. 27, 2023, 2:13 p.m. UTC | #2
On Sat, 23 Sept 2023 at 08:30, Nathaniel Shead via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> On Sat, Sep 23, 2023 at 07:40:48AM +0100, Jonathan Wakely wrote:
> > On Sat, 23 Sept 2023, 01:39 Nathaniel Shead via Libstdc++, <
> > libstdc++@gcc.gnu.org> wrote:
> >
> > > Now that bootstrap has finished, I have gotten regressions in the
> > > following libstdc++ tests:
> > >
> > > Running libstdc++:libstdc++-dg/conformance.exp ...
> > > FAIL: 20_util/bitset/access/constexpr.cc -std=gnu++23 (test for excess
> > > errors)
> > > FAIL: 20_util/bitset/access/constexpr.cc -std=gnu++26 (test for excess
> > > errors)
> > > FAIL: 20_util/variant/constexpr.cc -std=gnu++20 (test for excess errors)
> > > FAIL: 20_util/variant/constexpr.cc -std=gnu++26 (test for excess errors)
> > > FAIL: 21_strings/basic_string/cons/char/constexpr.cc -std=gnu++20 (test
> > > for excess errors)
> > > FAIL: 21_strings/basic_string/cons/char/constexpr.cc -std=gnu++26 (test
> > > for excess errors)
> > > FAIL: 21_strings/basic_string/cons/wchar_t/constexpr.cc -std=gnu++20 (test
> > > for excess errors)
> > > FAIL: 21_strings/basic_string/cons/wchar_t/constexpr.cc -std=gnu++26 (test
> > > for excess errors)
> > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr-wchar_t.cc
> > > -std=gnu++20 (test for excess errors)
> > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr-wchar_t.cc
> > > -std=gnu++26 (test for excess errors)
> > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr.cc -std=gnu++20
> > > (test for excess errors)
> > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr.cc -std=gnu++26
> > > (test for excess errors)
> > > FAIL: std/ranges/adaptors/join_with/1.cc -std=gnu++23 (test for excess
> > > errors)
> > > UNRESOLVED: std/ranges/adaptors/join_with/1.cc -std=gnu++23 compilation
> > > failed to produce executable
> > > FAIL: std/ranges/adaptors/join_with/1.cc -std=gnu++26 (test for excess
> > > errors)
> > > UNRESOLVED: std/ranges/adaptors/join_with/1.cc -std=gnu++26 compilation
> > > failed to produce executable
> > >
> > > On investigation though it looks like the issue might be with libstdc++
> > > rather than the patch itself; running the failing tests using clang with
> > > libstdc++ also produces similar errors, and my reading of the code
> > > suggests that this is correct.
> > >
> > > What's the way forward here? Should I look at creating a patch to fix
> > > the libstdc++ issues before resubmitting this patch for the C++
> > > frontend? Or should I submit a version of this patch without the
> > > `std::construct_at` changes and wait till libstdc++ gets fixed for that?
> > >
> >
> > I think we should fix libstdc++. There are probably only a few places that
> > need a fix, which cause all those failures.
> >
> > I can help with those fixes. I'll look into it after the weekend.
> >
>
> Thanks. I did end up getting a chance to look at it earlier today, and
> with the following patch I had no regressions when applying the frontend
> changes. Bootstrapped and regtested on x86_64-pc-linux-gnu.
>
> -- >8 --
>
> This patch ensures that the union members for std::string and
> std::variant are always properly set when a change occurs.
>
> libstdc++-v3/ChangeLog:
>
>         * include/bits/basic_string.h: (basic_string(basic_string&&)):
>         Activate _M_local_buf when needed.
>         (basic_string(basic_string&&, const _Alloc&)): Likewise.
>         * include/bits/basic_string.tcc: (basic_string::swap): Likewise.
>         * include/std/variant: (__detail::__variant::__construct_n): New.
>         (__detail::_variant::__emplace): Use __construct_n.
>
> Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
> ---
>  libstdc++-v3/include/bits/basic_string.h   |  7 +++--
>  libstdc++-v3/include/bits/basic_string.tcc |  8 +++---
>  libstdc++-v3/include/std/variant           | 32 ++++++++++++++++++++--
>  3 files changed, 38 insertions(+), 9 deletions(-)
>
> diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
> index 09fd62afa66..7c342879827 100644
> --- a/libstdc++-v3/include/bits/basic_string.h
> +++ b/libstdc++-v3/include/bits/basic_string.h
> @@ -678,7 +678,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>        {
>         if (__str._M_is_local())
>           {
> -           traits_type::copy(_M_local_buf, __str._M_local_buf,
> +           traits_type::copy(_M_use_local_data(), __str._M_local_buf,
>                               __str.length() + 1);
>           }
>         else
> @@ -691,7 +691,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>         // basic_stringbuf relies on writing into unallocated capacity so
>         // we mess up the contents if we put a '\0' in the string.
>         _M_length(__str.length());
> -       __str._M_data(__str._M_local_data());
> +       __str._M_data(__str._M_use_local_data());
>         __str._M_set_length(0);
>        }
>
> @@ -717,6 +717,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>        {
>         if (__str._M_is_local())
>           {
> +           _M_use_local_data();

Lets add a cast to void to make it clear we're intentionally
discarding the return value here, and only calling it for its
constexpr "side effects":

    (void) _M_use_local_data();


>             traits_type::copy(_M_local_buf, __str._M_local_buf,
>                               __str.length() + 1);
>             _M_length(__str.length());
> @@ -728,7 +729,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>             _M_data(__str._M_data());
>             _M_length(__str.length());
>             _M_capacity(__str._M_allocated_capacity);
> -           __str._M_data(__str._M_local_buf);
> +           __str._M_data(__str._M_use_local_data());
>             __str._M_set_length(0);
>           }
>         else
> diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc
> index 104a517f794..ee6e57da555 100644
> --- a/libstdc++-v3/include/bits/basic_string.tcc
> +++ b/libstdc++-v3/include/bits/basic_string.tcc
> @@ -79,7 +79,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>               }
>             else if (__s.length())
>               {
> -               traits_type::copy(_M_local_buf, __s._M_local_buf,
> +               traits_type::copy(_M_use_local_data(), __s._M_local_buf,

I think we should call _M_use_local_data() before calling
traits_type::copy, as you did for
basic_string(basic_string&& __str, const _Alloc& __a)
above.

The problem is that _M_use_local_data() returns the allocator's
pointer type, but traits_type::copy expects char_type*, and those
might not be the same type.
So:

@@ -79,6 +79,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
             }
           else if (__s.length())
             {
+               (void) _M_use_local_data();
               traits_type::copy(_M_local_buf, __s._M_local_buf,
                                 __s.length() + 1);
               _M_length(__s.length());



>                                   __s.length() + 1);
>                 _M_length(__s.length());
>                 __s._M_set_length(0);
> @@ -87,7 +87,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>               }
>             else if (length())
>               {
> -               traits_type::copy(__s._M_local_buf, _M_local_buf,
> +               traits_type::copy(__s._M_use_local_data(), _M_local_buf,

Same here, for __s._M_use_local_data()

>                                   length() + 1);
>                 __s._M_length(length());
>                 _M_set_length(0);
> @@ -97,7 +97,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>         else
>           {
>             const size_type __tmp_capacity = __s._M_allocated_capacity;
> -           traits_type::copy(__s._M_local_buf, _M_local_buf,
> +           traits_type::copy(__s._M_use_local_data(), _M_local_buf,

And again.

>                               length() + 1);
>             _M_data(__s._M_data());
>             __s._M_data(__s._M_local_buf);
> @@ -108,7 +108,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>           const size_type __tmp_capacity = _M_allocated_capacity;
>           if (__s._M_is_local())
>             {
> -             traits_type::copy(_M_local_buf, __s._M_local_buf,
> +             traits_type::copy(_M_use_local_data(), __s._M_local_buf,

And again.

>                                 __s.length() + 1);
>               __s._M_data(_M_data());
>               _M_data(_M_local_buf);
> diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant
> index c0e41740dcf..7f24e760bb1 100644
> --- a/libstdc++-v3/include/std/variant
> +++ b/libstdc++-v3/include/std/variant
> @@ -320,6 +320,33 @@ namespace __variant
>      __get(_Variant&& __v) noexcept
>      { return __variant::__get_n<_Np>(std::forward<_Variant>(__v)._M_u); }
>
> +  // Gets the _Uninitialized to construct into for __u.
> +  template<size_t _Np, typename _Union>
> +    constexpr decltype(auto)
> +    __construct_n(_Union& __u) noexcept
> +    {
> +      if constexpr (_Np == 0)
> +       return &__u._M_first;
> +      else if constexpr (_Np == 1)
> +       {
> +         std::_Construct(&__u._M_rest);
> +         return &__u._M_rest._M_first;
> +       }
> +      else if constexpr (_Np == 2)
> +       {
> +         std::_Construct(&__u._M_rest);
> +         std::_Construct(&__u._M_rest._M_rest);
> +         return &__u._M_rest._M_rest._M_first;
> +       }
> +      else
> +       {
> +         std::_Construct(&__u._M_rest);
> +         std::_Construct(&__u._M_rest._M_rest);
> +         std::_Construct(&__u._M_rest._M_rest._M_rest);
> +         return __variant::__construct_n<_Np - 3>(__u._M_rest._M_rest._M_rest);
> +       }
> +    }

This is nice, thanks for optimizing it to reduce the recursion depth.

> +
>    template<typename... _Types>
>      struct _Traits
>      {
> @@ -536,8 +563,9 @@ namespace __variant
>      __emplace(_Variant_storage<_Triv, _Types...>& __v, _Args&&... __args)
>      {
>        __v._M_reset();
> -      auto* __addr = std::__addressof(__variant::__get_n<_Np>(__v._M_u));
> -      std::_Construct(__addr, std::forward<_Args>(__args)...);
> +      auto* __addr = __variant::__construct_n<_Np>(__v._M_u);
> +      std::_Construct(__addr, in_place_index<0>,
> +                     std::forward<_Args>(__args)...);
>        // Construction didn't throw, so can set the new index now:
>        __v._M_index = _Np;
>      }
> --
> 2.41.0
>
Nathaniel Shead Sept. 28, 2023, 11:25 p.m. UTC | #3
On Wed, Sep 27, 2023 at 03:13:35PM +0100, Jonathan Wakely wrote:
> On Sat, 23 Sept 2023 at 08:30, Nathaniel Shead via Libstdc++
> <libstdc++@gcc.gnu.org> wrote:
> >
> > On Sat, Sep 23, 2023 at 07:40:48AM +0100, Jonathan Wakely wrote:
> > > On Sat, 23 Sept 2023, 01:39 Nathaniel Shead via Libstdc++, <
> > > libstdc++@gcc.gnu.org> wrote:
> > >
> > > > Now that bootstrap has finished, I have gotten regressions in the
> > > > following libstdc++ tests:
> > > >
> > > > Running libstdc++:libstdc++-dg/conformance.exp ...
> > > > FAIL: 20_util/bitset/access/constexpr.cc -std=gnu++23 (test for excess
> > > > errors)
> > > > FAIL: 20_util/bitset/access/constexpr.cc -std=gnu++26 (test for excess
> > > > errors)
> > > > FAIL: 20_util/variant/constexpr.cc -std=gnu++20 (test for excess errors)
> > > > FAIL: 20_util/variant/constexpr.cc -std=gnu++26 (test for excess errors)
> > > > FAIL: 21_strings/basic_string/cons/char/constexpr.cc -std=gnu++20 (test
> > > > for excess errors)
> > > > FAIL: 21_strings/basic_string/cons/char/constexpr.cc -std=gnu++26 (test
> > > > for excess errors)
> > > > FAIL: 21_strings/basic_string/cons/wchar_t/constexpr.cc -std=gnu++20 (test
> > > > for excess errors)
> > > > FAIL: 21_strings/basic_string/cons/wchar_t/constexpr.cc -std=gnu++26 (test
> > > > for excess errors)
> > > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr-wchar_t.cc
> > > > -std=gnu++20 (test for excess errors)
> > > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr-wchar_t.cc
> > > > -std=gnu++26 (test for excess errors)
> > > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr.cc -std=gnu++20
> > > > (test for excess errors)
> > > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr.cc -std=gnu++26
> > > > (test for excess errors)
> > > > FAIL: std/ranges/adaptors/join_with/1.cc -std=gnu++23 (test for excess
> > > > errors)
> > > > UNRESOLVED: std/ranges/adaptors/join_with/1.cc -std=gnu++23 compilation
> > > > failed to produce executable
> > > > FAIL: std/ranges/adaptors/join_with/1.cc -std=gnu++26 (test for excess
> > > > errors)
> > > > UNRESOLVED: std/ranges/adaptors/join_with/1.cc -std=gnu++26 compilation
> > > > failed to produce executable
> > > >
> > > > On investigation though it looks like the issue might be with libstdc++
> > > > rather than the patch itself; running the failing tests using clang with
> > > > libstdc++ also produces similar errors, and my reading of the code
> > > > suggests that this is correct.
> > > >
> > > > What's the way forward here? Should I look at creating a patch to fix
> > > > the libstdc++ issues before resubmitting this patch for the C++
> > > > frontend? Or should I submit a version of this patch without the
> > > > `std::construct_at` changes and wait till libstdc++ gets fixed for that?
> > > >
> > >
> > > I think we should fix libstdc++. There are probably only a few places that
> > > need a fix, which cause all those failures.
> > >
> > > I can help with those fixes. I'll look into it after the weekend.
> > >
> >
> > Thanks. I did end up getting a chance to look at it earlier today, and
> > with the following patch I had no regressions when applying the frontend
> > changes. Bootstrapped and regtested on x86_64-pc-linux-gnu.
> >
> > -- >8 --
> >
> > This patch ensures that the union members for std::string and
> > std::variant are always properly set when a change occurs.
> >
> > libstdc++-v3/ChangeLog:
> >
> >         * include/bits/basic_string.h: (basic_string(basic_string&&)):
> >         Activate _M_local_buf when needed.
> >         (basic_string(basic_string&&, const _Alloc&)): Likewise.
> >         * include/bits/basic_string.tcc: (basic_string::swap): Likewise.
> >         * include/std/variant: (__detail::__variant::__construct_n): New.
> >         (__detail::_variant::__emplace): Use __construct_n.
> >
> > Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
> > ---
> >  libstdc++-v3/include/bits/basic_string.h   |  7 +++--
> >  libstdc++-v3/include/bits/basic_string.tcc |  8 +++---
> >  libstdc++-v3/include/std/variant           | 32 ++++++++++++++++++++--
> >  3 files changed, 38 insertions(+), 9 deletions(-)
> >
> > diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
> > index 09fd62afa66..7c342879827 100644
> > --- a/libstdc++-v3/include/bits/basic_string.h
> > +++ b/libstdc++-v3/include/bits/basic_string.h
> > @@ -678,7 +678,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> >        {
> >         if (__str._M_is_local())
> >           {
> > -           traits_type::copy(_M_local_buf, __str._M_local_buf,
> > +           traits_type::copy(_M_use_local_data(), __str._M_local_buf,
> >                               __str.length() + 1);
> >           }
> >         else
> > @@ -691,7 +691,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> >         // basic_stringbuf relies on writing into unallocated capacity so
> >         // we mess up the contents if we put a '\0' in the string.
> >         _M_length(__str.length());
> > -       __str._M_data(__str._M_local_data());
> > +       __str._M_data(__str._M_use_local_data());
> >         __str._M_set_length(0);
> >        }
> >
> > @@ -717,6 +717,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> >        {
> >         if (__str._M_is_local())
> >           {
> > +           _M_use_local_data();
> 
> Lets add a cast to void to make it clear we're intentionally
> discarding the return value here, and only calling it for its
> constexpr "side effects":
> 
>     (void) _M_use_local_data();
> 
> 
> >             traits_type::copy(_M_local_buf, __str._M_local_buf,
> >                               __str.length() + 1);
> >             _M_length(__str.length());
> > @@ -728,7 +729,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> >             _M_data(__str._M_data());
> >             _M_length(__str.length());
> >             _M_capacity(__str._M_allocated_capacity);
> > -           __str._M_data(__str._M_local_buf);
> > +           __str._M_data(__str._M_use_local_data());
> >             __str._M_set_length(0);
> >           }
> >         else
> > diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc
> > index 104a517f794..ee6e57da555 100644
> > --- a/libstdc++-v3/include/bits/basic_string.tcc
> > +++ b/libstdc++-v3/include/bits/basic_string.tcc
> > @@ -79,7 +79,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >               }
> >             else if (__s.length())
> >               {
> > -               traits_type::copy(_M_local_buf, __s._M_local_buf,
> > +               traits_type::copy(_M_use_local_data(), __s._M_local_buf,
> 
> I think we should call _M_use_local_data() before calling
> traits_type::copy, as you did for
> basic_string(basic_string&& __str, const _Alloc& __a)
> above.
> 
> The problem is that _M_use_local_data() returns the allocator's
> pointer type, but traits_type::copy expects char_type*, and those
> might not be the same type.
> So:
> 
> @@ -79,6 +79,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>              }
>            else if (__s.length())
>              {
> +               (void) _M_use_local_data();
>                traits_type::copy(_M_local_buf, __s._M_local_buf,
>                                  __s.length() + 1);
>                _M_length(__s.length());
> 
> 
> 
> >                                   __s.length() + 1);
> >                 _M_length(__s.length());
> >                 __s._M_set_length(0);
> > @@ -87,7 +87,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >               }
> >             else if (length())
> >               {
> > -               traits_type::copy(__s._M_local_buf, _M_local_buf,
> > +               traits_type::copy(__s._M_use_local_data(), _M_local_buf,
> 
> Same here, for __s._M_use_local_data()
> 
> >                                   length() + 1);
> >                 __s._M_length(length());
> >                 _M_set_length(0);
> > @@ -97,7 +97,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >         else
> >           {
> >             const size_type __tmp_capacity = __s._M_allocated_capacity;
> > -           traits_type::copy(__s._M_local_buf, _M_local_buf,
> > +           traits_type::copy(__s._M_use_local_data(), _M_local_buf,
> 
> And again.
> 
> >                               length() + 1);
> >             _M_data(__s._M_data());
> >             __s._M_data(__s._M_local_buf);
> > @@ -108,7 +108,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >           const size_type __tmp_capacity = _M_allocated_capacity;
> >           if (__s._M_is_local())
> >             {
> > -             traits_type::copy(_M_local_buf, __s._M_local_buf,
> > +             traits_type::copy(_M_use_local_data(), __s._M_local_buf,
> 
> And again.
> 
> >                                 __s.length() + 1);
> >               __s._M_data(_M_data());
> >               _M_data(_M_local_buf);
> > diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant
> > index c0e41740dcf..7f24e760bb1 100644
> > --- a/libstdc++-v3/include/std/variant
> > +++ b/libstdc++-v3/include/std/variant
> > @@ -320,6 +320,33 @@ namespace __variant
> >      __get(_Variant&& __v) noexcept
> >      { return __variant::__get_n<_Np>(std::forward<_Variant>(__v)._M_u); }
> >
> > +  // Gets the _Uninitialized to construct into for __u.
> > +  template<size_t _Np, typename _Union>
> > +    constexpr decltype(auto)
> > +    __construct_n(_Union& __u) noexcept
> > +    {
> > +      if constexpr (_Np == 0)
> > +       return &__u._M_first;
> > +      else if constexpr (_Np == 1)
> > +       {
> > +         std::_Construct(&__u._M_rest);
> > +         return &__u._M_rest._M_first;
> > +       }
> > +      else if constexpr (_Np == 2)
> > +       {
> > +         std::_Construct(&__u._M_rest);
> > +         std::_Construct(&__u._M_rest._M_rest);
> > +         return &__u._M_rest._M_rest._M_first;
> > +       }
> > +      else
> > +       {
> > +         std::_Construct(&__u._M_rest);
> > +         std::_Construct(&__u._M_rest._M_rest);
> > +         std::_Construct(&__u._M_rest._M_rest._M_rest);
> > +         return __variant::__construct_n<_Np - 3>(__u._M_rest._M_rest._M_rest);
> > +       }
> > +    }
> 
> This is nice, thanks for optimizing it to reduce the recursion depth.
> 
> > +
> >    template<typename... _Types>
> >      struct _Traits
> >      {
> > @@ -536,8 +563,9 @@ namespace __variant
> >      __emplace(_Variant_storage<_Triv, _Types...>& __v, _Args&&... __args)
> >      {
> >        __v._M_reset();
> > -      auto* __addr = std::__addressof(__variant::__get_n<_Np>(__v._M_u));
> > -      std::_Construct(__addr, std::forward<_Args>(__args)...);
> > +      auto* __addr = __variant::__construct_n<_Np>(__v._M_u);
> > +      std::_Construct(__addr, in_place_index<0>,
> > +                     std::forward<_Args>(__args)...);
> >        // Construction didn't throw, so can set the new index now:
> >        __v._M_index = _Np;
> >      }
> > --
> > 2.41.0
> >
> 

Thanks for the comments, here's an updated version of the patch.
Bootstrapped and regtested on x86_64-pc-linux-gnu.

I'll note that there are some existing calls to `_M_use_local_data()`
already used only for their side effects without a cast to void, e.g.

      /**
       *  @brief  Default constructor creates an empty string.
       */
      _GLIBCXX20_CONSTEXPR
      basic_string()
      _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
      : _M_dataplus(_M_local_data())
      {
	_M_use_local_data();
	_M_set_length(0);
      }

I haven't updated these, but should this be changed for consistency?

-- >8 --

This patch ensures that the union members for std::string and
std::variant are always properly set when a change occurs.

libstdc++-v3/ChangeLog:

	* include/bits/basic_string.h: (basic_string(basic_string&&)):
	Activate _M_local_buf when needed.
	(basic_string(basic_string&&, const _Alloc&)): Likewise.
	* include/bits/basic_string.tcc: (basic_string::swap): Likewise.
	* include/std/variant: (__detail::__variant::__construct_n): New.
	(__detail::__variant::__emplace): Use __construct_n.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
---
 libstdc++-v3/include/bits/basic_string.h   |  6 ++--
 libstdc++-v3/include/bits/basic_string.tcc |  4 +++
 libstdc++-v3/include/std/variant           | 32 ++++++++++++++++++++--
 3 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
index 09fd62afa66..4f94cd967cf 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -678,6 +678,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       {
 	if (__str._M_is_local())
 	  {
+	    (void)_M_use_local_data();
 	    traits_type::copy(_M_local_buf, __str._M_local_buf,
 			      __str.length() + 1);
 	  }
@@ -691,7 +692,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
 	// basic_stringbuf relies on writing into unallocated capacity so
 	// we mess up the contents if we put a '\0' in the string.
 	_M_length(__str.length());
-	__str._M_data(__str._M_local_data());
+	__str._M_data(__str._M_use_local_data());
 	__str._M_set_length(0);
       }
 
@@ -717,6 +718,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       {
 	if (__str._M_is_local())
 	  {
+	    (void)_M_use_local_data();
 	    traits_type::copy(_M_local_buf, __str._M_local_buf,
 			      __str.length() + 1);
 	    _M_length(__str.length());
@@ -728,7 +730,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
 	    _M_data(__str._M_data());
 	    _M_length(__str.length());
 	    _M_capacity(__str._M_allocated_capacity);
-	    __str._M_data(__str._M_local_buf);
+	    __str._M_data(__str._M_use_local_data());
 	    __str._M_set_length(0);
 	  }
 	else
diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc
index 104a517f794..4bc98f2aea7 100644
--- a/libstdc++-v3/include/bits/basic_string.tcc
+++ b/libstdc++-v3/include/bits/basic_string.tcc
@@ -79,6 +79,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	      }
 	    else if (__s.length())
 	      {
+		(void)_M_use_local_data();
 		traits_type::copy(_M_local_buf, __s._M_local_buf,
 				  __s.length() + 1);
 		_M_length(__s.length());
@@ -87,6 +88,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	      }
 	    else if (length())
 	      {
+		(void)__s._M_use_local_data();
 		traits_type::copy(__s._M_local_buf, _M_local_buf,
 				  length() + 1);
 		__s._M_length(length());
@@ -97,6 +99,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	else
 	  {
 	    const size_type __tmp_capacity = __s._M_allocated_capacity;
+	    (void)__s._M_use_local_data();
 	    traits_type::copy(__s._M_local_buf, _M_local_buf,
 			      length() + 1);
 	    _M_data(__s._M_data());
@@ -108,6 +111,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  const size_type __tmp_capacity = _M_allocated_capacity;
 	  if (__s._M_is_local())
 	    {
+	      (void)_M_use_local_data();
 	      traits_type::copy(_M_local_buf, __s._M_local_buf,
 				__s.length() + 1);
 	      __s._M_data(_M_data());
diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant
index c0e41740dcf..7f24e760bb1 100644
--- a/libstdc++-v3/include/std/variant
+++ b/libstdc++-v3/include/std/variant
@@ -320,6 +320,33 @@ namespace __variant
     __get(_Variant&& __v) noexcept
     { return __variant::__get_n<_Np>(std::forward<_Variant>(__v)._M_u); }
 
+  // Gets the _Uninitialized to construct into for __u.
+  template<size_t _Np, typename _Union>
+    constexpr decltype(auto)
+    __construct_n(_Union& __u) noexcept
+    {
+      if constexpr (_Np == 0)
+	return &__u._M_first;
+      else if constexpr (_Np == 1)
+	{
+	  std::_Construct(&__u._M_rest);
+	  return &__u._M_rest._M_first;
+	}
+      else if constexpr (_Np == 2)
+	{
+	  std::_Construct(&__u._M_rest);
+	  std::_Construct(&__u._M_rest._M_rest);
+	  return &__u._M_rest._M_rest._M_first;
+	}
+      else
+	{
+	  std::_Construct(&__u._M_rest);
+	  std::_Construct(&__u._M_rest._M_rest);
+	  std::_Construct(&__u._M_rest._M_rest._M_rest);
+	  return __variant::__construct_n<_Np - 3>(__u._M_rest._M_rest._M_rest);
+	}
+    }
+
   template<typename... _Types>
     struct _Traits
     {
@@ -536,8 +563,9 @@ namespace __variant
     __emplace(_Variant_storage<_Triv, _Types...>& __v, _Args&&... __args)
     {
       __v._M_reset();
-      auto* __addr = std::__addressof(__variant::__get_n<_Np>(__v._M_u));
-      std::_Construct(__addr, std::forward<_Args>(__args)...);
+      auto* __addr = __variant::__construct_n<_Np>(__v._M_u);
+      std::_Construct(__addr, in_place_index<0>,
+		      std::forward<_Args>(__args)...);
       // Construction didn't throw, so can set the new index now:
       __v._M_index = _Np;
     }
Jonathan Wakely Sept. 29, 2023, 9:32 a.m. UTC | #4
On Fri, 29 Sept 2023 at 00:25, Nathaniel Shead
<nathanieloshead@gmail.com> wrote:
>
> On Wed, Sep 27, 2023 at 03:13:35PM +0100, Jonathan Wakely wrote:
> > On Sat, 23 Sept 2023 at 08:30, Nathaniel Shead via Libstdc++
> > <libstdc++@gcc.gnu.org> wrote:
> > >
> > > On Sat, Sep 23, 2023 at 07:40:48AM +0100, Jonathan Wakely wrote:
> > > > On Sat, 23 Sept 2023, 01:39 Nathaniel Shead via Libstdc++, <
> > > > libstdc++@gcc.gnu.org> wrote:
> > > >
> > > > > Now that bootstrap has finished, I have gotten regressions in the
> > > > > following libstdc++ tests:
> > > > >
> > > > > Running libstdc++:libstdc++-dg/conformance.exp ...
> > > > > FAIL: 20_util/bitset/access/constexpr.cc -std=gnu++23 (test for excess
> > > > > errors)
> > > > > FAIL: 20_util/bitset/access/constexpr.cc -std=gnu++26 (test for excess
> > > > > errors)
> > > > > FAIL: 20_util/variant/constexpr.cc -std=gnu++20 (test for excess errors)
> > > > > FAIL: 20_util/variant/constexpr.cc -std=gnu++26 (test for excess errors)
> > > > > FAIL: 21_strings/basic_string/cons/char/constexpr.cc -std=gnu++20 (test
> > > > > for excess errors)
> > > > > FAIL: 21_strings/basic_string/cons/char/constexpr.cc -std=gnu++26 (test
> > > > > for excess errors)
> > > > > FAIL: 21_strings/basic_string/cons/wchar_t/constexpr.cc -std=gnu++20 (test
> > > > > for excess errors)
> > > > > FAIL: 21_strings/basic_string/cons/wchar_t/constexpr.cc -std=gnu++26 (test
> > > > > for excess errors)
> > > > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr-wchar_t.cc
> > > > > -std=gnu++20 (test for excess errors)
> > > > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr-wchar_t.cc
> > > > > -std=gnu++26 (test for excess errors)
> > > > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr.cc -std=gnu++20
> > > > > (test for excess errors)
> > > > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr.cc -std=gnu++26
> > > > > (test for excess errors)
> > > > > FAIL: std/ranges/adaptors/join_with/1.cc -std=gnu++23 (test for excess
> > > > > errors)
> > > > > UNRESOLVED: std/ranges/adaptors/join_with/1.cc -std=gnu++23 compilation
> > > > > failed to produce executable
> > > > > FAIL: std/ranges/adaptors/join_with/1.cc -std=gnu++26 (test for excess
> > > > > errors)
> > > > > UNRESOLVED: std/ranges/adaptors/join_with/1.cc -std=gnu++26 compilation
> > > > > failed to produce executable
> > > > >
> > > > > On investigation though it looks like the issue might be with libstdc++
> > > > > rather than the patch itself; running the failing tests using clang with
> > > > > libstdc++ also produces similar errors, and my reading of the code
> > > > > suggests that this is correct.
> > > > >
> > > > > What's the way forward here? Should I look at creating a patch to fix
> > > > > the libstdc++ issues before resubmitting this patch for the C++
> > > > > frontend? Or should I submit a version of this patch without the
> > > > > `std::construct_at` changes and wait till libstdc++ gets fixed for that?
> > > > >
> > > >
> > > > I think we should fix libstdc++. There are probably only a few places that
> > > > need a fix, which cause all those failures.
> > > >
> > > > I can help with those fixes. I'll look into it after the weekend.
> > > >
> > >
> > > Thanks. I did end up getting a chance to look at it earlier today, and
> > > with the following patch I had no regressions when applying the frontend
> > > changes. Bootstrapped and regtested on x86_64-pc-linux-gnu.
> > >
> > > -- >8 --
> > >
> > > This patch ensures that the union members for std::string and
> > > std::variant are always properly set when a change occurs.
> > >
> > > libstdc++-v3/ChangeLog:
> > >
> > >         * include/bits/basic_string.h: (basic_string(basic_string&&)):
> > >         Activate _M_local_buf when needed.
> > >         (basic_string(basic_string&&, const _Alloc&)): Likewise.
> > >         * include/bits/basic_string.tcc: (basic_string::swap): Likewise.
> > >         * include/std/variant: (__detail::__variant::__construct_n): New.
> > >         (__detail::_variant::__emplace): Use __construct_n.
> > >
> > > Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
> > > ---
> > >  libstdc++-v3/include/bits/basic_string.h   |  7 +++--
> > >  libstdc++-v3/include/bits/basic_string.tcc |  8 +++---
> > >  libstdc++-v3/include/std/variant           | 32 ++++++++++++++++++++--
> > >  3 files changed, 38 insertions(+), 9 deletions(-)
> > >
> > > diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
> > > index 09fd62afa66..7c342879827 100644
> > > --- a/libstdc++-v3/include/bits/basic_string.h
> > > +++ b/libstdc++-v3/include/bits/basic_string.h
> > > @@ -678,7 +678,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> > >        {
> > >         if (__str._M_is_local())
> > >           {
> > > -           traits_type::copy(_M_local_buf, __str._M_local_buf,
> > > +           traits_type::copy(_M_use_local_data(), __str._M_local_buf,
> > >                               __str.length() + 1);
> > >           }
> > >         else
> > > @@ -691,7 +691,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> > >         // basic_stringbuf relies on writing into unallocated capacity so
> > >         // we mess up the contents if we put a '\0' in the string.
> > >         _M_length(__str.length());
> > > -       __str._M_data(__str._M_local_data());
> > > +       __str._M_data(__str._M_use_local_data());
> > >         __str._M_set_length(0);
> > >        }
> > >
> > > @@ -717,6 +717,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> > >        {
> > >         if (__str._M_is_local())
> > >           {
> > > +           _M_use_local_data();
> >
> > Lets add a cast to void to make it clear we're intentionally
> > discarding the return value here, and only calling it for its
> > constexpr "side effects":
> >
> >     (void) _M_use_local_data();
> >
> >
> > >             traits_type::copy(_M_local_buf, __str._M_local_buf,
> > >                               __str.length() + 1);
> > >             _M_length(__str.length());
> > > @@ -728,7 +729,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> > >             _M_data(__str._M_data());
> > >             _M_length(__str.length());
> > >             _M_capacity(__str._M_allocated_capacity);
> > > -           __str._M_data(__str._M_local_buf);
> > > +           __str._M_data(__str._M_use_local_data());
> > >             __str._M_set_length(0);
> > >           }
> > >         else
> > > diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc
> > > index 104a517f794..ee6e57da555 100644
> > > --- a/libstdc++-v3/include/bits/basic_string.tcc
> > > +++ b/libstdc++-v3/include/bits/basic_string.tcc
> > > @@ -79,7 +79,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> > >               }
> > >             else if (__s.length())
> > >               {
> > > -               traits_type::copy(_M_local_buf, __s._M_local_buf,
> > > +               traits_type::copy(_M_use_local_data(), __s._M_local_buf,
> >
> > I think we should call _M_use_local_data() before calling
> > traits_type::copy, as you did for
> > basic_string(basic_string&& __str, const _Alloc& __a)
> > above.
> >
> > The problem is that _M_use_local_data() returns the allocator's
> > pointer type, but traits_type::copy expects char_type*, and those
> > might not be the same type.
> > So:
> >
> > @@ -79,6 +79,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >              }
> >            else if (__s.length())
> >              {
> > +               (void) _M_use_local_data();
> >                traits_type::copy(_M_local_buf, __s._M_local_buf,
> >                                  __s.length() + 1);
> >                _M_length(__s.length());
> >
> >
> >
> > >                                   __s.length() + 1);
> > >                 _M_length(__s.length());
> > >                 __s._M_set_length(0);
> > > @@ -87,7 +87,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> > >               }
> > >             else if (length())
> > >               {
> > > -               traits_type::copy(__s._M_local_buf, _M_local_buf,
> > > +               traits_type::copy(__s._M_use_local_data(), _M_local_buf,
> >
> > Same here, for __s._M_use_local_data()
> >
> > >                                   length() + 1);
> > >                 __s._M_length(length());
> > >                 _M_set_length(0);
> > > @@ -97,7 +97,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> > >         else
> > >           {
> > >             const size_type __tmp_capacity = __s._M_allocated_capacity;
> > > -           traits_type::copy(__s._M_local_buf, _M_local_buf,
> > > +           traits_type::copy(__s._M_use_local_data(), _M_local_buf,
> >
> > And again.
> >
> > >                               length() + 1);
> > >             _M_data(__s._M_data());
> > >             __s._M_data(__s._M_local_buf);
> > > @@ -108,7 +108,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> > >           const size_type __tmp_capacity = _M_allocated_capacity;
> > >           if (__s._M_is_local())
> > >             {
> > > -             traits_type::copy(_M_local_buf, __s._M_local_buf,
> > > +             traits_type::copy(_M_use_local_data(), __s._M_local_buf,
> >
> > And again.
> >
> > >                                 __s.length() + 1);
> > >               __s._M_data(_M_data());
> > >               _M_data(_M_local_buf);
> > > diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant
> > > index c0e41740dcf..7f24e760bb1 100644
> > > --- a/libstdc++-v3/include/std/variant
> > > +++ b/libstdc++-v3/include/std/variant
> > > @@ -320,6 +320,33 @@ namespace __variant
> > >      __get(_Variant&& __v) noexcept
> > >      { return __variant::__get_n<_Np>(std::forward<_Variant>(__v)._M_u); }
> > >
> > > +  // Gets the _Uninitialized to construct into for __u.
> > > +  template<size_t _Np, typename _Union>
> > > +    constexpr decltype(auto)
> > > +    __construct_n(_Union& __u) noexcept
> > > +    {
> > > +      if constexpr (_Np == 0)
> > > +       return &__u._M_first;
> > > +      else if constexpr (_Np == 1)
> > > +       {
> > > +         std::_Construct(&__u._M_rest);
> > > +         return &__u._M_rest._M_first;
> > > +       }
> > > +      else if constexpr (_Np == 2)
> > > +       {
> > > +         std::_Construct(&__u._M_rest);
> > > +         std::_Construct(&__u._M_rest._M_rest);
> > > +         return &__u._M_rest._M_rest._M_first;
> > > +       }
> > > +      else
> > > +       {
> > > +         std::_Construct(&__u._M_rest);
> > > +         std::_Construct(&__u._M_rest._M_rest);
> > > +         std::_Construct(&__u._M_rest._M_rest._M_rest);
> > > +         return __variant::__construct_n<_Np - 3>(__u._M_rest._M_rest._M_rest);
> > > +       }
> > > +    }
> >
> > This is nice, thanks for optimizing it to reduce the recursion depth.
> >
> > > +
> > >    template<typename... _Types>
> > >      struct _Traits
> > >      {
> > > @@ -536,8 +563,9 @@ namespace __variant
> > >      __emplace(_Variant_storage<_Triv, _Types...>& __v, _Args&&... __args)
> > >      {
> > >        __v._M_reset();
> > > -      auto* __addr = std::__addressof(__variant::__get_n<_Np>(__v._M_u));
> > > -      std::_Construct(__addr, std::forward<_Args>(__args)...);
> > > +      auto* __addr = __variant::__construct_n<_Np>(__v._M_u);
> > > +      std::_Construct(__addr, in_place_index<0>,
> > > +                     std::forward<_Args>(__args)...);
> > >        // Construction didn't throw, so can set the new index now:
> > >        __v._M_index = _Np;
> > >      }
> > > --
> > > 2.41.0
> > >
> >
>
> Thanks for the comments, here's an updated version of the patch.
> Bootstrapped and regtested on x86_64-pc-linux-gnu.

Great, I'll get this committed today - thanks!

>
> I'll note that there are some existing calls to `_M_use_local_data()`
> already used only for their side effects without a cast to void, e.g.
>
>       /**
>        *  @brief  Default constructor creates an empty string.
>        */
>       _GLIBCXX20_CONSTEXPR
>       basic_string()
>       _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
>       : _M_dataplus(_M_local_data())
>       {
>         _M_use_local_data();
>         _M_set_length(0);
>       }
>
> I haven't updated these, but should this be changed for consistency?

Yes, good idea. I can do that.

Thanks again for fixing these. I think this might fix some bug reports
about clang rejecting our std::string in constant expressions, so I'll
check those.


>
> -- >8 --
>
> This patch ensures that the union members for std::string and
> std::variant are always properly set when a change occurs.
>
> libstdc++-v3/ChangeLog:
>
>         * include/bits/basic_string.h: (basic_string(basic_string&&)):
>         Activate _M_local_buf when needed.
>         (basic_string(basic_string&&, const _Alloc&)): Likewise.
>         * include/bits/basic_string.tcc: (basic_string::swap): Likewise.
>         * include/std/variant: (__detail::__variant::__construct_n): New.
>         (__detail::__variant::__emplace): Use __construct_n.
>
> Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
> ---
>  libstdc++-v3/include/bits/basic_string.h   |  6 ++--
>  libstdc++-v3/include/bits/basic_string.tcc |  4 +++
>  libstdc++-v3/include/std/variant           | 32 ++++++++++++++++++++--
>  3 files changed, 38 insertions(+), 4 deletions(-)
>
> diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
> index 09fd62afa66..4f94cd967cf 100644
> --- a/libstdc++-v3/include/bits/basic_string.h
> +++ b/libstdc++-v3/include/bits/basic_string.h
> @@ -678,6 +678,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>        {
>         if (__str._M_is_local())
>           {
> +           (void)_M_use_local_data();
>             traits_type::copy(_M_local_buf, __str._M_local_buf,
>                               __str.length() + 1);
>           }
> @@ -691,7 +692,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>         // basic_stringbuf relies on writing into unallocated capacity so
>         // we mess up the contents if we put a '\0' in the string.
>         _M_length(__str.length());
> -       __str._M_data(__str._M_local_data());
> +       __str._M_data(__str._M_use_local_data());
>         __str._M_set_length(0);
>        }
>
> @@ -717,6 +718,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>        {
>         if (__str._M_is_local())
>           {
> +           (void)_M_use_local_data();
>             traits_type::copy(_M_local_buf, __str._M_local_buf,
>                               __str.length() + 1);
>             _M_length(__str.length());
> @@ -728,7 +730,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>             _M_data(__str._M_data());
>             _M_length(__str.length());
>             _M_capacity(__str._M_allocated_capacity);
> -           __str._M_data(__str._M_local_buf);
> +           __str._M_data(__str._M_use_local_data());
>             __str._M_set_length(0);
>           }
>         else
> diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc
> index 104a517f794..4bc98f2aea7 100644
> --- a/libstdc++-v3/include/bits/basic_string.tcc
> +++ b/libstdc++-v3/include/bits/basic_string.tcc
> @@ -79,6 +79,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>               }
>             else if (__s.length())
>               {
> +               (void)_M_use_local_data();
>                 traits_type::copy(_M_local_buf, __s._M_local_buf,
>                                   __s.length() + 1);
>                 _M_length(__s.length());
> @@ -87,6 +88,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>               }
>             else if (length())
>               {
> +               (void)__s._M_use_local_data();
>                 traits_type::copy(__s._M_local_buf, _M_local_buf,
>                                   length() + 1);
>                 __s._M_length(length());
> @@ -97,6 +99,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>         else
>           {
>             const size_type __tmp_capacity = __s._M_allocated_capacity;
> +           (void)__s._M_use_local_data();
>             traits_type::copy(__s._M_local_buf, _M_local_buf,
>                               length() + 1);
>             _M_data(__s._M_data());
> @@ -108,6 +111,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>           const size_type __tmp_capacity = _M_allocated_capacity;
>           if (__s._M_is_local())
>             {
> +             (void)_M_use_local_data();
>               traits_type::copy(_M_local_buf, __s._M_local_buf,
>                                 __s.length() + 1);
>               __s._M_data(_M_data());
> diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant
> index c0e41740dcf..7f24e760bb1 100644
> --- a/libstdc++-v3/include/std/variant
> +++ b/libstdc++-v3/include/std/variant
> @@ -320,6 +320,33 @@ namespace __variant
>      __get(_Variant&& __v) noexcept
>      { return __variant::__get_n<_Np>(std::forward<_Variant>(__v)._M_u); }
>
> +  // Gets the _Uninitialized to construct into for __u.
> +  template<size_t _Np, typename _Union>
> +    constexpr decltype(auto)
> +    __construct_n(_Union& __u) noexcept
> +    {
> +      if constexpr (_Np == 0)
> +       return &__u._M_first;
> +      else if constexpr (_Np == 1)
> +       {
> +         std::_Construct(&__u._M_rest);
> +         return &__u._M_rest._M_first;
> +       }
> +      else if constexpr (_Np == 2)
> +       {
> +         std::_Construct(&__u._M_rest);
> +         std::_Construct(&__u._M_rest._M_rest);
> +         return &__u._M_rest._M_rest._M_first;
> +       }
> +      else
> +       {
> +         std::_Construct(&__u._M_rest);
> +         std::_Construct(&__u._M_rest._M_rest);
> +         std::_Construct(&__u._M_rest._M_rest._M_rest);
> +         return __variant::__construct_n<_Np - 3>(__u._M_rest._M_rest._M_rest);
> +       }
> +    }
> +
>    template<typename... _Types>
>      struct _Traits
>      {
> @@ -536,8 +563,9 @@ namespace __variant
>      __emplace(_Variant_storage<_Triv, _Types...>& __v, _Args&&... __args)
>      {
>        __v._M_reset();
> -      auto* __addr = std::__addressof(__variant::__get_n<_Np>(__v._M_u));
> -      std::_Construct(__addr, std::forward<_Args>(__args)...);
> +      auto* __addr = __variant::__construct_n<_Np>(__v._M_u);
> +      std::_Construct(__addr, in_place_index<0>,
> +                     std::forward<_Args>(__args)...);
>        // Construction didn't throw, so can set the new index now:
>        __v._M_index = _Np;
>      }
> --
> 2.41.0
>
Jonathan Wakely Sept. 29, 2023, 3:06 p.m. UTC | #5
On Fri, 29 Sept 2023 at 10:32, Jonathan Wakely <jwakely@redhat.com> wrote:
> > Thanks for the comments, here's an updated version of the patch.
> > Bootstrapped and regtested on x86_64-pc-linux-gnu.
>
> Great, I'll get this committed today - thanks!

That's done now.

> >
> > I'll note that there are some existing calls to `_M_use_local_data()`
> > already used only for their side effects without a cast to void, e.g.
> >
> >       /**
> >        *  @brief  Default constructor creates an empty string.
> >        */
> >       _GLIBCXX20_CONSTEXPR
> >       basic_string()
> >       _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
> >       : _M_dataplus(_M_local_data())
> >       {
> >         _M_use_local_data();
> >         _M_set_length(0);
> >       }
> >
> > I haven't updated these, but should this be changed for consistency?
>
> Yes, good idea. I can do that.

I started to do that, and decided it made more sense to split out the
constexpr loop from _M_use_local_data() into a separate function,
_M_init_local_buf(). Then we can use that for all the places where we
don't care about the return value. That avoids the overhead of using
pointer_traits::pointer_to when we don't need the return value (which
is admittedly only going to be an issue for -O0 code, but I think it's
cleaner this way anyway).

Please see the attached patch and let me know what you think.


> Thanks again for fixing these. I think this might fix some bug reports
> about clang rejecting our std::string in constant expressions, so I'll
> check those.

Your patch fixes https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110900
(so we should backport it to gcc-13 and gcc-12 too).
commit 2668979d3206ff6c039ac0165aae29377a15666c
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Sep 29 12:12:22 2023

    libstdc++: Split std::basic_string::_M_use_local_data into two functions
    
    This splits out the activate-the-union-member-for-constexpr logic from
    _M_use_local_data, so that it can be used separately in cases that don't
    need to use std::pointer_traits<pointer>::pointer_to to obtain the
    return value.
    
    This leaves only three uses of _M_use_local_data() which are all the
    same form:
    
      __s._M_data(_M_use_local_data());
      __s._M_set_length(0);
    
    We could remove _M_use_local_data() and change those three places to use
    a new _M_reset() function that does:
    
      _M_init_local_buf();
      _M_data(_M_local_data());
      _M_set_length(0);
    
    This is left for a future change.
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/basic_string.h (_M_init_local_buf()): New
            function.
            (_M_use_local_data()): Use _M_init_local_buf.
            (basic_string(), basic_string(const Alloc&))
            (basic_string(basic_string&&))
            (basic_string(basic_string&&, const Alloc&)): Use
            _M_init_local_buf instead of _M_use_local_data().
            * include/bits/basic_string.tcc (swap(basic_string&))
            (_M_construct(InIter, InIter, forward_iterator_tag))
            (_M_construct(size_type, CharT), reserve()): Likewise.
            (_M_construct(InIter, InIter, input_iterator_tag)): Remove call
            to _M_use_local_data() and initialize the local buffer directly.

diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
index 4f94cd967cf..18a19b8dcbc 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -353,13 +353,23 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       // Ensure that _M_local_buf is the active member of the union.
       __attribute__((__always_inline__))
       _GLIBCXX14_CONSTEXPR
-      pointer
-      _M_use_local_data() _GLIBCXX_NOEXCEPT
+      void
+      _M_init_local_buf() _GLIBCXX_NOEXCEPT
       {
 #if __cpp_lib_is_constant_evaluated
 	if (std::is_constant_evaluated())
 	  for (size_type __i = 0; __i <= _S_local_capacity; ++__i)
 	    _M_local_buf[__i] = _CharT();
+#endif
+      }
+
+      __attribute__((__always_inline__))
+      _GLIBCXX14_CONSTEXPR
+      pointer
+      _M_use_local_data() _GLIBCXX_NOEXCEPT
+      {
+#if __glibcxx_is_constant_evaluated
+	_M_init_local_buf();
 #endif
 	return _M_local_data();
       }
@@ -522,7 +532,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
       : _M_dataplus(_M_local_data())
       {
-	_M_use_local_data();
+	_M_init_local_buf();
 	_M_set_length(0);
       }
 
@@ -534,7 +544,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
       : _M_dataplus(_M_local_data(), __a)
       {
-	_M_use_local_data();
+	_M_init_local_buf();
 	_M_set_length(0);
       }
 
@@ -678,7 +688,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       {
 	if (__str._M_is_local())
 	  {
-	    (void)_M_use_local_data();
+	    _M_init_local_buf();
 	    traits_type::copy(_M_local_buf, __str._M_local_buf,
 			      __str.length() + 1);
 	  }
@@ -718,7 +728,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       {
 	if (__str._M_is_local())
 	  {
-	    (void)_M_use_local_data();
+	    _M_init_local_buf();
 	    traits_type::copy(_M_local_buf, __str._M_local_buf,
 			      __str.length() + 1);
 	    _M_length(__str.length());
diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc
index 4bc98f2aea7..052eeb9e846 100644
--- a/libstdc++-v3/include/bits/basic_string.tcc
+++ b/libstdc++-v3/include/bits/basic_string.tcc
@@ -79,7 +79,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	      }
 	    else if (__s.length())
 	      {
-		(void)_M_use_local_data();
+		_M_init_local_buf();
 		traits_type::copy(_M_local_buf, __s._M_local_buf,
 				  __s.length() + 1);
 		_M_length(__s.length());
@@ -88,7 +88,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	      }
 	    else if (length())
 	      {
-		(void)__s._M_use_local_data();
+		__s._M_init_local_buf();
 		traits_type::copy(__s._M_local_buf, _M_local_buf,
 				  length() + 1);
 		__s._M_length(length());
@@ -99,7 +99,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	else
 	  {
 	    const size_type __tmp_capacity = __s._M_allocated_capacity;
-	    (void)__s._M_use_local_data();
+	    __s._M_init_local_buf();
 	    traits_type::copy(__s._M_local_buf, _M_local_buf,
 			      length() + 1);
 	    _M_data(__s._M_data());
@@ -111,7 +111,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  const size_type __tmp_capacity = _M_allocated_capacity;
 	  if (__s._M_is_local())
 	    {
-	      (void)_M_use_local_data();
+	      _M_init_local_buf();
 	      traits_type::copy(_M_local_buf, __s._M_local_buf,
 				__s.length() + 1);
 	      __s._M_data(_M_data());
@@ -174,14 +174,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	size_type __len = 0;
 	size_type __capacity = size_type(_S_local_capacity);
 
-	pointer __p = _M_use_local_data();
-
 	while (__beg != __end && __len < __capacity)
 	  {
-	    __p[__len++] = *__beg;
+	    _M_local_buf[__len++] = *__beg;
 	    ++__beg;
 	  }
 
+#if __glibcxx_is_constant_evaluated
+	if (std::is_constant_evaluated())
+	  for (size_type __i = __len; __i <= __capacity; ++__i)
+	    _M_local_buf[__i] = _CharT();
+#endif
+
 	struct _Guard
 	{
 	  _GLIBCXX20_CONSTEXPR
@@ -230,7 +234,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	    _M_capacity(__dnew);
 	  }
 	else
-	  _M_use_local_data();
+	  _M_init_local_buf();
 
 	// Check for out_of_range and length_error exceptions.
 	struct _Guard
@@ -263,7 +267,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  _M_capacity(__n);
 	}
       else
-	_M_use_local_data();
+	_M_init_local_buf();
 
       if (__n)
 	this->_S_assign(_M_data(), __n, __c);
@@ -372,7 +376,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       if (__length <= size_type(_S_local_capacity))
 	{
-	  this->_S_copy(_M_use_local_data(), _M_data(), __length + 1);
+	  _M_init_local_buf();
+	  this->_S_copy(_M_local_buf, _M_data(), __length + 1);
 	  _M_destroy(__capacity);
 	  _M_data(_M_local_data());
 	}
Nathaniel Shead Sept. 29, 2023, 4:29 p.m. UTC | #6
On Fri, Sep 29, 2023 at 04:06:33PM +0100, Jonathan Wakely wrote:
> On Fri, 29 Sept 2023 at 10:32, Jonathan Wakely <jwakely@redhat.com> wrote:
> > > Thanks for the comments, here's an updated version of the patch.
> > > Bootstrapped and regtested on x86_64-pc-linux-gnu.
> >
> > Great, I'll get this committed today - thanks!
> 
> That's done now.
> 

Thanks!

> > >
> > > I'll note that there are some existing calls to `_M_use_local_data()`
> > > already used only for their side effects without a cast to void, e.g.
> > >
> > >       /**
> > >        *  @brief  Default constructor creates an empty string.
> > >        */
> > >       _GLIBCXX20_CONSTEXPR
> > >       basic_string()
> > >       _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
> > >       : _M_dataplus(_M_local_data())
> > >       {
> > >         _M_use_local_data();
> > >         _M_set_length(0);
> > >       }
> > >
> > > I haven't updated these, but should this be changed for consistency?
> >
> > Yes, good idea. I can do that.
> 
> I started to do that, and decided it made more sense to split out the
> constexpr loop from _M_use_local_data() into a separate function,
> _M_init_local_buf(). Then we can use that for all the places where we
> don't care about the return value. That avoids the overhead of using
> pointer_traits::pointer_to when we don't need the return value (which
> is admittedly only going to be an issue for -O0 code, but I think it's
> cleaner this way anyway).
> 
> Please see the attached patch and let me know what you think.

I agree, and it also looks clearer to me what is happening.

> 
> > Thanks again for fixing these. I think this might fix some bug reports
> > about clang rejecting our std::string in constant expressions, so I'll
> > check those.
> 
> Your patch fixes https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110900
> (so we should backport it to gcc-13 and gcc-12 too).

> commit 2668979d3206ff6c039ac0165aae29377a15666c
> Author: Jonathan Wakely <jwakely@redhat.com>
> Date:   Fri Sep 29 12:12:22 2023
> 
>     libstdc++: Split std::basic_string::_M_use_local_data into two functions
>     
>     This splits out the activate-the-union-member-for-constexpr logic from
>     _M_use_local_data, so that it can be used separately in cases that don't
>     need to use std::pointer_traits<pointer>::pointer_to to obtain the
>     return value.
>     
>     This leaves only three uses of _M_use_local_data() which are all the
>     same form:
>     
>       __s._M_data(_M_use_local_data());
>       __s._M_set_length(0);
>     
>     We could remove _M_use_local_data() and change those three places to use
>     a new _M_reset() function that does:
>     
>       _M_init_local_buf();
>       _M_data(_M_local_data());
>       _M_set_length(0);
>     
>     This is left for a future change.
>     
>     libstdc++-v3/ChangeLog:
>     
>             * include/bits/basic_string.h (_M_init_local_buf()): New
>             function.
>             (_M_use_local_data()): Use _M_init_local_buf.
>             (basic_string(), basic_string(const Alloc&))
>             (basic_string(basic_string&&))
>             (basic_string(basic_string&&, const Alloc&)): Use
>             _M_init_local_buf instead of _M_use_local_data().
>             * include/bits/basic_string.tcc (swap(basic_string&))
>             (_M_construct(InIter, InIter, forward_iterator_tag))
>             (_M_construct(size_type, CharT), reserve()): Likewise.
>             (_M_construct(InIter, InIter, input_iterator_tag)): Remove call
>             to _M_use_local_data() and initialize the local buffer directly.
> 
> diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
> index 4f94cd967cf..18a19b8dcbc 100644
> --- a/libstdc++-v3/include/bits/basic_string.h
> +++ b/libstdc++-v3/include/bits/basic_string.h
> @@ -353,13 +353,23 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>        // Ensure that _M_local_buf is the active member of the union.
>        __attribute__((__always_inline__))
>        _GLIBCXX14_CONSTEXPR
> -      pointer
> -      _M_use_local_data() _GLIBCXX_NOEXCEPT
> +      void
> +      _M_init_local_buf() _GLIBCXX_NOEXCEPT
>        {
>  #if __cpp_lib_is_constant_evaluated
>  	if (std::is_constant_evaluated())
>  	  for (size_type __i = 0; __i <= _S_local_capacity; ++__i)
>  	    _M_local_buf[__i] = _CharT();
> +#endif
> +      }
> +
> +      __attribute__((__always_inline__))
> +      _GLIBCXX14_CONSTEXPR
> +      pointer
> +      _M_use_local_data() _GLIBCXX_NOEXCEPT
> +      {
> +#if __glibcxx_is_constant_evaluated
> +	_M_init_local_buf();
>  #endif
>  	return _M_local_data();
>        }

What's the difference between __cpp_lib_is_constant_evaluated and
__glibcxx_is_constant_evaluated? Should these lines be using the same
macro here?

> @@ -522,7 +532,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>        _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
>        : _M_dataplus(_M_local_data())
>        {
> -	_M_use_local_data();
> +	_M_init_local_buf();
>  	_M_set_length(0);
>        }
>  
> @@ -534,7 +544,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>        basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
>        : _M_dataplus(_M_local_data(), __a)
>        {
> -	_M_use_local_data();
> +	_M_init_local_buf();
>  	_M_set_length(0);
>        }
>  
> @@ -678,7 +688,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>        {
>  	if (__str._M_is_local())
>  	  {
> -	    (void)_M_use_local_data();
> +	    _M_init_local_buf();
>  	    traits_type::copy(_M_local_buf, __str._M_local_buf,
>  			      __str.length() + 1);
>  	  }
> @@ -718,7 +728,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>        {
>  	if (__str._M_is_local())
>  	  {
> -	    (void)_M_use_local_data();
> +	    _M_init_local_buf();
>  	    traits_type::copy(_M_local_buf, __str._M_local_buf,
>  			      __str.length() + 1);
>  	    _M_length(__str.length());
> diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc
> index 4bc98f2aea7..052eeb9e846 100644
> --- a/libstdc++-v3/include/bits/basic_string.tcc
> +++ b/libstdc++-v3/include/bits/basic_string.tcc
> @@ -79,7 +79,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>  	      }
>  	    else if (__s.length())
>  	      {
> -		(void)_M_use_local_data();
> +		_M_init_local_buf();
>  		traits_type::copy(_M_local_buf, __s._M_local_buf,
>  				  __s.length() + 1);
>  		_M_length(__s.length());
> @@ -88,7 +88,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>  	      }
>  	    else if (length())
>  	      {
> -		(void)__s._M_use_local_data();
> +		__s._M_init_local_buf();
>  		traits_type::copy(__s._M_local_buf, _M_local_buf,
>  				  length() + 1);
>  		__s._M_length(length());
> @@ -99,7 +99,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>  	else
>  	  {
>  	    const size_type __tmp_capacity = __s._M_allocated_capacity;
> -	    (void)__s._M_use_local_data();
> +	    __s._M_init_local_buf();
>  	    traits_type::copy(__s._M_local_buf, _M_local_buf,
>  			      length() + 1);
>  	    _M_data(__s._M_data());
> @@ -111,7 +111,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>  	  const size_type __tmp_capacity = _M_allocated_capacity;
>  	  if (__s._M_is_local())
>  	    {
> -	      (void)_M_use_local_data();
> +	      _M_init_local_buf();
>  	      traits_type::copy(_M_local_buf, __s._M_local_buf,
>  				__s.length() + 1);
>  	      __s._M_data(_M_data());
> @@ -174,14 +174,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>  	size_type __len = 0;
>  	size_type __capacity = size_type(_S_local_capacity);
>  
> -	pointer __p = _M_use_local_data();
> -
>  	while (__beg != __end && __len < __capacity)
>  	  {
> -	    __p[__len++] = *__beg;
> +	    _M_local_buf[__len++] = *__beg;
>  	    ++__beg;
>  	  }
>  
> +#if __glibcxx_is_constant_evaluated
> +	if (std::is_constant_evaluated())
> +	  for (size_type __i = __len; __i <= __capacity; ++__i)
> +	    _M_local_buf[__i] = _CharT();
> +#endif
> +

I wonder if maybe this should still be a call to `_M_init_local_buf()`
above, where the `_M_use_local_data()` used to be? That way the logic
stays in one place, and I don't imagine the compile time savings of not
immediately overwriting the first __len characters would be significant.

>  	struct _Guard
>  	{
>  	  _GLIBCXX20_CONSTEXPR
> @@ -230,7 +234,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>  	    _M_capacity(__dnew);
>  	  }
>  	else
> -	  _M_use_local_data();
> +	  _M_init_local_buf();
>  
>  	// Check for out_of_range and length_error exceptions.
>  	struct _Guard
> @@ -263,7 +267,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>  	  _M_capacity(__n);
>  	}
>        else
> -	_M_use_local_data();
> +	_M_init_local_buf();
>  
>        if (__n)
>  	this->_S_assign(_M_data(), __n, __c);
> @@ -372,7 +376,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>  
>        if (__length <= size_type(_S_local_capacity))
>  	{
> -	  this->_S_copy(_M_use_local_data(), _M_data(), __length + 1);
> +	  _M_init_local_buf();
> +	  this->_S_copy(_M_local_buf, _M_data(), __length + 1);
>  	  _M_destroy(__capacity);
>  	  _M_data(_M_local_data());
>  	}

But looks good to me; I tried applying this on top of my frontend patch
and it looks like everything still works correctly.
Jonathan Wakely Sept. 29, 2023, 4:46 p.m. UTC | #7
On Fri, 29 Sept 2023 at 17:29, Nathaniel Shead
<nathanieloshead@gmail.com> wrote:
>
> On Fri, Sep 29, 2023 at 04:06:33PM +0100, Jonathan Wakely wrote:
> > On Fri, 29 Sept 2023 at 10:32, Jonathan Wakely <jwakely@redhat.com> wrote:
> > > > Thanks for the comments, here's an updated version of the patch.
> > > > Bootstrapped and regtested on x86_64-pc-linux-gnu.
> > >
> > > Great, I'll get this committed today - thanks!
> >
> > That's done now.
> >
>
> Thanks!
>
> > > >
> > > > I'll note that there are some existing calls to `_M_use_local_data()`
> > > > already used only for their side effects without a cast to void, e.g.
> > > >
> > > >       /**
> > > >        *  @brief  Default constructor creates an empty string.
> > > >        */
> > > >       _GLIBCXX20_CONSTEXPR
> > > >       basic_string()
> > > >       _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
> > > >       : _M_dataplus(_M_local_data())
> > > >       {
> > > >         _M_use_local_data();
> > > >         _M_set_length(0);
> > > >       }
> > > >
> > > > I haven't updated these, but should this be changed for consistency?
> > >
> > > Yes, good idea. I can do that.
> >
> > I started to do that, and decided it made more sense to split out the
> > constexpr loop from _M_use_local_data() into a separate function,
> > _M_init_local_buf(). Then we can use that for all the places where we
> > don't care about the return value. That avoids the overhead of using
> > pointer_traits::pointer_to when we don't need the return value (which
> > is admittedly only going to be an issue for -O0 code, but I think it's
> > cleaner this way anyway).
> >
> > Please see the attached patch and let me know what you think.
>
> I agree, and it also looks clearer to me what is happening.

Good, I'll make this change next week then.


>
> >
> > > Thanks again for fixing these. I think this might fix some bug reports
> > > about clang rejecting our std::string in constant expressions, so I'll
> > > check those.
> >
> > Your patch fixes https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110900
> > (so we should backport it to gcc-13 and gcc-12 too).
>
> > commit 2668979d3206ff6c039ac0165aae29377a15666c
> > Author: Jonathan Wakely <jwakely@redhat.com>
> > Date:   Fri Sep 29 12:12:22 2023
> >
> >     libstdc++: Split std::basic_string::_M_use_local_data into two functions
> >
> >     This splits out the activate-the-union-member-for-constexpr logic from
> >     _M_use_local_data, so that it can be used separately in cases that don't
> >     need to use std::pointer_traits<pointer>::pointer_to to obtain the
> >     return value.
> >
> >     This leaves only three uses of _M_use_local_data() which are all the
> >     same form:
> >
> >       __s._M_data(_M_use_local_data());
> >       __s._M_set_length(0);
> >
> >     We could remove _M_use_local_data() and change those three places to use
> >     a new _M_reset() function that does:
> >
> >       _M_init_local_buf();
> >       _M_data(_M_local_data());
> >       _M_set_length(0);
> >
> >     This is left for a future change.
> >
> >     libstdc++-v3/ChangeLog:
> >
> >             * include/bits/basic_string.h (_M_init_local_buf()): New
> >             function.
> >             (_M_use_local_data()): Use _M_init_local_buf.
> >             (basic_string(), basic_string(const Alloc&))
> >             (basic_string(basic_string&&))
> >             (basic_string(basic_string&&, const Alloc&)): Use
> >             _M_init_local_buf instead of _M_use_local_data().
> >             * include/bits/basic_string.tcc (swap(basic_string&))
> >             (_M_construct(InIter, InIter, forward_iterator_tag))
> >             (_M_construct(size_type, CharT), reserve()): Likewise.
> >             (_M_construct(InIter, InIter, input_iterator_tag)): Remove call
> >             to _M_use_local_data() and initialize the local buffer directly.
> >
> > diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
> > index 4f94cd967cf..18a19b8dcbc 100644
> > --- a/libstdc++-v3/include/bits/basic_string.h
> > +++ b/libstdc++-v3/include/bits/basic_string.h
> > @@ -353,13 +353,23 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> >        // Ensure that _M_local_buf is the active member of the union.
> >        __attribute__((__always_inline__))
> >        _GLIBCXX14_CONSTEXPR
> > -      pointer
> > -      _M_use_local_data() _GLIBCXX_NOEXCEPT
> > +      void
> > +      _M_init_local_buf() _GLIBCXX_NOEXCEPT
> >        {
> >  #if __cpp_lib_is_constant_evaluated
> >       if (std::is_constant_evaluated())
> >         for (size_type __i = 0; __i <= _S_local_capacity; ++__i)
> >           _M_local_buf[__i] = _CharT();
> > +#endif
> > +      }
> > +
> > +      __attribute__((__always_inline__))
> > +      _GLIBCXX14_CONSTEXPR
> > +      pointer
> > +      _M_use_local_data() _GLIBCXX_NOEXCEPT
> > +      {
> > +#if __glibcxx_is_constant_evaluated
> > +     _M_init_local_buf();
> >  #endif
> >       return _M_local_data();
> >        }
>
> What's the difference between __cpp_lib_is_constant_evaluated and
> __glibcxx_is_constant_evaluated? Should these lines be using the same
> macro here?

Ah, yeah, they could be the same.

The difference is that (for most feature test macros) the
__glibcxx_ftm form is defined after including <bits/version.h>, but
the __cpp_lib_ftm form is only defined only by headers that do:

#define __glibcxx_want_ftm
#include <bits/version.h>

This means we can ensure that the __cpp_lib_ftm form is only defined
by headers where we actually want to define it, e.g. in the headers
that [version.syn] in the standard says should define the macro. So
for our own internal uses, we should generally rely on the
__glibcxx_ftm one. Users should rely on __cpp_lib_ftm after including
the correct header. For example, __glibcxx_atomic_wait is defined
after including <bits/atomic_wait.h> and so is available for our own
uses, but __cpp_lib_atomic_wait is only defined after including
<atomic>, not just <bits/atomic_wait.h>. Or at least, that's the plan
- I have a pending patch to make everything I just said true :-)
Currently we "leak" the __cpp_lib_ftm forms into lots of <bits/xxx.h>
internal headers. That will change next week.

In this specific case, it doesn't matter, because
__cpp_lib_is_constant_evaluated is defined by <type_traits>, and every
header includes that. So it doesn't really matter whether our internal
uses are __cpp_lib_is_constant_evaluated or
__glibcxx_is_constant_evaluated. But it would be good to be
consistent.

>
> > @@ -522,7 +532,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> >        _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
> >        : _M_dataplus(_M_local_data())
> >        {
> > -     _M_use_local_data();
> > +     _M_init_local_buf();
> >       _M_set_length(0);
> >        }
> >
> > @@ -534,7 +544,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> >        basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
> >        : _M_dataplus(_M_local_data(), __a)
> >        {
> > -     _M_use_local_data();
> > +     _M_init_local_buf();
> >       _M_set_length(0);
> >        }
> >
> > @@ -678,7 +688,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> >        {
> >       if (__str._M_is_local())
> >         {
> > -         (void)_M_use_local_data();
> > +         _M_init_local_buf();
> >           traits_type::copy(_M_local_buf, __str._M_local_buf,
> >                             __str.length() + 1);
> >         }
> > @@ -718,7 +728,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> >        {
> >       if (__str._M_is_local())
> >         {
> > -         (void)_M_use_local_data();
> > +         _M_init_local_buf();
> >           traits_type::copy(_M_local_buf, __str._M_local_buf,
> >                             __str.length() + 1);
> >           _M_length(__str.length());
> > diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc
> > index 4bc98f2aea7..052eeb9e846 100644
> > --- a/libstdc++-v3/include/bits/basic_string.tcc
> > +++ b/libstdc++-v3/include/bits/basic_string.tcc
> > @@ -79,7 +79,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >             }
> >           else if (__s.length())
> >             {
> > -             (void)_M_use_local_data();
> > +             _M_init_local_buf();
> >               traits_type::copy(_M_local_buf, __s._M_local_buf,
> >                                 __s.length() + 1);
> >               _M_length(__s.length());
> > @@ -88,7 +88,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >             }
> >           else if (length())
> >             {
> > -             (void)__s._M_use_local_data();
> > +             __s._M_init_local_buf();
> >               traits_type::copy(__s._M_local_buf, _M_local_buf,
> >                                 length() + 1);
> >               __s._M_length(length());
> > @@ -99,7 +99,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >       else
> >         {
> >           const size_type __tmp_capacity = __s._M_allocated_capacity;
> > -         (void)__s._M_use_local_data();
> > +         __s._M_init_local_buf();
> >           traits_type::copy(__s._M_local_buf, _M_local_buf,
> >                             length() + 1);
> >           _M_data(__s._M_data());
> > @@ -111,7 +111,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >         const size_type __tmp_capacity = _M_allocated_capacity;
> >         if (__s._M_is_local())
> >           {
> > -           (void)_M_use_local_data();
> > +           _M_init_local_buf();
> >             traits_type::copy(_M_local_buf, __s._M_local_buf,
> >                               __s.length() + 1);
> >             __s._M_data(_M_data());
> > @@ -174,14 +174,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >       size_type __len = 0;
> >       size_type __capacity = size_type(_S_local_capacity);
> >
> > -     pointer __p = _M_use_local_data();
> > -
> >       while (__beg != __end && __len < __capacity)
> >         {
> > -         __p[__len++] = *__beg;
> > +         _M_local_buf[__len++] = *__beg;
> >           ++__beg;
> >         }
> >
> > +#if __glibcxx_is_constant_evaluated
> > +     if (std::is_constant_evaluated())
> > +       for (size_type __i = __len; __i <= __capacity; ++__i)
> > +         _M_local_buf[__i] = _CharT();
> > +#endif
> > +
>
> I wonder if maybe this should still be a call to `_M_init_local_buf()`
> above, where the `_M_use_local_data()` used to be? That way the logic
> stays in one place, and I don't imagine the compile time savings of not
> immediately overwriting the first __len characters would be significant.


Yeah, I went back and forth on that, but you're probably right. I'll
do as you suggested.
Jonathan Wakely Oct. 21, 2023, 2:45 p.m. UTC | #8
On Fri, 29 Sept 2023 at 17:46, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>
> On Fri, 29 Sept 2023 at 17:29, Nathaniel Shead
> <nathanieloshead@gmail.com> wrote:
> >
> > On Fri, Sep 29, 2023 at 04:06:33PM +0100, Jonathan Wakely wrote:
> > > On Fri, 29 Sept 2023 at 10:32, Jonathan Wakely <jwakely@redhat.com> wrote:
> > > > > Thanks for the comments, here's an updated version of the patch.
> > > > > Bootstrapped and regtested on x86_64-pc-linux-gnu.
> > > >
> > > > Great, I'll get this committed today - thanks!
> > >
> > > That's done now.
> > >
> >
> > Thanks!
> >
> > > > >
> > > > > I'll note that there are some existing calls to `_M_use_local_data()`
> > > > > already used only for their side effects without a cast to void, e.g.
> > > > >
> > > > >       /**
> > > > >        *  @brief  Default constructor creates an empty string.
> > > > >        */
> > > > >       _GLIBCXX20_CONSTEXPR
> > > > >       basic_string()
> > > > >       _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
> > > > >       : _M_dataplus(_M_local_data())
> > > > >       {
> > > > >         _M_use_local_data();
> > > > >         _M_set_length(0);
> > > > >       }
> > > > >
> > > > > I haven't updated these, but should this be changed for consistency?
> > > >
> > > > Yes, good idea. I can do that.
> > >
> > > I started to do that, and decided it made more sense to split out the
> > > constexpr loop from _M_use_local_data() into a separate function,
> > > _M_init_local_buf(). Then we can use that for all the places where we
> > > don't care about the return value. That avoids the overhead of using
> > > pointer_traits::pointer_to when we don't need the return value (which
> > > is admittedly only going to be an issue for -O0 code, but I think it's
> > > cleaner this way anyway).
> > >
> > > Please see the attached patch and let me know what you think.
> >
> > I agree, and it also looks clearer to me what is happening.
>
> Good, I'll make this change next week then.
>
>
> >
> > >
> > > > Thanks again for fixing these. I think this might fix some bug reports
> > > > about clang rejecting our std::string in constant expressions, so I'll
> > > > check those.
> > >
> > > Your patch fixes https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110900
> > > (so we should backport it to gcc-13 and gcc-12 too).
> >
> > > commit 2668979d3206ff6c039ac0165aae29377a15666c
> > > Author: Jonathan Wakely <jwakely@redhat.com>
> > > Date:   Fri Sep 29 12:12:22 2023
> > >
> > >     libstdc++: Split std::basic_string::_M_use_local_data into two functions
> > >
> > >     This splits out the activate-the-union-member-for-constexpr logic from
> > >     _M_use_local_data, so that it can be used separately in cases that don't
> > >     need to use std::pointer_traits<pointer>::pointer_to to obtain the
> > >     return value.
> > >
> > >     This leaves only three uses of _M_use_local_data() which are all the
> > >     same form:
> > >
> > >       __s._M_data(_M_use_local_data());
> > >       __s._M_set_length(0);
> > >
> > >     We could remove _M_use_local_data() and change those three places to use
> > >     a new _M_reset() function that does:
> > >
> > >       _M_init_local_buf();
> > >       _M_data(_M_local_data());
> > >       _M_set_length(0);
> > >
> > >     This is left for a future change.
> > >
> > >     libstdc++-v3/ChangeLog:
> > >
> > >             * include/bits/basic_string.h (_M_init_local_buf()): New
> > >             function.
> > >             (_M_use_local_data()): Use _M_init_local_buf.
> > >             (basic_string(), basic_string(const Alloc&))
> > >             (basic_string(basic_string&&))
> > >             (basic_string(basic_string&&, const Alloc&)): Use
> > >             _M_init_local_buf instead of _M_use_local_data().
> > >             * include/bits/basic_string.tcc (swap(basic_string&))
> > >             (_M_construct(InIter, InIter, forward_iterator_tag))
> > >             (_M_construct(size_type, CharT), reserve()): Likewise.
> > >             (_M_construct(InIter, InIter, input_iterator_tag)): Remove call
> > >             to _M_use_local_data() and initialize the local buffer directly.
> > >
> > > diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
> > > index 4f94cd967cf..18a19b8dcbc 100644
> > > --- a/libstdc++-v3/include/bits/basic_string.h
> > > +++ b/libstdc++-v3/include/bits/basic_string.h
> > > @@ -353,13 +353,23 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> > >        // Ensure that _M_local_buf is the active member of the union.
> > >        __attribute__((__always_inline__))
> > >        _GLIBCXX14_CONSTEXPR
> > > -      pointer
> > > -      _M_use_local_data() _GLIBCXX_NOEXCEPT
> > > +      void
> > > +      _M_init_local_buf() _GLIBCXX_NOEXCEPT
> > >        {
> > >  #if __cpp_lib_is_constant_evaluated
> > >       if (std::is_constant_evaluated())
> > >         for (size_type __i = 0; __i <= _S_local_capacity; ++__i)
> > >           _M_local_buf[__i] = _CharT();
> > > +#endif
> > > +      }
> > > +
> > > +      __attribute__((__always_inline__))
> > > +      _GLIBCXX14_CONSTEXPR
> > > +      pointer
> > > +      _M_use_local_data() _GLIBCXX_NOEXCEPT
> > > +      {
> > > +#if __glibcxx_is_constant_evaluated
> > > +     _M_init_local_buf();
> > >  #endif
> > >       return _M_local_data();
> > >        }
> >
> > What's the difference between __cpp_lib_is_constant_evaluated and
> > __glibcxx_is_constant_evaluated? Should these lines be using the same
> > macro here?
>
> Ah, yeah, they could be the same.
>
> The difference is that (for most feature test macros) the
> __glibcxx_ftm form is defined after including <bits/version.h>, but
> the __cpp_lib_ftm form is only defined only by headers that do:
>
> #define __glibcxx_want_ftm
> #include <bits/version.h>
>
> This means we can ensure that the __cpp_lib_ftm form is only defined
> by headers where we actually want to define it, e.g. in the headers
> that [version.syn] in the standard says should define the macro. So
> for our own internal uses, we should generally rely on the
> __glibcxx_ftm one. Users should rely on __cpp_lib_ftm after including
> the correct header. For example, __glibcxx_atomic_wait is defined
> after including <bits/atomic_wait.h> and so is available for our own
> uses, but __cpp_lib_atomic_wait is only defined after including
> <atomic>, not just <bits/atomic_wait.h>. Or at least, that's the plan
> - I have a pending patch to make everything I just said true :-)
> Currently we "leak" the __cpp_lib_ftm forms into lots of <bits/xxx.h>
> internal headers. That will change next week.
>
> In this specific case, it doesn't matter, because
> __cpp_lib_is_constant_evaluated is defined by <type_traits>, and every
> header includes that. So it doesn't really matter whether our internal
> uses are __cpp_lib_is_constant_evaluated or
> __glibcxx_is_constant_evaluated. But it would be good to be
> consistent.
>
> >
> > > @@ -522,7 +532,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> > >        _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
> > >        : _M_dataplus(_M_local_data())
> > >        {
> > > -     _M_use_local_data();
> > > +     _M_init_local_buf();
> > >       _M_set_length(0);
> > >        }
> > >
> > > @@ -534,7 +544,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> > >        basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
> > >        : _M_dataplus(_M_local_data(), __a)
> > >        {
> > > -     _M_use_local_data();
> > > +     _M_init_local_buf();
> > >       _M_set_length(0);
> > >        }
> > >
> > > @@ -678,7 +688,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> > >        {
> > >       if (__str._M_is_local())
> > >         {
> > > -         (void)_M_use_local_data();
> > > +         _M_init_local_buf();
> > >           traits_type::copy(_M_local_buf, __str._M_local_buf,
> > >                             __str.length() + 1);
> > >         }
> > > @@ -718,7 +728,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> > >        {
> > >       if (__str._M_is_local())
> > >         {
> > > -         (void)_M_use_local_data();
> > > +         _M_init_local_buf();
> > >           traits_type::copy(_M_local_buf, __str._M_local_buf,
> > >                             __str.length() + 1);
> > >           _M_length(__str.length());
> > > diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc
> > > index 4bc98f2aea7..052eeb9e846 100644
> > > --- a/libstdc++-v3/include/bits/basic_string.tcc
> > > +++ b/libstdc++-v3/include/bits/basic_string.tcc
> > > @@ -79,7 +79,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> > >             }
> > >           else if (__s.length())
> > >             {
> > > -             (void)_M_use_local_data();
> > > +             _M_init_local_buf();
> > >               traits_type::copy(_M_local_buf, __s._M_local_buf,
> > >                                 __s.length() + 1);
> > >               _M_length(__s.length());
> > > @@ -88,7 +88,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> > >             }
> > >           else if (length())
> > >             {
> > > -             (void)__s._M_use_local_data();
> > > +             __s._M_init_local_buf();
> > >               traits_type::copy(__s._M_local_buf, _M_local_buf,
> > >                                 length() + 1);
> > >               __s._M_length(length());
> > > @@ -99,7 +99,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> > >       else
> > >         {
> > >           const size_type __tmp_capacity = __s._M_allocated_capacity;
> > > -         (void)__s._M_use_local_data();
> > > +         __s._M_init_local_buf();
> > >           traits_type::copy(__s._M_local_buf, _M_local_buf,
> > >                             length() + 1);
> > >           _M_data(__s._M_data());
> > > @@ -111,7 +111,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> > >         const size_type __tmp_capacity = _M_allocated_capacity;
> > >         if (__s._M_is_local())
> > >           {
> > > -           (void)_M_use_local_data();
> > > +           _M_init_local_buf();
> > >             traits_type::copy(_M_local_buf, __s._M_local_buf,
> > >                               __s.length() + 1);
> > >             __s._M_data(_M_data());
> > > @@ -174,14 +174,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> > >       size_type __len = 0;
> > >       size_type __capacity = size_type(_S_local_capacity);
> > >
> > > -     pointer __p = _M_use_local_data();
> > > -
> > >       while (__beg != __end && __len < __capacity)
> > >         {
> > > -         __p[__len++] = *__beg;
> > > +         _M_local_buf[__len++] = *__beg;
> > >           ++__beg;
> > >         }
> > >
> > > +#if __glibcxx_is_constant_evaluated
> > > +     if (std::is_constant_evaluated())
> > > +       for (size_type __i = __len; __i <= __capacity; ++__i)
> > > +         _M_local_buf[__i] = _CharT();
> > > +#endif
> > > +
> >
> > I wonder if maybe this should still be a call to `_M_init_local_buf()`
> > above, where the `_M_use_local_data()` used to be? That way the logic
> > stays in one place, and I don't imagine the compile time savings of not
> > immediately overwriting the first __len characters would be significant.
>
>
> Yeah, I went back and forth on that, but you're probably right. I'll
> do as you suggested.

Here's what I've pushed to trunk after testing on x86_64-linux.

Thanks again for the fixes.
commit 405a4140fc30bce86b1ec885a98bb17704e0c8c6
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Sep 29 12:12:22 2023

    libstdc++: Split std::basic_string::_M_use_local_data into two functions
    
    This splits out the activate-the-union-member-for-constexpr logic from
    _M_use_local_data, so that it can be used separately in cases that don't
    need to use std::pointer_traits<pointer>::pointer_to to obtain the
    return value.
    
    This leaves only three uses of _M_use_local_data() which are all of the
    same form:
    
      __s._M_data(_M_use_local_data());
      __s._M_set_length(0);
    
    We could remove _M_use_local_data() and change those three places to use
    a new _M_reset() function that does:
    
      _M_init_local_buf();
      _M_data(_M_local_data());
      _M_set_length(0);
    
    This is left for a future change.
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/basic_string.h (_M_init_local_buf()): New
            function.
            (_M_use_local_data()): Use _M_init_local_buf.
            (basic_string(), basic_string(const Alloc&))
            (basic_string(basic_string&&))
            (basic_string(basic_string&&, const Alloc&)): Use
            _M_init_local_buf instead of _M_use_local_data().
            * include/bits/basic_string.tcc (swap(basic_string&))
            (_M_construct(InIter, InIter, input_iterator_tag))
            (_M_construct(InIter, InIter, forward_iterator_tag))
            (_M_construct(size_type, CharT), reserve()): Likewise.

diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
index 4f94cd967cf..0fa32afeb84 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -353,13 +353,23 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       // Ensure that _M_local_buf is the active member of the union.
       __attribute__((__always_inline__))
       _GLIBCXX14_CONSTEXPR
-      pointer
-      _M_use_local_data() _GLIBCXX_NOEXCEPT
+      void
+      _M_init_local_buf() _GLIBCXX_NOEXCEPT
       {
 #if __cpp_lib_is_constant_evaluated
 	if (std::is_constant_evaluated())
 	  for (size_type __i = 0; __i <= _S_local_capacity; ++__i)
 	    _M_local_buf[__i] = _CharT();
+#endif
+      }
+
+      __attribute__((__always_inline__))
+      _GLIBCXX14_CONSTEXPR
+      pointer
+      _M_use_local_data() _GLIBCXX_NOEXCEPT
+      {
+#if __cpp_lib_is_constant_evaluated
+	_M_init_local_buf();
 #endif
 	return _M_local_data();
       }
@@ -522,7 +532,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
       : _M_dataplus(_M_local_data())
       {
-	_M_use_local_data();
+	_M_init_local_buf();
 	_M_set_length(0);
       }
 
@@ -534,7 +544,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
       : _M_dataplus(_M_local_data(), __a)
       {
-	_M_use_local_data();
+	_M_init_local_buf();
 	_M_set_length(0);
       }
 
@@ -678,7 +688,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       {
 	if (__str._M_is_local())
 	  {
-	    (void)_M_use_local_data();
+	    _M_init_local_buf();
 	    traits_type::copy(_M_local_buf, __str._M_local_buf,
 			      __str.length() + 1);
 	  }
@@ -718,7 +728,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       {
 	if (__str._M_is_local())
 	  {
-	    (void)_M_use_local_data();
+	    _M_init_local_buf();
 	    traits_type::copy(_M_local_buf, __str._M_local_buf,
 			      __str.length() + 1);
 	    _M_length(__str.length());
diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc
index 4bc98f2aea7..f0a44e5e881 100644
--- a/libstdc++-v3/include/bits/basic_string.tcc
+++ b/libstdc++-v3/include/bits/basic_string.tcc
@@ -79,7 +79,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	      }
 	    else if (__s.length())
 	      {
-		(void)_M_use_local_data();
+		_M_init_local_buf();
 		traits_type::copy(_M_local_buf, __s._M_local_buf,
 				  __s.length() + 1);
 		_M_length(__s.length());
@@ -88,7 +88,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	      }
 	    else if (length())
 	      {
-		(void)__s._M_use_local_data();
+		__s._M_init_local_buf();
 		traits_type::copy(__s._M_local_buf, _M_local_buf,
 				  length() + 1);
 		__s._M_length(length());
@@ -99,7 +99,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	else
 	  {
 	    const size_type __tmp_capacity = __s._M_allocated_capacity;
-	    (void)__s._M_use_local_data();
+	    __s._M_init_local_buf();
 	    traits_type::copy(__s._M_local_buf, _M_local_buf,
 			      length() + 1);
 	    _M_data(__s._M_data());
@@ -111,7 +111,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  const size_type __tmp_capacity = _M_allocated_capacity;
 	  if (__s._M_is_local())
 	    {
-	      (void)_M_use_local_data();
+	      _M_init_local_buf();
 	      traits_type::copy(_M_local_buf, __s._M_local_buf,
 				__s.length() + 1);
 	      __s._M_data(_M_data());
@@ -174,11 +174,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	size_type __len = 0;
 	size_type __capacity = size_type(_S_local_capacity);
 
-	pointer __p = _M_use_local_data();
+	_M_init_local_buf();
 
 	while (__beg != __end && __len < __capacity)
 	  {
-	    __p[__len++] = *__beg;
+	    _M_local_buf[__len++] = *__beg;
 	    ++__beg;
 	  }
 
@@ -230,7 +230,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	    _M_capacity(__dnew);
 	  }
 	else
-	  _M_use_local_data();
+	  _M_init_local_buf();
 
 	// Check for out_of_range and length_error exceptions.
 	struct _Guard
@@ -263,7 +263,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  _M_capacity(__n);
 	}
       else
-	_M_use_local_data();
+	_M_init_local_buf();
 
       if (__n)
 	this->_S_assign(_M_data(), __n, __c);
@@ -372,7 +372,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       if (__length <= size_type(_S_local_capacity))
 	{
-	  this->_S_copy(_M_use_local_data(), _M_data(), __length + 1);
+	  _M_init_local_buf();
+	  this->_S_copy(_M_local_buf, _M_data(), __length + 1);
 	  _M_destroy(__capacity);
 	  _M_data(_M_local_data());
 	}
diff mbox series

Patch

diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
index 09fd62afa66..7c342879827 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -678,7 +678,7 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
       {
 	if (__str._M_is_local())
 	  {
-	    traits_type::copy(_M_local_buf, __str._M_local_buf,
+	    traits_type::copy(_M_use_local_data(), __str._M_local_buf,
 			      __str.length() + 1);
 	  }
 	else
@@ -691,7 +691,7 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
 	// basic_stringbuf relies on writing into unallocated capacity so
 	// we mess up the contents if we put a '\0' in the string.
 	_M_length(__str.length());
-	__str._M_data(__str._M_local_data());
+	__str._M_data(__str._M_use_local_data());
 	__str._M_set_length(0);
       }
 
@@ -717,6 +717,7 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
       {
 	if (__str._M_is_local())
 	  {
+	    _M_use_local_data();
 	    traits_type::copy(_M_local_buf, __str._M_local_buf,
 			      __str.length() + 1);
 	    _M_length(__str.length());
@@ -728,7 +729,7 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
 	    _M_data(__str._M_data());
 	    _M_length(__str.length());
 	    _M_capacity(__str._M_allocated_capacity);
-	    __str._M_data(__str._M_local_buf);
+	    __str._M_data(__str._M_use_local_data());
 	    __str._M_set_length(0);
 	  }
 	else
diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc
index 104a517f794..ee6e57da555 100644
--- a/libstdc++-v3/include/bits/basic_string.tcc
+++ b/libstdc++-v3/include/bits/basic_string.tcc
@@ -79,7 +79,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	      }
 	    else if (__s.length())
 	      {
-		traits_type::copy(_M_local_buf, __s._M_local_buf,
+		traits_type::copy(_M_use_local_data(), __s._M_local_buf,
 				  __s.length() + 1);
 		_M_length(__s.length());
 		__s._M_set_length(0);
@@ -87,7 +87,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	      }
 	    else if (length())
 	      {
-		traits_type::copy(__s._M_local_buf, _M_local_buf,
+		traits_type::copy(__s._M_use_local_data(), _M_local_buf,
 				  length() + 1);
 		__s._M_length(length());
 		_M_set_length(0);
@@ -97,7 +97,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	else
 	  {
 	    const size_type __tmp_capacity = __s._M_allocated_capacity;
-	    traits_type::copy(__s._M_local_buf, _M_local_buf,
+	    traits_type::copy(__s._M_use_local_data(), _M_local_buf,
 			      length() + 1);
 	    _M_data(__s._M_data());
 	    __s._M_data(__s._M_local_buf);
@@ -108,7 +108,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  const size_type __tmp_capacity = _M_allocated_capacity;
 	  if (__s._M_is_local())
 	    {
-	      traits_type::copy(_M_local_buf, __s._M_local_buf,
+	      traits_type::copy(_M_use_local_data(), __s._M_local_buf,
 				__s.length() + 1);
 	      __s._M_data(_M_data());
 	      _M_data(_M_local_buf);
diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant
index c0e41740dcf..7f24e760bb1 100644
--- a/libstdc++-v3/include/std/variant
+++ b/libstdc++-v3/include/std/variant
@@ -320,6 +320,33 @@  namespace __variant
     __get(_Variant&& __v) noexcept
     { return __variant::__get_n<_Np>(std::forward<_Variant>(__v)._M_u); }
 
+  // Gets the _Uninitialized to construct into for __u.
+  template<size_t _Np, typename _Union>
+    constexpr decltype(auto)
+    __construct_n(_Union& __u) noexcept
+    {
+      if constexpr (_Np == 0)
+	return &__u._M_first;
+      else if constexpr (_Np == 1)
+	{
+	  std::_Construct(&__u._M_rest);
+	  return &__u._M_rest._M_first;
+	}
+      else if constexpr (_Np == 2)
+	{
+	  std::_Construct(&__u._M_rest);
+	  std::_Construct(&__u._M_rest._M_rest);
+	  return &__u._M_rest._M_rest._M_first;
+	}
+      else
+	{
+	  std::_Construct(&__u._M_rest);
+	  std::_Construct(&__u._M_rest._M_rest);
+	  std::_Construct(&__u._M_rest._M_rest._M_rest);
+	  return __variant::__construct_n<_Np - 3>(__u._M_rest._M_rest._M_rest);
+	}
+    }
+
   template<typename... _Types>
     struct _Traits
     {
@@ -536,8 +563,9 @@  namespace __variant
     __emplace(_Variant_storage<_Triv, _Types...>& __v, _Args&&... __args)
     {
       __v._M_reset();
-      auto* __addr = std::__addressof(__variant::__get_n<_Np>(__v._M_u));
-      std::_Construct(__addr, std::forward<_Args>(__args)...);
+      auto* __addr = __variant::__construct_n<_Np>(__v._M_u);
+      std::_Construct(__addr, in_place_index<0>,
+		      std::forward<_Args>(__args)...);
       // Construction didn't throw, so can set the new index now:
       __v._M_index = _Np;
     }