From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27994 invoked by alias); 9 Sep 2015 00:39:53 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 27918 invoked by uid 48); 9 Sep 2015 00:39:48 -0000 From: "myriachan at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/65892] gcc fails to implement N685 aliasing of union members Date: Wed, 09 Sep 2015 00:39:00 -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: 5.0 X-Bugzilla-Keywords: alias X-Bugzilla-Severity: normal X-Bugzilla-Who: myriachan at gmail dot com X-Bugzilla-Status: SUSPENDED 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: cc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2015-09/txt/msg00671.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65892 Melissa changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |myriachan at gmail dot com --- Comment #12 from Melissa --- This is broken in C++ as well, and in C++, the rules are much more clear that GCC isn't following them. Quoting the C++ Standard, revision 4296 (post-C++14?): 16. The "common initial sequence" of two standard-layout struct (Clause 9) types is the longest sequence of non-static data members and bit-fields in declaration order, starting with the first such entity in each of the structs, such that the corresponding entries have layout-compatible types and either neither entity is a bit-field or both are bit-fields with the same width. 19. In a standard-layout union with an active member (9.5) of struct type T1, it is permitted to read a non-static data member m of another union member of struct type T2 provided m is part of the common initial sequence of T1 and T2. A C++ conversion of the original example is below. I asked about the word "read" on the C++ Standard Discussion (std-discussion) mailing list, because it probably should also allow writing if it allows reads. As a result, I modified the below to only *read* in an aliasing way, to fully comply with the written word of the Standard. #include struct t1 { int m; }; struct t2 { int m; }; union U { t1 s1; t2 s2; }; int f (t1 *p1, t2 *p2) { // union U visible here, p1->m and p2->m may alias // p1 is the active member; read from p2 per [class.mem]/19. if (p2->m < 0) p1->m = -p1->m; return p2->m; } int main (void) { union U u = { { -1 } }; int n = f (&u.s1, &u.s2); assert (1 == n); return 0; }