1: Sample code that reproduces the problem.
Save as bug.c.
struct pair { int x, y; };
union U { struct pair p; int other; };
int test(void)
{
struct pair initial = {1, 2};
union U u = {.p = initial, .p.y = 7};
return u.p.y;
}
2: Exact command used to run SDCC on this sample code.
sdcc -mz80 --std=c11 -S bug.c -o bug-z80.asm
3: SDCC version tested.
SDCC 4.6.4 #17200 (Linux x86_64), unmodified official snapshot, tested on 19 November 2026.
4: Observed and expected behaviour.
SDCC compiles without a diagnostic. The generated code copies initial into u.p and returns the copied value of u.p.y, which is 2. It emits no store for .p.y = 7.
The first initialiser supplies the value of the entire structure u.p. C11 6.7.9 paragraph 19 requires the later initialiser to override the earlier value for the designated sub-object. Therefore u.p.x should retain the value 1, u.p.y should have the value 7, and test() should return 7. See WG14 DR 413 for the corresponding structure example.
5: Additional checks.
The generated STM8 code also returns 2 when compiled with sdcc -mstm8 --std=c11 -S bug.c -o bug-stm8.asm. GCC 14.2.0 returns 7 with -std=c11 -pedantic-errors -O2. In the analogous case with a structure enclosing p in place of the union, SDCC's generated code gives u.p.y the value 7 on both tested ports.
Related to [bugs:#2948], which reports another failure when initialising a structure member of a union.
The candidate patch attached to [bugs:#2948] includes a fix and regression test for this bug. Please review the patch there.
Related
Bugs: #2948