From 4cf64d9cc2faf4001f037a50a350abd0f95f3e29 Mon Sep 17 00:00:00 2001 From: Patrick Palka Date: Wed, 28 Jun 2023 15:43:33 -0400 Subject: [PATCH] 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 ahead of time means we strip it of typedefs, yielding m::q, typename C::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) : 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. --- 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 +- .../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-templ82.C diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 0d7a6c153dc6..fbeff33b75e9 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 dd3665c8ccf4..9501050d5cd3 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 2345a18becc1..ec2753e89135 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 29c3bf2662a4..8dc3336a1f97 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 d1af8d1bbb20..52c0362ec019 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 using enable_if_t = int; -template bool has_P_match_v; +template