From patchwork Thu Jun 1 14:49:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1789110 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=DlP9ktjC; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QX8Dv6bZHz20QB for ; Fri, 2 Jun 2023 00:50:07 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DD96A3856DDA for ; Thu, 1 Jun 2023 14:50:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DD96A3856DDA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685631005; bh=pIM4eYUJN+VeOqOJh15bO4WtBwZnJQCRAC6nR9sCfSw=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=DlP9ktjCNR6gD6yv9WqGr869v2WU/QKj/QpT7k0GTQvYlp2FZLryKsaTBvVFeyp89 MMmNFMLrpS8HdJK3d1kOt0T4KNkylPGnwx/3Hmks+hC1PLZ5RvVSiJyY8QS8sd4eWx bAA/tw7/nuC9Evmxc3es3fFF1SBxzGvozt3SyfeY= 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 C69433857357 for ; Thu, 1 Jun 2023 14:49:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C69433857357 Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-10-XA-rhloAMX-Nj2ZXI9GypQ-1; Thu, 01 Jun 2023 10:49:42 -0400 X-MC-Unique: XA-rhloAMX-Nj2ZXI9GypQ-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-75b12ae55b1so95362785a.2 for ; Thu, 01 Jun 2023 07:49:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685630981; x=1688222981; 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=pIM4eYUJN+VeOqOJh15bO4WtBwZnJQCRAC6nR9sCfSw=; b=evZqhqbWMXJ9t6JPTrKXxUZcgN92U1qaZSz22Dh4Krxhp0csk8UkhOuSU5nI3A3/qw o9z62mpNeQzqL2sjBJG/tC6xPaEzt7nin/7IHMIQh2BQBRWhQzNlae8wzErU7q66bCpd W8pVVc7NdWjrgqQneugJNsgM6r28ONpn+76jxzpO+DGcrtLGJLr57E9lTxJcKTNSOQIO 4fvVwERY67Xn4aiUjKNUwBfTEHialkT9PxAVv+p4eVioGAzmMuyRH0l/1l0hD3TB1Hpb xDoAeej0bAgutLaizpeXc+FNS9oYO7+bLgzbC9GaqRyg6b9CH6mDYljDBLAVDkXp4sKM QyLA== X-Gm-Message-State: AC+VfDwOugzP8CDEOV5aPssRmfCUJQ+J5HUENr3fFL1PlSaqPT1RkrsK A8w7G8c55V6roFlJas7rsnl+T8zWRys5JczqQlR6lkIcVKv3brcpmb1GceDjtVZsnjJgSMvPjPW JvKnvIxLLSBH205Rd6HPG7PlI/cWT07P2REO7ZPvsXmt9iB69zwnDZ1S8Ewff3ae8o2A61MR3sT I= X-Received: by 2002:a05:620a:8904:b0:75b:23a1:8e44 with SMTP id ql4-20020a05620a890400b0075b23a18e44mr8826341qkn.21.1685630980971; Thu, 01 Jun 2023 07:49:40 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7ujumDIx2mnM/YHIkm7fD492RKncp9BFzzpecqROiAQC6wR6RUt1M8X9YpZK7KVbju7JSBBg== X-Received: by 2002:a05:620a:8904:b0:75b:23a1:8e44 with SMTP id ql4-20020a05620a890400b0075b23a18e44mr8826316qkn.21.1685630980445; Thu, 01 Jun 2023 07:49:40 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id h18-20020a05620a13f200b0075bb2aa9a67sm6636661qkl.59.2023.06.01.07.49.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Jun 2023 07:49:40 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH 1/2] c++: refine dependent_alias_template_spec_p [PR90679] Date: Thu, 1 Jun 2023 10:49:36 -0400 Message-ID: <20230601144938.765175-1-ppalka@redhat.com> X-Mailer: git-send-email 2.41.0.rc1.10.g9e49351c30 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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, 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 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" For a complex alias template-id, dependent_alias_template_spec_p returns true if any template argument of the template-id is dependent. This predicate indicates that substitution into the template-id may behave differently with respect to SFINAE than substitution into the expanded alias, and so the alias is in a way non-transparent. For example 'first_t' in template using first_t = T; template first_t f(); is such an alias template-id since first_t doesn't use its second template parameter and so the substitution into the expanded alias would discard the SFINAE effects of the corresponding (dependent) argument 'T&'. But this predicate is overly conservative since what really matters for sake of SFINAE equivalence is whether a template argument corresponding to an _unused_ template parameter is dependent. So the predicate should return false for e.g. 'first_t' or 'first_t'. This patch refines the predicate appropriately. We need to be able to efficiently determine which template parameters of a complex alias template are unused, so to that end we add a new out parameter to complex_alias_template_p and cache its result in an on-the-side hash_map that replaces the existing TEMPLATE_DECL_COMPLEX_ALIAS_P flag. And in doing so, we fix a latent bug that this flag wasn't being propagated during partial instantiation, and so we were treating all partially instantiated member alias templates as non-complex. PR c++/90679 gcc/cp/ChangeLog: * cp-tree.h (TEMPLATE_DECL_COMPLEX_ALIAS_P): Remove. (most_general_template): Constify parameter. * pt.cc (push_template_decl): Adjust after removing TEMPLATE_DECL_COMPLEX_ALIAS_P. (complex_alias_tmpl_info): New hash_map. (uses_all_template_parms_data::seen): Change type to tree* from bool*. (complex_alias_template_r): Adjust accordingly. (complex_alias_template_p): Add 'seen_out' out parameter. Call most_general_template and check PRIMARY_TEMPLATE_P. Use complex_alias_tmpl_info to cache the result and set '*seen_out' accordigly. (dependent_alias_template_spec_p): Add !processing_template_decl early exit test. Consider dependence of only template arguments corresponding to seen template parameters as per gcc/testsuite/ChangeLog: * g++.dg/cpp0x/alias-decl-75.C: New test. --- gcc/cp/cp-tree.h | 7 +- gcc/cp/pt.cc | 101 +++++++++++++++------ gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C | 24 +++++ 3 files changed, 100 insertions(+), 32 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index a1b882f11fe..5330d1e1f62 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -543,7 +543,6 @@ extern GTY(()) tree cp_global_trees[CPTI_MAX]; 2: DECL_THIS_EXTERN (in VAR_DECL, FUNCTION_DECL or PARM_DECL) DECL_IMPLICIT_TYPEDEF_P (in a TYPE_DECL) DECL_CONSTRAINT_VAR_P (in a PARM_DECL) - TEMPLATE_DECL_COMPLEX_ALIAS_P (in TEMPLATE_DECL) DECL_INSTANTIATING_NSDMI_P (in a FIELD_DECL) USING_DECL_UNRELATED_P (in USING_DECL) 3: DECL_IN_AGGR_P. @@ -3655,10 +3654,6 @@ struct GTY(()) lang_decl { #define TYPE_DECL_ALIAS_P(NODE) \ DECL_LANG_FLAG_6 (TYPE_DECL_CHECK (NODE)) -/* Nonzero for TEMPLATE_DECL means that it is a 'complex' alias template. */ -#define TEMPLATE_DECL_COMPLEX_ALIAS_P(NODE) \ - DECL_LANG_FLAG_2 (TEMPLATE_DECL_CHECK (NODE)) - /* Nonzero for a type which is an alias for another type; i.e, a type which declaration was written 'using name-of-type = another-type'. */ @@ -7403,7 +7398,7 @@ extern tree tsubst_argument_pack (tree, tree, tsubst_flags_t, tree); extern tree tsubst_template_args (tree, tree, tsubst_flags_t, tree); extern tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree); extern tree tsubst_function_parms (tree, tree, tsubst_flags_t, tree); -extern tree most_general_template (tree); +extern tree most_general_template (const_tree); extern tree get_mostly_instantiated_function_type (tree); extern bool problematic_instantiation_changed (void); extern void record_last_problematic_instantiation (void); diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 7fb3e75bceb..1b28195e10d 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -211,7 +211,6 @@ static tree listify (tree); static tree listify_autos (tree, tree); static tree tsubst_template_parm (tree, tree, tsubst_flags_t); static tree instantiate_alias_template (tree, tree, tsubst_flags_t); -static bool complex_alias_template_p (const_tree tmpl); static tree get_underlying_template (tree); static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree); static tree canonicalize_expr_argument (tree, tsubst_flags_t); @@ -6233,8 +6232,6 @@ push_template_decl (tree decl, bool is_friend) constr = build_constraints (constr, NULL_TREE); set_constraints (decl, constr); } - if (complex_alias_template_p (tmpl)) - TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true; } } @@ -6526,12 +6523,16 @@ alias_template_specialization_p (const_tree t, return NULL_TREE; } +/* A cache of the result of complex_alias_template_p. */ + +static GTY(()) hash_map *complex_alias_tmpl_info; + /* Data structure for complex_alias_template_*. */ struct uses_all_template_parms_data { int level; - bool *seen; + tree *seen; }; /* walk_tree callback for complex_alias_template_p. */ @@ -6551,7 +6552,7 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_) { tree idx = get_template_parm_index (t); if (TEMPLATE_PARM_LEVEL (idx) == data.level) - data.seen[TEMPLATE_PARM_IDX (idx)] = true; + data.seen[TEMPLATE_PARM_IDX (idx)] = boolean_true_node; } default:; @@ -6576,7 +6577,7 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_) return t; /* Consider the expanded packs to be used outside the expansion... */ - data.seen[idx] = true; + data.seen[idx] = boolean_true_node; } /* ...but don't walk into the pattern. Consider PR104008: @@ -6598,12 +6599,17 @@ complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_) using that alias can be ill-formed when the expansion is not, as with the void_t template. - Returns 1 if always complex, 0 if not complex, -1 if complex iff any of the - template arguments are empty packs. */ + If this predicate returns true in the ordinary case, the out parameter + SEEN_OUT is set to a TREE_VEC containing boolean_true_node at element I if + the I'th template parameter of the alias template is used in the alias. */ static bool -complex_alias_template_p (const_tree tmpl) +complex_alias_template_p (const_tree tmpl, tree *seen_out) { + tmpl = most_general_template (tmpl); + if (!PRIMARY_TEMPLATE_P (tmpl)) + return false; + /* A renaming alias isn't complex. */ if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl) return false; @@ -6612,26 +6618,52 @@ complex_alias_template_p (const_tree tmpl) if (get_constraints (tmpl)) return true; + if (tree *slot = hash_map_safe_get (complex_alias_tmpl_info, tmpl)) + { + tree result = *slot; + if (result == boolean_false_node) + return false; + if (result == boolean_true_node) + return true; + gcc_assert (TREE_CODE (result) == TREE_VEC); + if (seen_out) + *seen_out = result; + return true; + } + struct uses_all_template_parms_data data; tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); tree parms = DECL_TEMPLATE_PARMS (tmpl); data.level = TMPL_PARMS_DEPTH (parms); int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms)); - data.seen = XALLOCAVEC (bool, len); + tree seen = make_tree_vec (len); + data.seen = TREE_VEC_BEGIN (seen); for (int i = 0; i < len; ++i) - data.seen[i] = false; + data.seen[i] = boolean_false_node; if (cp_walk_tree_without_duplicates (&pat, complex_alias_template_r, &data)) - return true; - for (int i = 0; i < len; ++i) - if (!data.seen[i]) + { + hash_map_safe_put (complex_alias_tmpl_info, tmpl, boolean_true_node); return true; + } + + for (int i = 0; i < len; ++i) + if (data.seen[i] != boolean_true_node) + { + hash_map_safe_put (complex_alias_tmpl_info, tmpl, seen); + if (seen_out) + *seen_out = seen; + return true; + } + + hash_map_safe_put (complex_alias_tmpl_info, tmpl, boolean_false_node); return false; } -/* If T is a specialization of a complex alias template with dependent - template-arguments, return it; otherwise return NULL_TREE. If T is a - typedef to such a specialization, return the specialization. */ +/* If T is a specialization of a complex alias template with a dependent + argument for an unused template parameter, return it; otherwise return + NULL_TREE. If T is a typedef to such a specialization, return the + specialization. */ tree dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs) @@ -6640,15 +6672,32 @@ dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs) return NULL_TREE; gcc_assert (TYPE_P (t)); - if (!typedef_variant_p (t)) + if (!processing_template_decl || !typedef_variant_p (t)) return NULL_TREE; - tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t); - if (tinfo - && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)) - && (any_dependent_template_arguments_p - (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))) - return CONST_CAST_TREE (t); + if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t)) + { + tree seen = NULL_TREE; + if (complex_alias_template_p (TI_TEMPLATE (tinfo), &seen)) + { + tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)); + if (!seen) + { + if (any_dependent_template_arguments_p (args)) + return CONST_CAST_TREE (t); + } + else + { + gcc_assert (TREE_VEC_LENGTH (args) == TREE_VEC_LENGTH (seen)); + for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i) + if (TREE_VEC_ELT (seen, i) != boolean_true_node + && dependent_template_arg_p (TREE_VEC_ELT (args, i))) + return CONST_CAST_TREE (t); + } + + return NULL_TREE; + } + } if (transparent_typedefs) { @@ -25958,7 +26007,7 @@ most_specialized_instantiation (tree templates) `template template S::f(U)'. */ tree -most_general_template (tree decl) +most_general_template (const_tree decl) { if (TREE_CODE (decl) != TEMPLATE_DECL) { @@ -25992,7 +26041,7 @@ most_general_template (tree decl) decl = DECL_TI_TEMPLATE (decl); } - return decl; + return CONST_CAST_TREE (decl); } /* Return the most specialized of the template partial specializations diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C new file mode 100644 index 00000000000..10592f521a0 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75.C @@ -0,0 +1,24 @@ +// PR c++/90679 +// { dg-do compile { target c++11 } } + +template +using first_t = T; + +template +struct A; + +template +struct traits; + +template +struct traits>> { + static constexpr int value = 1; +}; + +template +struct traits>> { + static constexpr int value = 2; +}; + +static_assert(traits>::value == 1, ""); +static_assert(traits>::value == 2, ""); // { dg-bogus "ambiguous" }