Fix #21210 - ImportC: Initializing struct containing array with = {0} fails (#21211)

Fixes https://github.com/dlang/dmd/issues/21210
This commit is contained in:
drpriver 2025-04-12 04:17:24 -07:00 committed by GitHub
parent ce6cef9762
commit d66ef6a26d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 2 deletions

View file

@ -761,9 +761,13 @@ Initializer initializerSemantic(Initializer init, Scope* sc, ref Type tx, NeedIn
// C11 6.2.5-20 "element type shall be complete whenever the array type is specified"
assert(0); // should have been detected by parser
}
auto bt = tsa.nextOf().toBasetype();
auto tnsa = tsa.nextOf().toBasetype().isTypeSArray();
if (auto tnss = bt.isTypeStruct())
{
return subStruct(tnss, index);
}
auto tnsa = bt.isTypeSArray();
auto ai = new ArrayInitializer(ci.loc);
ai.isCarray = true;

View file

@ -201,6 +201,30 @@ void test8()
assert(s8.leaf.state == 50);
}
/**************************************/
// https://github.com/dlang/dmd/issues/21210
struct Sa {int x, y;};
struct Sb {
struct Sa v[1];
};
struct Sb s9 = {0};
void test9()
{
struct Sb v = {1};
__check(v.v[0].x == 1);
__check(v.v[0].y == 0);
struct Sb v2[1] = {1};
__check(v2[0].v[0].x == 1);
__check(v2[0].v[0].y == 0);
struct Sb v3[2] = {1, 2, 3, 4};
__check(v3[0].v[0].x == 1);
__check(v3[0].v[0].y == 2);
__check(v3[1].v[0].x == 3);
__check(v3[1].v[0].y == 4);
}
/**************************************/
int main()
@ -213,5 +237,6 @@ int main()
test6();
test7();
test8();
test9();
return 0;
}