From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 7E0E93858C50; Tue, 21 Nov 2023 17:48:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7E0E93858C50 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1700588893; bh=UC5bLbAOZwRDx1ynxpq/z37KmVppzNjn0Ci9BQX5Ij4=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ezOO0aoJ0Z38V91aqWXupYTDvP2ZjMaOC2Poa9bkI4sX/DZw7Uh7+rLPm7aLg4c7I yGdHVtA7D1cwTzL5pjYUJAaTksW4z0Vhr8WUtZzCihCdhvDdO/9+dOR0ahSWdJdzno KYK1F9oMB7DLEmQ87HNStzxHruJEhuBNiU0To1Fw= From: "miro.palmu at helsinki dot fi" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/112642] ranges::fold_left tries to access inactive union member of string in constant expression Date: Tue, 21 Nov 2023 17:48:12 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 13.2.1 X-Bugzilla-Keywords: rejects-valid X-Bugzilla-Severity: normal X-Bugzilla-Who: miro.palmu at helsinki dot fi X-Bugzilla-Status: NEW 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: Message-ID: In-Reply-To: References: 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=3D112642 --- Comment #5 from Miro Palmu --- I have been trying to figure out where exactly the bug is and these are my findings. > Or: > > #include > > consteval void bar() { > auto _ =3D [](std::string s) { return s; }({}); > } > > int main() { > bar();=20=20=20=20 > } > > Or: >=20 > #include > constexpr auto foo(std::string init) { return init; } > constexpr auto bar() { return foo("").size(); } > constexpr auto i =3D bar(); > >Clang compiles both of these without problems (it can't compile anything u= sing ""s in a constant expression, maybe due to https://github.com/llvm/llv= m-project/issues/68527) > > So I am pretty sure this is a g++ front end bug. If you use libstdc++ on clang these will not compile but with different err= ors. Then with following example I try to showcase the bug without std::string. Try it out: https://godbolt.org/z/rvoeMEaxc This is bare minimum of libstdc++ basic_string to reproduce this bug: --- struct S { union { char a[1]; }; char* ptr;=20 constexpr S() : ptr{a} { a[0] =3D {}; } constexpr S(S&&) =3D delete; constexpr S(const S&) =3D delete; constexpr S operator=3D(S&&) =3D delete; constexpr S operator=3D(const S&) =3D delete; constexpr ~S() =3D default; } --- Then to reproduce the bug instance of this class has to be function paramet= er and the function has to be constant evaluated. This can happens in std::basic_string move constructor bits/basic_string.h:= 682 and following tester functions tries to emulate what happens in it. --- // Should always be false constexpr bool test(const S& s){ return s.ptr !=3D s.a; } consteval void tester1(S param =3D {}) {=20 S notparam =3D {}; if (test(notparam)){ throw std::logic_error("compiletime notparam!"); } if (test(param)) { // gcc ends up here so fails to compile // in std::basic_string move constructor // compilation would fail due to accessing // inactive union member // clang and msvc never end up here throw std::logic_error("compiletime param"); } } int main() { tester(); ) --- Notice that here only the parameter version fails. In non-constant evaluated context (see godbolt link) all of the test evaluate false as they should.=