Bug 110900 - std::string initializes SSO object subfield without making the SSO object active in the union
Summary: std::string initializes SSO object subfield without making the SSO object act...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: 12.4
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-08-04 15:32 UTC by danakj
Modified: 2024-03-29 17:41 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2023-08-04 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description danakj 2023-08-04 15:32:08 UTC
Specific errors by clang:

note: construction of subobject of member '_M_local_buf' of union with no active member is not allowed in a constant expression

error: accessing ‘std::__cxx11::basic_string<char>::<unnamed union>::_M_allocated_capacity’ member instead of initialized ‘std::__cxx11::basic_string<char>::<unnamed union>::_M_local_buf’ member in constant expression

Specific error by GCC:

error: accessing ‘std::__cxx11::basic_string<char>::<unnamed union>::_M_allocated_capacity’ member instead of initialized ‘std::__cxx11::basic_string<char>::<unnamed union>::_M_local_buf’ member in constant expression

Full errors:

Here's the clang 17 error:

/usr/include/c++/12/bits/stl_construct.h:97:14: note: construction of subobject of member '_M_local_buf' of union with no active member is not allowed in a constant expression
   97 |     { return ::new((void*)__location) _Tp(std::forward<_Args>(__args)...); }
      |              ^
/usr/include/c++/12/bits/char_traits.h:262:6: note: in call to 'construct_at(&[]() {
    std::string acc;
    sus::Array<char, 5>::with('a', 'b', 'c', 'd', 'e').into_iter().for_each([&](char v) {
        acc.push_back(v);
    });
    return acc;
}().._M_local_buf[0], acc.._M_local_buf[0])'
  262 |             std::construct_at(__s1 + __i, __s2[__i]);
      |             ^
/usr/include/c++/12/bits/char_traits.h:429:11: note: in call to 'copy(&[]() {
    std::string acc;
    sus::Array<char, 5>::with('a', 'b', 'c', 'd', 'e').into_iter().for_each([&](char v) {
        acc.push_back(v);
    });
    return acc;
}().._M_local_buf[0], &acc.._M_local_buf[0], 6)'
  429 |           return __gnu_cxx::char_traits<char_type>::copy(__s1, __s2, __n);
      |                  ^
