c++: var tmpl w/ dependent constrained auto type [PR103341]

Message ID 20220127165814.304303-1-ppalka@redhat.com
State New
Headers
Series c++: var tmpl w/ dependent constrained auto type [PR103341] |

Commit Message

Patrick Palka Jan. 27, 2022, 4:58 p.m. UTC
  When deducing the type of a variable template specialization with a
constrained auto type, we might need the template arguments for
satisfaction in case the constraint is dependent.

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

	PR c++/103341

gcc/cp/ChangeLog:

	* decl.cc (cp_finish_decl): Pass the template arguments of a
	variable template specialization to do_auto_deduction when
	the auto is constrained.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/concepts-var-templ2.C: New test.
---
 gcc/cp/decl.cc                                   |  7 ++++++-
 gcc/testsuite/g++.dg/cpp2a/concepts-var-templ2.C | 13 +++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-var-templ2.C
  

Comments

Patrick Palka Jan. 27, 2022, 6:06 p.m. UTC | #1
On Thu, 27 Jan 2022, Patrick Palka wrote:

> When deducing the type of a variable template specialization with a
> constrained auto type, we might need the template arguments for
> satisfaction in case the constraint is dependent.

It looks like templated static data members need similar treatment.
Here's a patch that handles both kinds of entities, what they have
in common is that they're templated and not at function scope
(bootstrap and regtesting in progress):

-- >8 --

Subject: [PATCH] c++: var tmpl w/ dependent constrained auto type [PR103341]

When deducing the type of a variable template specialization (or a
template static data member) with a constrained auto type, we might
need the template arguments during satisfaction from do_auto_deduction
in case the constraint is dependent.

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

	PR c++/103341

gcc/cp/ChangeLog:

	* decl.cc (cp_finish_decl): Pass the template arguments of a
	variable template specialization or a templated static data
	member to do_auto_deduction when the auto is constrained.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/concepts-class4.C: New test.
	* g++.dg/cpp2a/concepts-var-templ2.C: New test.
---
 gcc/cp/decl.cc                                   | 12 +++++++++++-
 gcc/testsuite/g++.dg/cpp2a/concepts-class4.C     | 12 ++++++++++++
 gcc/testsuite/g++.dg/cpp2a/concepts-var-templ2.C | 13 +++++++++++++
 3 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-class4.C
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-var-templ2.C

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 10e6956117e..465f5620f2c 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -7958,9 +7958,19 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
       enum auto_deduction_context adc = adc_variable_type;
       if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
 	adc = adc_decomp_type;
+      tree outer_targs = NULL_TREE;
+      if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
+	  && VAR_P (decl)
+	  && DECL_LANG_SPECIFIC (decl)
+	  && DECL_TEMPLATE_INFO (decl)
+	  && !DECL_FUNCTION_SCOPE_P (decl))
+	/* The outer template arguments might be needed for satisfaction.
+	   For function scope decls, do_auto_deduction obtains the outer
+	   template arguments from current_function_decl.  */
+	outer_targs = DECL_TI_ARGS (decl);
       type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
 						   tf_warning_or_error, adc,
-						   NULL_TREE, flags);
+						   outer_targs, flags);
       if (type == error_mark_node)
 	return;
       if (TREE_CODE (type) == FUNCTION_TYPE)
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-class4.C b/gcc/testsuite/g++.dg/cpp2a/concepts-class4.C
new file mode 100644
index 00000000000..9d694e758e0
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-class4.C
@@ -0,0 +1,12 @@
+// PR c++/103341
+// { dg-do compile { target c++20 } }
+
+template<class T, class U> concept C = __is_same(T, U);
+
+template<class T>
+struct A {
+  static inline C<T> auto value = 0; // { dg-error "constraint" }
+};
+
+template struct A<int>; // { dg-bogus "" }
+template struct A<bool>; // { dg-message "required from here" }
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ2.C b/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ2.C
new file mode 100644
index 00000000000..e1802aca75f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ2.C
@@ -0,0 +1,13 @@
+// PR c++/103341
+// { dg-do compile { target c++20 } }
+
+template<class T, class U> concept same_as = __is_same(T, U);
+template<class T> same_as<T> auto v1a = 1;
+template<class T> same_as<T> auto v1b = T();
+template<class T> same_as<T*> auto v2a = 1; // { dg-error "constraints" }
+template<class T> same_as<T*> auto v2b = T(); // { dg-error "constraints" }
+
+template int v1a<int>;
+template int v1b<int>;
+template int v2a<int>; // { dg-message "required from here" }
+template int v2b<int>; // { dg-message "required from here" }
  
Jason Merrill Jan. 27, 2022, 10:27 p.m. UTC | #2
On 1/27/22 13:06, Patrick Palka wrote:
> On Thu, 27 Jan 2022, Patrick Palka wrote:
> 
>> When deducing the type of a variable template specialization with a
>> constrained auto type, we might need the template arguments for
>> satisfaction in case the constraint is dependent.
> 
> It looks like templated static data members need similar treatment.
> Here's a patch that handles both kinds of entities, what they have
> in common is that they're templated and not at function scope
> (bootstrap and regtesting in progress):
> 
> -- >8 --
> 
> Subject: [PATCH] c++: var tmpl w/ dependent constrained auto type [PR103341]
> 
> When deducing the type of a variable template specialization (or a
> template static data member) with a constrained auto type, we might
> need the template arguments during satisfaction from do_auto_deduction
> in case the constraint is dependent.
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK
> for trunk and perhaps 11?

