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 [63.128.21.124]) by sourceware.org (Postfix) with ESMTP id 13CC6385ED4A for ; Thu, 25 Feb 2021 22:09:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 13CC6385ED4A Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-280-Kp2I8me9PhOLbQwknBcxgA-1; Thu, 25 Feb 2021 17:09:14 -0500 X-MC-Unique: Kp2I8me9PhOLbQwknBcxgA-1 Received: by mail-qk1-f199.google.com with SMTP id y79so5623820qka.23 for ; Thu, 25 Feb 2021 14:09:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:in-reply-to:message-id :references:mime-version; bh=Gc2RCOO9nbyD0fwx8Lbcc2MtrwA5s6vxUru8OwT+kiU=; b=Tf3i1knXl7S1ILQDMUGMahD5LFnQy9simBZjOJhUs4KnlpgKaBlUuAMTT1qshsM+e+ dCbrPsGa3sYsC4mQsURyQQmDUkJxYO1JUPPkKFIuWAM9BYXZahS9QwP4C6BivXL3O2un 0Du6O9TCzHV25bGO6CJrW6GAUq+xL9avChBshy8wD0NK8xcP31TyQ1hiHPDu1RolEYuk MR+C5ve7G/Ovs///GLK0ml7LxtNWKcPKnJKjRbhfxCQCgqqPp58gEPXRLMTRS/u0C83y qT1f9vGLM9W2wR8OKog4H9QcSZL3gz/Xl2bumEGNDeiG9IJg+fsFNeo4XiTsJKLg40oH ZKNw== X-Gm-Message-State: AOAM531I5hQSVFJjAUILOm3vFWs+d1EhEF/wPQi0KijvnvAm1F0XV4tW azACawh+b6afPYccpe/wUYaITvtP89peB7fHLpTwTOR6XlPq/S7lAaqf04Ovv81DmhOQWaYXODi z7tOmlPAnZBCWum4KKw== X-Received: by 2002:a37:b1c2:: with SMTP id a185mr4898280qkf.95.1614290953582; Thu, 25 Feb 2021 14:09:13 -0800 (PST) X-Google-Smtp-Source: ABdhPJynotjoYjkeR4xRwKbKrwjNhymLgMCRjvCc+iaGoInCnxTKQLHwKDiEZ2H0ie2WqxyZ2rtSog== X-Received: by 2002:a37:b1c2:: with SMTP id a185mr4898259qkf.95.1614290953243; Thu, 25 Feb 2021 14:09:13 -0800 (PST) Received: from [192.168.1.130] (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id p6sm4905663qkg.36.2021.02.25.14.09.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Feb 2021 14:09:12 -0800 (PST) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Thu, 25 Feb 2021 17:09:11 -0500 (EST) To: Patrick Palka cc: gcc-patches@gcc.gnu.org, jason@redhat.com Subject: Re: [PATCH] c++: abbreviated function template return type rewriting [PR98990] In-Reply-To: <20210209183144.2164107-1-ppalka@redhat.com> Message-ID: <82ec5f6e-612a-e996-ee8c-aba3396446a1@idea> References: <20210209183144.2164107-1-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=-16.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-List-Received-Date: Thu, 25 Feb 2021 22:09:19 -0000 On Tue, 9 Feb 2021, Patrick Palka wrote: > When an abbreviated function template has a complex placeholder return > type such auto& or auto**, the level adjustment performed by > splice_late_return_type directly replaces the 'auto' inside the original > return type with the level-adjusted 'auto', but that breaks > TYPE_CANONICAL caching. Instead, we should rebuild the entire return > type using the adjusted 'auto'. > > This patch makes this happen by tsubsting the original return type with > an argument vector that maps the original 'auto' to the adjusted 'auto'. > In passing, this patch also reverts the misguided changes to > find_type_usage in r10-6571 that made find_type_usage return a tree* > instead of a tree so as to discourage this kind of in-place type > modification. > > It occurred to me that the constraint also needs to be rebuilt so that > it refers to the adjusted 'auto', but this oversight doesn't seem to > cause any issues at the moment due to how do_auto_deduction "manually" > substitutes the 'auto' inside the constraint before performing > satisfaction. So this will instead be fixed as part of a broader rework > of placeholder type constraint checking. > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > trunk/10? Ping. Note that this patch will cause a merge conflict with the uncommitted patch at https://gcc.gnu.org/pipermail/gcc-patches/2021-February/565205.html, so I plan to address the FIXME below as part of a new version of that patch. > > gcc/cp/ChangeLog: > > PR c++/98990 > * pt.c (splice_late_return_type): Rebuild the entire return type > if we have to adjust the level of the auto. Use > TEMPLATE_TYPE_LEVEL for brevity. > (type_uses_auto): Adjust call to find_type_usage. > * type-utils.h (find_type_usage): Revert r10-6571 change that > made this function return a pointer to the auto node. > > gcc/testsuite/ChangeLog: > > PR c++/98990 > * g++.dg/concepts/abbrev8.C: New test. > --- > gcc/cp/pt.c | 39 +++++++++++++------------ > gcc/cp/type-utils.h | 23 +++++++-------- > gcc/testsuite/g++.dg/concepts/abbrev8.C | 22 ++++++++++++++ > 3 files changed, 53 insertions(+), 31 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/concepts/abbrev8.C > > diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c > index c5b0a9292db..46cd322fbf4 100644 > --- a/gcc/cp/pt.c > +++ b/gcc/cp/pt.c > @@ -29601,22 +29601,25 @@ splice_late_return_type (tree type, tree late_return_type) > return late_return_type; > } > > - if (tree *auto_node = find_type_usage (&type, is_auto)) > - { > - tree idx = get_template_parm_index (*auto_node); > - if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl) > - { > - /* In an abbreviated function template we didn't know we were dealing > - with a function template when we saw the auto return type, so update > - it to have the correct level. */ > - tree new_auto = make_auto_1 (TYPE_IDENTIFIER (*auto_node), false); > - PLACEHOLDER_TYPE_CONSTRAINTS (new_auto) > - = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node); > - TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto); > - new_auto = cp_build_qualified_type (new_auto, TYPE_QUALS (*auto_node)); > - *auto_node = new_auto; > - } > - } > + if (tree auto_node = find_type_usage (type, is_auto)) > + if (TEMPLATE_TYPE_LEVEL (auto_node) <= processing_template_decl) > + { > + /* In an abbreviated function template we didn't know we were dealing > + with a function template when we saw the auto return type, so rebuild > + the return type using an auto with the correct level. */ > + tree new_auto = make_auto_1 (TYPE_IDENTIFIER (auto_node), false); > + tree auto_vec = make_tree_vec (1); > + TREE_VEC_ELT (auto_vec, 0) = new_auto; > + tree targs = add_outermost_template_args (current_template_args (), > + auto_vec); > + /* FIXME: We should also rebuild the constraint to refer to the new > + auto. */ > + PLACEHOLDER_TYPE_CONSTRAINTS (new_auto) > + = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node); > + TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto); > + new_auto = cp_build_qualified_type (new_auto, TYPE_QUALS (auto_node)); > + return tsubst (type, targs, tf_none, NULL_TREE); > + } > return type; > } > > @@ -29661,10 +29664,8 @@ type_uses_auto (tree type) > else > return NULL_TREE; > } > - else if (tree *tp = find_type_usage (&type, is_auto)) > - return *tp; > else > - return NULL_TREE; > + return find_type_usage (type, is_auto); > } > > /* Report ill-formed occurrences of auto types in ARGUMENTS. If > diff --git a/gcc/cp/type-utils.h b/gcc/cp/type-utils.h > index 5551e8f5ef1..138fed6c51e 100644 > --- a/gcc/cp/type-utils.h > +++ b/gcc/cp/type-utils.h > @@ -20,22 +20,21 @@ along with GCC; see the file COPYING3. If not see > #ifndef GCC_CP_TYPE_UTILS_H > #define GCC_CP_TYPE_UTILS_H > > -/* Returns a pointer to the first tree within *TP that is directly matched by > - PRED. *TP may be a type or PARM_DECL and is incrementally decomposed toward > - its type-specifier until a match is found. NULL is returned if PRED does not > - match any part of *TP. > +/* Returns the first tree within T that is directly matched by PRED. T may be a > + type or PARM_DECL and is incrementally decomposed toward its type-specifier > + until a match is found. NULL is returned if PRED does not match any > + part of T. > > - This is primarily intended for detecting whether *TP uses `auto' or a concept > + This is primarily intended for detecting whether T uses `auto' or a concept > identifier. Since either of these can only appear as a type-specifier for > the declaration in question, only top-level qualifications are traversed; > find_type_usage does not look through the whole type. */ > > -inline tree * > -find_type_usage (tree *tp, bool (*pred) (const_tree)) > +inline tree > +find_type_usage (tree t, bool (*pred) (const_tree)) > { > - tree t = *tp; > if (pred (t)) > - return tp; > + return t; > > enum tree_code code = TREE_CODE (t); > > @@ -43,13 +42,13 @@ find_type_usage (tree *tp, bool (*pred) (const_tree)) > || code == PARM_DECL || code == OFFSET_TYPE > || code == FUNCTION_TYPE || code == METHOD_TYPE > || code == ARRAY_TYPE) > - return find_type_usage (&TREE_TYPE (t), pred); > + return find_type_usage (TREE_TYPE (t), pred); > > if (TYPE_PTRMEMFUNC_P (t)) > return find_type_usage > - (&TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (t)), pred); > + (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (t)), pred); > > - return NULL; > + return NULL_TREE; > } > > #endif // GCC_CP_TYPE_UTILS_H > diff --git a/gcc/testsuite/g++.dg/concepts/abbrev8.C b/gcc/testsuite/g++.dg/concepts/abbrev8.C > new file mode 100644 > index 00000000000..ece9b0e44fd > --- /dev/null > +++ b/gcc/testsuite/g++.dg/concepts/abbrev8.C > @@ -0,0 +1,22 @@ > +// PR c++/98990 > +// { dg-do compile { target concepts } } > + > +int x; > + > +auto& f() { return x; } > +auto& f(auto) { return x; } > + > +using T1 = int&; > +using T1 = decltype(f('a')); > + > +int* y; > + > +template > +struct S > +{ > + static auto** f() { return &y; } > + static auto** f(auto) { return &y; } > +}; > + > +using T2 = int**; > +using T2 = decltype(S::f('a')); > -- > 2.30.0.452.gfb7fa4a1fd > >