c++: deleted fn and noexcept inst [PR101532, PR104225]

Message ID 20220125170818.1898984-1-ppalka@redhat.com
State New
Headers
Series c++: deleted fn and noexcept inst [PR101532, PR104225] |

Commit Message

Patrick Palka Jan. 25, 2022, 5:08 p.m. UTC
  Here when attempting to use B's implicitly deleted default constructor,
mark_used rightfully returns false, but for the wrong reason: it
tries to instantiate the implicit noexcept specifier, which only
silently fails because get_defaulted_eh_spec suppresses diagnostics
for deleted functions.  This causes us to ICE on the first testcase
(thanks to the sanity check in finish_expr_stmt) and accept the second
invalid testcase without complaint.

To fix this, this patch makes mark_used avoid attempting to instantiate
the noexcept specifier on a deleted function, so that mark_used will
instead directly reject (and diagnose) such a function due to its
deletedness.

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

	PR c++/101532
	PR c++/104225

gcc/cp/ChangeLog:

	* decl2.cc (mark_used): Don't call with
	maybe_instantiate_noexcept on a deleted function.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/nsdmi-template21.C: New test.
	* g++.dg/cpp0x/nsdmi-template22.C: New test.
---
 gcc/cp/decl2.cc                               |  1 +
 gcc/testsuite/g++.dg/cpp0x/nsdmi-template21.C | 10 ++++++++++
 gcc/testsuite/g++.dg/cpp0x/nsdmi-template22.C | 12 ++++++++++++
 3 files changed, 23 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-template21.C
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-template22.C
  

Comments

Patrick Palka Jan. 25, 2022, 5:33 p.m. UTC | #1
On Tue, 25 Jan 2022, Patrick Palka wrote:

> Here when attempting to use B's implicitly deleted default constructor,
> mark_used rightfully returns false, but for the wrong reason: it
> tries to instantiate the implicit noexcept specifier, which only
> silently fails because get_defaulted_eh_spec suppresses diagnostics
> for deleted functions.  This causes us to ICE on the first testcase
> (thanks to the sanity check in finish_expr_stmt) and accept the second
> invalid testcase without complaint.
> 
> To fix this, this patch makes mark_used avoid attempting to instantiate
> the noexcept specifier on a deleted function, so that mark_used will
> instead directly reject (and diagnose) such a function due to its
> deletedness.
> 
> Bootstrap and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?

... and perhaps release branches given that the second PR is a
regression relative to GCC 8?

> 
> 	PR c++/101532
> 	PR c++/104225
> 
> gcc/cp/ChangeLog:
> 
> 	* decl2.cc (mark_used): Don't call with
> 	maybe_instantiate_noexcept on a deleted function.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp0x/nsdmi-template21.C: New test.
> 	* g++.dg/cpp0x/nsdmi-template22.C: New test.
> ---
>  gcc/cp/decl2.cc                               |  1 +
>  gcc/testsuite/g++.dg/cpp0x/nsdmi-template21.C | 10 ++++++++++
>  gcc/testsuite/g++.dg/cpp0x/nsdmi-template22.C | 12 ++++++++++++
>  3 files changed, 23 insertions(+)
>  create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-template21.C
>  create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-template22.C
> 
> diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
> index 69b97449eac..b68cf96fa81 100644
> --- a/gcc/cp/decl2.cc
> +++ b/gcc/cp/decl2.cc
> @@ -5774,6 +5774,7 @@ mark_used (tree decl, tsubst_flags_t complain)
>      used_types_insert (DECL_CONTEXT (decl));
>  
>    if (TREE_CODE (decl) == FUNCTION_DECL
> +      && !DECL_DELETED_FN (decl)
>        && !maybe_instantiate_noexcept (decl, complain))
>      return false;
>  
> diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-template21.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template21.C
> new file mode 100644
> index 00000000000..b9d4f4a4b9a
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template21.C
> @@ -0,0 +1,10 @@
> +// PR c++/101532
> +// { dg-do compile { target c++11 } }
> +
> +class A { ~A(); };
> +
> +template<class> class B { // { dg-error "private" }
> +  A f = A();
> +};
> +
> +B<int> b; // { dg-error "deleted" }
> diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-template22.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template22.C
> new file mode 100644
> index 00000000000..bbcc7d9732e
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template22.C
> @@ -0,0 +1,12 @@
> +// PR c++/104225
> +// { dg-do compile { target c++11 } }
> +
> +class A { ~A(); };
> +
> +template<class> class B { // { dg-error "private" }
> +  A f = A();
> +};
> +
> +int main() {
> +  new B<int>; // { dg-error "deleted" }
> +}
> -- 
> 2.35.0
> 
>
  
