https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124030
Bug ID: 124030
Summary: Missing 'note' for previous definition and misleading
error location when multiple 'default' labels are
present in a switch statement
Product: gcc
Version: 15.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: 220246428 at seu dot edu.cn
Target Milestone: ---
In a switch statement containing multiple default labels, GCC only identifies
the last occurrence as the error location and fails to provide a note pointing
to the previous definition. This behavior is inconsistent with how GCC handles
other redefinition errors (like variables) and makes debugging significantly
harder in large or machine-generated source files.
According to the C11 Standard (ISO/IEC 9899:2011):
6.8.4.2 The switch statement / Constraints (P3): "There shall be at most one
default label in a switch statement."
While GCC does emit a diagnostic to satisfy 5.1.1.3 (Diagnostics), its quality
of implementation (QoI) is poor because it lacks the "reference to the original
definition" that modern developers expect.
Minimal Reproducible Example:
/* test_default.c */
void test_switch(int x) {
switch (x) {
default: /* First definition: Line 4 */
break;
case 1:
break;
default: /* Collision point: Line 8 */
break;
}
}
Steps to reproduce: gcc -c test_default.c
Actual Results :
test_default.c: In function 'test_switch':
test_default.c:8:9: error: multiple default labels in one switch
8 | default:
| ^~~~~~~
(Note: There is no indication of where the first 'default' was defined.)
In the case of duplicate variable declarations or even some other label types,
GCC typically provides a note: previous definition was here. The absence of
such a note for default labels creates an inconsistent user experience. Without
pointing to the first occurrence, the compiler assumes the second one is the
error. However, the logical error might actually be the first default label. A
high-quality diagnostic should present both points of the conflict to the
developer.