Bug 22652 – importC: Braceless initializer of nested struct is rejected.
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-01-05T01:40:11Z
Last change time
2022-09-17T08:44:33Z
Keywords
ImportC, rejects-valid
Assigned to
No Owner
Creator
dave287091
Comments
Comment #0 by dave287091 — 2022-01-05T01:40:11Z
Consider the following C program:
// s.c
struct S1 {
int x, y;
};
struct S2 {
struct S1 s;
};
int printf(const char*, ...);
int main(void){
int x = {1};
struct S1 a = {0}; // ok
struct S2 b = {{0}}; // ok
struct S2 c = {1}; // Error: cannot implicitly convert expression `1` of type `int` to `S1`
struct S2 c2 = {1, 2}; // Error: cannot implicitly convert expression `1` of type `int` to `S1`
struct S1 d[2] = {1, 2}; // Error: variable `s.main.d` is not a static and cannot have static initializer
struct S1 e[] = {3, 4}; // Error: variable `s.main.e` is not a static and cannot have static initializer
printf("%d,%d\n", b.s.x, b.s.y); // should print 0, 0
printf("%d,%d\n", c.s.x, c.s.y); // should print 1, 0
printf("%d,%d\n", c2.s.x, c2.s.y); // should print 1, 2
printf("%d,%d\n", d[0].x, d[0].y); // should print 1, 2
printf("%d,%d\n", d[1].x, d[1].y); // should print 0, 0
printf("%zu\n", sizeof(e) / sizeof(e[0])); // should print 1
printf("%d,%d\n", e[0].x, e[0].y); // should print 3, 4
return 0;
}
C11 does not require braces. Clauses 20 and 21 of 6.7.9 Initialization address what should be done.
"variable `s.main.d` is not a static and cannot have static initializer” is also a weird error message in this case.
Comment #1 by dave287091 — 2022-01-05T05:22:28Z
(In reply to dave287091 from comment #0)
> C11 does not require braces.
Should’ve been “C11 does not require braces for subobjects."