From patchwork Tue Oct 26 17:44:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 46666 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E6F1C3858436 for ; Tue, 26 Oct 2021 17:44:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E6F1C3858436 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1635270285; bh=w1e6D2NYifPWrDjpnnwNW0c3xUl+fkbJLVuwobvFXOc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ce8fpcuJeiurHMEDnrECGiuXmDwCrU0Qf8fVa3bIuTOSXmAURKUFnCv4xz9q1ZsKg ruJlGd1TOHUyMv2+/lUy5NuiqATLbs8qLxJVT7vuhHctND3mZOAdHfciGYnP5fRS4d IuKOd/K1LzsHJvL7cTp/XXr6G2AB6jLOFIGcD5Fo= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTPS id 3B20E3858D39 for ; Tue, 26 Oct 2021 17:44:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3B20E3858D39 Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-563-8o8fS-Y7NQmyZqGT1yTcVA-1; Tue, 26 Oct 2021 13:44:14 -0400 X-MC-Unique: 8o8fS-Y7NQmyZqGT1yTcVA-1 Received: by mail-qt1-f197.google.com with SMTP id z10-20020ac83e0a000000b002a732692afaso8565874qtf.2 for ; Tue, 26 Oct 2021 10:44:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=w1e6D2NYifPWrDjpnnwNW0c3xUl+fkbJLVuwobvFXOc=; b=1YstDBpUpVEKeW8BjH0vhba2pJtChGBTPDCv3/ZJmWeP9knOlWdKK0cvIs5gbvVYCY dQdtD+kXZ9bfwI7915rBhUCsoZc7Z6m+dM3UIBdrDi/EexWEr00SN9Q0dSCnbwyWKKbo kKH+sikoqH8fObW9uEhlHlKdl9L8h+D7BzZdE4sWDns19NwSExe9HzBCPhSa5v9EI3M3 KLyatkHC5uYK+yfQ0mKSNXZIcYGFasQ38pX+lNgksHeKffuNAta67CSHeAJI/e3pYdRN OUBX2oD5g6OlnC7BmdYxICnp3fY75SGFLKxa7ZRIpUmD14lKzHFmehC0U68tBLov3utb IJyA== X-Gm-Message-State: AOAM533A63A3dkwl2UCz9h9APtBZcAUSQ7LgdAoeZwTvJk+i2UavVNd4 HHv0O0KAdf8zhbCl+AJexv/7+/TJt+NalzajoeYXr40Ty7j4CpQ3APIhzv1hlBFP0mucpwcPDV5 fU8UbNKwwr1CJXKLyOesJBp0dqzuiDUVthxR1Otrn9jNQTVn5b9xEU29ub1zNOyykydM= X-Received: by 2002:a05:622a:1049:: with SMTP id f9mr26737621qte.349.1635270253991; Tue, 26 Oct 2021 10:44:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxGEthslSSXUp6wF365eN2n4y5k3AJgkU0EAypuQ6T+n+8o/RF8mdo3LM0gMN4DXg+PO5edPA== X-Received: by 2002:a05:622a:1049:: with SMTP id f9mr26737571qte.349.1635270253567; Tue, 26 Oct 2021 10:44:13 -0700 (PDT) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id i2sm9464806qti.92.2021.10.26.10.44.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Oct 2021 10:44:13 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: CTAD within template argument [PR102933] Date: Tue, 26 Oct 2021 13:44:09 -0400 Message-Id: <20211026174409.3142598-1-ppalka@redhat.com> X-Mailer: git-send-email 2.33.1.711.g9d530dc002 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-15.7 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_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Here when checking for erroneous occurrences of 'auto' inside a template argument (which is allowed by the concepts TS for class templates), extract_autos_r picks up the CTAD placeholder for X{T{0}} which causes check_auto_in_tmpl_args to reject this valid template argument. This patch fixes this by making extract_autos_r ignore CTAD placeholders. However, it seems we don't need to call check_auto_in_tmpl_args at all outside of the concepts TS since using 'auto' as a type-id is otherwise rejected more generally at parse time. So this patch guards calls to check_auto_in_tmpl_args with flag_concepts_ts instead of flag_concepts. Relatedly, I think the concepts code paths in do_auto_deduction and type_uses_auto are also necessary only for the concepts TS, so this patch also restricts these code paths accordingly. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk and perhaps 11? PR c++/102933 gcc/cp/ChangeLog: * parser.c (cp_parser_template_id): Call check_auto_in_tmpl_args only for the concepts TS not also for standard concepts. (cp_parser_simple_type_specifier): Adjust diagnostic for using auto in parameter declaration. * pt.c (tsubst_qualified_id): Call check_auto_in_tmpl_args only for the concepts TS not also for standard concepts. (extract_autos_r): Ignore CTAD placeholders. (extract_autos): Use range-based for. (do_auto_deduction): Use extract_autos only for the concepts TS and not also for standard concepts. (type_uses_auto): Likewise with for_each_template_parm. (check_auto_in_tmpl_args): Assert that this function is used only for the concepts tS. Simplify. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/nontype-class50.C: New test. * g++.dg/cpp2a/nontype-class50a.C: New test. --- gcc/cp/parser.c | 4 ++-- gcc/cp/pt.c | 24 +++++++++---------- gcc/testsuite/g++.dg/cpp2a/nontype-class50.C | 13 ++++++++++ gcc/testsuite/g++.dg/cpp2a/nontype-class50a.C | 5 ++++ 4 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class50.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class50a.C diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 49d951cfb19..5052f534d40 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -18168,7 +18168,7 @@ cp_parser_template_id (cp_parser *parser, types. We reject them in functions, but if what we have is an identifier, even with none_type we can't conclude it's NOT a type, we have to wait for template substitution. */ - if (flag_concepts && check_auto_in_tmpl_args (templ, arguments)) + if (flag_concepts_ts && check_auto_in_tmpl_args (templ, arguments)) template_id = error_mark_node; /* Build a representation of the specialization. */ else if (identifier_p (templ)) @@ -19505,7 +19505,7 @@ cp_parser_simple_type_specifier (cp_parser* parser, else if (!flag_concepts) pedwarn (token->location, 0, "use of % in parameter declaration " - "only available with %<-fconcepts-ts%>"); + "only available with %<-std=c++20%> or %<-fconcepts%>"); } else type = make_auto (); diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 287cf4ce9d0..3321601e6ff 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -16456,7 +16456,7 @@ tsubst_qualified_id (tree qualified_id, tree args, we want to catch is when we couldn't tell then, and can now, namely when templ prior to substitution was an identifier. */ - if (flag_concepts && check_auto_in_tmpl_args (expr, template_args)) + if (flag_concepts_ts && check_auto_in_tmpl_args (expr, template_args)) return error_mark_node; if (variable_template_p (expr)) @@ -28560,7 +28560,7 @@ static int extract_autos_r (tree t, void *data) { hash_table &hash = *(hash_table*)data; - if (is_auto (t)) + if (is_auto (t) && !template_placeholder_p (t)) { /* All the autos were built with index 0; fix that up now. */ tree *p = hash.find_slot (t, INSERT); @@ -28594,10 +28594,8 @@ extract_autos (tree type) for_each_template_parm (type, extract_autos_r, &hash, &visited, true); tree tree_vec = make_tree_vec (hash.elements()); - for (hash_table::iterator iter = hash.begin(); - iter != hash.end(); ++iter) + for (tree elt : hash) { - tree elt = *iter; unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt)); TREE_VEC_ELT (tree_vec, i) = build_tree_list (NULL_TREE, TYPE_NAME (elt)); @@ -29837,7 +29835,7 @@ do_auto_deduction (tree type, tree init, tree auto_node, tree parms = build_tree_list (NULL_TREE, type); tree tparms; - if (flag_concepts) + if (flag_concepts_ts) tparms = extract_autos (type); else { @@ -30025,7 +30023,7 @@ type_uses_auto (tree type) { if (type == NULL_TREE) return NULL_TREE; - else if (flag_concepts) + else if (flag_concepts_ts) { /* The Concepts TS allows multiple autos in one type-specifier; just return the first one we find, do_auto_deduction will collect all of @@ -30048,6 +30046,9 @@ type_uses_auto (tree type) bool check_auto_in_tmpl_args (tree tmpl, tree args) { + /* Only the concepts TS allows 'auto' as a type-id. */ + gcc_assert (flag_concepts_ts); + /* If there were previous errors, nevermind. */ if (!args || TREE_CODE (args) != TREE_VEC) return false; @@ -30057,11 +30058,10 @@ check_auto_in_tmpl_args (tree tmpl, tree args) We'll only be able to tell during template substitution, so we expect to be called again then. If concepts are enabled and we know we have a type, we're ok. */ - if (flag_concepts - && (identifier_p (tmpl) - || (DECL_P (tmpl) - && (DECL_TYPE_TEMPLATE_P (tmpl) - || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))))) + if (identifier_p (tmpl) + || (DECL_P (tmpl) + && (DECL_TYPE_TEMPLATE_P (tmpl) + || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))) return false; /* Quickly search for any occurrences of auto; usually there won't diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class50.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class50.C new file mode 100644 index 00000000000..1c2786a59e7 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class50.C @@ -0,0 +1,13 @@ +// PR c++/102933 +// { dg-do compile { target c++20 } } + +template struct X { T t; }; + +template void f(); + +template +void g() { + f(); +} + +template void g(); diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class50a.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class50a.C new file mode 100644 index 00000000000..eb8a6ad9375 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class50a.C @@ -0,0 +1,5 @@ +// PR c++/102933 +// { dg-do compile { target c++20 } } +// { dg-additional-options "-fconcepts-ts" } + +#include "nontype-class50.C"