https://gcc.gnu.org/g:b1ab6f77ae53b37eaf9db6e0defae67f3df0b246

commit r17-6368-gb1ab6f77ae53b37eaf9db6e0defae67f3df0b246
Author: Vineet Gupta <[email protected]>
Date:   Tue Nov 17 15:36:19 2026 -0800

    c: keep the address space in a C23 canonical type quietly [PR128789]
    
    Since r17-1272 (PR125252), c_type_canonical replaces a tagged structure
    nested behind a pointer member with an incomplete type, and copies its
    qualifiers with qualify_type.  qualify_type merges two types and
    diagnoses disjoint address spaces.  The new type is generic, so on x86
    a member such as
    
      struct S { int x; };
      struct C { struct S __seg_gs *p; };
    
    is rejected in C23 mode with "'struct S' and '__seg_gs struct S' are in
    disjoint named address spaces".  GCC 16.1 accepts it.
    
    Nothing is being merged here: the incomplete type should just take the
    qualifiers of the type it replaces.  Build it with those directly.
    
            PR c/128789
    
    gcc/c/ChangeLog:
    
            * c-typeck.cc (c_type_canonical): Copy the qualifiers of the
            replaced type directly instead of using qualify_type.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/i386/addr-space-7.c: New test.

Diff:
---
 gcc/c/c-typeck.cc                            |  4 +++-
 gcc/testsuite/gcc.target/i386/addr-space-7.c | 17 +++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index d7a77ea9a021..6c9e7d75139c 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -675,7 +675,9 @@ c_type_canonical (tree type)
          tree new_node = make_node (TREE_CODE (m));
          TYPE_NAME (new_node) = TYPE_NAME (m);
          SET_TYPE_STRUCTURAL_EQUALITY (new_node);
-         new_node = qualify_type (new_node, m);
+         new_node = c_build_qualified_type
+                      (new_node, {TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (m),
+                                  TYPE_ADDR_SPACE (m)});
          TREE_TYPE (f) = c_reconstruct_complex_type (TREE_TYPE (x),
                                                      new_node);
        }
diff --git a/gcc/testsuite/gcc.target/i386/addr-space-7.c 
b/gcc/testsuite/gcc.target/i386/addr-space-7.c
new file mode 100644
index 000000000000..706ba929a150
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/addr-space-7.c
@@ -0,0 +1,17 @@
+/* A member pointing to a tagged structure in a named address space must
+   not make the canonical type of the enclosing structure ill-formed.  */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu23 -O2" } */
+
+struct S { int x; };
+
+struct C { struct S __seg_gs *p; };
+struct D { struct S __seg_fs **pp; const struct S __seg_gs *cp; };
+
+int
+f (struct C *c)
+{
+  return c->p->x;
+}
+
+/* { dg-final { scan-assembler "%gs:" } } */

Reply via email to