From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id D817E3857835; Fri, 4 Aug 2023 15:32:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D817E3857835 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691163128; bh=hhfB5jypEIum0NFutLhoaIpdrbVvTAORqnrvgzxKNlw=; h=From:To:Subject:Date:From; b=HYCpIy/OXLnAkNmpfBYpE0mH5wr1onZ8xlINgpWglxLst7r1YOHNk8fiizj4MTNx4 XDP9IyWUGAh4OI6XJ5tuZRlAO5aJtQxbHqvsMMFYZSQN87Kt92mLdX7aLuAd3AOgKV LXQVhMzuwGGRa1hVLbj0hrVGWxZiLsp0FyR74aWc= From: "danakj at orodu dot net" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/110900] New: std::string initializes SSO object subfield without making the SSO object active in the union Date: Fri, 04 Aug 2023 15:32:08 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 11.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: danakj at orodu dot net X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D110900 Bug ID: 110900 Summary: std::string initializes SSO object subfield without making the SSO object active in the union Product: gcc Version: 11.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: danakj at orodu dot net Target Milestone: --- 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 =E2=80=98std::__cxx11::basic_string::::_M_allocated_capacity=E2=80=99 member instead of initialized =E2=80=98std::__cxx11::basic_string::::_M_local_buf=E2= =80=99 member in constant expression Specific error by GCC: error: accessing =E2=80=98std::__cxx11::basic_string::::_M_allocated_capacity=E2=80=99 member instead of initialized =E2=80=98std::__cxx11::basic_string::::_M_local_buf=E2= =80=99 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 subob= ject 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::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::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::copy(__s1, __s2, __n); | ^ /usr/include/c++/12/bits/basic_string.h:675:6: note: in call to 'copy(&[]()= { std::string acc; sus::Array::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::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::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 | }) =3D=3D "abcde"); | ~~~^~~~~~~~~~ /home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc:1792:35: = in =E2=80=98constexpr=E2=80=99 expansion of =E2=80=98sus::iter::IteratorBase::fold(B, F) && [with B =3D std::__cxx11::basic_string; F =3D {anonymous}::Iterator_Fold_Test::TestBody()::; I= ter =3D sus::containers::ArrayIntoIter; ItemT =3D char](std::__cxx11::basic_string(), ({anonymous}::Iterator_Fold_Test::TestBody()::(), {anonymous}::Iterator_Fold_Test::TestBody()::()))=E2=80=99 /home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc:1792:35: = in =E2=80=98constexpr=E2=80=99 expansion of =E2=80=98sus::fn::call_mut(F&&, Ar= gs&& ...) [with F =3D {anonymous}::Iterator_Fold_Test::TestBody()::&; = Args =3D {std::__cxx11::basic_string, std::allocator >, char}]((* & sus::mem::move&>(init)), (& sus::mem::move&>(o))->sus::option::Option::= unwrap())=E2=80=99 /home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc:1792:35: = in =E2=80=98constexpr=E2=80=99 expansion of =E2=80=98std::invoke(_Callable&&, = _Args&& ...) [with _Callable =3D {anonymous}::Iterator_Fold_Test::TestBody()::&; _= Args =3D {__cxx11::basic_string, allocator >, char}; invoke_result_t<_Fn, _Args ...> =3D __cxx11::basic_string]((* & sus::mem::forward >((* & args#0))), (* & sus::mem::forward((* & args#1))))=E2=80=99 /home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc:1792:35: = in =E2=80=98constexpr=E2=80=99 expansion of =E2=80=98std::__invoke(_Callable&&= , _Args&& ...) [with _Callable =3D {anonymous}::Iterator_Fold_Test::TestBody()::&; _Args =3D {__cxx11::basic_string, allocator >, char}; typename __invoke_result<_Functor, _ArgTypes>::ty= pe =3D __cxx11::basic_string]((* & std::forward<__cxx11::basic_string = >((* & __args#0))), (* & std::forward((* & __args#1))))=E2=80=99 /home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc:1792:35: = in =E2=80=98constexpr=E2=80=99 expansion of =E2=80=98std::__invoke_impl(__invo= ke_other, _Fn&&, _Args&& ...) [with _Res =3D __cxx11::basic_string; _Fn =3D {anonymous}::Iterator_Fold_Test::TestBody()::&; _Args= =3D {__cxx11::basic_string, allocator >, char}]((= * & std::forward<{anonymous}::Iterator_Fold_Test::TestBody()::&>((* & __fn))), (* & std::forward<__cxx11::basic_string >((* & __args#0))), (* & std::forward((* & __args#1))))=E2=80=99 /home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc:1792:35: = in =E2=80=98constexpr=E2=80=99 expansion of =E2=80=98std::__cxx11::basic_strin= g((* & std::forward<__cxx11::basic_string >((* & __args#0))))=E2=80=99 /home/runner/work/subspace/subspace/sus/iter/iterator_unittest.cc:1792:24: error: accessing =E2=80=98std::__cxx11::basic_string::::_M_allocated_capacity=E2=80=99 member instead of initialized =E2=80=98std::__cxx11::basic_string::::_M_local_buf=E2= =80=99 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=3Dpath/to/gcc-13 cmake -B out -DSUBSPACE_BUILD_TESTS=3DON cmake --build out -j 20=