From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id A7C5D385840B; Sun, 24 Oct 2021 15:54:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A7C5D385840B From: "wjwray at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/55227] designated initializer for char array by string constant Date: Sun, 24 Oct 2021 15:54:56 +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: 4.8.0 X-Bugzilla-Keywords: rejects-valid X-Bugzilla-Severity: normal X-Bugzilla-Who: wjwray at gmail dot com 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: cc 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 24 Oct 2021 15:54:56 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D55227 Will Wray changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |wjwray at gmail dot com --- Comment #6 from Will Wray --- Hit this in implementing P1997 array copy semantics (which generalizes the char-array <- string-literal case to array <- array initialization). Compiler Explorer link https://godbolt.org/z/vednE3bYz The reshape_init_r code that ensadc points to is one problem. The code effectively special-cases the C rule that allows optional braces around a string-literal initializer: A brace-enclosed string-literal initializer has its braces stripped, but then the code does not account at all for nested designators (as a recursive call to reshape_init would or should do). This can be seen in the way that bogus nested designators are ignored: struct C {char a[2];} C y =3D {{.bogus=3D"y"}}; // Invalid, g++ accepts C z =3D {{[0]=3D"z"}}; // Invalid, g++ accepts (pedantic warning GNU= [0]) (so the 'solution' of adding braces is misleading, non-standard, non-portab= le). Then, in placing the designator correctly outside the braces, it is not matched with the brace-enclosed initializer (haven't tracked down why yet): C w =3D {.a=3D{"w"}}; // Valid, g++ rejects designated braced // (gcc C accepts) (other C++ compilers accept) A second problem is that the 'matching' of designator to initializer is brittle even without optional braces; only the exactly-matching char-array type (i.e. and extent) is accepted in an unbraced string literal: C u =3D {.a=3D"u"}; // Valid, designated char[2] literal for char[2] fi= eld C r =3D {.a=3D""}; // Valid, g++ rejects designated char[1] for char[2] A workaround is to match the size of the char array by padding with zeros: C r =3D {.a=3D"\0"}; I've spent over a day trying to isolate the 'matching' issue - could do with assist from a designated-initializer mechanic.=