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

commit r16-6883-ge3bb887b3ee2baf118a67082c2bed2c8cc262875
Author: Simon Martin <[email protected]>
Date:   Sun Jan 18 11:07:10 2026 +0100

    c++: don't crash determining linkage of invalid TYPE_DECL [PR122391]
    
    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.

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

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