c++: respect complain for -Wctad-maybe-unsupported [PR105143]

Message ID 20220406151158.3381793-1-ppalka@redhat.com
State New
Headers
Series c++: respect complain for -Wctad-maybe-unsupported [PR105143] |

Commit Message

Patrick Palka April 6, 2022, 3:11 p.m. UTC
  We were attempting to issue a -Wctad-maybe-unsupported warning even when
complain=tf_none, which led to a crash in the first testcase below and a
bogus error during SFINAE in the second testcase.

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk/11?

	PR c++/105143

gcc/cp/ChangeLog:

	* pt.cc (do_class_deduction): Check complain before issuing a
	-Wctad-maybe-unsupported warning.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/nodiscard1.C: New test.
	* g++.dg/warn/Wctad-maybe-unsupported4.C: New test.
---
 gcc/cp/pt.cc                                        |  2 +-
 gcc/testsuite/g++.dg/cpp2a/nodiscard1.C             | 13 +++++++++++++
 .../g++.dg/warn/Wctad-maybe-unsupported4.C          | 12 ++++++++++++
 3 files changed, 26 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/nodiscard1.C
 create mode 100644 gcc/testsuite/g++.dg/warn/Wctad-maybe-unsupported4.C
  

Comments

Jason Merrill April 6, 2022, 3:19 p.m. UTC | #1
On 4/6/22 11:11, Patrick Palka wrote:
> We were attempting to issue a -Wctad-maybe-unsupported warning even when
> complain=tf_none, which led to a crash in the first testcase below and a
> bogus error during SFINAE in the second testcase.
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk/11?

OK.

> 	PR c++/105143
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (do_class_deduction): Check complain before issuing a
> 	-Wctad-maybe-unsupported warning.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp2a/nodiscard1.C: New test.
> 	* g++.dg/warn/Wctad-maybe-unsupported4.C: New test.
> ---
>   gcc/cp/pt.cc                                        |  2 +-
>   gcc/testsuite/g++.dg/cpp2a/nodiscard1.C             | 13 +++++++++++++
>   .../g++.dg/warn/Wctad-maybe-unsupported4.C          | 12 ++++++++++++
>   3 files changed, 26 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/nodiscard1.C
>   create mode 100644 gcc/testsuite/g++.dg/warn/Wctad-maybe-unsupported4.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 1f0231f70e6..1805fa68440 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -30075,7 +30075,7 @@ do_class_deduction (tree ptype, tree tmpl, tree init,
>   
>     /* If CTAD succeeded but the type doesn't have any explicit deduction
>        guides, this deduction might not be what the user intended.  */
> -  if (fndecl != error_mark_node && !any_dguides_p)
> +  if ((complain & tf_warning) && fndecl != error_mark_node && !any_dguides_p)
>       {
>         if ((!DECL_IN_SYSTEM_HEADER (fndecl)
>   	   || global_dc->dc_warn_system_headers)
> diff --git a/gcc/testsuite/g++.dg/cpp2a/nodiscard1.C b/gcc/testsuite/g++.dg/cpp2a/nodiscard1.C
> new file mode 100644
> index 00000000000..c3c5094b619
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/nodiscard1.C
> @@ -0,0 +1,13 @@
> +// PR c++/105143
> +// { dg-do compile { target c++20 } }
> +// We used to crash here with "Error reporting routines re-entered".
> +
> +template<class...> struct A { };
> +
> +template<A V> using type = int;
> +
> +template<A V> [[nodiscard]] type<V> get();
> +
> +int main() {
> +  get<{}>(); // { dg-warning "nodiscard" }
> +}
> diff --git a/gcc/testsuite/g++.dg/warn/Wctad-maybe-unsupported4.C b/gcc/testsuite/g++.dg/warn/Wctad-maybe-unsupported4.C
> new file mode 100644
> index 00000000000..0d0e2fb45eb
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wctad-maybe-unsupported4.C
> @@ -0,0 +1,12 @@
> +// PR c++/105143
> +// { dg-do compile { target c++17 } }
> +// { dg-additional-options "-Werror=ctad-maybe-unsupported" }
> +
> +template<class...> struct A { };
> +
> +template<template<class...> class TT> auto f(...) -> decltype(TT()); // #1
> +template<template<class...> class TT> void f(int); // #2
> +
> +int main() {
> +  f<A>(0); // Calls #2 without issuing a -Wctad-maybe-unsupported diagnostic.
> +}
  

Patch

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 1f0231f70e6..1805fa68440 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -30075,7 +30075,7 @@  do_class_deduction (tree ptype, tree tmpl, tree init,
 
   /* If CTAD succeeded but the type doesn't have any explicit deduction
      guides, this deduction might not be what the user intended.  */
-  if (fndecl != error_mark_node && !any_dguides_p)
+  if ((complain & tf_warning) && fndecl != error_mark_node && !any_dguides_p)
     {
       if ((!DECL_IN_SYSTEM_HEADER (fndecl)
 	   || global_dc->dc_warn_system_headers)
diff --git a/gcc/testsuite/g++.dg/cpp2a/nodiscard1.C b/gcc/testsuite/g++.dg/cpp2a/nodiscard1.C
new file mode 100644
index 00000000000..c3c5094b619
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/nodiscard1.C
@@ -0,0 +1,13 @@ 
+// PR c++/105143
+// { dg-do compile { target c++20 } }
+// We used to crash here with "Error reporting routines re-entered".
+
+template<class...> struct A { };
+
+template<A V> using type = int;
+
+template<A V> [[nodiscard]] type<V> get();
+
+int main() {
+  get<{}>(); // { dg-warning "nodiscard" }
+}
diff --git a/gcc/testsuite/g++.dg/warn/Wctad-maybe-unsupported4.C b/gcc/testsuite/g++.dg/warn/Wctad-maybe-unsupported4.C
new file mode 100644
index 00000000000..0d0e2fb45eb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wctad-maybe-unsupported4.C
@@ -0,0 +1,12 @@ 
+// PR c++/105143
+// { dg-do compile { target c++17 } }
+// { dg-additional-options "-Werror=ctad-maybe-unsupported" }
+
+template<class...> struct A { };
+
+template<template<class...> class TT> auto f(...) -> decltype(TT()); // #1
+template<template<class...> class TT> void f(int); // #2
+
+int main() {
+  f<A>(0); // Calls #2 without issuing a -Wctad-maybe-unsupported diagnostic.
+}