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 1138A3858430 for ; Fri, 15 Dec 2023 19:07:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1138A3858430 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1138A3858430 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702667279; cv=none; b=Usbt+1/gUDfYrvISWJNnBjsL7xtOvaMJdH5aIC/rQr01pF/z4IWlePy8PBvmX98muKGzVMSE/D/NoHEU4SbbNw3rKqk3R7SHejSd3fg5HhVwACMnz9XxAOQok/e3LS8lSqv2vckfE7pnqjpVho2qDtHvPgbipwaX7zY7+6USp4g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702667279; c=relaxed/simple; bh=wEOAc2S8dc7qWLgLRjcNwHcdibtl/sTloPsm/drNMq8=; h=DKIM-Signature:From:Date:To:Subject:Message-ID:MIME-Version; b=bTyu5Lcc+zKcNomhJ1QIP0RhiOfkO6oawt3RpmjGMJ9cbdinbvpn64T7h3Xrb+7CBGJvCpz5vYuJX1lWCmmbZOJCTjv/eesYYR0j32pgLGjSLZxIVUK0D/Nm7j0Rp/2EEKFZpCXT+1aPUnRwGMmRin9Zj56ZuReT+B9Qfdaa6A4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702667276; 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: in-reply-to:in-reply-to:references:references; bh=5htHn34W06kN0aPzInpQVHqeN9vQqPflZ5fZCWvLtfw=; b=ixkUt/Y2hmJ/M58OXx1aYJaIdnZpF/F/miMwrCMhnzFjxYxBhfzt0MH/qgrr+qN+yluneb GBPcpl7bKCYldVZ/7SxQ0KCQsS9v16X+E8MpLej9b7rI6z+k4c9MKsnsEZ/pZzYesSkSxy u4Vu3JBlZ1fIvoz9cR05FcwZLxL/36E= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-61-ckhe7Bp0MsG11ODg2S860g-1; Fri, 15 Dec 2023 14:07:55 -0500 X-MC-Unique: ckhe7Bp0MsG11ODg2S860g-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-77f515c2a5fso134973885a.1 for ; Fri, 15 Dec 2023 11:07:55 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702667274; x=1703272074; h=mime-version:references:message-id:in-reply-to:subject:cc:to:date :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5htHn34W06kN0aPzInpQVHqeN9vQqPflZ5fZCWvLtfw=; b=TF4yRXD6WMvnylqa04IErzcilwR4WOsdHFKV4enr43lmOhdM2cLLoTGJ82NBBLJssC PdsEFo1qQr/L+jw7eZ2jpJiEjaFki0cMo8yQVX9rEXX4/w3yTQAyazVV5wkCJHjJ+A9B MjUA/QUFt1q0GNp4H+05tZLAD9Tfv4ylt3kLrv14kSBTcrwmTfQzPnbisPrcGp5e1sg4 QoW0MucWJCw36jjlCHtTXKK5Sij2ENOqisaHCZLVtNQKzdVb/5ShR1UikBtK1jzGLKKS gLSf2hFaj6zJ+R82Dq2V3lWbRvfc+IycyRpJc9al3368sfDdvG79fZP0wc3KRYbq62SE wIcw== X-Gm-Message-State: AOJu0YxM4CAjRa5LkGtdj0YLEYO84rJqikzc63rS9Pa7ABO9+HcKi6qn RJgCcxWD2Zcc2QwSSveamD4/9Nazvdk4zynlrPxvdTopj60P1ZR7RlkmcMpfMri0gcCnYiFwO20 3+vdPFz2VgyMI+0aEdyZWjK1Jig== X-Received: by 2002:a37:ad03:0:b0:77f:64b:4b1d with SMTP id f3-20020a37ad03000000b0077f064b4b1dmr12826215qkm.115.1702667274077; Fri, 15 Dec 2023 11:07:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IHhUHJs7aew5b3Xw36jeDgsH6UM3CQHAjBLDoERtnlJjNRBokE4H5LRy4q1tBecZnJmNWhrIA== X-Received: by 2002:a37:ad03:0:b0:77f:64b:4b1d with SMTP id f3-20020a37ad03000000b0077f064b4b1dmr12826201qkm.115.1702667273685; Fri, 15 Dec 2023 11:07:53 -0800 (PST) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id qt6-20020a05620a8a0600b0077d8622ee6csm6302147qkn.81.2023.12.15.11.07.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 11:07:53 -0800 (PST) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Fri, 15 Dec 2023 14:07:52 -0500 (EST) To: Patrick Palka cc: gcc-patches@gcc.gnu.org, jason@redhat.com Subject: Re: [PATCH 2/2] c++: partial ordering and dep alias tmpl specs [PR90679] In-Reply-To: <20230601144938.765175-2-ppalka@redhat.com> Message-ID: References: <20230601144938.765175-1-ppalka@redhat.com> <20230601144938.765175-2-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII 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_H4,RCVD_IN_MSPIKE_WL,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: On Thu, 1 Jun 2023, Patrick Palka wrote: > During partial ordering, we want to look through dependent alias > template specializations within template arguments and otherwise > treat them as opaque in other contexts (see e.g. r7-7116-g0c942f3edab108 > and r11-7011-g6e0a231a4aa240). To that end template_args_equal was > given a partial_order flag that controls this behavior. This flag > does the right thing when a dependent alias template specialization > appears as template argument of the partial specialization, e.g. in > > template using first_t = T; > template struct traits; > template struct traits> { }; // #1 > template struct traits> { }; // #2 > > we correctly consider #2 to be more specialized than #1. But if > the alias specialization appears as a template argument of another > class template specialization, e.g. in > > template struct traits>> { }; // #1 > template struct traits>> { }; // #2 > > then we incorrectly consider #1 and #2 to be unordered. This is because > > 1. we don't propagate the flag to recursive template_args_equal calls > 2. we don't use structural equality for class template specializations > written in terms of dependent alias template specializations > > This patch fixes the first issue by turning the partial_order flag into > a global. This patch fixes the second issue by making us propagate > structural equality appropriately when building a class template > specialization. In passing this patch also improves hashing of > specializations that use structural equality. > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > trunk? > > PR c++/90679 > > gcc/cp/ChangeLog: > > * cp-tree.h (comp_template_args): Remove partial_order > parameter. > (template_args_equal): Likewise. > * pt.cc (iterative_hash_template_arg) : Hash > the template and arguments for specializations that use > structural equality. > (comparing_for_partial_ordering): New flag. > (template_args_equal): Remove partial order parameter and > use comparing_for_partial_ordering instead. > (comp_template_args): Likewise. > (comp_template_args_porder): Set comparing_for_partial_ordering > instead. Make static. > (any_template_arguments_need_structural_equality_p): Return true > for an argument that's a dependent alias template specialization > or a class template specialization that itself needs structural > equality. > * tree.cc (cp_tree_equal) : Adjust call to > comp_template_args. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp0x/alias-decl-75a.C: New test. > * g++.dg/cpp0x/alias-decl-75b.C: New test. Ping. Here's the rebased patch: -- >8 -- Subject: [PATCH 2/2] c++: partial ordering and dep alias tmpl specs [PR90679] During partial ordering, we want to look through dependent alias template specializations within template arguments and otherwise treat them as opaque in other contexts (see e.g. r7-7116-g0c942f3edab108 and r11-7011-g6e0a231a4aa240). To that end template_args_equal was given a partial_order flag that controls this behavior. This flag does the right thing when a dependent alias template specialization appears as template argument of the partial specialization, e.g. in template using first_t = T; template struct traits; template struct traits> { }; // #1 template struct traits> { }; // #2 we correctly consider #2 to be more specialized than #1. But if the alias specialization appears as a template argument of another class template specialization, e.g. in template struct traits>> { }; // #1 template struct traits>> { }; // #2 then we incorrectly consider #1 and #2 to be unordered. This is because 1. we don't propagate the flag to recursive template_args_equal calls 2. we don't use structural equality for class template specializations written in terms of dependent alias template specializations This patch fixes the first issue by turning the partial_order flag into a global. This patch fixes the second issue by making us propagate structural equality appropriately when building a class template specialization. In passing this patch also improves hashing of specializations that use structural equality. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/90679 gcc/cp/ChangeLog: * cp-tree.h (comp_template_args): Remove partial_order parameter. (template_args_equal): Likewise. * pt.cc (iterative_hash_template_arg) : Hash the template and arguments for specializations that use structural equality. (comparing_for_partial_ordering): New flag. (template_args_equal): Remove partial order parameter and use comparing_for_partial_ordering instead. (comp_template_args): Likewise. (comp_template_args_porder): Set comparing_for_partial_ordering instead. Make static. (any_template_arguments_need_structural_equality_p): Return true for an argument that's a dependent alias template specialization or a class template specialization that itself needs structural equality. * tree.cc (cp_tree_equal) : Adjust call to comp_template_args. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/alias-decl-75a.C: New test. * g++.dg/cpp0x/alias-decl-75b.C: New test. --- gcc/cp/cp-tree.h | 4 +-- gcc/cp/pt.cc | 40 +++++++++++++++++---- gcc/cp/tree.cc | 2 +- gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C | 26 ++++++++++++++ gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C | 26 ++++++++++++++ 5 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 85980c9ad9b..1979572c365 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7507,8 +7507,8 @@ extern int template_class_depth (tree); extern int is_specialization_of (tree, tree); extern bool is_specialization_of_friend (tree, tree); extern bool comp_template_args (tree, tree, tree * = NULL, - tree * = NULL, bool = false); -extern int template_args_equal (tree, tree, bool = false); + tree * = NULL); +extern int template_args_equal (tree, tree); extern tree maybe_process_partial_specialization (tree); extern tree most_specialized_instantiation (tree); extern tree most_specialized_partial_spec (tree, tsubst_flags_t, bool = false); diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index a3a79713236..6b0ef496dc5 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -1894,6 +1894,11 @@ iterative_hash_template_arg (tree arg, hashval_t val) default: if (tree canonical = TYPE_CANONICAL (arg)) val = iterative_hash_object (TYPE_HASH (canonical), val); + else if (tree ti = TYPE_TEMPLATE_INFO (arg)) + { + val = iterative_hash_template_arg (TI_TEMPLATE (ti), val); + val = iterative_hash_template_arg (TI_ARGS (ti), val); + } break; } @@ -9306,6 +9311,12 @@ coerce_template_parms (tree parms, return return_full_args ? new_args : new_inner_args; } +/* Whether we are comparing template arguments during partial ordering + (and therefore want the comparison to look through dependent alias + template specializations). */ + +static int comparing_for_partial_ordering; + /* Returns true if T is a wrapper to make a C++20 template parameter object const. */ @@ -9322,7 +9333,7 @@ class_nttp_const_wrapper_p (tree t) /* Returns 1 if template args OT and NT are equivalent. */ int -template_args_equal (tree ot, tree nt, bool partial_order /* = false */) +template_args_equal (tree ot, tree nt) { if (nt == ot) return 1; @@ -9345,7 +9356,7 @@ template_args_equal (tree ot, tree nt, bool partial_order /* = false */) During partial ordering, however, we need to treat them normally so we can order uses of the same alias with different cv-qualification (79960). */ auto cso = make_temp_override (comparing_dependent_aliases); - if (!partial_order) + if (!comparing_for_partial_ordering) ++comparing_dependent_aliases; if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC) @@ -9393,8 +9404,7 @@ template_args_equal (tree ot, tree nt, bool partial_order /* = false */) bool comp_template_args (tree oldargs, tree newargs, - tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */, - bool partial_order /* = false */) + tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */) { if (oldargs == newargs) return true; @@ -9410,7 +9420,7 @@ comp_template_args (tree oldargs, tree newargs, tree nt = TREE_VEC_ELT (newargs, i); tree ot = TREE_VEC_ELT (oldargs, i); - if (! template_args_equal (ot, nt, partial_order)) + if (! template_args_equal (ot, nt)) { if (oldarg_ptr != NULL) *oldarg_ptr = ot; @@ -9422,10 +9432,13 @@ comp_template_args (tree oldargs, tree newargs, return true; } -inline bool +static bool comp_template_args_porder (tree oargs, tree nargs) { - return comp_template_args (oargs, nargs, NULL, NULL, true); + ++comparing_for_partial_ordering; + bool equal = comp_template_args (oargs, nargs); + --comparing_for_partial_ordering; + return equal; } /* Implement a freelist interface for objects of type T. @@ -28727,6 +28740,19 @@ any_template_arguments_need_structural_equality_p (tree args) mutated after the fact by duplicate_decls), so just require structural equality in this case (PR52830). */ return true; + else if (TYPE_P (arg) + && TYPE_STRUCTURAL_EQUALITY_P (arg) + && dependent_alias_template_spec_p (arg, nt_transparent)) + /* Require structural equality for specializations written + in terms of a dependent alias template specialization. */ + return true; + else if (CLASS_TYPE_P (arg) + && TYPE_TEMPLATE_INFO (arg) + && TYPE_STRUCTURAL_EQUALITY_P (arg)) + /* Require structural equality for specializations written + in terms of a class template specialization that itself + needs structural equality. */ + return true; } } } diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index d26e73aaf95..d17b9b34891 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -4131,7 +4131,7 @@ cp_tree_equal (tree t1, tree t2) case TREE_VEC: /* These are template args. Really we should be getting the caller to do this as it knows it to be true. */ - if (!comp_template_args (t1, t2, NULL, NULL, false)) + if (!comp_template_args (t1, t2)) return false; return true; diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C new file mode 100644 index 00000000000..8adbd6f65ac --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75a.C @@ -0,0 +1,26 @@ +// PR c++/90679 +// A version of alias-decl-75.C where the specializations of the +// complex alias template first_t are dependent. +// { 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" } diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C new file mode 100644 index 00000000000..b89ea5cad1e --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-75b.C @@ -0,0 +1,26 @@ +// PR c++/90679 +// A version of alias-decl-75a.C where the alias template specialization +// appears as a more deeply nested template argument. +// { 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" } -- 2.43.0.76.g1a87c842ec