From: Martin Uecker Date: Fri, 9 Jan 2026 13:38:31 +0000 (+0100) Subject: c: Fix ignored qualifier issue for enumerations [PR123435,PR123463] X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=00b6346d5374efab8dabbf99d2647ad69b1df706;p=thirdparty%2Fgcc.git c: Fix ignored qualifier issue for enumerations [PR123435,PR123463] We accept a mismatch in qualifiers for enumerations and integers because we switch to the underlying type before checking that qualifiers match. PR c/123435 PR c/123463 gcc/c/ChangeLog: * c-typeck.cc (comptypes_internal): Test for qualifiers first. gcc/testsuite/ChangeLog: * gcc.dg/pr123435-1.c: New test. * gcc.dg/pr123435-2.c: New test. * gcc.dg/pr123463.c: New test. --- diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 43d4a5d5d9e..a762f3c47d1 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -1666,6 +1666,11 @@ comptypes_internal (const_tree type1, const_tree type2, || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK) return true; + /* Qualifiers must match. C99 6.7.3p9 */ + + if (TYPE_QUALS (t1) != TYPE_QUALS (t2)) + return false; + /* Enumerated types are compatible with integer types, but this is not transitive: two enumerated types in the same translation unit are compatible with each other only if they are the same type. */ @@ -1701,11 +1706,6 @@ comptypes_internal (const_tree type1, const_tree type2, if (TREE_CODE (t1) != TREE_CODE (t2)) return false; - /* Qualifiers must match. C99 6.7.3p9 */ - - if (TYPE_QUALS (t1) != TYPE_QUALS (t2)) - return false; - /* Allow for two different type nodes which have essentially the same definition. Note that we already checked for equality of the type qualifiers (just above). */ diff --git a/gcc/testsuite/gcc.dg/pr123435-1.c b/gcc/testsuite/gcc.dg/pr123435-1.c new file mode 100644 index 00000000000..18d579ee112 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr123435-1.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c23" } */ + +const enum E { A } *x; + +/* qualifier mismatch */ +_Static_assert(_Generic(x, unsigned int *: 0, default: 1), ""); + diff --git a/gcc/testsuite/gcc.dg/pr123435-2.c b/gcc/testsuite/gcc.dg/pr123435-2.c new file mode 100644 index 00000000000..7ad605cccc5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr123435-2.c @@ -0,0 +1,6 @@ +/* { dg-do compile } */ + +enum E { E1 = -1, E2 = 0, E3 = 1 }; +const volatile enum E i2; +extern int i2; /* { dg-error "conflicting type qualifiers" } */ + diff --git a/gcc/testsuite/gcc.dg/pr123463.c b/gcc/testsuite/gcc.dg/pr123463.c new file mode 100644 index 00000000000..1ae445aed41 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr123463.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ + +enum mm { L }; +extern const enum mm m[1]; +void f(unsigned egno) { + f(m); /* { dg-error "integer from pointer" } */ +} +