public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-2170] c++: ahead of time variable template-id coercion [PR89442]
@ 2023-06-28 19:44 Patrick Palka
  0 siblings, 0 replies; only message in thread
From: Patrick Palka @ 2023-06-28 19:44 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:4cf64d9cc2faf4001f037a50a350abd0f95f3e29

commit r14-2170-g4cf64d9cc2faf4001f037a50a350abd0f95f3e29
Author: Patrick Palka <ppalka@redhat.com>
Date:   Wed Jun 28 15:43:33 2023 -0400

    c++: ahead of time variable template-id coercion [PR89442]
    
    This patch makes us coerce the arguments of a variable template-id ahead
    of time, as we do for class template-ids, which causes us to immediately
    diagnose template parm/arg kind mismatches and arity mismatches.
    
    Unfortunately this causes a regression in cpp1z/constexpr-if20.C: coercing
    the variable template-id m<ar, as> ahead of time means we strip it of
    typedefs, yielding m<typename C<i>::q, typename C<j>::q>, but in this
    stripped form we're directly using 'i' and so we expect to have captured
    it.  This is a variable template version of PR107437.
    
            PR c++/89442
            PR c++/107437
    
    gcc/cp/ChangeLog:
    
            * cp-tree.h (lookup_template_variable): Add complain parameter.
            * parser.cc (cp_parser_template_id): Pass tf_warning_or_error
            to lookup_template_variable.
            * pt.cc (lookup_template_variable): Add complain parameter.
            Coerce template arguments here ...
            (finish_template_variable): ... instead of here.
            (lookup_and_finish_template_variable): Check for error_mark_node
            result from lookup_template_variable.
            (tsubst_copy) <case TEMPLATE_ID_EXPR>: Pass complain to
            lookup_template_variable.
            (instantiate_template): Use build2 instead of
            lookup_template_variable to build a TEMPLATE_ID_EXPR
            for most_specialized_partial_spec.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp/pr64127.C: Expect "expected unqualified-id at end
            of input" error.
            * g++.dg/cpp0x/alias-decl-ttp1.C: Fix template parameter/argument
            kind mismatch for variable template has_P_match_V.
            * g++.dg/cpp1y/pr72759.C: Expect "template argument 1 is invalid"
            error.
            * g++.dg/cpp1z/constexpr-if20.C: XFAIL test due to bogus "'i' is
            not captured" error.
            * g++.dg/cpp1z/noexcept-type21.C: Fix arity of variable template d.
            * g++.dg/diagnostic/not-a-function-template-1.C: Add default
            template argument to variable template A so that A<> is valid.
            * g++.dg/parse/error56.C: Don't expect "ISO C++ forbids
            declaration with no type" error.
            * g++.dg/parse/template30.C: Don't expect "parse error in
            template argument list" error.
            * g++.dg/cpp1y/var-templ82.C: New test.

Diff:
---
 gcc/cp/cp-tree.h                                     |  2 +-
 gcc/cp/parser.cc                                     |  2 +-
 gcc/cp/pt.cc                                         | 20 +++++++++++---------
 gcc/testsuite/g++.dg/cpp/pr64127.C                   |  2 +-
 gcc/testsuite/g++.dg/cpp0x/alias-decl-ttp1.C         |  2 +-
 gcc/testsuite/g++.dg/cpp1y/pr72759.C                 |  2 +-
 gcc/testsuite/g++.dg/cpp1y/var-templ82.C             | 12 ++++++++++++
 gcc/testsuite/g++.dg/cpp1z/constexpr-if20.C          |  1 +
 gcc/testsuite/g++.dg/cpp1z/noexcept-type21.C         |  2 +-
 .../g++.dg/diagnostic/not-a-function-template-1.C    |  2 +-
 gcc/testsuite/g++.dg/parse/error56.C                 |  1 -
 gcc/testsuite/g++.dg/parse/template30.C              |  3 +--
 12 files changed, 32 insertions(+), 19 deletions(-)

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 0d7a6c153dc..fbeff33b75e 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -7358,7 +7358,7 @@ extern bool redeclare_class_template		(tree, tree, tree);
 extern tree lookup_template_class		(tree, tree, tree, tree,
 						 int, tsubst_flags_t);
 extern tree lookup_template_function		(tree, tree);
-extern tree lookup_template_variable		(tree, tree);
+extern tree lookup_template_variable		(tree, tree, tsubst_flags_t);
 extern bool uses_template_parms			(tree);
 extern bool uses_template_parms_level		(tree, int);
 extern bool uses_outer_template_parms_in_constraints (tree);
diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index dd3665c8ccf..9501050d5cd 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -18525,7 +18525,7 @@ cp_parser_template_id (cp_parser *parser,
     }
   else if (variable_template_p (templ))
     {
-      template_id = lookup_template_variable (templ, arguments);
+      template_id = lookup_template_variable (templ, arguments, tf_warning_or_error);
       if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
 	SET_EXPR_LOCATION (template_id, combined_loc);
     }
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 2345a18becc..ec2753e8913 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -10335,11 +10335,16 @@ lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST.  */
 
 tree
-lookup_template_variable (tree templ, tree arglist)
+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);
+  arglist = coerce_template_parms (parms, arglist, templ, complain);
+  if (arglist == error_mark_node)
+    return error_mark_node;
+
   /* The type of the expression is NULL_TREE since the template-id could refer
      to an explicit or partial specialization. */
   return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
@@ -10360,11 +10365,6 @@ finish_template_variable (tree var, tsubst_flags_t complain)
       || any_dependent_template_arguments_p (arglist))
     return var;
 
-  tree parms = DECL_TEMPLATE_PARMS (templ);
-  arglist = coerce_template_parms (parms, arglist, templ, complain);
-  if (arglist == error_mark_node)
-    return error_mark_node;
-
   if (flag_concepts && !constraints_satisfied_p (templ, arglist))
     {
       if (complain & tf_error)
@@ -10386,7 +10386,9 @@ tree
 lookup_and_finish_template_variable (tree templ, tree targs,
 				     tsubst_flags_t complain)
 {
-  tree var = lookup_template_variable (templ, targs);
+  tree var = lookup_template_variable (templ, targs, complain);
+  if (var == error_mark_node)
+    return error_mark_node;
   /* We may be called while doing a partial substitution, but the
      type of the variable template may be auto, in which case we
      will call do_auto_deduction in mark_used (which clears tf_partial)
@@ -17766,7 +17768,7 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 	  targs = tsubst_template_args (targs, args, complain, in_decl);
 
 	if (variable_template_p (tmpl))
-	  return lookup_template_variable (tmpl, targs);
+	  return lookup_template_variable (tmpl, targs, complain);
 	else
 	  return lookup_template_function (tmpl, targs);
       }
@@ -22084,7 +22086,7 @@ instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
       /* We need to determine if we're using a partial or explicit
 	 specialization now, because the type of the variable could be
 	 different.  */
-      tree tid = lookup_template_variable (tmpl, targ_ptr);
+      tree tid = build2 (TEMPLATE_ID_EXPR, NULL_TREE, tmpl, targ_ptr);
       tree elt = most_specialized_partial_spec (tid, complain);
       if (elt == error_mark_node)
 	pattern = error_mark_node;
diff --git a/gcc/testsuite/g++.dg/cpp/pr64127.C b/gcc/testsuite/g++.dg/cpp/pr64127.C
index 29c3bf2662a..8dc3336a1f9 100644
--- a/gcc/testsuite/g++.dg/cpp/pr64127.C
+++ b/gcc/testsuite/g++.dg/cpp/pr64127.C
@@ -1,4 +1,4 @@
 /* { dg-do compile { target c++98_only } } */
 
 template <0> int __copy_streambufs_eof; // { dg-error "expected identifier|numeric constant|variable templates" }
-__copy_streambufs_eof < // { dg-error "template argument|parse error|not name a type" }
+__copy_streambufs_eof < // { dg-error "template argument|expected unqualified-id" }
diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-ttp1.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-ttp1.C
index d1af8d1bbb2..52c0362ec01 100644
--- a/gcc/testsuite/g++.dg/cpp0x/alias-decl-ttp1.C
+++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-ttp1.C
@@ -2,5 +2,5 @@
 // { dg-do compile { target c++14 } }
 
 template <bool> using enable_if_t = int;
-template <class> bool has_P_match_v;
+template <template <class> class> bool has_P_match_v;
 template <template <class> class... List> enable_if_t<has_P_match_v<List...>> a;
diff --git a/gcc/testsuite/g++.dg/cpp1y/pr72759.C b/gcc/testsuite/g++.dg/cpp1y/pr72759.C
index 4af6ea47120..5c2fb9fba0b 100644
--- a/gcc/testsuite/g++.dg/cpp1y/pr72759.C
+++ b/gcc/testsuite/g++.dg/cpp1y/pr72759.C
@@ -14,5 +14,5 @@ template <> struct SpecPerType<Specializer> {
 };
 template <unsigned X> void Specializer::A<X>::InnerMemberFn() {
   using Spec = SpecPerType<Specializer>;
-  Spec ErrorSite = Spec::SpecMbrFnPtr<SpecMbrFnPtr>;  // { dg-error "not declared" }
+  Spec ErrorSite = Spec::SpecMbrFnPtr<SpecMbrFnPtr>;  // { dg-error "not declared|invalid" }
 }
diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ82.C b/gcc/testsuite/g++.dg/cpp1y/var-templ82.C
new file mode 100644
index 00000000000..5670f232a84
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/var-templ82.C
@@ -0,0 +1,12 @@
+// PR c++/89442
+// { dg-do compile { target c++14 } }
+
+template<class T, class U> bool vt;
+
+template<class T>
+void f() {
+  bool a = vt<T>;       // { dg-error "wrong number" }
+  bool b = vt<T, T>;
+  bool c = vt<T, T, T>; // { dg-error "number" }
+  bool d = vt<T, 42>;   // { dg-error "mismatch" }
+}
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if20.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-if20.C
index 2fd678bb383..cd4f80253bb 100644
--- a/gcc/testsuite/g++.dg/cpp1z/constexpr-if20.C
+++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-if20.C
@@ -12,6 +12,7 @@ void ao() {
       [](auto j) {
 	using as = typename C<j>::q;
 	if constexpr (m<ar, as>) {}
+	// { dg-bogus "'i' is not captured" "PR107437" { xfail { *-*-* } } .-1 }
       }(g());
   }(g());
 }
diff --git a/gcc/testsuite/g++.dg/cpp1z/noexcept-type21.C b/gcc/testsuite/g++.dg/cpp1z/noexcept-type21.C
index d0a61d95e87..4e0a9eaa91a 100644
--- a/gcc/testsuite/g++.dg/cpp1z/noexcept-type21.C
+++ b/gcc/testsuite/g++.dg/cpp1z/noexcept-type21.C
@@ -2,7 +2,7 @@
 // { dg-do compile { target c++17 } }
 
 template <typename a> using b = typename a ::c;
-template <typename> bool d;
+template <typename, typename> bool d;
 template <typename, typename> struct e {
   template <typename f, typename g> e(f, g) {}
   template <typename h, typename i, typename j>
diff --git a/gcc/testsuite/g++.dg/diagnostic/not-a-function-template-1.C b/gcc/testsuite/g++.dg/diagnostic/not-a-function-template-1.C
index caf8afa2697..56f15609405 100644
--- a/gcc/testsuite/g++.dg/diagnostic/not-a-function-template-1.C
+++ b/gcc/testsuite/g++.dg/diagnostic/not-a-function-template-1.C
@@ -1,6 +1,6 @@
 // { dg-do compile { target c++14 } }
 
-template<typename> int A;  // { dg-message "24:variable template" }
+template<typename=int> int A;  // { dg-message "28:variable template" }
 
 template int A<>();  // { dg-error "14:template<class>" }
 
diff --git a/gcc/testsuite/g++.dg/parse/error56.C b/gcc/testsuite/g++.dg/parse/error56.C
index bd3d27e8fcd..7c81ab4488f 100644
--- a/gcc/testsuite/g++.dg/parse/error56.C
+++ b/gcc/testsuite/g++.dg/parse/error56.C
@@ -2,5 +2,4 @@
 
 template <0> int __copy_streambufs_eof; // { dg-error "" }
 class {
-// { dg-error "forbids" "" { target *-*-* } .+1 }
     friend __copy_streambufs_eof <> ( // { dg-error "" }
diff --git a/gcc/testsuite/g++.dg/parse/template30.C b/gcc/testsuite/g++.dg/parse/template30.C
index fa89889a2bc..3bd7dda1a12 100644
--- a/gcc/testsuite/g++.dg/parse/template30.C
+++ b/gcc/testsuite/g++.dg/parse/template30.C
@@ -29,8 +29,7 @@ qux ()
 {
   return z<A<int>>=0;		// { dg-error "'>>=' should be '>> =' to terminate a template argument list" "" { target c++11 } }
 }				// { dg-error "'>>=' should be '> > =' to terminate a template argument list" "" { target c++98_only } .-1 }
-				// { dg-error "parse error in template argument list" "" { target *-*-* } .-2 }
-				// { dg-error "template argument 1 is invalid" "" { target *-*-* } .-3 }
+				// { dg-error "template argument 1 is invalid" "" { target *-*-* } .-2 }
 
 void
 quux ()

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-06-28 19:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-28 19:44 [gcc r14-2170] c++: ahead of time variable template-id coercion [PR89442] Patrick Palka

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).