diff mbox series

c++: implicit 'this' in noexcept-spec within class tmpl [PR94944]

Message ID 20220217142640.2979190-1-ppalka@redhat.com
State New
Headers show
Series c++: implicit 'this' in noexcept-spec within class tmpl [PR94944] | expand

Commit Message

Patrick Palka Feb. 17, 2022, 2:26 p.m. UTC
Here when instantiating the noexcept-spec we fail to resolve the
implicit object parameter for the call A<T>::f() ultimately because
maybe_instantiate_noexcept sets current_class_ptr/ref to the dependent
'this' (of type B<T>) rather than the specialized 'this' (of type B<int>).
This ends up causing maybe_dummy_object (called from
finish_qualified_id_expr) to return a dummy object instead of 'this'.

This patch corrects this by making maybe_instantiate_noexcept always set
current_class_ptr/ref to the specialized 'this', consistent with what
tsubst_function_type does when substituting into a trailing return type.

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

	PR c++/94944

gcc/cp/ChangeLog:

	* pt.cc (maybe_instantiate_noexcept): For non-static member
	functions, set current_class_ptr/ref to the specialized 'this'
	instead.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/noexcept34.C: Adjusted expected diagnostics.
	* g++.dg/cpp0x/noexcept75.C: New test.
---
 gcc/cp/pt.cc                            | 19 +++++++------------
 gcc/testsuite/g++.dg/cpp0x/noexcept34.C |  8 ++++----
 gcc/testsuite/g++.dg/cpp0x/noexcept75.C | 17 +++++++++++++++++
 3 files changed, 28 insertions(+), 16 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept75.C

Comments

Jason Merrill Feb. 17, 2022, 11:17 p.m. UTC | #1
On 2/17/22 09:26, Patrick Palka wrote:
> Here when instantiating the noexcept-spec we fail to resolve the
> implicit object parameter for the call A<T>::f() ultimately because
> maybe_instantiate_noexcept sets current_class_ptr/ref to the dependent
> 'this' (of type B<T>) rather than the specialized 'this' (of type B<int>).
> This ends up causing maybe_dummy_object (called from
> finish_qualified_id_expr) to return a dummy object instead of 'this'.
> 
> This patch corrects this by making maybe_instantiate_noexcept always set
> current_class_ptr/ref to the specialized 'this', consistent with what
> tsubst_function_type does when substituting into a trailing return type.
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk perhaps?

OK.

