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 33D123858416 for ; Wed, 31 Jan 2024 18:14:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 33D123858416 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 33D123858416 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=1706724865; cv=none; b=sQHBUh4EmiLD8Es/SG4sV69EcQ02X5Y3+xJILhLS8I+X8YgJVFD7EKOnDqDgP6aizW3uGOY1RrE5XRKeqr6J3oxKlQB79j2Hv0Ar4aY60kVBfGZ1SZT/XwWgmFz5/5Wc+EXQBmRrc0SbxPJub0lYj8lcXp1HIPwF8qwNH6BBjho= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706724865; c=relaxed/simple; bh=OrzJrqbXHb/PjpA19Hu/s0aArK5Q63TOSZ0I+Pehek0=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=hhx8g3AIKxifE8ab54AaxMmEpSvzvx6LlVO2ZjUbmv41rvRPTveqFO+JdOdQQ2jDdQCPHAH9JCws971sAdVuK7GRObMOwiPljdKOaq8sVjf3uk4bBywXauPJup/lyeUGeGt6eAekiaHLjrLn+Bare3uUCFSCbWAbEcSTOaqy+VA= 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 7DC4BCE1F0D for ; Wed, 31 Jan 2024 18:14:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1B4FDC433C7 for ; Wed, 31 Jan 2024 18:14:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706724857; bh=OrzJrqbXHb/PjpA19Hu/s0aArK5Q63TOSZ0I+Pehek0=; h=Date:From:To:Subject:References:In-Reply-To:From; b=tahxcwVvTmreTC2eXlZfgStLv+S+zOHi9DuE3VM6gCwGf8LqsfWkaUA4vzE6Q2ngp NjUfW2RjlprHpce7nk3K1bbhLKnOkzeiB3ue4ZwNtisPkpSGN2HRgZDPjhkJA6+IxC cVlu/4DNKmHF0Rz/V5bmRSFMuXHQNc5ZdKm6Vbd9SIB+obgIrG4F8rgdo0tp/G8Cw1 Gy+oTsG3suv2uTCFWL9PkZv48h2dnecoKiwaxQRbqal/dEBKQ+2BaDSPKjYyUCJs/D 8wy4NYFRl7eciv1aVn0LYuhA0LWqb9izXzBLMez/46gyQWZHiZW5qrOn5Zi1RE2SsK BAAyBGox6dJhg== Date: Wed, 31 Jan 2024 19:14:14 +0100 From: Alejandro Colomar To: gcc-help@gcc.gnu.org Subject: Re: Assignment of union containing const-qualifier member Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="lJfqYs+Ran8h6GtG" Content-Disposition: inline In-Reply-To: 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: --lJfqYs+Ran8h6GtG Content-Type: text/plain; protected-headers=v1; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Date: Wed, 31 Jan 2024 19:14:14 +0100 From: Alejandro Colomar To: gcc-help@gcc.gnu.org Subject: Re: Assignment of union containing const-qualifier member On Tue, Jan 30, 2024 at 10:45:11PM +0100, Alejandro Colomar wrote: > Hi, >=20 > I'm trying to do something like the following: >=20 > $ cat const.c=20 > union u { > int a; > const int b; > }; >=20 > int > main(void) > { > union u u, v; >=20 > 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 [-W= unused-but-set-variable] > 9 | union u u, v; > | ^ >=20 > 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: >=20 > struct rstr { > const size_t length; > const char *const start; > }; >=20 > union str { > struct { > size_t length; > char *start; > } w; > struct rstr r; > }; >=20 > I don't see anywhere in C11 that makes this a constraint violation, and Ahh, I didn't find it. It's specified undex 6.3 (Conversions), 6.3.2.1 (Lvalues, arrays, and function designators) : < A modifiable lvalue is an lvalue that does not have array type, does < not have an incomplete type, does not have a const- qualified type, < and if it is a structure or union, does not have any member < (including, recursively, any member or element of all contained < aggregates or unions) with a const- qualified type. I'm wondering if this is a bit conservative, and a union like this could be a modifiable lvalue. I find it useful, since it allows inheriting a non-modifiable version of the string which cannot be made modifiable again, but would let you edit the string as long as you keep the union. I added the named member 'w' to allow copying v.w =3D u.w, but when the union is part of a larger structure and I need to copy the entire structure, that doesn't help. memcpy(3) does help, but it looses all type safety. Maybe this could be allowed as an extension. Any thoughts? Cheers, Alex --=20 Looking for a remote C programming job at the moment. --lJfqYs+Ran8h6GtG Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEE6jqH8KTroDDkXfJAnowa+77/2zIFAmW6jfYACgkQnowa+77/ 2zLFTw/5ARwIi8+U5CeI41P7sPBls4c2rFKP+y6fcfsNfDkCGn/Bbv1Iq3iY35DJ kt9RAUONNPoGyo5vbwyyyAUbu4JBjfHOqD+ZbrUugGhkDqF60XHlhVDX10E9uxV2 1mL5Lxs3S8fgzuwOnebwYCSZ03CECeHdsUwrIqW/xznwB7wRHCHTT6eP13XO6EYo 1eol7pNqo0Dk7lY28toBxw6rhU7FGZsRWGcm/zRPhnCIfFHtkws1KY6ZKqf08zda pcbN97xfdCBllTGIbsgzK9Efrp6xpM2GaWiQyl/pZq750INk8uCm/7SMxrhTlS8I ijdqC+zNmPUtyRFMrAMuPGlhEHMe1rPZk5sXHfK7x9ZKz2HxWH8vPyYfMnqnqCf+ WkiCljzG5IkK8RPMwmeOEIF1xx7u6wrYalRWaJKdOt5lihqJRHokaL/XUkkJZOeh d51ZeWBVBIEeJ+Nsfgyvw2ZQZy3ButhigJYux3/sbk6hY8PbJbQQk5VSl1qqMcvG DPqg1neVea/eKXNLsKjwl8eIgaUTkvdD22bmHRI9eF+7q3cuVuufKAwUKp3vHgO/ Cb3Da2U5Jav73WqqyAZQ9g8+5/RdexKpnXrd+Qx7Q7ivadjxAuwt/iXoLj9bmKWB E/rlor0r9bp2XdSZJ/62oygUlWY/Ya9Mswv1SorvjcfUukQuDr4= =4nuZ -----END PGP SIGNATURE----- --lJfqYs+Ran8h6GtG--