Menu ▾ ▴

#4210 Assignment to aggregates containing const members goes undiagnosed

closed-fixed
None
Front-end
5
7 hours ago
2026-10-23
No

1: Sample code that reproduces the problem.

struct S { const int x; };
struct S a, b;
void f(void)
{
  a = b;
}

2: Exact command used to run SDCC on this sample code.

sdcc -mz80 --std=c2y --nostdinc -S bug.c -o bug.asm

3: SDCC version tested.

SDCC 4.6.4 #17068 (Linux), tested 23 October 2026.

4: Observed and expected behaviour.

SDCC exits successfully and emits no diagnostic.

N3886 6.5.17.1 paragraph 2 requires a modifiable lvalue on the left of assignment. The definition in 6.3.3.1 paragraph 1 excludes structures and unions containing const-qualified members, recursively including elements of contained aggregates. The declaration of a need not itself include const for this rule to apply.

Reference: N3886.

5: Additional checks.

The missing diagnostic also occurs with a nested structure containing a const member, a union containing a const member, a const-qualified array member, and a const-qualified pointer member. Structures with ordinary members or pointers to const objects are valid and compile silently. GCC 13.3.0 rejects all five invalid assignments and accepts these valid cases.

All invalid cases reproduce on Z80, MCS51, STM8, PDK14 and MOS6502 in C99, C11, C23 and C2Y modes. GCC was invoked with -std=c2x -pedantic-errors -fsyntax-only. GCC 16.2 and Clang were not tested.

Discussion

  • Christopher Bazley

    Attaching a candidate fix for review. This is a straightforward patch that reuses the existing diagnostic message: E_CODE_WRITE.

     
    • Philipp Klaus Krause

      Looks good to me.

       
      • Christopher Bazley

        Thanks for the review. Committed as [r17126].

         

        Related

        Commit: [r17126]

  • Christopher Bazley

    • status: open --> closed-fixed
    • assigned_to: Christopher Bazley
     

Log in to post a comment.