From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by sourceware.org (Postfix) with ESMTPS id 3D03238582B7 for ; Tue, 30 Jan 2024 21:45:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3D03238582B7 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=kernel.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3D03238582B7 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2604:1380:40e1:4800::1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706651126; cv=none; b=CpwP2Ng4ORxn2ATlHX3vA9+ApS4POV5A8Ttu6yGJfrHHVNIiRsJ8XMz9fyXw0vtWoXac+hC/aQsdp6OP8jyG0m7qK9ZMXzIrzeJFP8BEBxHs5BU7CMSSpsBokZ3VQap1ElD2EYrWPJGIstMvsTrQ022PssZfWJr0BQ1AENgSY4A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706651126; c=relaxed/simple; bh=hiwn+gixR8CE3JlhPQ51hxGwA0nNVOFa3kbM7Zz3kH4=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=bcQpPCadOrAg2LQRdFnZA5Ebk2WmWDiGbNx1uM6+uv5Nzg9OJFxsIF/Q02XSE2fHcWNuhf0hDF0gBGXeuf+DLQFGae7Fo9Y4WStPnFi304P+pRvqGS1RvXIoCBB78Lh0a2hXiXVGG1xB6kcB7qyujyAFvIMatk9Yoi4kFfu7OJ0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 79C3BCE1AFC for ; Tue, 30 Jan 2024 21:45:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 43B3FC433C7 for ; Tue, 30 Jan 2024 21:45:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706651120; bh=hiwn+gixR8CE3JlhPQ51hxGwA0nNVOFa3kbM7Zz3kH4=; h=Date:From:To:Subject:From; b=VWoxbfFSSrjgtWkdRstG61CzFYjA4oXZVrRTdAl1o1SuwIlDZ/pt8U836CMeOxGG1 c1fo5UbBym0jZjM3Y7qCWs6IVw3+CiGCKHVphaOnoYmYcuCSz9NPJls6wSP03+FIOs qGVu7Sb6KLgqxrdLT0STAbm2oMZufQWuaaaC6mpGnCP6c9FqwXU13DTo36z6wDGDhj 529HmAdKfu2GhO9oKPXlat4NiL/JC4uijv5RvoSkU8U0Mq2Bq6WrnXUWveTHdjct3p bGzFJnZDhBB7HnObif1O04NKjGS6xMDJX4BF6O+0jl1/HLzjXYs3S2USioZmEsazEm Fn1PX5NHTejdQ== Date: Tue, 30 Jan 2024 22:45:11 +0100 From: Alejandro Colomar To: gcc-help@gcc.gnu.org Subject: Assignment of union containing const-qualifier member Message-ID: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="QE0jJgnZFtGiq9pV" Content-Disposition: inline X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --QE0jJgnZFtGiq9pV Content-Type: text/plain; protected-headers=v1; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Date: Tue, 30 Jan 2024 22:45:11 +0100 From: Alejandro Colomar To: gcc-help@gcc.gnu.org Subject: Assignment of union containing const-qualifier member Hi, I'm trying to do something like the following: $ cat const.c=20 union u { int a; const int b; }; int main(void) { union u u, v; u.a =3D 42; v =3D u; } $ cc -Wall -Wextra const.c=20 const.c: In function =E2=80=98main=E2=80=99: const.c:12:11: error: assignment of read-only variable =E2=80=98v=E2=80=99 12 | v =3D u; | ^ const.c:9:21: warning: variable =E2=80=98v=E2=80=99 set but not used [-Wun= used-but-set-variable] 9 | union u u, v; | ^ The actual data I'm using is not just an int, but that serves to reproduce the problem easily. In reality, the union is more like this: struct rstr { const size_t length; const char *const start; }; union str { struct { size_t length; char *start; } w; struct rstr r; }; I don't see anywhere in C11 that makes this a constraint violation, and considering that the const member is fully overlapped by a non-const member, I don't see why this assignment would be bad. I couldn't find what's the exact semantics of assignment to a union, but I guess it's similar to initialization, and since the first member is non-const, this should be doable. Maybe it's a bit of unspecified behavior, precisely because nothing seems to specify it. Is that really a constraint violation, or should the compiler accept the code? Have a lovely day, Alex --=20 Looking for a remote C programming job at the moment. --QE0jJgnZFtGiq9pV Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEE6jqH8KTroDDkXfJAnowa+77/2zIFAmW5becACgkQnowa+77/ 2zKyYw/+Mfz19FwkQYvFwa6F8fKVbtSaETnW5sIUT50n2R9WCe9fPgBU7wAC6fOS N14T7PRZEI3L38as3vwgXYb9ZyQfZSva85h5fZ5r6z/DKmn0McrnsqjiMR1SdJBS JaOXsVDGFtHfJipsnEXUEgYuQV4P1kFtLiqjy5hDykF0IwpJ2mshb5OJbwGEOOtI mfwlHuOpM05MX+4lS1nkr00kzKGD57D2pHLhSTJGChmgJcuzk1P6IRaid4ClZqMI /gOweP3mGJ8DMSeBPEEEAeyXGu48pAwNKJw6MdxaQXeXRmdEzbjbpYwg8+ZdvoqB vf85l7Q3k3BjR6mmVO6HOO1U+2ikNYg6OnGzOkefRLvAaFpBgytm19FrtSK6EJh4 GZK3UpDU8gDD+0M0pjyevRUL1Wi9RSFEgP1Uwue1b5zdMweDCXLjkZw+LsqgV7Zd ofSDXReqgY2JaCGWrjfWl6H3f3H5y4WNYuDlMctOpS+KEqmkCOCaK0dOn7krNacW +vnogtq/6xIxkxo4BHWfwg/COhGz6zZwNJfLJJlHrPIAhA3Z2gerJIsPH+qZSWxQ UfxBI33JYSyObxt1sw6Vp5/sWuZR7ruIYexZy++uvTOruShRO1eAoCR+GbDT/Ojb 0Lcd1fNXkoFSCrKUtAkKU5pCwdw/6A+EQs5GC9IJ1BUlMsHtvEE= =Azkr -----END PGP SIGNATURE----- --QE0jJgnZFtGiq9pV--