From patchwork Sat Feb 18 21:42:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 59026 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp562826wrn; Sat, 18 Feb 2023 13:44:08 -0800 (PST) X-Google-Smtp-Source: AK7set+uFf3K47s1h27jnAgxZC0vS6tfjL3R7coGqrD53IhC/8S1Z3dgIUUXkYrBBqkUiYddRIO0 X-Received: by 2002:aa7:d307:0:b0:4ae:eab6:9fac with SMTP id p7-20020aa7d307000000b004aeeab69facmr473368edq.32.1676756648004; Sat, 18 Feb 2023 13:44:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676756647; cv=none; d=google.com; s=arc-20160816; b=Lpu0+USvyzjmbc8sg3DFnbzu3+imddieiMBotKo2XhWd2sNdkQ16QKhl3qu6g82Rgn lJKWvWZ1YtwwsqVWmWm9oa++gy7ItKkbtroNBXoJ+U9vmQAXkPHdY0kWsA0nGKSMvVyF h/iogvHBtXDSjlnzYaSlJWcQKZnGhgTgdq4iD/PkXRibWjG65Ilft6EzWdD1r6kl1LSW UCV0rx0Z1ehIsQp18z3aphDiUqmjxFrLN4AFltR/BZFwqHxOzEpQfCn9y4hK3CYYeg8/ GtNa9MhWFbOS0i8bavD4pxpiLTyPboFzq4bp/JJkrWCQucNQ+ensTwbnAj7NZ2LnB95J Yz7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=bGjw5OHQRqdFgeSrZbqQOMv+Iw7yRa54NAnMWcRLu5I=; b=pwtwccib6gijPY9GKj/jCRw/o+GeEbOgVkkhFqHSvHTh6IhqjnA38uesy4v3p76Mc/ 0P0xrAYavQieSO1wcTkYY0NmUqouu6bP4aVQGERQ3aZcwOEdLouNknJJPAcIFSc2mkS7 k0V3GAtrqMrE2zmXJmTVpWcHFtD0PrfB6Z0o5eOB08h5RZjeW/za5nHMaDWz8K3Zgakb dgFF+NPrk6f/Dgeq5tK3sRMmcSUsWyk96f+2qheCZ8/MBT0AgcM/FQw+FfvQkQxFvwFM vn7XbN8AHXWuLR8/6+1QAVPIG6wIrLU7wPlftfY39HBQXeI1NMbJelvQM1m+ODutB0pg DPnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="UFPgrC/3"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id w12-20020aa7cb4c000000b004acc42357cfsi9811432edt.364.2023.02.18.13.44.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Feb 2023 13:44:07 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="UFPgrC/3"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3C3EA3854806 for ; Sat, 18 Feb 2023 21:43:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3C3EA3854806 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1676756612; bh=bGjw5OHQRqdFgeSrZbqQOMv+Iw7yRa54NAnMWcRLu5I=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=UFPgrC/3B9XIwtqfydu2GseJHU6Dsg7QLmfYDVa6KZUTR87sNJOOoBas/Jokx8BL7 H2GdHGP/ErVt4AUmc83jQ+dLzvR9nmMdVXxe1IfHKZVWTGfLLpp4+0LqIFH/HT+5PY Bjm8CLplWj3MVjyiKmE+98f+wsYxcRR7gsr6rkkQ= 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 [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id C043F3858D32 for ; Sat, 18 Feb 2023 21:42:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C043F3858D32 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-534-QgFDl4FMObSoCcRUKUDX4w-1; Sat, 18 Feb 2023 16:42:44 -0500 X-MC-Unique: QgFDl4FMObSoCcRUKUDX4w-1 Received: by mail-qv1-f70.google.com with SMTP id mv6-20020a056214338600b00570ccb820abso1021602qvb.5 for ; Sat, 18 Feb 2023 13:42:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bGjw5OHQRqdFgeSrZbqQOMv+Iw7yRa54NAnMWcRLu5I=; b=vrElGEctbFwmIgT/D2HRu4zOrpdgtyXTpbk1dDa95jMIim4HtZApI2zoLYTbgbyTUS BvdBUG9zZiA5ZZXjQKgMT+R/H+SGI7geVuzhJ8HVsRJkXgBs5xxwM15wOnFx2J3W0oob kGFvL5oagawfirccSXnmsJqjR+JC+PWQw5qa7tBRdggfZMi//3YinHrCwdOGzDauoWri YJp+4gFwci8edCgULQlq1jpoYOswoJvuaAS8PbcmfURqRL4U7p9SQcXhwUHl5y4dVs3a sruDZz3SiYBr8UyuwYvEfQ9xcnWX2nzn7ySvjG7hC6nmQTv4bdczKQqD89kH29EjSqhP VXKw== X-Gm-Message-State: AO0yUKVDIyNn+oE1V7xNRBuLyH13C7zszGzUpLspBspxGz4/BhN7rKaY 0nFpcelVCx9ChjyHpms/+iBl5q9Uxv+IqPDRUpNp+r/8S1Onozp1LUdL/UmBMmCdBZiW6ZRqo3g 9Kis2GImAXjQDvhvMMnhO+fAUZZyOEciGm77y72M05CXf5Ljz06YSFqJ0doW3hVpzRCY6ip4= X-Received: by 2002:ad4:5963:0:b0:56c:1574:6538 with SMTP id eq3-20020ad45963000000b0056c15746538mr464492qvb.42.1676756563179; Sat, 18 Feb 2023 13:42:43 -0800 (PST) X-Received: by 2002:ad4:5963:0:b0:56c:1574:6538 with SMTP id eq3-20020ad45963000000b0056c15746538mr464454qvb.42.1676756562522; Sat, 18 Feb 2023 13:42:42 -0800 (PST) Received: from jason.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id u62-20020a372e41000000b0073d85e77fa9sm263717qkh.74.2023.02.18.13.42.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Feb 2023 13:42:41 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [PATCH RFC 1/3] c++: add __is_deducible trait [PR105841] Date: Sat, 18 Feb 2023 16:42:37 -0500 Message-Id: <20230218214239.2297623-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758206779226647361?= X-GMAIL-MSGID: =?utf-8?q?1758206779226647361?= Tested x86_64-pc-linux-gnu. Since this is fixing experimental (C++20) functionality, I think it's reasonable to apply now; I'm interested in other opinions, and thoughts about the user-facing functionality. I'm thinking to make it internal-only for GCC 13 at least by adding a space in the name, but does this look useful to the library? -- 8< -- C++20 class template argument deduction for an alias template involves adding a constraint that the template arguments for the alias template can be deduced from the return type of the deduction guide for the underlying class template. In the standard, this is modeled as defining a class template with a partial specialization, but it's much more efficient to implement with a trait that directly tries to perform the deduction. The first argument to the trait is a template rather than a type, so various places needed to be adjusted to accommodate that. PR c++/105841 gcc/ChangeLog: * doc/extend.texi (Type Traits):: Document __is_deducible. gcc/cp/ChangeLog: * cp-trait.def (IS_DEDUCIBLE): New. * cxx-pretty-print.cc (pp_cxx_trait): Handle non-type. * parser.cc (cp_parser_trait): Likewise. * pt.cc (tsubst_copy_and_build): Likewise. (type_targs_deducible_from): New. (alias_ctad_tweaks): Use it. * semantics.cc (trait_expr_value): Handle CPTK_IS_DEDUCIBLE. (finish_trait_expr): Likewise. * constraint.cc (diagnose_trait_expr): Likewise. * cp-tree.h (type_targs_deducible_from): Declare. gcc/testsuite/ChangeLog: * g++.dg/ext/is_deducible1.C: New test. --- gcc/doc/extend.texi | 4 +++ gcc/cp/cp-tree.h | 1 + gcc/cp/constraint.cc | 3 ++ gcc/cp/cxx-pretty-print.cc | 5 +++- gcc/cp/parser.cc | 20 +++++++++++--- gcc/cp/pt.cc | 35 +++++++++++++++++------- gcc/cp/semantics.cc | 11 ++++++++ gcc/testsuite/g++.dg/ext/is_deducible1.C | 27 ++++++++++++++++++ gcc/cp/cp-trait.def | 1 + 9 files changed, 92 insertions(+), 15 deletions(-) create mode 100644 gcc/testsuite/g++.dg/ext/is_deducible1.C base-commit: 9944ca17c0766623bce260684edc614def7ea761 diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 1ae68b0f20a..898701424ad 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -25207,6 +25207,10 @@ type. A diagnostic is produced if this requirement is not met. If @code{type} is a cv-qualified class type, and not a union type ([basic.compound]) the trait is @code{true}, else it is @code{false}. +@item __is_deducible (template, type) +If template arguments for @code{template} can be deduced from +@code{type} or obtained from default template arguments. + @item __is_empty (type) If @code{__is_class (type)} is @code{false} then the trait is @code{false}. Otherwise @code{type} is considered empty if and only if: @code{type} diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 5595335bbf7..e79150ca4d8 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7372,6 +7372,7 @@ extern tree fn_type_unification (tree, tree, tree, bool, bool); extern void mark_decl_instantiated (tree, int); extern int more_specialized_fn (tree, tree, int); +extern bool type_targs_deducible_from (tree, tree); extern void do_decl_instantiation (tree, tree); extern void do_type_instantiation (tree, tree, tsubst_flags_t); extern bool always_instantiate_p (tree); diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc index 9374327008b..a28c85178fe 100644 --- a/gcc/cp/constraint.cc +++ b/gcc/cp/constraint.cc @@ -3797,6 +3797,9 @@ diagnose_trait_expr (tree expr, tree args) inform (loc, " %qT is not a reference that binds to a temporary " "object of type %qT (copy-initialization)", t1, t2); break; + case CPTK_IS_DEDUCIBLE: + inform (loc, " %qD is not deducible from %qT", t1, t2); + break; #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \ case CPTK_##CODE: #include "cp-trait.def" diff --git a/gcc/cp/cxx-pretty-print.cc b/gcc/cp/cxx-pretty-print.cc index bea52a608f1..4ebd957decd 100644 --- a/gcc/cp/cxx-pretty-print.cc +++ b/gcc/cp/cxx-pretty-print.cc @@ -2626,7 +2626,10 @@ pp_cxx_trait (cxx_pretty_printer *pp, tree t) } pp_cxx_left_paren (pp); - pp->type_id (type1); + if (DECL_P (type1)) + pp->expression (type1); + else + pp->type_id (type1); if (type2) { if (TREE_CODE (type2) != TREE_LIST) diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 1a124f5395e..68950cace78 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -10960,10 +10960,22 @@ cp_parser_trait (cp_parser* parser, enum rid keyword) matching_parens parens; parens.require_open (parser); - { - type_id_in_expr_sentinel s (parser); - type1 = cp_parser_type_id (parser); - } + if (kind == CPTK_IS_DEDUCIBLE) + { + const cp_token* token = cp_lexer_peek_token (parser->lexer); + type1 = cp_parser_id_expression (parser, + /*template_keyword_p=*/false, + /*check_dependency_p=*/true, + nullptr, + /*declarator_p=*/false, + /*optional_p=*/false); + type1 = cp_parser_lookup_name_simple (parser, type1, token->location); + } + else + { + type_id_in_expr_sentinel s (parser); + type1 = cp_parser_type_id (parser); + } if (type1 == error_mark_node) return error_mark_node; diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index b1ac7d4beb4..2aa06557b99 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -21577,8 +21577,9 @@ tsubst_copy_and_build (tree t, case TRAIT_EXPR: { - tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args, - complain, in_decl); + tree type1 = TRAIT_EXPR_TYPE1 (t); + if (TREE_CODE (type1) != TEMPLATE_DECL) + type1 = tsubst (type1, args, complain, in_decl); tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args, complain, in_decl); RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t), @@ -29979,7 +29980,7 @@ alias_ctad_tweaks (tree tmpl, tree uguides) /* This implementation differs from the above in two significant ways: 1) We include all template parameters of A, not just some. - 2) The added constraint is same_type instead of deducible. + 2) [fixed] The added constraint is same_type instead of deducible. I believe that while it's probably possible to construct a testcase that behaves differently with this simplification, it should have the same @@ -30079,7 +30080,7 @@ alias_ctad_tweaks (tree tmpl, tree uguides) /* FIXME this should mean they don't compare as equivalent. */ || dependent_alias_template_spec_p (atype, nt_opaque)) { - tree same = finish_trait_expr (loc, CPTK_IS_SAME, atype, ret); + tree same = finish_trait_expr (loc, CPTK_IS_DEDUCIBLE, tmpl, ret); ci = append_constraint (ci, same); } @@ -30093,12 +30094,7 @@ alias_ctad_tweaks (tree tmpl, tree uguides) { /* For a non-template deduction guide, if the arguments of A aren't deducible from the return type, don't add the candidate. */ - tree targs = make_tree_vec (natparms); - int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false); - for (unsigned i = 0; !err && i < natparms; ++i) - if (TREE_VEC_ELT (targs, i) == NULL_TREE) - err = true; - if (err) + if (!type_targs_deducible_from (tmpl, ret)) continue; } @@ -30108,6 +30104,25 @@ alias_ctad_tweaks (tree tmpl, tree uguides) return aguides; } +/* True iff template arguments for TMPL can be deduced from TYPE. + Used to implement CPTK_IS_DEDUCIBLE for alias CTAD. */ + +bool +type_targs_deducible_from (tree tmpl, tree type) +{ + tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); + int len = TREE_VEC_LENGTH (tparms); + tree targs = make_tree_vec (len); + if (unify (tparms, targs, TREE_TYPE (tmpl), type, + UNIFY_ALLOW_NONE, false)) + return false; + /* Maybe add in default template args. */ + targs = coerce_template_parms (tparms, targs, tmpl, tf_none); + if (targs == error_mark_node) + return false; + return constraints_satisfied_p (tmpl, targs); +} + /* Return artificial deduction guides built from the constructors of class template TMPL. */ diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 79b7cc72f21..9103f5de2f4 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -12048,6 +12048,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2) case CPTK_REF_CONVERTS_FROM_TEMPORARY: return ref_xes_from_temporary (type1, type2, /*direct_init=*/false); + case CPTK_IS_DEDUCIBLE: + return type_targs_deducible_from (type1, type2); + #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \ case CPTK_##CODE: #include "cp-trait.def" @@ -12205,6 +12208,14 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2) return error_mark_node; break; + case CPTK_IS_DEDUCIBLE: + if (!DECL_TYPE_TEMPLATE_P (type1)) + { + error ("%qD is not a class or alias template", type1); + return error_mark_node; + } + break; + #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \ case CPTK_##CODE: #include "cp-trait.def" diff --git a/gcc/testsuite/g++.dg/ext/is_deducible1.C b/gcc/testsuite/g++.dg/ext/is_deducible1.C new file mode 100644 index 00000000000..857f59db4c8 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/is_deducible1.C @@ -0,0 +1,27 @@ +// { dg-do compile { target c++20 } } + +template struct A { }; +template struct B { }; + +// Simple forms. +static_assert (__is_deducible (::A, A)); +static_assert (__is_deducible (B, B)); +static_assert (!__is_deducible (A, B)); +static_assert (!__is_deducible (::B, A)); + +// This is the interesting use case for alias CTAD. +template using AP = A; +static_assert (__is_deducible (AP, A)); +static_assert (!__is_deducible (AP, A)); + +// Can't deduce a parameter not used on the RHS. +template using C = void; +static_assert (!__is_deducible (C, C)); + +// But a default template argument counts. +template using D = void; +static_assert (__is_deducible (D, D)); + +// We don't try to support this. +template void f(T); +bool b = __is_deducible (f, void (int)); // { dg-error "class or alias" } diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def index 823899a26c5..e43fb464f42 100644 --- a/gcc/cp/cp-trait.def +++ b/gcc/cp/cp-trait.def @@ -84,6 +84,7 @@ DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1) DEFTRAIT_EXPR (IS_UNION, "__is_union", 1) DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_temporary", 2) DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, "__reference_converts_from_temporary", 2) +DEFTRAIT_EXPR (IS_DEDUCIBLE, "__is_deducible", 2) DEFTRAIT_TYPE (REMOVE_CV, "__remove_cv", 1) DEFTRAIT_TYPE (REMOVE_REFERENCE, "__remove_reference", 1) From patchwork Sat Feb 18 21:42:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 59028 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp562873wrn; Sat, 18 Feb 2023 13:44:16 -0800 (PST) X-Google-Smtp-Source: AK7set9eJ8mBN6Nar7EjKByEQJFllhvTIFboFnyxhSWRL8XEXHeFFu9jwGM3VrjLI/j4sGJe5vlH X-Received: by 2002:a17:907:2da8:b0:8b1:7274:1a72 with SMTP id gt40-20020a1709072da800b008b172741a72mr1569433ejc.6.1676756656661; Sat, 18 Feb 2023 13:44:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676756656; cv=none; d=google.com; s=arc-20160816; b=TQFocoev7tUaZzsPDqpGE5frdIQzGIL9oBxhWQVrmcABR9LUBJF5mIz6W9rMNg7Yd+ 4+X/1rS3nUzkEY3N6E8TejQjh3zD9ffa04k5GW/VZuobbCt6cmgL48khjPcAmqMkhBSL HU0xEJEOnRZZUsFFqXSkFHn7/s/mMbd+fqTbUYgXET3ckzMY4e8F5i5gqukQGdkOJ/Uq 7HIx5QGalrRYeLCxXGUmt9Kkc3IUztnXHm4QkmLG464YssvkrURhi5dJnWoGiq19/Apu EaAmqBDhD2NocUaLA5hSaIJPQggrhf4TLDiekEbdLhN2/Q3lP8e9R41DNbA5jDCPnCuf E8TA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=LDuDGnWt4jiH242VOFJTiIFnK0ySK+KnJBC9TFV27Qc=; b=b4hUY9K/gH5DgW2gR9BkEZ0LVZTfMdMZxKbDIuKPYI1go7Kp5vkGy2TFZinxYGXhiO iwzjMvCMa3/qvBL7QXBMwwBgcRyGRrqKXNvEtYriINPXwQ1ZKItGL2utg8nVV63xCuDc xbeIz72ngaEA7f3kRYORr4mFsPAaGC7X0w5fOnJ0CebzqQochABW+TLV1CRTPBLKtc5I HFAtiLArLi38XvyGbs2OZddbkla71py3k4627RfTl5lq+y4/3Uh7vEgmknfG2+zmGR/n fSjAmigOJAInjezodHs6Y84HQYlUS8eMG0j6utsBOB9Pp3wVWUFR9R7GLekJYx8m+cr3 7bwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Yzd9jsJX; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ui4-20020a170907c90400b008b12d7f9788si8920269ejc.789.2023.02.18.13.44.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Feb 2023 13:44:16 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Yzd9jsJX; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 086B9382E6BE for ; Sat, 18 Feb 2023 21:43:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 086B9382E6BE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1676756617; bh=LDuDGnWt4jiH242VOFJTiIFnK0ySK+KnJBC9TFV27Qc=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Yzd9jsJXeqwZzxu63+WUJ9lXZfjUORwqowK4fe/umMY4biNuv1pQcVftirM4IXCWG ZO5Br/Yzb2llgbESi3Bp0AAMdPo6PNF4xaQd9W1bikDSThqL7SVke/KVTmQYz7XOky ToOcYYG+3C1JkmexxZf3MVxUsWlurCOEG8WYRJHE= 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 [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 594743858D38 for ; Sat, 18 Feb 2023 21:42:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 594743858D38 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_128_GCM_SHA256) id us-mta-396-FZc183CgP9ekCj4ZDnNA1w-1; Sat, 18 Feb 2023 16:42:45 -0500 X-MC-Unique: FZc183CgP9ekCj4ZDnNA1w-1 Received: by mail-qt1-f200.google.com with SMTP id k17-20020ac86051000000b003b9a3ab9153so398562qtm.8 for ; Sat, 18 Feb 2023 13:42:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LDuDGnWt4jiH242VOFJTiIFnK0ySK+KnJBC9TFV27Qc=; b=tbfUIPlc6y2918Sp2y3eTNkhMnPIqQ14TOAdlb7XUfyp+nHQGviWh5TZa/EdtHd102 fV0/xvp8Qdz09VjvpYamxZhYt1XkKCa4NJNNX3S3IOKB7XZ583jQ+AnzDzMwPeL33ony 3oirIvDJZLBkLbolxapXy/ptixFP4EslcvJX1vRCoG8dP1CptE6CijzSwT0ySd9J1cL3 rOpHyl76FwFUdIam8sgSJEAhqIlmJJHTmmhogzTPfY50p0N3Mht/pbr9ga+XwoyYyGEX +7fh+UZ1SMuihKeJ93e55VUvVYpKoeTZlKXh6b8xLDOtr3uUlIY8CBdWiXX0pohyIZHL H81w== X-Gm-Message-State: AO0yUKXTwYlqqFQR6qhD6Cnp8F5VIyxE8bhygFBJ/F/7iVVZ0pEYvRBZ FZtTC+jjSQ4BDAKyGXMPuIXYYxCU/VBie36aYOQlwHioFNUfuvvJiyFrnrY9/AgRrqs3+BgxG86 kwWtwm6sM69h1NTvR7R3mvRnJ3EEhbURWgnk+kEIjGAg54qimgnhMzyreuWHNbXkEnbxT2vM= X-Received: by 2002:ac8:5f49:0:b0:3bd:1647:15cd with SMTP id y9-20020ac85f49000000b003bd164715cdmr12284629qta.2.1676756564767; Sat, 18 Feb 2023 13:42:44 -0800 (PST) X-Received: by 2002:ac8:5f49:0:b0:3bd:1647:15cd with SMTP id y9-20020ac85f49000000b003bd164715cdmr12284608qta.2.1676756564206; Sat, 18 Feb 2023 13:42:44 -0800 (PST) Received: from jason.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id u62-20020a372e41000000b0073d85e77fa9sm263717qkh.74.2023.02.18.13.42.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Feb 2023 13:42:43 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: Michael Spertus Subject: [PATCH 2/3] c++: fix alias CTAD [PR105841] Date: Sat, 18 Feb 2023 16:42:38 -0500 Message-Id: <20230218214239.2297623-2-jason@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230218214239.2297623-1-jason@redhat.com> References: <20230218214239.2297623-1-jason@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-24.9 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758206788307932601?= X-GMAIL-MSGID: =?utf-8?q?1758206788307932601?= In my initial implementation of alias CTAD, I described a couple of differences from the specification that I thought would not have a practical effect; this testcase demonstrates that I was wrong. One difference is resolved by the CPTK_IS_DEDUCIBLE commit; the other (adding too many of the alias template parameters to the new deduction guide) is fixed by this patch. PR c++/105841 gcc/cp/ChangeLog: * pt.cc (corresponding_template_parameter_list): Split out... (corresponding_template_parameter): ...from here. (find_template_parameters): Factor out... (find_template_parameter_info::find_in): ...this function. (find_template_parameter_info::find_in_recursive): New. (find_template_parameter_info::found): New. (alias_ctad_tweaks): Only add parms used in the deduced args. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/class-deduction-alias14.C: New test. Co-authored-by: Michael Spertus --- gcc/cp/pt.cc | 133 +++++++++++++----- .../g++.dg/cpp2a/class-deduction-alias14.C | 13 ++ 2 files changed, 114 insertions(+), 32 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/class-deduction-alias14.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 2aa06557b99..1934c9dafac 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -10367,7 +10367,7 @@ lookup_and_finish_template_variable (tree templ, tree targs, return NULL_TREE. */ static tree -corresponding_template_parameter (tree parms, int level, int index) +corresponding_template_parameter_list (tree parms, int level, int index) { while (TMPL_PARMS_DEPTH (parms) > level) parms = TREE_CHAIN (parms); @@ -10376,7 +10376,30 @@ corresponding_template_parameter (tree parms, int level, int index) || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index) return NULL_TREE; - tree t = TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), index)); + return TREE_VEC_ELT (TREE_VALUE (parms), index); +} + +/* Return the TREE_LIST for the template parameter from PARMS that positionally + corresponds to the template parameter PARM, or else return NULL_TREE. */ + +static tree +corresponding_template_parameter_list (tree parms, tree parm) +{ + int level, index; + template_parm_level_and_index (parm, &level, &index); + return corresponding_template_parameter_list (parms, level, index); +} + +/* As above, but pull out the actual parameter. */ + +static tree +corresponding_template_parameter (tree parms, tree parm) +{ + tree list = corresponding_template_parameter_list (parms, parm); + if (!list) + return NULL_TREE; + + tree t = TREE_VALUE (list); /* As in template_parm_to_arg. */ if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL) t = TREE_TYPE (t); @@ -10386,18 +10409,6 @@ corresponding_template_parameter (tree parms, int level, int index) gcc_assert (TEMPLATE_PARM_P (t)); return t; } - -/* Return the template parameter from PARMS that positionally corresponds - to the template parameter PARM, or else return NULL_TREE. */ - -static tree -corresponding_template_parameter (tree parms, tree parm) -{ - int level, index; - template_parm_level_and_index (parm, &level, &index); - return corresponding_template_parameter (parms, level, index); -} - struct pair_fn_data { @@ -10670,6 +10681,11 @@ struct find_template_parameter_info tree *parm_list_tail = &parm_list; tree ctx_parms; int max_depth; + + tree find_in (tree); + tree find_in_recursive (tree); + bool found (tree); + unsigned num_found () { return parms.elements (); } }; /* Appends the declaration of T to the list in DATA. */ @@ -10812,6 +10828,52 @@ any_template_parm_r (tree t, void *data) return 0; } +/* Look through T for template parameters. */ + +tree +find_template_parameter_info::find_in (tree t) +{ + return for_each_template_parm (t, keep_template_parm, this, &visited, + /*include_nondeduced*/true, + any_template_parm_r); +} + +/* As above, but also recursively look into the default arguments of template + parameters we found. Used for alias CTAD. */ + +tree +find_template_parameter_info::find_in_recursive (tree t) +{ + if (tree r = find_in (t)) + return r; + /* Since newly found parms are added to the end of the list, we + can just walk it until we reach the end. */ + for (tree pl = parm_list; pl; pl = TREE_CHAIN (pl)) + { + tree parm = TREE_VALUE (pl); + tree list = corresponding_template_parameter_list (ctx_parms, parm); + if (tree r = find_in (TREE_PURPOSE (list))) + return r; + } + return NULL_TREE; +} + +/* True if PARM was found by a previous call to find_in. PARM can be a + TREE_LIST, a DECL_TEMPLATE_PARM_P, or a TEMPLATE_PARM_P. */ + +bool +find_template_parameter_info::found (tree parm) +{ + if (TREE_CODE (parm) == TREE_LIST) + parm = TREE_VALUE (parm); + if (TREE_CODE (parm) == TYPE_DECL) + parm = TREE_TYPE (parm); + else + parm = DECL_INITIAL (parm); + gcc_checking_assert (TEMPLATE_PARM_P (parm)); + return parms.contains (parm); +} + /* Returns a list of unique template parameters found within T, where CTX_PARMS are the template parameters in scope. */ @@ -10822,8 +10884,7 @@ find_template_parameters (tree t, tree ctx_parms) return NULL_TREE; find_template_parameter_info ftpi (ctx_parms); - for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited, - /*include_nondeduced*/true, any_template_parm_r); + ftpi.find_in (t); return ftpi.parm_list; } @@ -29977,22 +30038,11 @@ alias_ctad_tweaks (tree tmpl, tree uguides) * The explicit-specifier of f' is the explicit-specifier of g (if any). */ - /* This implementation differs from the above in two significant ways: - - 1) We include all template parameters of A, not just some. - 2) [fixed] The added constraint is same_type instead of deducible. - - I believe that while it's probably possible to construct a testcase that - behaves differently with this simplification, it should have the same - effect for real uses. Including all template parameters means that we - deduce all parameters of A when resolving the call, so when we're in the - constraint we don't need to deduce them again, we can just check whether - the deduction produced the desired result. */ - tsubst_flags_t complain = tf_warning_or_error; tree atype = TREE_TYPE (tmpl); tree aguides = NULL_TREE; - tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl)); + tree fullatparms = DECL_TEMPLATE_PARMS (tmpl); + tree atparms = INNERMOST_TEMPLATE_PARMS (fullatparms); unsigned natparms = TREE_VEC_LENGTH (atparms); tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); for (ovl_iterator iter (uguides); iter; ++iter) @@ -30022,16 +30072,27 @@ alias_ctad_tweaks (tree tmpl, tree uguides) for (unsigned i = 0; i < len; ++i) if (TREE_VEC_ELT (targs, i) == NULL_TREE) ++ndlen; - tree gtparms = make_tree_vec (natparms + ndlen); + find_template_parameter_info ftpi (fullatparms); + ftpi.find_in_recursive (targs); + unsigned nusedatparms = ftpi.num_found (); + unsigned nfparms = nusedatparms + ndlen; + tree gtparms = make_tree_vec (nfparms); /* Set current_template_parms as in build_deduction_guide. */ auto ctp = make_temp_override (current_template_parms); current_template_parms = copy_node (DECL_TEMPLATE_PARMS (tmpl)); TREE_VALUE (current_template_parms) = gtparms; + j = 0; /* First copy over the parms of A. */ - for (j = 0; j < natparms; ++j) - TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j); + for (unsigned i = 0; i < natparms; ++i) + { + tree elt = TREE_VEC_ELT (atparms, i); + if (ftpi.found (elt)) + TREE_VEC_ELT (gtparms, j++) = elt; + } + gcc_checking_assert (j == nusedatparms); + /* Now rewrite the non-deduced parms of f. */ for (unsigned i = 0; ndlen && i < len; ++i) if (TREE_VEC_ELT (targs, i) == NULL_TREE) @@ -30058,6 +30119,13 @@ alias_ctad_tweaks (tree tmpl, tree uguides) } if (g == error_mark_node) continue; + if (nfparms == 0) + { + /* The targs are all non-dependent, so g isn't a template. */ + fprime = g; + ret = TREE_TYPE (TREE_TYPE (fprime)); + goto non_template; + } DECL_USE_TEMPLATE (g) = 0; fprime = build_template_decl (g, gtparms, false); DECL_TEMPLATE_RESULT (fprime) = g; @@ -30094,6 +30162,7 @@ alias_ctad_tweaks (tree tmpl, tree uguides) { /* For a non-template deduction guide, if the arguments of A aren't deducible from the return type, don't add the candidate. */ + non_template: if (!type_targs_deducible_from (tmpl, ret)) continue; } diff --git a/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias14.C b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias14.C new file mode 100644 index 00000000000..22b96bcd5d3 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias14.C @@ -0,0 +1,13 @@ +// PR c++/105841 +// { dg-do compile { target c++20 } } + +template +struct A { A(...); }; + +template +A(T, Ts...) -> A; + +template +using B = A; + +B b(0, 0); From patchwork Sat Feb 18 21:42:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 59027 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp562840wrn; Sat, 18 Feb 2023 13:44:10 -0800 (PST) X-Google-Smtp-Source: AK7set/zt+wauS/gAOYG3RNCtpEDvkhp22Aw1JlqK+l24QWCe9ql2ARsqyic4MNerieY094dOLex X-Received: by 2002:a17:906:792:b0:8b1:94cc:4a6b with SMTP id l18-20020a170906079200b008b194cc4a6bmr6935325ejc.37.1676756649948; Sat, 18 Feb 2023 13:44:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676756649; cv=none; d=google.com; s=arc-20160816; b=Sig+U7e0PDg4+ysAJ7zNe6HECde2jFcs72cxNTDvZ/Tb28CklYBuxpvtYONuWyqejj w6JI4Y09vQuk92ifqKvS+91EiYe/HfBT+ulSgh6VTbqMa7evwWRPQmgHSLxy7kxETg62 g4WC4cFdNBTacHuyBg08XcDYlJ9RPc+i2J92l5N3hLbFrw6UOKUbVG+BoSm+7JiQpwZn smNM3rOmI6HH/3H1nFhg+vTYdZGyPqLdY0nOOdD3xJta1u7MO7wGgO43X/ik1BbmQ3ep Skq6TuYPvDPiGLm+waEklz446sXOKTRyfCS25QDSlQghFRZaS7KtVabXgLnItknHQnXz XMdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=XIQL1hfAHgbPG9IhkhIwjvZT6aFB17R1yin7hcae0XQ=; b=0O8uZYcVt67/rDzEk7JX4uBq+82da8sfmtfQfZPpS7JtzqanXV6O2UCR76TCVqoQyE Lw5Iq171ztMpPT5Uz/rRHS6/kqfkr0GIvmVhex1d18MCwepUVXQp3hypYLdbha9nDR3V UpszjWvIOcw4KBWQUPcMGPqWw3e0SpEX+8gHb0Trh8m8MsJ8T/4YBBxEx9S6iPfYwJ9Z JaNdFFUVz8u2LK3WsTkuZiZCWc1zmj5BXbCMq4SR8NwfFaUjuduaut+Y+U3RVOuUS6JV 2SCAOR6EJa3fYxbg+UMWgLYbmNyMboaPMr2rhrpphQW6/TDv36LxyAcPKi/EfqNJv5E8 q7iA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=J71D5tqa; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id d8-20020a056402516800b004acb4614ed5si654002ede.117.2023.02.18.13.44.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Feb 2023 13:44:09 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=J71D5tqa; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A71F03846414 for ; Sat, 18 Feb 2023 21:43:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A71F03846414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1676756613; bh=XIQL1hfAHgbPG9IhkhIwjvZT6aFB17R1yin7hcae0XQ=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=J71D5tqayTWCXD4NfA/wj6H3wf72Dy8UJM/c/pzWb7MYW8c6YCJYCcoWDkx4PObpN GOznf70XC0oravUi558k92VjB95aRLw1PqQn0ROOqYgRyM5898sKvbiZqJ7X+frrEa 6aWVH7xcvymmhDqcO7efAeoqVF4VkGwLwNtSUVqU= 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 [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id AE4913858D3C for ; Sat, 18 Feb 2023 21:42:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AE4913858D3C Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-572-ig83LRplO4qFuK7tIv6hxA-1; Sat, 18 Feb 2023 16:42:46 -0500 X-MC-Unique: ig83LRplO4qFuK7tIv6hxA-1 Received: by mail-qt1-f198.google.com with SMTP id z16-20020ac84550000000b003b849aa2cd6so415757qtn.15 for ; Sat, 18 Feb 2023 13:42:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XIQL1hfAHgbPG9IhkhIwjvZT6aFB17R1yin7hcae0XQ=; b=FynXn4VvHJ0bg56Nbn3gHxtWnxw9FJqw2cBCZ7N7QLVKxZZ+EJWXDoaJtpSWNH3wsn 18LzLpTXNbI0KLqHqCURBf1dk1IDVIGaXMUdbHMNYuAmFF54pBgdSnEnj6w6mcVE23ld ucbTsrxlwASKu98J8dUbDwGrkxmTx0HDJ13JhN8KR6IBb7JXpfizUAX4loZL8dkydeEI UOUFD9CTUlQggcunTo2AfasFt/qYqZA+qM7phbnJT6jFJ7Lc95W6xCZJedCAmRo1fFrQ 5VmCGQI/iJIgbBr6WM6dLVGGjl+tmeij69VwHDyzMcU/6P+b1qPl9VrP3yfU81+gFnxw KGaQ== X-Gm-Message-State: AO0yUKWtd1a3NRdeK98yqYZ/cP8LwOQLyxd1BA699fARwHCVpdfGemls 2BlAyaz7VrkQJcCCF7+bm9wGAs1gcqsdSqS3WuR/CJ3pOsQpc3IHgxvwDQqh9/1R8MFvomI4kTi 6pMRyaj7Dp0aWfIoRwFbKPEd0v5bBvxeatjh7rHFo5W8Tyz8l2N3zXC/SWj3s8rT+OejKVWk= X-Received: by 2002:a05:6214:d49:b0:56e:c00c:bf5c with SMTP id 9-20020a0562140d4900b0056ec00cbf5cmr824775qvr.31.1676756565617; Sat, 18 Feb 2023 13:42:45 -0800 (PST) X-Received: by 2002:a05:6214:d49:b0:56e:c00c:bf5c with SMTP id 9-20020a0562140d4900b0056ec00cbf5cmr824756qvr.31.1676756565220; Sat, 18 Feb 2023 13:42:45 -0800 (PST) Received: from jason.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id u62-20020a372e41000000b0073d85e77fa9sm263717qkh.74.2023.02.18.13.42.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Feb 2023 13:42:44 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [PATCH 3/3] c++: CTAD for less-specialized alias template [PR102529] Date: Sat, 18 Feb 2023 16:42:39 -0500 Message-Id: <20230218214239.2297623-3-jason@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230218214239.2297623-1-jason@redhat.com> References: <20230218214239.2297623-1-jason@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758206780866970489?= X-GMAIL-MSGID: =?utf-8?q?1758206780866970489?= The standard was unclear what happens with the transformation of a deduction guide if the initial template argument deduction fails for a reason other than not deducing all the arguments; my implementation assumed that the right thing was to give up on the deduction guide. But in consideration of CWG2664 this week I realized that we get a better result by just continuing with an empty set of deductions, so the alias deduction guide is the same as the original deduction guide plus the deducible constraint. DR 2664 PR c++/102529 gcc/cp/ChangeLog: * pt.cc (alias_ctad_tweaks): Continue after deduction failure. gcc/testsuite/ChangeLog: * g++.dg/DRs/dr2664.C: New test. * g++.dg/cpp2a/class-deduction-alias15.C: New test. --- gcc/cp/pt.cc | 4 +++- gcc/testsuite/g++.dg/DRs/dr2664.C | 17 +++++++++++++++++ .../g++.dg/cpp2a/class-deduction-alias15.C | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/DRs/dr2664.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/class-deduction-alias15.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 1934c9dafac..69391450b75 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -30063,7 +30063,9 @@ alias_ctad_tweaks (tree tmpl, tree uguides) tree targs = make_tree_vec (len); int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false); if (err) - continue; + /* CWG2664: Discard any deductions, still build the guide. */ + for (unsigned i = 0; i < len; ++i) + TREE_VEC_ELT (targs, i) = NULL_TREE; /* The number of parms for f' is the number of parms for A plus non-deduced parms of f. */ diff --git a/gcc/testsuite/g++.dg/DRs/dr2664.C b/gcc/testsuite/g++.dg/DRs/dr2664.C new file mode 100644 index 00000000000..f6bf8e2ecef --- /dev/null +++ b/gcc/testsuite/g++.dg/DRs/dr2664.C @@ -0,0 +1,17 @@ +// CWG 2664 +// { dg-do compile { target c++20 } } + +template struct C { + C(...); +}; + +template C(T1) -> C; +template C(T1, T2) -> C; + +template using A = C; + +C c1{""}; +A a1{""}; + +C c2{"", 1}; +A a2{"", 1}; diff --git a/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias15.C b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias15.C new file mode 100644 index 00000000000..db615faf8e2 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias15.C @@ -0,0 +1,18 @@ +// PR c++/102529 +// { dg-do compile { target c++20 } } + +template +struct C { + template + C(U); +}; + +template +C(U) -> C; + +template + requires true +using A = C; + +C ok(1); // ok, a is a C +A bad(2); // fails