On 4/28/21 4:55 PM, Marek Polacek wrote:
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.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK.

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                                | 9 ++++++++-
  gcc/testsuite/g++.dg/concepts/diagnostic18.C | 7 +++++++
  2 files changed, 15 insertions(+), 1 deletion(-)
  create mode 100644 gcc/testsuite/g++.dg/concepts/diagnostic18.C

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 60dc2bf182d..1437457241e 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -9702,7 +9702,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
index 00000000000..79f371b8092
--- /dev/null
+++ b/gcc/testsuite/g++.dg/concepts/diagnostic18.C
@@ -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 ([]{}());

base-commit: c99f3747131377956e3bd8e393911c959ef5ff34


Reply via email to