/usr/include/c++/12/bits/basic_string.h:675:6: note: in call to 'copy(&[]() {
    std::string acc;
    sus::Array<char, 5>::with('a', 'b', 'c', 'd', 'e').into_iter().for_each([&](char v) {
        acc.push_back(v);
    });
    return acc;
}().._M_local_buf[0], &acc.._M_local_buf[0], 6)'
  675 |             traits_type::copy(_M_local_buf, __str._M_local_buf,
      |             ^
/home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc:1864:12: note: in call to 'basic_string(acc)'
 1864 |     return acc;
      |            ^
/home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc:1859:17: note: in call to '[]() {
    std::string acc;
    sus::Array<char, 5>::with('a', 'b', 'c', 'd', 'e').into_iter().for_each([&](char v) {
        acc.push_back(v);
    });
    return acc;
}.operator()()'
 1859 |   static_assert([]() {
      |                 ^

Here's the g++ 13 error:
/home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc:1792:24: error: non-constant condition for static assertion
 1787 |   static_assert(sus::Array<char, 5>::with('a', 'b', 'c', 'd', 'e')
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1788 |                     .into_iter()
      |                     ~~~~~~~~~~~~
 1789 |                     .fold(std::string(), [](std::string acc, char v) {
      |                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1790 |                       acc.push_back(v);
      |                       ~~~~~~~~~~~~~~~~~
 1791 |                       return acc;
      |                       ~~~~~~~~~~~
 1792 |                     }) == "abcde");
      |                     ~~~^~~~~~~~~~
/home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc:1792:35:   in ‘constexpr’ expansion of ‘sus::iter::IteratorBase<Iter, Item>::fold(B, F) && [with B = std::__cxx11::basic_string<char>; F = {anonymous}::Iterator_Fold_Test::TestBody()::<lambda(std::string, char)>; Iter = sus::containers::ArrayIntoIter<char, 5>; ItemT = char](std::__cxx11::basic_string<char>(), (<lambda closure object>{anonymous}::Iterator_Fold_Test::TestBody()::<lambda(std::string, char)>(), {anonymous}::Iterator_Fold_Test::TestBody()::<lambda(std::string, char)>()))’
/home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc:1792:35:   in ‘constexpr’ expansion of ‘sus::fn::call_mut(F&&, Args&& ...) [with F = {anonymous}::Iterator_Fold_Test::TestBody()::<lambda(std::string, char)>&; Args = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char}]((* & sus::mem::move<std::__cxx11::basic_string<char>&>(init)), (& sus::mem::move<sus::option::Option<char>&>(o))->sus::option::Option<char>::unwrap())’
/home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc:1792:35:   in ‘constexpr’ expansion of ‘std::invoke(_Callable&&, _Args&& ...) [with _Callable = {anonymous}::Iterator_Fold_Test::TestBody()::<lambda(string, char)>&; _Args = {__cxx11::basic_string<char, char_traits<char>, allocator<char> >, char}; invoke_result_t<_Fn, _Args ...> = __cxx11::basic_string<char>]((* & sus::mem::forward<std::__cxx11::basic_string<char> >((* & args#0))), (* & sus::mem::forward<char>((* & args#1))))’
/home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc:1792:35:   in ‘constexpr’ expansion of ‘std::__invoke(_Callable&&, _Args&& ...) [with _Callable = {anonymous}::Iterator_Fold_Test::TestBody()::<lambda(string, char)>&; _Args = {__cxx11::basic_string<char, char_traits<char>, allocator<char> >, char}; typename __invoke_result<_Functor, _ArgTypes>::type = __cxx11::basic_string<char>]((* & std::forward<__cxx11::basic_string<char> >((* & __args#0))), (* & std::forward<char>((* & __args#1))))’
/home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc:1792:35:   in ‘constexpr’ expansion of ‘std::__invoke_impl(__invoke_other, _Fn&&, _Args&& ...) [with _Res = __cxx11::basic_string<char>; _Fn = {anonymous}::Iterator_Fold_Test::TestBody()::<lambda(string, char)>&; _Args = {__cxx11::basic_string<char, char_traits<char>, allocator<char> >, char}]((* & std::forward<{anonymous}::Iterator_Fold_Test::TestBody()::<lambda(string, char)>&>((* & __fn))), (* & std::forward<__cxx11::basic_string<char> >((* & __args#0))), (* & std::forward<char>((* & __args#1))))’
/home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc:1792:35:   in ‘constexpr’ expansion of ‘std::__cxx11::basic_string<char>((* & std::forward<__cxx11::basic_string<char> >((* & __args#0))))’
/home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc:1792:24: error: accessing ‘std::__cxx11::basic_string<char>::<unnamed union>::_M_allocated_capacity’ member instead of initialized ‘std::__cxx11::basic_string<char>::<unnamed union>::_M_local_buf’ member in constant expression

I don't have a great minimal repo atm, and I don't have a local machine that uses libstdc++, but the Iterator.Fold unittest in iterator_unittest.cc is an example that causes it here: https://github.com/danakj/subspace/blob/experiment-iters/sus/iter/iterator_unittest.cc#L1787-L1792

With gcc-13:

git clone --recurse-submodules https://github.com/danakj/subspace
cd subspace
git checkout test origin/experiment-iters
CXX=path/to/gcc-13 cmake -B out -DSUBSPACE_BUILD_TESTS=ON
cmake --build out -j 20
Comment 1 danakj 2023-08-04 15:43:07 UTC
I am going to try work around this by not using std::string in constant expressions..

So in the meantime I pushed a branch where this bug will continue to reproduce.

With gcc-13:

git clone --recurse-submodules https://github.com/danakj/subspace
cd subspace
git checkout test origin/libstd-bug-sso
CXX=path/to/gcc-13 cmake -B out -DSUBSPACE_BUILD_TESTS=ON
cmake --build out -j 20
Comment 2 Andrew Pinski 2023-08-04 15:52:21 UTC
Can you please read https://gcc.gnu.org/bugs/ on what we need?
Comment 3 Andrew Pinski 2023-08-04 15:55:40 UTC
Dup of bug 110158.

*** This bug has been marked as a duplicate of bug 110158 ***
Comment 4 danakj 2023-08-04 16:01:20 UTC
The error message is the same as 110158 but to be clear the std::string is not in a union. The error message is about the union _inside_ std::string.
Comment 5 danakj 2023-08-04 16:04:24 UTC
> Can you please read https://gcc.gnu.org/bugs/ on what we need?

Yeah, sorry I can't reproduce this locally on my Mac or Windows machine. It reproduces on github Linux CI bots, and I have diagnosed it from there.

https://github.com/chromium/subspace/actions/runs/5758764036/job/15611774084?pr=306

This job is using gcc 13.1.0, and it installs libstdc++-13-dev.

Here's the command that fails:

/usr/bin/g++-13  -I/home/runner/work/subspace/subspace -I/home/runner/work/subspace/subspace/third_party/googletest -I/home/runner/work/subspace/subspace/third_party/fmt/include -isystem /home/runner/work/subspace/subspace/third_party/googletest/googletest/include -isystem /home/runner/work/subspace/subspace/third_party/googletest/googletest -isystem /usr/include/c++/13 -isystem /usr/include/x86_64-linux-gnu/c++/13 -isystem /usr/include/c++/13/backward -isystem /usr/lib/gcc/x86_64-linux-gnu/13/include -isystem /usr/local/include -isystem /usr/include/x86_64-linux-gnu -isystem /usr/include -O3 -DNDEBUG -std=gnu++20 -fno-rtti -Werror -MD -MT sus/CMakeFiles/subspace_unittests.dir/iter/iterator_unittest.cc.o -MF sus/CMakeFiles/subspace_unittests.dir/iter/iterator_unittest.cc.o.d -o sus/CMakeFiles/subspace_unittests.dir/iter/iterator_unittest.cc.o -c /home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc

I think it's simplest to just do a git clone and build that though... as I can't easily minmize this.
Comment 6 danakj 2023-08-04 16:17:34 UTC
Thanks for the link, I used the godbolt from that bug to set up the right environment and that let me minimize it. I posted it into the dupe bug.
Comment 7 Jonathan Wakely 2023-09-29 11:08:45 UTC
I don't think this is a duplicate.

This is just a libstdc++ bug, which we have a patch for.
Comment 8 Jonathan Wakely 2023-09-29 15:07:31 UTC
This should be fixed on trunk now by r14-4334-g28adad7a32ed92

That needs to be backported too.