From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 4CCAF3858D28 for ; Wed, 3 May 2023 13:50:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4CCAF3858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683121857; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=V1icJ5JaLgxs1rmWTojHCRyBzYoYiPdkCfAZanREmRw=; b=PM7plk8KBtTzydlDuiCgrsVcV1up6RLuZky42PRrMqvommrdoiEzx286PeDVxJubosE73n cyMMx8D0UEnnYKtJXyzSNlxyQ8Y3z1+2lTWVRoeU3LkiTfDzyDX/SNSODaLnnNXx1TmXZ5 YMm+FOM7S5M8jwZC2/G7eI4WYw+EdV0= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-321-SWtAvPwyMjOSWKh8pVA_ww-1; Wed, 03 May 2023 09:50:56 -0400 X-MC-Unique: SWtAvPwyMjOSWKh8pVA_ww-1 Received: by mail-qt1-f200.google.com with SMTP id d75a77b69052e-3ef5c322d53so28095931cf.1 for ; Wed, 03 May 2023 06:50:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683121855; x=1685713855; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=V1icJ5JaLgxs1rmWTojHCRyBzYoYiPdkCfAZanREmRw=; b=AN1rEKBUdjckpSy3aqvafC+bCU/scj1N7eAaFnUqZkb5LPeT5QxgKRscZ1vLbf9aZi eaC/KogQaK6LEIsOzQPYsiQQ0JPqhZKc8E29Gr8ACybxVdzzRL2Jizdja13bzpNseh71 L84Ism8jY7kpTjKaF2jBfbpYRwQ8XDBxukwyjmk9q8A6zkFtNfxu3hKX9a1B8G8NzgFL jRmS9QbUePD/xPX59AbzpfCAbEll841Fep9fpxai/Vzu0FzT9uDgenSrA5j64Gy0+4eA ntruGZlrtS6Rv9cvkXMW6IPhVhEAzimmPKQLwP49MkJlyjmAj4ovVc6m0YWuoQjQlWo4 0uKQ== X-Gm-Message-State: AC+VfDwLArccK+u1azshjPfbzNKXwasWON4/Ig/wTYZMreRq7r0qRo4L B3S9ysNKlQS04+8OQmv4GhqZua+wDMVulXBiv+t9N2LtKs/2u137y+jhVOmh9vD+jpzZPL9qrhU jCEEHRw6cGItZYUpWPqfI0IPWKc4aEpt9wFKDut75rWma2C/4fe5jrXVMV/H4MZqYt7G3eawSh1 M= X-Received: by 2002:a05:622a:19a4:b0:3ef:3402:dda4 with SMTP id u36-20020a05622a19a400b003ef3402dda4mr211835qtc.32.1683121854969; Wed, 03 May 2023 06:50:54 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6h76yLLtle3GQj5Sm7dBw6NEfMGfSVPIPtbKAPk3lIwtiejSl7Kl3y7OaAKz2NClVRWXn9dA== X-Received: by 2002:a05:622a:19a4:b0:3ef:3402:dda4 with SMTP id u36-20020a05622a19a400b003ef3402dda4mr211791qtc.32.1683121854558; Wed, 03 May 2023 06:50:54 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id g13-20020ac8774d000000b003eae8609f64sm11358639qtu.13.2023.05.03.06.50.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 May 2023 06:50:54 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: ahead of time variable template-id coercion [PR89442] Date: Wed, 3 May 2023 09:50:43 -0400 Message-ID: <20230503135043.273442-1-ppalka@redhat.com> X-Mailer: git-send-email 2.40.1.459.g48d89b51b3 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 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 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) : 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 using enable_if_t = int; -template bool has_P_match_v; +template