1: Sample code that reproduces the problem.
int object = _Generic(0, float: 1, float: 2, default: 0);
2: Exact command used to run SDCC on this sample code.
sdcc -mz80 --std=c2y -S bug.c
3: SDCC version tested.
SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/r4k/r5k/r6k/sm83/tlcs90/ez80/z80n/r800/ds390/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15 TD- 4.6.4 #0 (Linux)
published under GNU General Public License (GPL)
Built from clean official SVN r16977 sources on Linux.
4: Observed and expected behaviour.
SDCC exits successfully and emits no diagnostic. A constraint violation should be diagnosed under WG14 N3886, 6.5.2.1 paragraph 2:
A generic selection shall have no more than one default generic association. No two generic
associations in the same generic selection shall specify compatible types.
This restriction applies even when neither association matches the controlling type. SDCC diagnoses duplicate matching associations, but silently accepts duplicate unselected associations. A typedef alias for float behaves the same way.
5: Additional checks.
All invalid variants listed below are silently accepted in C2Y mode on all 19 configurations tested. Every corresponding valid control compiles without diagnostics. GCC 13.3.0, invoked with -std=c2x -pedantic-errors -fsyntax-only, rejects all invalid variants and accepts all valid cases.
| Case | Expected | SDCC | GCC |
|---|---|---|---|
generic-unselected-duplicates.c |
Diagnostic required | Accepted silently | Rejected |
generic-unselected-alias.c |
Diagnostic required | Accepted silently | Rejected |
generic-distinct-control.c |
Valid | Accepted silently | Accepted |
Configurations tested: MCS51 default/large/medium/stack-auto, DS390, Z80, Z180, Rabbit 2000/4000, SM83, TLCS-90, HC08, S08, STM8 default/large, PDK13/14/15, and eZ80.
Implementation observation: The association loop in src/SDCCast.c checks each association against the controlling type, but does not compare association types with one another.
Please consider the attached candidate patch.
It checks association types for compatibility independently of whether they match the controlling type. It retains the checks for multiple defaults and multiple matches. It uses
compatibleTypesso compatible pointers to arrays with an unspecified versus specified bound are detected too.The proposed patch can only be applied after the candidate fix for [bugs:#4133], whose qualifier check distinguishes bit-precise types correctly. The new diagnostic follows the existing enum sequence, whether or not the [bugs:#4135] diagnostic has already been added.
Testing: 98 diagnostic checks passed. Diagnostic configurations tested: Z80, STM8, MCS51, MCS51 large, MCS51 stack-auto, PDK14, S08. All focused checks passed. The counts include positive controls as well as expected diagnostics.
Related
Bugs: #4133
Bugs: #4135