diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index dc2d39a875122df7ff21083aabb90b8f3ca3b47a..9153c74f0694de2ddf3053da6966c2492a80992d 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -48218,7 +48218,8 @@ synthesize_implicit_template_parm  (cp_parser *parser, tree constr)
      function template is equivalent to an explicit template.
 
      Note that DECL_ARTIFICIAL is used elsewhere for template parameters.  */
-  DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
+  if (TREE_VALUE (new_parm) != error_mark_node)
+    DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
 
   // Chain the new parameter to the list of implicit parameters.
   if (parser->implicit_template_parms)
diff --git a/gcc/testsuite/g++.dg/cpp2a/pr96437.C b/gcc/testsuite/g++.dg/cpp2a/pr96437.C
new file mode 100644
index 0000000000000000000000000000000000000000..79d481e70027e01fad5fed65133c0a9ffa8de85e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/pr96437.C
@@ -0,0 +1,4 @@
+/* { dg-do compile } */
+/* { dg-excess-errors "" } */
+
+template <()> void A(auto){}