c++: Fix ICE with unexpanded parameter pack [PR99844]

Message ID 20210407215810.347038-1-polacek@redhat.com
State New
Headers show
Series
  • c++: Fix ICE with unexpanded parameter pack [PR99844]
Related show

Commit Message

Martin Sebor via Gcc-patches April 7, 2021, 9:58 p.m.
In explicit17.C, we weren't detecting an unexpanded parameter pack in
explicit(bool), so we crashed on a TEMPLATE_PARM_INDEX in constexpr.

I noticed the same is true for noexcept(), but only since my patch to
implement delayed parsing of noexcept.  Previously, we would detect the
unexpanded pack in push_template_decl but now the noexcept expression
has not yet been parsed, so we need to do it a bit later.

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

gcc/cp/ChangeLog:

	PR c++/99844
	* decl.c (build_explicit_specifier): Call
	check_for_bare_parameter_packs.
	* except.c (build_noexcept_spec): Likewise.

gcc/testsuite/ChangeLog:

	PR c++/99844
	* g++.dg/cpp2a/explicit16.C: Use c++20.
	* g++.dg/cpp0x/noexcept66.C: New test.
	* g++.dg/cpp2a/explicit17.C: New test.
---
 gcc/cp/decl.c                           |  3 +++
 gcc/cp/except.c                         |  2 ++
 gcc/testsuite/g++.dg/cpp0x/noexcept66.C | 13 +++++++++++++
 gcc/testsuite/g++.dg/cpp2a/explicit16.C |  2 +-
 gcc/testsuite/g++.dg/cpp2a/explicit17.C |  9 +++++++++
 5 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept66.C
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/explicit17.C


base-commit: 2f3d9104610cb2058cf091707a20c1c6eff8d470
-- 
2.30.2

Comments

Martin Sebor via Gcc-patches April 8, 2021, 4:24 a.m. | #1
On 4/7/21 5:58 PM, Marek Polacek wrote:
> In explicit17.C, we weren't detecting an unexpanded parameter pack in

> explicit(bool), so we crashed on a TEMPLATE_PARM_INDEX in constexpr.

> 

> I noticed the same is true for noexcept(), but only since my patch to

> implement delayed parsing of noexcept.  Previously, we would detect the

> unexpanded pack in push_template_decl but now the noexcept expression

> has not yet been parsed, so we need to do it a bit later.

> 

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


OK.

> gcc/cp/ChangeLog:

> 

> 	PR c++/99844

> 	* decl.c (build_explicit_specifier): Call

> 	check_for_bare_parameter_packs.

> 	* except.c (build_noexcept_spec): Likewise.

> 

> gcc/testsuite/ChangeLog:

> 

> 	PR c++/99844

> 	* g++.dg/cpp2a/explicit16.C: Use c++20.

> 	* g++.dg/cpp0x/noexcept66.C: New test.

> 	* g++.dg/cpp2a/explicit17.C: New test.

> ---

>   gcc/cp/decl.c                           |  3 +++

>   gcc/cp/except.c                         |  2 ++

>   gcc/testsuite/g++.dg/cpp0x/noexcept66.C | 13 +++++++++++++

>   gcc/testsuite/g++.dg/cpp2a/explicit16.C |  2 +-

>   gcc/testsuite/g++.dg/cpp2a/explicit17.C |  9 +++++++++

>   5 files changed, 28 insertions(+), 1 deletion(-)

>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept66.C

>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/explicit17.C

> 

> diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c

> index edab147c78d..3294b4fa943 100644

> --- a/gcc/cp/decl.c

> +++ b/gcc/cp/decl.c

> @@ -17991,6 +17991,9 @@ require_deduced_type (tree decl, tsubst_flags_t complain)

>   tree

>   build_explicit_specifier (tree expr, tsubst_flags_t complain)

