[C] Fix ignored qualifier issue for enumerations [PR123435,PR123463]

Message ID d143d370c82b7d287d90aaadffc6f9f66b7a1f1a.camel@gmail.com
State New
Headers
Series [C] Fix ignored qualifier issue for enumerations [PR123435,PR123463] |

Commit Message

Martin Uecker Jan. 9, 2026, 4:02 p.m. UTC
  Bootstrapped and regression tested on x86_64.



    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.c
  

Comments

Joseph Myers Jan. 9, 2026, 8:43 p.m. UTC | #1
On Fri, 9 Jan 2026, Martin Uecker wrote:

>     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.c

OK.
  

Patch

diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index 26805e5cfd6..553aad73b4a 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..eb3777504d6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr123435-1.c
@@ -0,0 +1,7 @@ 
+/* { dg-do compile } */
+
+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" } */
+}
+