public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/55227] New: designated initializer for char array by string constant
@ 2012-11-07  8:27 pavel.v.chupin at gmail dot com
  2012-11-07 22:25 ` [Bug c++/55227] " daniel.kruegler at googlemail dot com
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: pavel.v.chupin at gmail dot com @ 2012-11-07  8:27 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55227

             Bug #: 55227
           Summary: designated initializer for char array by string
                    constant
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: pavel.v.chupin@gmail.com


t.C:

struct S { char a[25]; };

int main() {
   struct S s = {a:"aaaa"};
   (void)s;
   return 0;
}


t.C: In function 'int main()':
t.C:4:26: error: C99 designator 'a' outside aggregate initializer
    struct S s = {a:"aaaa"};
                          ^
Workarounds:
1) struct S s = {"aaaa"};
2) struct S { char *a; };


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [Bug c++/55227] designated initializer for char array by string constant
  2012-11-07  8:27 [Bug c++/55227] New: designated initializer for char array by string constant pavel.v.chupin at gmail dot com
@ 2012-11-07 22:25 ` daniel.kruegler at googlemail dot com
  2020-04-08 10:11 ` ensadc at mailnesia dot com
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2012-11-07 22:25 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55227

Daniel Krügler <daniel.kruegler at googlemail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |daniel.kruegler at
                   |                            |googlemail dot com

--- Comment #1 from Daniel Krügler <daniel.kruegler at googlemail dot com> 2012-11-07 22:25:29 UTC ---
I would expect that the designated initializer would be .a="aaaa" and not
a:"aaaa" (as specified by C99), but I agree that even with that change the
revised code gives the same error:

struct S { char a[25]; };

int main() {
   struct S s = {.a="aaaa"};
   (void)s;
   return 0;
}


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [Bug c++/55227] designated initializer for char array by string constant
  2012-11-07  8:27 [Bug c++/55227] New: designated initializer for char array by string constant pavel.v.chupin at gmail dot com
  2012-11-07 22:25 ` [Bug c++/55227] " daniel.kruegler at googlemail dot com
@ 2020-04-08 10:11 ` ensadc at mailnesia dot com
  2020-06-16 11:40 ` steven.spark at gmail dot com
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: ensadc at mailnesia dot com @ 2020-04-08 10:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55227

ensadc at mailnesia dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ensadc at mailnesia dot com

--- Comment #4 from ensadc at mailnesia dot com ---
This is accepted in `-Wno-pedantic` mode:

    struct A { int x; char a[10]; };
    A a = { .x = 1, { .a = "aaaa" } }; // #1

This is rejected:

    struct A { int x; char a[10]; };
    A a = { .x = 1, .a = "aaaa" }; // #2

It seems that #1 should be rejected, and #2 should be accepted.

I suspect that the bug is related to r182391 (which is a fix for PR51458),
which added call to `has_designator_problem` in `reshape_init_r`:

      /* If it's a string literal, then it's the initializer for the array
         as a whole. Otherwise, continue with normal initialization for
         array types (one value per array element).  */
      if (TREE_CODE (stripped_str_init) == STRING_CST)
        {
          if (has_designator_problem (d, complain))
            return error_mark_node;
          d->cur++;
          return str_init;
        }

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [Bug c++/55227] designated initializer for char array by string constant
  2012-11-07  8:27 [Bug c++/55227] New: designated initializer for char array by string constant pavel.v.chupin at gmail dot com
  2012-11-07 22:25 ` [Bug c++/55227] " daniel.kruegler at googlemail dot com
  2020-04-08 10:11 ` ensadc at mailnesia dot com
