]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Fix error recovery after export keyword [PR121832] master trunk
authorNathaniel Shead <nathanieloshead@gmail.com>
Fri, 12 Sep 2025 13:26:20 +0000 (23:26 +1000)
committerNathaniel Shead <nathanieloshead@gmail.com>
Sun, 1 Feb 2026 01:10:27 +0000 (12:10 +1100)
When we enter cp_parser_explicit_template_declaration with the following
tokens being 'template <>', we never parse a parameter list and so with
-fconcepts we crash dereferencing a null pointer.  This can currently
only happen after a non-modules 'export' declaration, as all other paths
check early for this case.

PR c++/121832

gcc/cp/ChangeLog:

* parser.cc (cp_parser_explicit_template_declaration): Check for
null.

gcc/testsuite/ChangeLog:

* g++.dg/concepts/pr121832.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
gcc/cp/parser.cc
gcc/testsuite/g++.dg/concepts/pr121832.C [new file with mode: 0644]

index 392d1f90ac81174340b320f746dbee048f5d4a58..4d988c27cb802edceca6b0c4c64741061a94aea4 100644 (file)
@@ -36275,7 +36275,7 @@ cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
   cp_parser_require_end_of_template_parameter_list (parser);
 
   /* Manage template requirements */
-  if (flag_concepts)
+  if (flag_concepts && current_template_parms)
   {
     tree reqs = get_shorthand_constraints (current_template_parms);
     if (tree treqs = cp_parser_requires_clause_opt (parser, false))
diff --git a/gcc/testsuite/g++.dg/concepts/pr121832.C b/gcc/testsuite/g++.dg/concepts/pr121832.C
new file mode 100644 (file)
index 0000000..9a4c5c0
--- /dev/null
@@ -0,0 +1,10 @@
+// { dg-do compile }
+// { dg-additional-options "-fconcepts -fno-modules" }
+
+export template<> a{};  // { dg-error "explicit spec" }
+// { dg-warning "'export' not implemented" "" { target c++98_only } .-1 }
+// { dg-warning "'export' is deprecated" "" { target { c++11 && c++17_down } } .-2 }
+// { dg-warning "'export' is enabled with '-fmodules'" "" { target c++20 } .-3 }
+
+// { dg-prune-output "'a' does not name a type" }
+// { dg-prune-output "extra ';' outside of a function" }