From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id D88FE3858D38; Sat, 13 Apr 2024 01:59:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D88FE3858D38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1712973562; bh=nowa95w1c97GX1wBqnVtFUgReyy0w4H9Riiq72heuyE=; h=From:To:Subject:Date:From; b=o4Ebegi3Z7N1fOSYfXkWRV1GrItMlwq9R2F6Gq58kJr8caVx3aIIhT88cd92NMCTp EtTDPzGu49o2HSv7fH9kUwuPnA9E6OkOU/BmzIoyK4PuYAvtYYutY5Bj4kvFeaj4LW WMDQHCC39scCfPaZl+wQfQo7Z9Sh1XapeC0KbWHI= From: "420 at zerberste dot es" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/114709] New: Incorrect handling of inactive union member access via pointer to member in constant evaluated context Date: Sat, 13 Apr 2024 01:59:22 +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: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: 420 at zerberste dot es 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=3D114709 Bug ID: 114709 Summary: Incorrect handling of inactive union member access via pointer to member in constant evaluated context Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: 420 at zerberste dot es Target Milestone: --- According to https://standards.pydong.org/c++/class.union#general-note-1 an inactive union member may be used to inspect the initial sequence common wi= th the active union member, given both are struct types. Furthermore https://standards.pydong.org/c++/class.mem#general-26 tells us the behavior= of reading a non-static member m of such a common initial sequence is _as if_ = the corresponding member were nominated. Since it is not expression-equivalent,= we are still accessing the wrong member as far as the abstract machine is concerned. Hence https://standards.pydong.org/c++/expr.const#5.10 still applies. GCC implements this correctly for the simple case. To reuse the example from [class.mem]/26 consider this code fragment: ```cpp struct T1 { int a, b; }; struct T2 { int c; double d; }; union U { T1 t1; T2 t2; }; consteval int f() { U u =3D { { 1, 2 } }; // active member is t1 return u.t2.c; // access through t2 } static constexpr auto foo =3D f(); ``` https://godbolt.org/z/x14n47T45 GCC correctly errors with the diagnostic=20 > accessing 'U::t2' member instead of initialized 'U::t1' member in constan= t expression However, consider the following code fragment: ```cpp struct T1 { int a, b; }; struct T2 { int c; double d; }; union U { T1 t1; T2 t2; }; consteval int f() { U u =3D { { 1, 2 } }; // active member is t1 return u.t2.*&T2::c; // access through t2 } static constexpr auto foo =3D f(); ``` https://godbolt.org/z/dK95jEPjq GCC 12 up until trunk accept this code. GCC 11 and Clang don't.=