public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: jason@redhat.com, Patrick Palka <ppalka@redhat.com>
Subject: [PATCH] c++: ahead of time variable template-id coercion [PR89442]
Date: Wed,  3 May 2023 09:50:43 -0400	[thread overview]
Message-ID: <20230503135043.273442-1-ppalka@redhat.com> (raw)

This patch makes us coerce the arguments of a variable template-id ahead
of time, as we do for other template-ids, which allows us to immediately
diagnose template parameter/argument 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 PR107437 but with a variable template instead of a class
template.  I'm not sure how to fix this :(

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

	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-templ80.C: New test.
---
 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-templ80.C      | 12 +++++++++++
 gcc/testsuite/g++.dg/cpp1z/constexpr-if20.C   |  1 +
 gcc/testsuite/g++.dg/cpp1z/noexcept-type21.C  |  2 +-
 .../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(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ80.C

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index c9c4cd6f32f..96807282ec5 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -7349,7 +7349,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 d89553e7da8..4982583809b 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 930291917f2..abf99feab20 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -10329,11 +10329,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);
@@ -10354,11 +10359,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)
@@ -10380,7 +10380,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)
@@ -17793,7 +17795,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);
       }
@@ -22097,7 +22099,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-templ80.C b/gcc/testsuite/g++.dg/cpp1y/var-templ80.C
new file mode 100644
index 00000000000..5670f232a84
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/var-templ80.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 ()
-- 
2.40.1.459.g48d89b51b3


             reply	other threads:[~2023-05-03 13:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-03 13:50 Patrick Palka [this message]
2023-05-03 14:11 ` Patrick Palka
2023-06-01 15:42 ` Patrick Palka
2023-06-28 15:28 ` Jason Merrill
2023-06-28 15:48   ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230503135043.273442-1-ppalka@redhat.com \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).