Menu

#3121 -:0: error 69: struct/union/array '': initialization needs curly braces

open
nobody
None
Front-end
5
2024-05-22
2020-09-30
No

When trying to compile the following code, I see two bugs in an error mesage:

struct T {
        int k;
        int l;
};
struct S {
        int i;
        struct T t;
};

struct T x = {.l = 43, .k = 42, };

void f(void)
{
        struct S l = { 1, .t = x, .t.l = 41, }; // Error on this line
        ASSERT (l.t.k == 41);
        ASSERT (l.t.k == 42);
}

The message is

-:0: error 69: struct/union/array '': initialization needs curly braces

1) The file name and line number are missing (apparently the error is triggered by the declaration inside f, so it should refer to that line.
2) There should be no error here. I understand that SDCC requires curly braces in some places where they are optional in the standard. But in this case, there never should be curly braces.

Related

Bugs: #3121
Bugs: #3408
Feature Requests: #562
Feature Requests: #797

Discussion

  • Benedikt Freisen

    Regarding point 2, i.e. "There should be no error here":

    Isn't that more or less documented in section 3.1.1 of the manual?
    It only says "initialization of structure arrays must be fully braced", but I believe that the problem occurs with all combinations of arrays, structs and unions.
    I tried to find a solution a year or two ago, but both approaches that I tried turned out to be deadends.
    I think that expanding the initializer list as early as possible, i.e right when they are being parsed, is the most realistic approach.

     
    • Philipp Klaus Krause

      But that "fully braced" can only mean that SDCC does not allow omitting braces, even where the standard does allow omitting them.
      In this case, there are omitted braces. To me that initalizer looks "fully braced".

       

      Last edit: Philipp Klaus Krause 2020-09-30
      • Benedikt Freisen

        Now that I look at it, again, yes, that should work.
        One potential problem that comes to mind is the struct assignment .t = x.
        Maybe older versions of SDCC would simply have thrown a different error and the special case of struct assignments in initializer lists slipped through, ever since.

         
  • Philipp Klaus Krause

    Over at [bugs:#3121], I posted a patch for initalizing struct/union.
    Unfortunately, it apparently causes a regression here: This code is now accepted, but the .t.l = 41 is ignored (with a warning, but that doesn't feel good enough).

     

    Related

    Bugs: #3121


    Last edit: Philipp Klaus Krause 2024-05-16
  • Nikita Koss

    Nikita Koss - 2024-05-22

    Is this same bug? Not-first element of the union is not initialized, reporting a warning. Only for const.

    struct A {
      char a;
      char b;
    };
    
    struct B {
      char a;
      union {
        char b;
        char c;
      };
    };
    
    const struct A foo = {
        .b = 42,
    };
    
    const struct B bar = {
        .c = 42,
    };
    
    > sdcc main.c
    main.c:18: warning 147: excess elements in struct initializer after 'bar'
    
          000000                        137 _foo:
          000000 00                     138     .db #0x00   ; 0
          000001 2A                     139     .db #0x2a   ; 42
          000002                        140 _bar:
          000002 00                     141     .db #0x00   ; 0
          000003 00                     142     .db #0x00   ; 0
    
     

Log in to post a comment.

MongoDB Logo MongoDB