OK for trunk.  And for 11, if you make it conditional on flag_concepts.

> 	PR c++/103341
> 
> gcc/cp/ChangeLog:
> 
> 	* decl.cc (cp_finish_decl): Pass the template arguments of a
> 	variable template specialization or a templated static data
> 	member to do_auto_deduction when the auto is constrained.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp2a/concepts-class4.C: New test.
> 	* g++.dg/cpp2a/concepts-var-templ2.C: New test.
> ---
>   gcc/cp/decl.cc                                   | 12 +++++++++++-
>   gcc/testsuite/g++.dg/cpp2a/concepts-class4.C     | 12 ++++++++++++
>   gcc/testsuite/g++.dg/cpp2a/concepts-var-templ2.C | 13 +++++++++++++
>   3 files changed, 36 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-class4.C
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-var-templ2.C
> 
> diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
> index 10e6956117e..465f5620f2c 100644
> --- a/gcc/cp/decl.cc
> +++ b/gcc/cp/decl.cc
> @@ -7958,9 +7958,19 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
>         enum auto_deduction_context adc = adc_variable_type;
>         if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
>   	adc = adc_decomp_type;
> +      tree outer_targs = NULL_TREE;
> +      if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
> +	  && VAR_P (decl)
> +	  && DECL_LANG_SPECIFIC (decl)
> +	  && DECL_TEMPLATE_INFO (decl)
> +	  && !DECL_FUNCTION_SCOPE_P (decl))
> +	/* The outer template arguments might be needed for satisfaction.
> +	   For function scope decls, do_auto_deduction obtains the outer
> +	   template arguments from current_function_decl.  */
> +	outer_targs = DECL_TI_ARGS (decl);
>         type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
>   						   tf_warning_or_error, adc,
> -						   NULL_TREE, flags);
> +						   outer_targs, flags);
>         if (type == error_mark_node)
>   	return;
>         if (TREE_CODE (type) == FUNCTION_TYPE)
> diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-class4.C b/gcc/testsuite/g++.dg/cpp2a/concepts-class4.C
> new file mode 100644
> index 00000000000..9d694e758e0
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-class4.C
> @@ -0,0 +1,12 @@
> +// PR c++/103341
> +// { dg-do compile { target c++20 } }
> +
> +template<class T, class U> concept C = __is_same(T, U);
> +
> +template<class T>
> +struct A {
> +  static inline C<T> auto value = 0; // { dg-error "constraint" }
> +};
> +
> +template struct A<int>; // { dg-bogus "" }
> +template struct A<bool>; // { dg-message "required from here" }
> diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ2.C b/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ2.C
> new file mode 100644
> index 00000000000..e1802aca75f
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ2.C
> @@ -0,0 +1,13 @@
> +// PR c++/103341
> +// { dg-do compile { target c++20 } }
> +
> +template<class T, class U> concept same_as = __is_same(T, U);
> +template<class T> same_as<T> auto v1a = 1;
> +template<class T> same_as<T> auto v1b = T();
> +template<class T> same_as<T*> auto v2a = 1; // { dg-error "constraints" }
> +template<class T> same_as<T*> auto v2b = T(); // { dg-error "constraints" }
> +
> +template int v1a<int>;
> +template int v1b<int>;
> +template int v2a<int>; // { dg-message "required from here" }
> +template int v2b<int>; // { dg-message "required from here" }
  

Patch

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 10e6956117e..b698fa9b95a 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -7958,9 +7958,14 @@  cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
       enum auto_deduction_context adc = adc_variable_type;
       if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
 	adc = adc_decomp_type;
+      tree outer_targs = NULL_TREE;
+      if (is_constrained_auto (auto_node)
+	  && variable_template_specialization_p (decl))
+	/* The outer template arguments might be needed for satisfaction.  */
+	outer_targs = DECL_TI_ARGS (decl);
       type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
 						   tf_warning_or_error, adc,
-						   NULL_TREE, flags);
+						   outer_targs, flags);
       if (type == error_mark_node)
 	return;
       if (TREE_CODE (type) == FUNCTION_TYPE)
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ2.C b/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ2.C
new file mode 100644
index 00000000000..56cead5e8c3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ2.C
@@ -0,0 +1,13 @@ 
+// PR c++/103341
+// { dg-do compile { target c++20 } }
+
+template<class T, class U> concept same_as = __is_same(T, U);
+template<class T> same_as<T> auto v1a{1};
+template<class T> same_as<T> auto v1b{1};
+template<class T> same_as<T*> auto v2a{T()}; // { dg-error "constraints" }
+template<class T> same_as<T*> auto v2b{T()}; // { dg-error "constraints" }
+
+template int v1a<int>;
+template int v1b<int>;
+template int v2a<int>; // { dg-message "required from here" }
+template int v2b<int>; // { dg-message "required from here" }