c++: ICE with unnamed tparm and concept [PR103408]

Message ID 20211201151645.1497965-1-polacek@redhat.com
State Committed
Commit 6f43a8a08053a871e785e2ebc80383e0849efb6f
Headers
Series c++: ICE with unnamed tparm and concept [PR103408] |

Commit Message

Marek Polacek Dec. 1, 2021, 3:16 p.m. UTC
  Here we crash when issuing the "constraint C has type T, not bool"
error, because pp_cxx_parameter_mapping wasn't prepared to see an
anonymous template parameter.  With this patch we print

error: constraint 'auto(<lambda>) [with <unnamed> = 0]' has type '<lambda()>', not 'bool'

The "<unnamed>" is what this patch adds.

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

	PR c++/103408

gcc/cp/ChangeLog:

	* cxx-pretty-print.c (pp_cxx_parameter_mapping): Print "<unnamed>"
	rather than crash on an unnamed template parameter.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp23/concepts-err1.C: New test.
---
 gcc/cp/cxx-pretty-print.c                  | 4 +++-
 gcc/testsuite/g++.dg/cpp23/concepts-err1.C | 6 ++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp23/concepts-err1.C


base-commit: 6b8ecbc6d6652d061d7c72c64352d51eca2df6ca
  

Comments

Jason Merrill Dec. 2, 2021, 4:17 a.m. UTC | #1
On 12/1/21 10:16, Marek Polacek wrote:
> Here we crash when issuing the "constraint C has type T, not bool"
> error, because pp_cxx_parameter_mapping wasn't prepared to see an
> anonymous template parameter.  With this patch we print
> 
> error: constraint 'auto(<lambda>) [with <unnamed> = 0]' has type '<lambda()>', not 'bool'
> 
> The "<unnamed>" is what this patch adds.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK.

> 	PR c++/103408
> 
> gcc/cp/ChangeLog:
> 
> 	* cxx-pretty-print.c (pp_cxx_parameter_mapping): Print "<unnamed>"
> 	rather than crash on an unnamed template parameter.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp23/concepts-err1.C: New test.
> ---
>   gcc/cp/cxx-pretty-print.c                  | 4 +++-
>   gcc/testsuite/g++.dg/cpp23/concepts-err1.C | 6 ++++++
>   2 files changed, 9 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp23/concepts-err1.C
> 
> diff --git a/gcc/cp/cxx-pretty-print.c b/gcc/cp/cxx-pretty-print.c
> index 25cabfee39f..3ea357deb80 100644
> --- a/gcc/cp/cxx-pretty-print.c
> +++ b/gcc/cp/cxx-pretty-print.c
> @@ -2891,8 +2891,10 @@ pp_cxx_parameter_mapping (cxx_pretty_printer *pp, tree map)
>   
>         if (TYPE_P (parm))
>   	pp->type_id (parm);
> +      else if (tree name = DECL_NAME (TEMPLATE_PARM_DECL (parm)))
> +	pp_cxx_tree_identifier (pp, name);
>         else
> -	pp_cxx_tree_identifier (pp, DECL_NAME (TEMPLATE_PARM_DECL (parm)));
> +	pp->translate_string ("<unnamed>");
>   
>         pp_cxx_whitespace (pp);
>         pp_equal (pp);
> diff --git a/gcc/testsuite/g++.dg/cpp23/concepts-err1.C b/gcc/testsuite/g++.dg/cpp23/concepts-err1.C
> new file mode 100644
> index 00000000000..e5bdc542bad
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp23/concepts-err1.C
> @@ -0,0 +1,6 @@
> +// PR c++/103408
> +// { dg-do compile { target c++23 } }
> +
> +template<int>
> +concept C = auto([]{}); // { dg-error "constraint" }
> +static_assert(C<0>); // { dg-error "non-constant condition for static assertion" }
> 
> base-commit: 6b8ecbc6d6652d061d7c72c64352d51eca2df6ca
>
  

Patch

diff --git a/gcc/cp/cxx-pretty-print.c b/gcc/cp/cxx-pretty-print.c
index 25cabfee39f..3ea357deb80 100644
--- a/gcc/cp/cxx-pretty-print.c
+++ b/gcc/cp/cxx-pretty-print.c
@@ -2891,8 +2891,10 @@  pp_cxx_parameter_mapping (cxx_pretty_printer *pp, tree map)
 
       if (TYPE_P (parm))
 	pp->type_id (parm);
+      else if (tree name = DECL_NAME (TEMPLATE_PARM_DECL (parm)))
+	pp_cxx_tree_identifier (pp, name);
       else
-	pp_cxx_tree_identifier (pp, DECL_NAME (TEMPLATE_PARM_DECL (parm)));
+	pp->translate_string ("<unnamed>");
 
       pp_cxx_whitespace (pp);
       pp_equal (pp);
diff --git a/gcc/testsuite/g++.dg/cpp23/concepts-err1.C b/gcc/testsuite/g++.dg/cpp23/concepts-err1.C
new file mode 100644
index 00000000000..e5bdc542bad
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp23/concepts-err1.C
@@ -0,0 +1,6 @@ 
+// PR c++/103408
+// { dg-do compile { target c++23 } }
+
+template<int>
+concept C = auto([]{}); // { dg-error "constraint" }
+static_assert(C<0>); // { dg-error "non-constant condition for static assertion" }