>   {

> +  if (check_for_bare_parameter_packs (expr))

> +    return error_mark_node;

> +

>     if (instantiation_dependent_expression_p (expr))

>       /* Wait for instantiation, tsubst_function_decl will handle it.  */

>       return expr;

> diff --git a/gcc/cp/except.c b/gcc/cp/except.c

> index 0bbc229490e..cbafc09629b 100644

> --- a/gcc/cp/except.c

> +++ b/gcc/cp/except.c

> @@ -1228,6 +1228,8 @@ type_throw_all_p (const_tree type)

>   tree

>   build_noexcept_spec (tree expr, tsubst_flags_t complain)

>   {

> +  if (check_for_bare_parameter_packs (expr))

> +    return error_mark_node;

>     if (TREE_CODE (expr) != DEFERRED_NOEXCEPT

>         && !value_dependent_expression_p (expr))

>       {

> diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept66.C b/gcc/testsuite/g++.dg/cpp0x/noexcept66.C

> new file mode 100644

> index 00000000000..6c76d9146ad

> --- /dev/null

> +++ b/gcc/testsuite/g++.dg/cpp0x/noexcept66.C

> @@ -0,0 +1,13 @@

> +// PR c++/99844

> +// { dg-do compile { target c++11 } }

> +

> +template <bool... B>

> +struct S {

> + void fn() noexcept(B); // { dg-error "parameter packs not expanded" }

> +};

> +

> +void fn ()

> +{

> +  S<true> s;

> +  s.fn();

> +}

> diff --git a/gcc/testsuite/g++.dg/cpp2a/explicit16.C b/gcc/testsuite/g++.dg/cpp2a/explicit16.C

> index 9d95b0d669e..9c20f6332e9 100644

> --- a/gcc/testsuite/g++.dg/cpp2a/explicit16.C

> +++ b/gcc/testsuite/g++.dg/cpp2a/explicit16.C

> @@ -1,5 +1,5 @@

>   // PR c++/95066 - explicit malfunction with dependent expression.

> -// { dg-do compile { target c++2a } }

> +// { dg-do compile { target c++20 } }

>   

>   template <typename T>

>   struct Foo {

> diff --git a/gcc/testsuite/g++.dg/cpp2a/explicit17.C b/gcc/testsuite/g++.dg/cpp2a/explicit17.C

> new file mode 100644

> index 00000000000..38a61f4a273

> --- /dev/null

> +++ b/gcc/testsuite/g++.dg/cpp2a/explicit17.C

> @@ -0,0 +1,9 @@

> +// PR c++/99844

> +// { dg-do compile { target c++20 } }

> +

> +template <bool... B>

> +struct S {

> +  constexpr explicit(B) S() {} // { dg-error "parameter packs not expanded" }

> +};

> +

> +constexpr S<true> s;

> 

> base-commit: 2f3d9104610cb2058cf091707a20c1c6eff8d470

>

Patch

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index edab147c78d..3294b4fa943 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -17991,6 +17991,9 @@  require_deduced_type (tree decl, tsubst_flags_t complain)
 tree
 build_explicit_specifier (tree expr, tsubst_flags_t complain)
 {
+  if (check_for_bare_parameter_packs (expr))
+    return error_mark_node;
+
   if (instantiation_dependent_expression_p (expr))
     /* Wait for instantiation, tsubst_function_decl will handle it.  */
     return expr;
diff --git a/gcc/cp/except.c b/gcc/cp/except.c
index 0bbc229490e..cbafc09629b 100644
--- a/gcc/cp/except.c
+++ b/gcc/cp/except.c
@@ -1228,6 +1228,8 @@  type_throw_all_p (const_tree type)
 tree
 build_noexcept_spec (tree expr, tsubst_flags_t complain)
 {
+  if (check_for_bare_parameter_packs (expr))
+    return error_mark_node;
   if (TREE_CODE (expr) != DEFERRED_NOEXCEPT
       && !value_dependent_expression_p (expr))
     {
diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept66.C b/gcc/testsuite/g++.dg/cpp0x/noexcept66.C
new file mode 100644
index 00000000000..6c76d9146ad
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/noexcept66.C
@@ -0,0 +1,13 @@ 
+// PR c++/99844
+// { dg-do compile { target c++11 } }
+
+template <bool... B>
+struct S {
+ void fn() noexcept(B); // { dg-error "parameter packs not expanded" }
+};
+
+void fn ()
+{
+  S<true> s;
+  s.fn();
+}
diff --git a/gcc/testsuite/g++.dg/cpp2a/explicit16.C b/gcc/testsuite/g++.dg/cpp2a/explicit16.C
index 9d95b0d669e..9c20f6332e9 100644
--- a/gcc/testsuite/g++.dg/cpp2a/explicit16.C
+++ b/gcc/testsuite/g++.dg/cpp2a/explicit16.C
@@ -1,5 +1,5 @@ 
 // PR c++/95066 - explicit malfunction with dependent expression.
-// { dg-do compile { target c++2a } }
+// { dg-do compile { target c++20 } }
 
 template <typename T>
 struct Foo {
diff --git a/gcc/testsuite/g++.dg/cpp2a/explicit17.C b/gcc/testsuite/g++.dg/cpp2a/explicit17.C
new file mode 100644
index 00000000000..38a61f4a273
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/explicit17.C
@@ -0,0 +1,9 @@ 
+// PR c++/99844
+// { dg-do compile { target c++20 } }
+
+template <bool... B>
+struct S {
+  constexpr explicit(B) S() {} // { dg-error "parameter packs not expanded" }
+};
+
+constexpr S<true> s;