c++: templated static local var has value-dep addr [PR98930]

Message ID 20211004210935.1348147-1-ppalka@redhat.com
State New
Headers
Series c++: templated static local var has value-dep addr [PR98930] |

Commit Message

Patrick Palka Oct. 4, 2021, 9:09 p.m. UTC
  Here uses_template_parms is returning false for the dependent type
A<&impl::i>, which causes tsubst_aggr_type to think it's non-dependent
and not bothering substituting into it, leading to breakage.

This patch fixes this by making has_value_dependent_address also return
true for templated static local variables.

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

	PR c++/98930

gcc/cp/ChangeLog:

	* pt.c (has_value_dependent_address): Return true for a static
	local variable from a function template.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp1z/nontype4.C: New test.
	* g++.dg/cpp1z/nontype4a.C: New test.
---
 gcc/cp/pt.c                            |  7 +++++++
 gcc/testsuite/g++.dg/cpp1z/nontype4.C  | 14 ++++++++++++++
 gcc/testsuite/g++.dg/cpp1z/nontype4a.C | 14 ++++++++++++++
 3 files changed, 35 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp1z/nontype4.C
 create mode 100644 gcc/testsuite/g++.dg/cpp1z/nontype4a.C
  

Comments

Jason Merrill Oct. 5, 2021, 1:19 a.m. UTC | #1
On 10/4/21 17:09, Patrick Palka wrote:
> Here uses_template_parms is returning false for the dependent type
> A<&impl::i>, which causes tsubst_aggr_type to think it's non-dependent
> and not bothering substituting into it, leading to breakage.
> 
> This patch fixes this by making has_value_dependent_address also return
> true for templated static local variables.
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?

OK.

> 	PR c++/98930
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.c (has_value_dependent_address): Return true for a static
> 	local variable from a function template.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp1z/nontype4.C: New test.
> 	* g++.dg/cpp1z/nontype4a.C: New test.
> ---
>   gcc/cp/pt.c                            |  7 +++++++
>   gcc/testsuite/g++.dg/cpp1z/nontype4.C  | 14 ++++++++++++++
>   gcc/testsuite/g++.dg/cpp1z/nontype4a.C | 14 ++++++++++++++
>   3 files changed, 35 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/cpp1z/nontype4.C
>   create mode 100644 gcc/testsuite/g++.dg/cpp1z/nontype4a.C
> 
> diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
> index 5e819c9598c..06579e1be07 100644
> --- a/gcc/cp/pt.c
> +++ b/gcc/cp/pt.c
> @@ -6763,8 +6763,15 @@ has_value_dependent_address (tree op)
>     if (DECL_P (op))
>       {
>         tree ctx = CP_DECL_CONTEXT (op);
> +
>         if (TYPE_P (ctx) && dependent_type_p (ctx))
>   	return true;
> +
> +      if (VAR_P (op)
> +	  && TREE_STATIC (op)
> +	  && TREE_CODE (ctx) == FUNCTION_DECL
> +	  && type_dependent_expression_p (ctx))
> +	return true;
>       }
>   
>     return false;
> diff --git a/gcc/testsuite/g++.dg/cpp1z/nontype4.C b/gcc/testsuite/g++.dg/cpp1z/nontype4.C
> new file mode 100644
> index 00000000000..3f345b339c6
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1z/nontype4.C
> @@ -0,0 +1,14 @@
> +// PR c++/98930
> +// { dg-do compile { target c++17 } }
> +
> +template<int*>
> +struct A { A() { } };
> +
> +template<class T>
> +void impl() {
> +  static int i;
> +  static A<&i> h;
> +}
> +
> +template void impl<int>();
> +template void impl<char>();
> diff --git a/gcc/testsuite/g++.dg/cpp1z/nontype4a.C b/gcc/testsuite/g++.dg/cpp1z/nontype4a.C
> new file mode 100644
> index 00000000000..5b742926b6d
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1z/nontype4a.C
> @@ -0,0 +1,14 @@
> +// PR c++/98930
> +// { dg-do compile { target c++17 } }
> +
> +template<int*>
> +struct A { };
> +
> +template<class T>
> +auto impl() {
> +  static int i;
> +  return A<&i>();
> +}
> +
> +using type = decltype(impl<int>());
> +using type = decltype(impl<char>()); // { dg-error "conflicting declaration" }
>
  

Patch

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 5e819c9598c..06579e1be07 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -6763,8 +6763,15 @@  has_value_dependent_address (tree op)
   if (DECL_P (op))
     {
       tree ctx = CP_DECL_CONTEXT (op);
+
       if (TYPE_P (ctx) && dependent_type_p (ctx))
 	return true;
+
+      if (VAR_P (op)
+	  && TREE_STATIC (op)
+	  && TREE_CODE (ctx) == FUNCTION_DECL
+	  && type_dependent_expression_p (ctx))
+	return true;
     }
 
   return false;
diff --git a/gcc/testsuite/g++.dg/cpp1z/nontype4.C b/gcc/testsuite/g++.dg/cpp1z/nontype4.C
new file mode 100644
index 00000000000..3f345b339c6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/nontype4.C
@@ -0,0 +1,14 @@ 
+// PR c++/98930
+// { dg-do compile { target c++17 } }
+
+template<int*>
+struct A { A() { } };
+
+template<class T>
+void impl() {
+  static int i;
+  static A<&i> h;
+}
+
+template void impl<int>();
+template void impl<char>();
diff --git a/gcc/testsuite/g++.dg/cpp1z/nontype4a.C b/gcc/testsuite/g++.dg/cpp1z/nontype4a.C
new file mode 100644
index 00000000000..5b742926b6d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/nontype4a.C
@@ -0,0 +1,14 @@ 
+// PR c++/98930
+// { dg-do compile { target c++17 } }
+
+template<int*>
+struct A { };
+
+template<class T>
+auto impl() {
+  static int i;
+  return A<&i>();
+}
+
+using type = decltype(impl<int>());
+using type = decltype(impl<char>()); // { dg-error "conflicting declaration" }