]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: Fix ignored qualifier issue for enumerations [PR123435,PR123463]
authorMartin Uecker <uecker@tugraz.at>
Fri, 9 Jan 2026 13:38:31 +0000 (14:38 +0100)
committerMartin Uecker <uecker@gcc.gnu.org>
Sat, 10 Jan 2026 07:55:37 +0000 (08:55 +0100)
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.

gcc/c/c-typeck.cc
gcc/testsuite/gcc.dg/pr123435-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr123435-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr123463.c [new file with mode: 0644]

index 43d4a5d5d9e8d0240db3e26fa3c97e9024f8dd54..a762f3c47d1cad09ff45fba7bd323488ec443538 100644 (file)
@@ -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 (file)
index 0000000..18d579e
--- /dev/null
@@ -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 (file)
index 0000000..7ad605c
--- /dev/null
@@ -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 (file)
index 0000000..1ae445a
--- /dev/null
@@ -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" } */
+}
+