diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 360216b96621..e5dd8d54c516 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -776,7 +776,7 @@ composite_type_internal (tree t1, tree t2, struct composite_cache* cache) construction, return it. */ for (struct composite_cache *c = cache; c != NULL; c = c->next) - if (c->t1 == t1 && c->t2 == t2) + if ((c->t1 == t1 && c->t2 == t2) || (c->t1 == t2 && c->t2 == t1)) return c->composite; /* Otherwise, create a new type node and link it into the cache. */ diff --git a/gcc/testsuite/gcc.dg/gnu23-tag-composite-6.c b/gcc/testsuite/gcc.dg/gnu23-tag-composite-6.c new file mode 100644 index 000000000000..2411b04d3884 --- /dev/null +++ b/gcc/testsuite/gcc.dg/gnu23-tag-composite-6.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-std=gnu23" } */ + +int f() +{ + typedef struct foo bar; + struct foo { typeof(({ (struct foo { bar * x; }){ }; })) * x; } *q; + typeof(q->x) p; + 1 ? p : q; +} + diff --git a/gcc/testsuite/gcc.dg/pr120381.c b/gcc/testsuite/gcc.dg/pr120381.c new file mode 100644 index 000000000000..5c017e60c6b2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr120381.c @@ -0,0 +1,10 @@ +/* PR120381 */ +/* { dg-do compile } */ + +struct A { + struct A { /* { dg-error "nested redefinition" } */ + struct A *p; + } *p; +}; +int foo(const struct A *q) { return q->p == q; } +