> 	PR c++/94944
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (maybe_instantiate_noexcept): For non-static member
> 	functions, set current_class_ptr/ref to the specialized 'this'
> 	instead.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp0x/noexcept34.C: Adjusted expected diagnostics.
> 	* g++.dg/cpp0x/noexcept75.C: New test.
> ---
>   gcc/cp/pt.cc                            | 19 +++++++------------
>   gcc/testsuite/g++.dg/cpp0x/noexcept34.C |  8 ++++----
>   gcc/testsuite/g++.dg/cpp0x/noexcept75.C | 17 +++++++++++++++++
>   3 files changed, 28 insertions(+), 16 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept75.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 6dda66081bd..a7a524fe9fc 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -26139,20 +26139,15 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
>   	  push_deferring_access_checks (dk_no_deferred);
>   	  input_location = DECL_SOURCE_LOCATION (fn);
>   
> -	  if (!DECL_LOCAL_DECL_P (fn))
> +	  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
> +	      && !DECL_LOCAL_DECL_P (fn))
>   	    {
>   	      /* If needed, set current_class_ptr for the benefit of
> -		 tsubst_copy/PARM_DECL.  The exception pattern will
> -		 refer to the parm of the template, not the
> -		 instantiation.  */
> -	      tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
> -	      if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
> -		{
> -		  tree this_parm = DECL_ARGUMENTS (tdecl);
> -		  current_class_ptr = NULL_TREE;
> -		  current_class_ref = cp_build_fold_indirect_ref (this_parm);
> -		  current_class_ptr = this_parm;
> -		}
> +		 tsubst_copy/PARM_DECL.  */
> +	      tree this_parm = DECL_ARGUMENTS (fn);
> +	      current_class_ptr = NULL_TREE;
> +	      current_class_ref = cp_build_fold_indirect_ref (this_parm);
> +	      current_class_ptr = this_parm;
>   	    }
>   
>   	  /* If this function is represented by a TEMPLATE_DECL, then
> diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept34.C b/gcc/testsuite/g++.dg/cpp0x/noexcept34.C
> index dce35652ef5..86129e7a520 100644
> --- a/gcc/testsuite/g++.dg/cpp0x/noexcept34.C
> +++ b/gcc/testsuite/g++.dg/cpp0x/noexcept34.C
> @@ -7,13 +7,13 @@ template<typename> struct A
>   {
>     constexpr int f () { return 0; }
>     bool b = true;
> -  void g () noexcept (f()) { } // { dg-error "use of parameter" }
> -  void g2 () noexcept (this->f()) { } // { dg-error "use of parameter" }
> +  void g () noexcept (f()) { } // { dg-error ".this. is not a constant" }
> +  void g2 () noexcept (this->f()) { } // { dg-error ".this. is not a constant" }
>     void g3 () noexcept (b) { } // { dg-error "use of .this. in a constant expression|use of parameter" }
>     void g4 (int i) noexcept (i) { } // { dg-error "use of parameter" }
> -  void g5 () noexcept (A::f()) { } // { dg-error "use of parameter" }
> +  void g5 () noexcept (A::f()) { } // { dg-error ".this. is not a constant" }
>     void g6 () noexcept (foo(b)) { } // { dg-error "use of .this. in a constant expression|use of parameter" }
> -  void g7 () noexcept (int{f()}) { } // { dg-error "use of parameter" }
> +  void g7 () noexcept (int{f()}) { } // { dg-error ".this. is not a constant" }
>   };
>   
>   int main ()
> diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept75.C b/gcc/testsuite/g++.dg/cpp0x/noexcept75.C
> new file mode 100644
> index 00000000000..d746f4768d0
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/noexcept75.C
> @@ -0,0 +1,17 @@
> +// PR c++/94944
> +// { dg-do compile { target c++11 } }
> +
> +template<class T>
> +struct A {
> +  void f();
> +};
> +
> +template<class T>
> +struct B : A<T> {
> +  void g() noexcept(noexcept(A<T>::f()));
> +};
> +
> +int main() {
> +  B<int> b;
> +  b.g();
> +}
diff mbox series

Patch

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 6dda66081bd..a7a524fe9fc 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -26139,20 +26139,15 @@  maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
 	  push_deferring_access_checks (dk_no_deferred);
 	  input_location = DECL_SOURCE_LOCATION (fn);
 
-	  if (!DECL_LOCAL_DECL_P (fn))
+	  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
+	      && !DECL_LOCAL_DECL_P (fn))
 	    {
 	      /* If needed, set current_class_ptr for the benefit of
-		 tsubst_copy/PARM_DECL.  The exception pattern will
-		 refer to the parm of the template, not the
-		 instantiation.  */
-	      tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
-	      if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
-		{
-		  tree this_parm = DECL_ARGUMENTS (tdecl);
-		  current_class_ptr = NULL_TREE;
-		  current_class_ref = cp_build_fold_indirect_ref (this_parm);
-		  current_class_ptr = this_parm;
-		}
+		 tsubst_copy/PARM_DECL.  */
+	      tree this_parm = DECL_ARGUMENTS (fn);
+	      current_class_ptr = NULL_TREE;
+	      current_class_ref = cp_build_fold_indirect_ref (this_parm);
+	      current_class_ptr = this_parm;
 	    }
 
 	  /* If this function is represented by a TEMPLATE_DECL, then
diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept34.C b/gcc/testsuite/g++.dg/cpp0x/noexcept34.C
index dce35652ef5..86129e7a520 100644
--- a/gcc/testsuite/g++.dg/cpp0x/noexcept34.C
+++ b/gcc/testsuite/g++.dg/cpp0x/noexcept34.C
@@ -7,13 +7,13 @@  template<typename> struct A
 {
   constexpr int f () { return 0; }
   bool b = true;
-  void g () noexcept (f()) { } // { dg-error "use of parameter" }
-  void g2 () noexcept (this->f()) { } // { dg-error "use of parameter" }
+  void g () noexcept (f()) { } // { dg-error ".this. is not a constant" }
+  void g2 () noexcept (this->f()) { } // { dg-error ".this. is not a constant" }
   void g3 () noexcept (b) { } // { dg-error "use of .this. in a constant expression|use of parameter" }
   void g4 (int i) noexcept (i) { } // { dg-error "use of parameter" }
-  void g5 () noexcept (A::f()) { } // { dg-error "use of parameter" }
+  void g5 () noexcept (A::f()) { } // { dg-error ".this. is not a constant" }
   void g6 () noexcept (foo(b)) { } // { dg-error "use of .this. in a constant expression|use of parameter" }
-  void g7 () noexcept (int{f()}) { } // { dg-error "use of parameter" }
+  void g7 () noexcept (int{f()}) { } // { dg-error ".this. is not a constant" }
 };
 
 int main ()
diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept75.C b/gcc/testsuite/g++.dg/cpp0x/noexcept75.C
new file mode 100644
index 00000000000..d746f4768d0
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/noexcept75.C
@@ -0,0 +1,17 @@ 
+// PR c++/94944
+// { dg-do compile { target c++11 } }
+
+template<class T>
+struct A {
+  void f();
+};
+
+template<class T>
+struct B : A<T> {
+  void g() noexcept(noexcept(A<T>::f()));
+};
+
+int main() {
+  B<int> b;
+  b.g();
+}