On 1/18/26 12:04 AM, Simon Martin wrote:
On Sat Jan 17, 2026 at 5:00 PM CET, Simon Martin wrote:
We currently ICE in decl_linkage for the following invalid input
because we access the TYPE_MAIN_VARIANT of error_mark_node

===
extern "C" {
   template <a> class b;
   struct {
     typedef b:
===

This patch fixes this by returning no linkage for any TYPE_DECL with an
erroneous type.

        PR c++/122391

gcc/cp/ChangeLog:

        * tree.cc (decl_linkage): Return lk_none for TYPE_DECLs with
        erroneous type.

gcc/testsuite/ChangeLog:

        * g++.dg/parse/bitfield10.C: New test.

I forgot to say that I've succesfully bootstrapped and regtested this on
x86_64-pc-linux-gnu. OK for trunk?

OK.

---
  gcc/cp/tree.cc                          | 11 +++++++----
  gcc/testsuite/g++.dg/parse/bitfield10.C |  9 +++++++++
  2 files changed, 16 insertions(+), 4 deletions(-)
  create mode 100644 gcc/testsuite/g++.dg/parse/bitfield10.C

diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index a57eefaf11f3..030af4d219d1 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -6440,10 +6440,13 @@ decl_linkage (tree decl)
      {
        /* But this could be a typedef name for linkage purposes, in which
         case we're interested in the linkage of the main decl.  */
-      if (decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)))
-         /* Likewise for the injected-class-name.  */
-         || DECL_SELF_REFERENCE_P (decl))
-       decl = TYPE_MAIN_DECL (TREE_TYPE (decl));
+      tree type = TREE_TYPE (decl);
+      if (type == error_mark_node)
+       return lk_none;
+      else if (decl == TYPE_NAME (TYPE_MAIN_VARIANT (type))
+              /* Likewise for the injected-class-name.  */
+              || DECL_SELF_REFERENCE_P (decl))
+       decl = TYPE_MAIN_DECL (type);
        else
        return lk_none;
      }
diff --git a/gcc/testsuite/g++.dg/parse/bitfield10.C 
b/gcc/testsuite/g++.dg/parse/bitfield10.C
new file mode 100644
index 000000000000..c5ab75932e7c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/parse/bitfield10.C
@@ -0,0 +1,9 @@
+/* PR c++/122391 */
+/* { dg-do "compile" } */
+
+extern "C" {
+  template <int> class b; /* { dg-error "with C linkage" } */
+  struct {
+    typedef b: /* { dg-error "at end of input" } */
+
+/* { dg-excess-errors "" } */



Reply via email to