]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Fix up recent cp_parser_template_id regression [PR123186]
authorJakub Jelinek <jakub@redhat.com>
Thu, 18 Dec 2025 15:41:10 +0000 (16:41 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Thu, 18 Dec 2025 15:41:10 +0000 (16:41 +0100)
On Fri, Dec 12, 2025 at 09:39:18AM -0500, Patrick Palka wrote:
> +      TREE_TYPE (templ)
> +     = build_typename_type (TYPE_CONTEXT (TREE_TYPE (templ)),
> +                            TYPE_NAME (TREE_TYPE (templ)),
> +                            fullname,
> +                            get_typename_type_tag (TREE_TYPE (templ)));
>        template_id = templ;
>      }
>    else

This change causes ICE e.g. on the following testcase.

The problem is that build_typename_type expects IDENTIFIER_NODE as the
second argument, e.g. it uses it as
      tree d = build_decl (input_location, TYPE_DECL, name, t);
argument.  But TYPE_NAME doesn't have to be an IDENTIFIER_NODE, it can
be a TYPE_DECL too and when we build a TYPE_DECL with TYPE_DECL as
DECL_NAME, it breaks all kinds of assumptions everywhere in the FE as well
as middle-end.

Fixed by using TYPE_IDENTIFIER instead.

2025-12-18  Jakub Jelinek  <jakub@redhat.com>

PR c++/123186
* parser.cc (cp_parser_template_id): Use TYPE_IDENTIFIER instead of
TYPE_NAME in second build_typename_type argument.

* g++.dg/template/crash133.C: New test.

gcc/cp/parser.cc
gcc/testsuite/g++.dg/template/crash133.C [new file with mode: 0644]

index 18df3b1014e9a4c8ed0f3a709d18af6da005aa15..521c05549f1ee6aecd7aeb165df5b7993166bb95 100644 (file)
@@ -20435,7 +20435,7 @@ cp_parser_template_id (cp_parser *parser,
                                   fullname, arguments);
       TREE_TYPE (templ)
        = build_typename_type (TYPE_CONTEXT (TREE_TYPE (templ)),
-                              TYPE_NAME (TREE_TYPE (templ)),
+                              TYPE_IDENTIFIER (TREE_TYPE (templ)),
                               fullname,
                               get_typename_tag (TREE_TYPE (templ)));
       template_id = templ;
diff --git a/gcc/testsuite/g++.dg/template/crash133.C b/gcc/testsuite/g++.dg/template/crash133.C
new file mode 100644 (file)
index 0000000..9a1a6b1
--- /dev/null
@@ -0,0 +1,6 @@
+// PR c++/123186
+
+template <class T>
+struct A : T {
+  typename A <T>::template B <42> C;
+};