c++: Fix ICE with invalid requires-expression [PR100055]
authorMarek Polacek <polacek@redhat.com>
Thu, 22 Apr 2021 21:32:01 +0000 (17:32 -0400)
committerMarek Polacek <polacek@redhat.com>
Mon, 3 May 2021 16:39:43 +0000 (12:39 -0400)
This fixes a crash on invalid requires-expression: in this test,
current_template_parms is null so accessing TEMPLATE_PARMS_CONSTRAINTS
is going to fail.  So don't crash, but make sure we've complained
already.

gcc/cp/ChangeLog:

PR c++/100055
* decl.c (grokfndecl): Check current_template_parms.

gcc/testsuite/ChangeLog:

PR c++/100055
* g++.dg/concepts/diagnostic18.C: New test.

gcc/cp/decl.c
gcc/testsuite/g++.dg/concepts/diagnostic18.C [new file with mode: 0644]

index d1e73373660561c3b3d8a3f181ceff06a2abba2f..316ad4c1426940bd4f51197a6297eefc24064fec 100644 (file)
@@ -9737,7 +9737,14 @@ grokfndecl (tree ctype,
                      && (processing_template_decl
                          > template_class_depth (ctx)));
       if (memtmpl)
-        tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
+       {
+         if (!current_template_parms)
+           /* If there are no template parameters, something must have
+              gone wrong.  */
+           gcc_assert (seen_error ());
+         else
+           tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
+       }
       tree ci = build_constraints (tmpl_reqs, decl_reqs);
       if (concept_p && ci)
         {
diff --git a/gcc/testsuite/g++.dg/concepts/diagnostic18.C b/gcc/testsuite/g++.dg/concepts/diagnostic18.C
new file mode 100644 (file)
index 0000000..79f371b
--- /dev/null
@@ -0,0 +1,7 @@
+// PR c++/100055
+// { dg-do compile { target concepts } }
+
+void foo(auto&& arg) requires({}); // { dg-error "statement-expressions are not allowed|braced-groups" }
+
+template <auto = 0> requires ([]{}()); // { dg-error "expected unqualified-id" }
+auto f() requires ([]{}());