@ 2020-06-16 11:40 ` steven.spark at gmail dot com
  2021-10-24 15:54 ` wjwray at gmail dot com
                   ` (10 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: steven.spark at gmail dot com @ 2020-06-16 11:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55227

Szikra <steven.spark at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |steven.spark at gmail dot com

--- Comment #5 from Szikra <steven.spark at gmail dot com> ---
I just run into this problem with gcc version 7.3.1 20180622 (release)
[ARM/embedded-7-branch revision 261907] (GNU Tools for Arm Embedded Processors
7-2018-q2-update)

Thanks J.R. Heisey and ensadc, the extra braces solved the error... for now.

I added this url to comments in case this bug is fixed and they need to be
removed.

typedef struct {
    /// ...
    uint16_t serverPort;
    char serverAddress[128];
} ConfigParameters_t;


ConfigParameters_t __attribute__((section(".eeprom"))) eeParameters = {
    /// ...
    .serverPort=1234,
    {.serverAddress="*****"} /// weird...
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55227
};

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [Bug c++/55227] designated initializer for char array by string constant
  2012-11-07  8:27 [Bug c++/55227] New: designated initializer for char array by string constant pavel.v.chupin at gmail dot com
                   ` (2 preceding siblings ...)
  2020-06-16 11:40 ` steven.spark at gmail dot com
@ 2021-10-24 15:54 ` wjwray at gmail dot com
  2021-10-24 21:25 ` wjwray at gmail dot com
                   ` (9 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: wjwray at gmail dot com @ 2021-10-24 15:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55227

Will Wray <wjwray at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |wjwray at gmail dot com

--- Comment #6 from Will Wray <wjwray at gmail dot com> ---
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 = {{.bogus="y"}}; // Invalid, g++ accepts
    C z = {{[0]="z"}};    // Invalid, g++ accepts (pedantic warning GNU [0])

(so the 'solution' of adding braces is misleading, non-standard, non-portable).

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 = {.a={"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 = {.a="u"}; // Valid, designated char[2] literal for char[2] field
    C r = {.a=""};  // 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 = {.a="\0"};

I've spent over a day trying to isolate the 'matching' issue -
could do with assist from a designated-initializer mechanic.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [Bug c++/55227] designated initializer for char array by string constant
  2012-11-07  8:27 [Bug c++/55227] New: designated initializer for char array by string constant pavel.v.chupin at gmail dot com
                   ` (3 preceding siblings ...)
  2021-10-24 15:54 ` wjwray at gmail dot com
@ 2021-10-24 21:25 ` wjwray at gmail dot com
  2021-10-24 21:31 ` wjwray at gmail dot com
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: wjwray at gmail dot com @ 2021-10-24 21:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55227

--- Comment #7 from Will Wray <wjwray at gmail dot com> ---
The patch below fixes the main issue (I think, checking)
by adding first_initializer_p to the error condition
it errors only where designators are not allowed.


diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 8a45411c995..f1210e862ca 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -6835,7 +6835,7 @@ reshape_init_r (tree type, reshape_iter *d, tree
first_initializer_p,
         array types (one value per array element).  */
       if (TREE_CODE (stripped_str_init) == STRING_CST)
        {
-         if (has_designator_problem (d, complain))
+         if (first_initializer_p && has_designator_problem (d, complain))
            return error_mark_node;
          d->cur++;
          return str_init;

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [Bug c++/55227] designated initializer for char array by string constant
  2012-11-07  8:27 [Bug c++/55227] New: designated initializer for char array by string constant pavel.v.chupin at gmail dot com
                   ` (4 preceding siblings ...)
  2021-10-24 21:25 ` wjwray at gmail dot com
@ 2021-10-24 21:31 ` wjwray at gmail dot com
  2021-10-25 17:06 ` wjwray at gmail dot com
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: wjwray at gmail dot com @ 2021-10-24 21:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55227

--- Comment #8 from Will Wray <wjwray at gmail dot com> ---
The patch above doesn't address the secondary issue,
of ignored and unchecked nested designators:

    C b {{.bogus="b"}};

Perhaps reshape_init should be recursed into once more?

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [Bug c++/55227] designated initializer for char array by string constant
  2012-11-07  8:27 [Bug c++/55227] New: designated initializer for char array by string constant pavel.v.chupin at gmail dot com
                   ` (5 preceding siblings ...)
  2021-10-24 21:31 ` wjwray at gmail dot com
@ 2021-10-25 17:06 ` wjwray at gmail dot com
  2021-10-25 17:44 ` wjwray at gmail dot com
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: wjwray at gmail dot com @ 2021-10-25 17:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55227

--- Comment #9 from Will Wray <wjwray at gmail dot com> ---
Adding a reshape_iter, and checking has_designator_problem,
for a brace-enclosed string-literal fixes this secondary issue

+         reshape_iter e {CONSTRUCTOR_ELT (stripped_a_init, 0), e.cur + 1};
+         if (has_designator_problem (&e, complain))
+           return error_mark_node;

(this breaks J.R.Heisey's workaround, correctly rejecting it as an error).

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [Bug c++/55227] designated initializer for char array by string constant
  2012-11-07  8:27 [Bug c++/55227] New: designated initializer for char array by string constant pavel.v.chupin at gmail dot com
                   ` (6 preceding siblings ...)
  2021-10-25 17:06 ` wjwray at gmail dot com
@ 2021-10-25 17:44 ` wjwray at gmail dot com
  2021-11-04 23:43 ` wjwray at gmail dot com
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: wjwray at gmail dot com @ 2021-10-25 17:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55227

--- Comment #10 from Will Wray <wjwray at gmail dot com> ---
Note that the initialization of 'c0' takes a different codepath:

    struct C {char a[2];};

    C c0{.a="a"};           // [dcl.init.aggr]
    C c1{.a=""};
    C c2{.a={"a"}};
    C c3{.a={""}};

c1, c2 and c3 are all dealt with under the condition:

    if (TREE_CODE (type) == ARRAY_TYPE
        ...

c0 is dealt with by the immediately preceding [dcl.init.aggr] condition:

    if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
        ...

Is this a problem? It seems surprising.

If the two condition blocks were switched so the ARRAY_TYPE check comes first
then all initializations would follow the same codepath in reshape_init_r

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [Bug c++/55227] designated initializer for char array by string constant
  2012-11-07  8:27 [Bug c++/55227] New: designated initializer for char array by string constant pavel.v.chupin at gmail dot com
                   ` (7 preceding siblings ...)
  2021-10-25 17:44 ` wjwray at gmail dot com
@ 2021-11-04 23:43 ` wjwray at gmail dot com
  2021-11-07  0:27 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: wjwray at gmail dot com @ 2021-11-04 23:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55227

--- Comment #11 from Will Wray <wjwray at gmail dot com> ---
Created attachment 51737
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51737&action=edit
Proposed patch Nov 4

Sent to gcc-patches for review
https://gcc.gnu.org/pipermail/gcc-patches/2021-November/583379.html

This patch aims to fix PR 55227; two underlying bugs that have caused:

(1) Rejection of valid designated initialization of char array fields by
string literals (a) when enclosed in optional braces or (b) unbraced
when the string literal is shorter than the target char array field.

(2) Acceptance of an invalid designator appearing within the braces of a
braced string literal, in which case the 'designator' was entirely
ignored and the string literal treated as a positional initializer.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [Bug c++/55227] designated initializer for char array by string constant
  2012-11-07  8:27 [Bug c++/55227] New: designated initializer for char array by string constant pavel.v.chupin at gmail dot com
                   ` (8 preceding siblings ...)
  2021-11-04 23:43 ` wjwray at gmail dot com
@ 2021-11-07  0:27 ` pinskia at gcc dot gnu.org
  2021-11-18  3:17 ` wjwray at gmail dot com
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-11-07  0:27 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55227

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                URL|                            |https://gcc.gnu.org/piperma
                   |                            |il/gcc-patches/2021-Novembe
                   |                            |r/583589.html

--- Comment #12 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Patch was submitted:
https://gcc.gnu.org/pipermail/gcc-patches/2021-November/583589.html

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [Bug c++/55227] designated initializer for char array by string constant
  2012-11-07  8:27 [Bug c++/55227] New: designated initializer for char array by string constant pavel.v.chupin at gmail dot com
                   ` (9 preceding siblings ...)
  2021-11-07  0:27 ` pinskia at gcc dot gnu.org
@ 2021-11-18  3:17 ` wjwray at gmail dot com
  2021-11-18  3:21 ` wjwray at gmail dot com
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: wjwray at gmail dot com @ 2021-11-18  3:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55227

Will Wray <wjwray at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
  Attachment #51737|0                           |1
        is obsolete|                            |

--- Comment #13 from Will Wray <wjwray at gmail dot com> ---
Created attachment 51828
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51828&action=edit
v2 patch Nov 17

Incorporates Marek's Nov 15 review comments.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [Bug c++/55227] designated initializer for char array by string constant
  2012-11-07  8:27 [Bug c++/55227] New: designated initializer for char array by string constant pavel.v.chupin at gmail dot com
                   ` (10 preceding siblings ...)
  2021-11-18  3:17 ` wjwray at gmail dot com
@ 2021-11-18  3:21 ` wjwray at gmail dot com
  2021-12-14  5:34 ` pinskia at gcc dot gnu.org
  2022-01-23 22:27 ` cvs-commit at gcc dot gnu.org
  13 siblings, 0 replies; 15+ messages in thread
From: wjwray at gmail dot com @ 2021-11-18  3:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55227

Will Wray <wjwray at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
  Attachment #51828|0                           |1
        is obsolete|                            |

--- Comment #14 from Will Wray <wjwray at gmail dot com> ---
Created attachment 51829
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51829&action=edit
V2 Patch Nov 17

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [Bug c++/55227] designated initializer for char array by string constant
  2012-11-07  8:27 [Bug c++/55227] New: designated initializer for char array by string constant pavel.v.chupin at gmail dot com
                   ` (11 preceding siblings ...)
  2021-11-18  3:21 ` wjwray at gmail dot com
@ 2021-12-14  5:34 ` pinskia at gcc dot gnu.org
  2022-01-23 22:27 ` cvs-commit at gcc dot gnu.org
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-14  5:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55227

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |iDingDong at outlook dot com

--- Comment #15 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 100698 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [Bug c++/55227] designated initializer for char array by string constant
  2012-11-07  8:27 [Bug c++/55227] New: designated initializer for char array by string constant pavel.v.chupin at gmail dot com
                   ` (12 preceding siblings ...)
  2021-12-14  5:34 ` pinskia at gcc dot gnu.org
@ 2022-01-23 22:27 ` cvs-commit at gcc dot gnu.org
  13 siblings, 0 replies; 15+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-01-23 22:27 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55227

--- Comment #16 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:2da90ad39bf8fa9ee287e040d1f4411cb7a2e7ed

commit r12-6825-g2da90ad39bf8fa9ee287e040d1f4411cb7a2e7ed
Author: Will Wray <wjwray@gmail.com>
Date:   Fri Jan 14 19:28:53 2022 -0500

    c++: designated init of char array by string constant [PR55227]

    There are two underlying bugs in the designated initialization of char
array
    fields by string literals that cause:

    (1) Rejection of valid cases with:
      (a) brace-enclosed string literal initializer (of any valid size), or
      (b) unbraced string literal shorter than the target char array field.

    (2) Acceptance of invalid cases with designators appearing within the
braces
        of a braced string literal, in which case the bogus 'designator' was
        being entirely ignored and the string literal treated as a positional
        initializer.

    The fixes above allow to address a FIXME in cp_complete_array_type:

      /* FIXME: this code is duplicated from reshape_init.
         Probably we should just call reshape_init here?  */

    I believe that this was obstructed by the designator bugs (see comment here
    https://patchwork.ozlabs.org/project/gcc/list/?series=199783)

            PR c++/55227

    gcc/cp/ChangeLog:

            * decl.cc (reshape_init_r): Only call has_designator_check when
            first_initializer_p or for the inner constructor element.
            (cp_complete_array_type): Call reshape_init on braced-init-list.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp2a/desig21.C: New test.

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2022-01-23 22:27 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-07  8:27 [Bug c++/55227] New: designated initializer for char array by string constant pavel.v.chupin at gmail dot com
2012-11-07 22:25 ` [Bug c++/55227] " daniel.kruegler at googlemail dot com
2020-04-08 10:11 ` ensadc at mailnesia dot com
2020-06-16 11:40 ` steven.spark at gmail dot com
2021-10-24 15:54 ` wjwray at gmail dot com
2021-10-24 21:25 ` wjwray at gmail dot com
2021-10-24 21:31 ` wjwray at gmail dot com
2021-10-25 17:06 ` wjwray at gmail dot com
2021-10-25 17:44 ` wjwray at gmail dot com
2021-11-04 23:43 ` wjwray at gmail dot com
2021-11-07  0:27 ` pinskia at gcc dot gnu.org
2021-11-18  3:17 ` wjwray at gmail dot com
2021-11-18  3:21 ` wjwray at gmail dot com
2021-12-14  5:34 ` pinskia at gcc dot gnu.org
2022-01-23 22:27 ` cvs-commit at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).