public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: ICE on unviable/ambiguous constrained dtors [PR96745]
@ 2023-01-30 21:36 Patrick Palka
  2023-02-02 22:33 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Patrick Palka @ 2023-01-30 21:36 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Patrick Palka

Here we're crashing from check_bases_and_members due to
CLASSTYPE_DESTRUCTOR being an OVERLOAD which, due to the pruning
performed by add_method, should only happen if there is no viable
destructor or the destructor is ambiguous.

This patch fixes this by making check_bases_and_members naturally handle
CLASSTYPE_DESTRUCTOR being an OVERLOAD.  It's then convenient to prune
the OVERLOAD after diagnosing the inevitable OR failure in check_methods.

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

	PR c++/96745

gcc/cp/ChangeLog:

	* class.cc (check_methods): Diagnose an unviable OVERLOAD
	set for CLASSTYPE_DESTRUCTOR differently from an ambiguous one.
	Then prune the OVERLOAD to a single function.
	(check_bases_and_members): Handle CLASSTYPE_DESTRUCTOR being
	an OVERLOAD when calling deduce_noexcept_on_destructor.
	Document why it has to be called before check_methods.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/concepts-dtor1.C: New test.
---
 gcc/cp/class.cc                             | 22 ++++++++++++++++++---
 gcc/testsuite/g++.dg/cpp2a/concepts-dtor1.C | 18 +++++++++++++++++
 2 files changed, 37 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-dtor1.C

diff --git a/gcc/cp/class.cc b/gcc/cp/class.cc
index d3ce8532d56..c6878cba2ae 100644
--- a/gcc/cp/class.cc
+++ b/gcc/cp/class.cc
@@ -4808,9 +4808,23 @@ check_methods (tree t)
 	     in that class with an empty argument list to select the destructor
 	     for the class, also known as the selected destructor. The program
 	     is ill-formed if overload resolution fails. */
+	  int viable = 0;
+	  for (tree fn : ovl_range (dtor))
+	    if (constraints_satisfied_p (fn))
+	      ++viable;
+	  gcc_checking_assert (viable != 1);
+
 	  auto_diagnostic_group d;
-	  error_at (location_of (t), "destructor for %qT is ambiguous", t);
+	  if (viable == 0)
+	    error_at (location_of (t), "no viable destructor for %qT", t);
+	  else
+	    error_at (location_of (t), "destructor for %qT is ambiguous", t);
 	  print_candidates (dtor);
+
+	  /* Arbitrarily prune the overload set to a single function for
+	     sake of error recovery.  */
+	  tree *slot = find_member_slot (t, dtor_identifier);
+	  *slot = get_first_fn (dtor);
 	}
       else if (user_provided_p (dtor))
 	TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = true;
@@ -6048,10 +6062,12 @@ check_bases_and_members (tree t)
   check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
 
   /* Deduce noexcept on destructor.  This needs to happen after we've set
-     triviality flags appropriately for our bases.  */
+     triviality flags appropriately for our bases, and before checking
+     overriden virtual functions via check_methods.  */
   if (cxx_dialect >= cxx11)
     if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
-      deduce_noexcept_on_destructor (dtor);
+      for (tree fn : ovl_range (dtor))
+	deduce_noexcept_on_destructor (fn);
 
   /* Check all the method declarations.  */
   check_methods (t);
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-dtor1.C b/gcc/testsuite/g++.dg/cpp2a/concepts-dtor1.C
new file mode 100644
index 00000000000..b1f3b4e579f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-dtor1.C
@@ -0,0 +1,18 @@
+// PR c++/96745
+// { dg-do compile { target c++20 } }
+
+template<class T>
+struct A { // { dg-error "destructor for 'A<int>' is ambiguous" }
+  ~A() requires true;
+  ~A() requires (!!true);
+};
+
+A<int> a;
+
+template<class T>
+struct B { // { dg-error "no viable destructor for 'B<int>'" }
+  ~B() requires false;
+  ~B() requires (!!false);
+};
+
+B<int> b;
-- 
2.39.1.367.g5cc9858f1b


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] c++: ICE on unviable/ambiguous constrained dtors [PR96745]
  2023-01-30 21:36 [PATCH] c++: ICE on unviable/ambiguous constrained dtors [PR96745] Patrick Palka
@ 2023-02-02 22:33 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2023-02-02 22:33 UTC (permalink / raw)
  To: Patrick Palka, gcc-patches