Jason Merrill Jan. 25, 2022, 7:25 p.m. UTC | #2
On 1/25/22 12:33, Patrick Palka wrote:
> On Tue, 25 Jan 2022, Patrick Palka wrote:
> 
>> Here when attempting to use B's implicitly deleted default constructor,
>> mark_used rightfully returns false, but for the wrong reason: it
>> tries to instantiate the implicit noexcept specifier, which only
>> silently fails because get_defaulted_eh_spec suppresses diagnostics
>> for deleted functions.  This causes us to ICE on the first testcase
>> (thanks to the sanity check in finish_expr_stmt) and accept the second
>> invalid testcase without complaint.
>>
>> To fix this, this patch makes mark_used avoid attempting to instantiate
>> the noexcept specifier on a deleted function, so that mark_used will
>> instead directly reject (and diagnose) such a function due to its
>> deletedness.
>>
>> Bootstrap and regtested on x86_64-pc-linux-gnu, does this look OK for
>> trunk?
> 
> ... and perhaps release branches given that the second PR is a
> regression relative to GCC 8?

OK for trunk and release branches.

>>
>> 	PR c++/101532
>> 	PR c++/104225
>>
>> gcc/cp/ChangeLog:
>>
>> 	* decl2.cc (mark_used): Don't call with
>> 	maybe_instantiate_noexcept on a deleted function.
>>
>> gcc/testsuite/ChangeLog:
>>
>> 	* g++.dg/cpp0x/nsdmi-template21.C: New test.
>> 	* g++.dg/cpp0x/nsdmi-template22.C: New test.
>> ---
>>   gcc/cp/decl2.cc                               |  1 +
>>   gcc/testsuite/g++.dg/cpp0x/nsdmi-template21.C | 10 ++++++++++
>>   gcc/testsuite/g++.dg/cpp0x/nsdmi-template22.C | 12 ++++++++++++
>>   3 files changed, 23 insertions(+)
>>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-template21.C
>>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-template22.C
>>
>> diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
>> index 69b97449eac..b68cf96fa81 100644
>> --- a/gcc/cp/decl2.cc
>> +++ b/gcc/cp/decl2.cc
>> @@ -5774,6 +5774,7 @@ mark_used (tree decl, tsubst_flags_t complain)
>>       used_types_insert (DECL_CONTEXT (decl));
>>   
>>     if (TREE_CODE (decl) == FUNCTION_DECL
>> +      && !DECL_DELETED_FN (decl)
>>         && !maybe_instantiate_noexcept (decl, complain))
>>       return false;
>>   
>> diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-template21.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template21.C
>> new file mode 100644
>> index 00000000000..b9d4f4a4b9a
>> --- /dev/null
>> +++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template21.C
>> @@ -0,0 +1,10 @@
>> +// PR c++/101532
>> +// { dg-do compile { target c++11 } }
>> +
>> +class A { ~A(); };
>> +
>> +template<class> class B { // { dg-error "private" }
>> +  A f = A();
>> +};
>> +
>> +B<int> b; // { dg-error "deleted" }
>> diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-template22.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template22.C
>> new file mode 100644
>> index 00000000000..bbcc7d9732e
>> --- /dev/null
>> +++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template22.C
>> @@ -0,0 +1,12 @@
>> +// PR c++/104225
>> +// { dg-do compile { target c++11 } }
>> +
>> +class A { ~A(); };
>> +
>> +template<class> class B { // { dg-error "private" }
>> +  A f = A();
>> +};
>> +
>> +int main() {
>> +  new B<int>; // { dg-error "deleted" }
>> +}
>> -- 
>> 2.35.0
>>
>>
>
  

Patch

diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index 69b97449eac..b68cf96fa81 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -5774,6 +5774,7 @@  mark_used (tree decl, tsubst_flags_t complain)
     used_types_insert (DECL_CONTEXT (decl));
 
   if (TREE_CODE (decl) == FUNCTION_DECL
+      && !DECL_DELETED_FN (decl)
       && !maybe_instantiate_noexcept (decl, complain))
     return false;
 
diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-template21.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template21.C
new file mode 100644
index 00000000000..b9d4f4a4b9a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template21.C
@@ -0,0 +1,10 @@ 
+// PR c++/101532
+// { dg-do compile { target c++11 } }
+
+class A { ~A(); };
+
+template<class> class B { // { dg-error "private" }
+  A f = A();
+};
+
+B<int> b; // { dg-error "deleted" }
diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-template22.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template22.C
new file mode 100644
index 00000000000..bbcc7d9732e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template22.C
@@ -0,0 +1,12 @@ 
+// PR c++/104225
+// { dg-do compile { target c++11 } }
+
+class A { ~A(); };
+
+template<class> class B { // { dg-error "private" }
+  A f = A();
+};
+
+int main() {
+  new B<int>; // { dg-error "deleted" }
+}