public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: coercing variable template from current inst [PR110580]
@ 2023-07-11 12:16 Patrick Palka
  2023-07-11 13:49 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Patrick Palka @ 2023-07-11 12:16 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Patrick Palka

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

-- >8 --

Here during ahead of time coercion of the variable template-id v1<int>,
since we pass only the innermost arguments to coerce_template_parms (and
outer arguments are still dependent at this point), substitution of the
default template argument V=U prematurely lowers U from level 2 to level 1.
Thus we incorrectly resolve v1<int> to v1<int, T> (effectively) instead
of to v1<int, int>.

Coercion of a class/alias template-id on the other hand is always done
using the full set of arguments relative to the most general template,
so ahead of time coercion there does the right thing.  I suppose we
should do the same for variable template-ids.

	PR c++/110580

gcc/cp/ChangeLog:

	* pt.cc (lookup_template_variable): Pass all arguments to
	coerce_template_parms, and use the innermost parameters from
	the most general template.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp1y/var-templ83.C: New test.
---
 gcc/cp/pt.cc                             |  4 +++-
 gcc/testsuite/g++.dg/cpp1y/var-templ83.C | 16 ++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ83.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 076f788281e..fa15b75b9c5 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -10345,7 +10345,9 @@ lookup_template_variable (tree templ, tree arglist, tsubst_flags_t complain)
   if (flag_concepts && variable_concept_p (templ))
     return build_concept_check (templ, arglist, tf_none);
 
-  tree parms = DECL_INNERMOST_TEMPLATE_PARMS (templ);
+  tree gen_templ = most_general_template (templ);
+  tree parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_templ);
+  arglist = add_outermost_template_args (templ, arglist);
   arglist = coerce_template_parms (parms, arglist, templ, complain);
   if (arglist == error_mark_node)
     return error_mark_node;
diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ83.C b/gcc/testsuite/g++.dg/cpp1y/var-templ83.C
new file mode 100644
index 00000000000..f5268f258d7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/var-templ83.C
@@ -0,0 +1,16 @@
+// PR c++/110580
+// { dg-do compile { target c++14 } }
+
+template<class T>
+struct A {
+  template<typename U, typename V = U>
+  static constexpr bool v1 = __is_same(U, V);
+
+  template<typename U, typename V = T>
+  static constexpr bool v2 = !__is_same(U, V);
+
+  static_assert(v1<int>, "");
+  static_assert(v2<int>, "");
+};
+
+template struct A<char>;
-- 
2.41.0.327.gaa9166bcc0


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

* Re: [PATCH] c++: coercing variable template from current inst [PR110580]
  2023-07-11 12:16 [PATCH] c++: coercing variable template from current inst [PR110580] Patrick Palka
@ 2023-07-11 13:49 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2023-07-11 13:49 UTC (permalink / raw)
  To: Patrick Palka; +Cc: gcc-patches List

[-- Attachment #1: Type: text/plain, Size: 2778 bytes --]

Ok.

On Tue, Jul 11, 2023, 9:16 AM Patrick Palka <ppalka@redhat.com> wrote:

> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?
>
> -- >8 --
>
> Here during ahead of time coercion of the variable template-id v1<int>,
> since we pass only the innermost arguments to coerce_template_parms (and
> outer arguments are still dependent at this point), substitution of the
> default template argument V=U prematurely lowers U from level 2 to level 1.
> Thus we incorrectly resolve v1<int> to v1<int, T> (effectively) instead
> of to v1<int, int>.
>
> Coercion of a class/alias template-id on the other hand is always done
> using the full set of arguments relative to the most general template,
> so ahead of time coercion there does the right thing.  I suppose we
> should do the same for variable template-ids.
>
>         PR c++/110580
>
> gcc/cp/ChangeLog:
>
>         * pt.cc (lookup_template_variable): Pass all arguments to
>         coerce_template_parms, and use the innermost parameters from
>         the most general template.
>
> gcc/testsuite/ChangeLog:
>
>         * g++.dg/cpp1y/var-templ83.C: New test.
> ---
>  gcc/cp/pt.cc                             |  4 +++-
>  gcc/testsuite/g++.dg/cpp1y/var-templ83.C | 16 ++++++++++++++++
>  2 files changed, 19 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ83.C
>
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 076f788281e..fa15b75b9c5 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -10345,7 +10345,9 @@ lookup_template_variable (tree templ, tree
> arglist, tsubst_flags_t complain)
>    if (flag_concepts && variable_concept_p (templ))
>      return build_concept_check (templ, arglist, tf_none);
>
> -  tree parms = DECL_INNERMOST_TEMPLATE_PARMS (templ);
> +  tree gen_templ = most_general_template (templ);
> +  tree parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_templ);
> +  arglist = add_outermost_template_args (templ, arglist);
>    arglist = coerce_template_parms (parms, arglist, templ, complain);
>    if (arglist == error_mark_node)
>      return error_mark_node;
> diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ83.C
> b/gcc/testsuite/g++.dg/cpp1y/var-templ83.C
> new file mode 100644
> index 00000000000..f5268f258d7
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ83.C
> @@ -0,0 +1,16 @@
> +// PR c++/110580
> +// { dg-do compile { target c++14 } }
> +
> +template<class T>
> +struct A {
> +  template<typename U, typename V = U>
> +  static constexpr bool v1 = __is_same(U, V);
> +
> +  template<typename U, typename V = T>
> +  static constexpr bool v2 = !__is_same(U, V);
> +
> +  static_assert(v1<int>, "");
> +  static_assert(v2<int>, "");
> +};
> +
> +template struct A<char>;
> --
> 2.41.0.327.gaa9166bcc0
>
>

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

end of thread, other threads:[~2023-07-11 13:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-11 12:16 [PATCH] c++: coercing variable template from current inst [PR110580] Patrick Palka
2023-07-11 13:49 ` 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).