On 1/30/23 16:36, Patrick Palka wrote:
> Here we're crashing from check_bases_and_members due to
> CLASSTYPE_DESTRUCTOR being an OVERLOAD which, due to the pruning
> performed by add_method, should only happen if there is no viable
> destructor or the destructor is ambiguous.
> 
> This patch fixes this by making check_bases_and_members naturally handle
> CLASSTYPE_DESTRUCTOR being an OVERLOAD.  It's then convenient to prune
> the OVERLOAD after diagnosing the inevitable OR failure in check_methods.
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk and perhaps 12?

OK for trunk, this doesn't seem important to backport.

> 	PR c++/96745
> 
> gcc/cp/ChangeLog:
> 
> 	* class.cc (check_methods): Diagnose an unviable OVERLOAD
> 	set for CLASSTYPE_DESTRUCTOR differently from an ambiguous one.
> 	Then prune the OVERLOAD to a single function.
> 	(check_bases_and_members): Handle CLASSTYPE_DESTRUCTOR being
> 	an OVERLOAD when calling deduce_noexcept_on_destructor.
> 	Document why it has to be called before check_methods.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp2a/concepts-dtor1.C: New test.
> ---
>   gcc/cp/class.cc                             | 22 ++++++++++++++++++---
>   gcc/testsuite/g++.dg/cpp2a/concepts-dtor1.C | 18 +++++++++++++++++
>   2 files changed, 37 insertions(+), 3 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-dtor1.C
> 
> diff --git a/gcc/cp/class.cc b/gcc/cp/class.cc
> index d3ce8532d56..c6878cba2ae 100644
> --- a/gcc/cp/class.cc
> +++ b/gcc/cp/class.cc
> @@ -4808,9 +4808,23 @@ check_methods (tree t)
>   	     in that class with an empty argument list to select the destructor
>   	     for the class, also known as the selected destructor. The program
>   	     is ill-formed if overload resolution fails. */
> +	  int viable = 0;
> +	  for (tree fn : ovl_range (dtor))
> +	    if (constraints_satisfied_p (fn))
> +	      ++viable;
> +	  gcc_checking_assert (viable != 1);
> +
>   	  auto_diagnostic_group d;
> -	  error_at (location_of (t), "destructor for %qT is ambiguous", t);
> +	  if (viable == 0)
> +	    error_at (location_of (t), "no viable destructor for %qT", t);
> +	  else
> +	    error_at (location_of (t), "destructor for %qT is ambiguous", t);
>   	  print_candidates (dtor);
> +
> +	  /* Arbitrarily prune the overload set to a single function for
> +	     sake of error recovery.  */
> +	  tree *slot = find_member_slot (t, dtor_identifier);
> +	  *slot = get_first_fn (dtor);
>   	}
>         else if (user_provided_p (dtor))
>   	TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = true;
> @@ -6048,10 +6062,12 @@ check_bases_and_members (tree t)
>     check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
>   
>     /* Deduce noexcept on destructor.  This needs to happen after we've set
> -     triviality flags appropriately for our bases.  */
> +     triviality flags appropriately for our bases, and before checking
> +     overriden virtual functions via check_methods.  */
>     if (cxx_dialect >= cxx11)
>       if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
> -      deduce_noexcept_on_destructor (dtor);
> +      for (tree fn : ovl_range (dtor))
> +	deduce_noexcept_on_destructor (fn);
>   
>     /* Check all the method declarations.  */
>     check_methods (t);
> diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-dtor1.C b/gcc/testsuite/g++.dg/cpp2a/concepts-dtor1.C
> new file mode 100644
> index 00000000000..b1f3b4e579f
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-dtor1.C
> @@ -0,0 +1,18 @@
> +// PR c++/96745
> +// { dg-do compile { target c++20 } }
> +
> +template<class T>
> +struct A { // { dg-error "destructor for 'A<int>' is ambiguous" }
> +  ~A() requires true;
> +  ~A() requires (!!true);
> +};
> +
> +A<int> a;
> +
> +template<class T>
> +struct B { // { dg-error "no viable destructor for 'B<int>'" }
> +  ~B() requires false;
> +  ~B() requires (!!false);
> +};
> +
> +B<int> b;


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-02-02 22:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-30 21:36 [PATCH] c++: ICE on unviable/ambiguous constrained dtors [PR96745] Patrick Palka
2023-02-02 22:33 ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).