]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: coercing variable template from current inst [PR110580]
authorPatrick Palka <ppalka@redhat.com>
Tue, 11 Jul 2023 14:05:19 +0000 (10:05 -0400)
committerPatrick Palka <ppalka@redhat.com>
Tue, 11 Jul 2023 14:05:19 +0000 (10:05 -0400)
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 just lowers U from level 2 to level 1 rather
than replacing it with int as expected.  Thus after coercion we incorrectly
end up with (effectively) v1<int, T> instead of v1<int, int>.

Coercion of a class/alias template-id on the other hand always passes
all levels arguments, which avoids this issue.  So this patch makes us
do the same for variable template-ids.

PR c++/110580

gcc/cp/ChangeLog:

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

gcc/testsuite/ChangeLog:

* g++.dg/cpp1y/var-templ83.C: New test.

gcc/cp/pt.cc
gcc/testsuite/g++.dg/cpp1y/var-templ83.C [new file with mode: 0644]

index 076f788281efaa70e506967eef21e15dbd762185..fa15b75b9c54246558df42d30f44df041b32b18f 100644 (file)
@@ -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 (file)
index 0000000..f5268f2
--- /dev/null
@@ -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>;