From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 98131 invoked by alias); 10 Feb 2020 13:20:42 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 98120 invoked by uid 89); 10 Feb 2020 13:20:41 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-24.0 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.1 spammy=subsequently, H*RU:sk:mail-qk, HX-Received:ac8, HX-Spam-Relays-External:sk:mail-qk X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (205.139.110.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 10 Feb 2020 13:20:39 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581340838; 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: content-transfer-encoding:content-transfer-encoding; bh=mpDQfflaXgOlng+n+CLlO5YTwbHqzLYG6iAsMUNltfo=; b=gJif/VinQATSEWqaXgp/sMHZHSXoYtz3YOA7A/bwfIdphAq5GxZxVvTcWqWIk0uQJQlGFd BeNjyKmIF7hRItgPxaloKu07H0pDWe3n33TWt35tuqjC3YyjEBBhVfQNo3Pvnw/qmmDucZ n4uRCo001qh60s3yrzuC8M5Jlg71dgg= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-56-eEXoPMgBMaOizoL4foZATw-1; Mon, 10 Feb 2020 08:20:35 -0500 Received: by mail-qk1-f200.google.com with SMTP id q2so4713970qkq.19 for ; Mon, 10 Feb 2020 05:20:35 -0800 (PST) Return-Path: Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id f59sm152844qtb.75.2020.02.10.05.20.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2020 05:20:34 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: Fix return type deduction with an abbreviated function template Date: Mon, 10 Feb 2020 13:20:00 -0000 Message-Id: <20200210132032.586931-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 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes X-SW-Source: 2020-02/txt/msg00533.txt.bz2 This patch fixes two issues with return type deduction in the presence of an abbreviated function template. The first issue (PR 69448) is that if a placeholder auto return type contai= ns any modifiers such as & or *, then the abbreviated-function-template compensation in splice_late_return_type does not get performed for the underlying auto node, leading to incorrect return type deduction. This hap= pens because splice_late_return_type checks for a placeholder auto with is_auto, which does not look through modifiers. This patch replaces the use of is_a= uto with find_type_usage, but it first refactors find_type_usage to return a po= inter to the matched tree so that we next subsequently replace it. The second issue (PR 80471) is that the AUTO_IS_DECLTYPE flag is not being preserved in splice_late_return_type when compensating for an abbreviated function template, leading to us treating a decltype(auto) return type as i= f it was an auto return type. Fixed by propagating AUTO_IS_DECLTYPE. The test = for PR 80471 is adjusted to expect the correct behavior. Bootstrapped and regtested on x86_64-pc-linux-gnu, is this OK to commit? gcc/cp/ChangeLog: PR c++/69448 PR c++/80471 * type-utils.h (find_type_usage): Refactor to take a tree * and to return a tree *, and update documentation accordingly. * pt.c (splice_late_return_type): Use find_type_usage to find and replace a possibly nested auto node. Preserve the AUTO_IS_DECLTYPE flag when replacing the node. (type_uses_auto): Adjust the call to find_type_usage. gcc/testsuite/ChangeLog: PR c++/69448 PR c++/80471 * g++.dg/concepts/abbrev3.C: New test. * g++.dg/cpp2a/concepts-pr80471.C: Adjust a static_assert to expect the correct behavior. --- gcc/cp/pt.c | 26 ++++++++++++------- gcc/cp/type-utils.h | 26 +++++++++---------- gcc/testsuite/g++.dg/concepts/abbrev3.C | 11 ++++++++ gcc/testsuite/g++.dg/cpp2a/concepts-pr80471.C | 2 +- 4 files changed, 41 insertions(+), 24 deletions(-) create mode 100644 gcc/testsuite/g++.dg/concepts/abbrev3.C diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 2fb52caa5d4..c83aaa7c7e1 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -28904,17 +28904,21 @@ do_auto_deduction (tree type, tree init, tree aut= o_node, tree splice_late_return_type (tree type, tree late_return_type) { - if (is_auto (type)) - { - if (late_return_type) - return late_return_type; + if (is_auto (type) && late_return_type) + return late_return_type; =20 - tree idx =3D get_template_parm_index (type); + if (tree *auto_node =3D find_type_usage (&type, is_auto)) + { + tree idx =3D get_template_parm_index (*auto_node); if (TEMPLATE_PARM_LEVEL (idx) <=3D 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. */ - return make_auto_1 (TYPE_IDENTIFIER (type), true); + { + /* 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_node =3D make_auto_1 (TYPE_IDENTIFIER (*auto_node), true); + AUTO_IS_DECLTYPE (new_node) =3D AUTO_IS_DECLTYPE (*auto_node); + *auto_node =3D new_node; + } } return type; } @@ -28960,8 +28964,10 @@ type_uses_auto (tree type) else return NULL_TREE; } + else if (tree *tp =3D find_type_usage (&type, is_auto)) + return *tp; else - return find_type_usage (type, is_auto); + return NULL_TREE; } =20 /* 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 680b2497a36..4ad0d822119 100644 --- a/gcc/cp/type-utils.h +++ b/gcc/cp/type-utils.h @@ -20,36 +20,36 @@ along with GCC; see the file COPYING3. If not see #ifndef GCC_CP_TYPE_UTILS_H #define GCC_CP_TYPE_UTILS_H =20 -/* Returns the first tree within T that is directly matched by PRED. T ma= y be a - type or PARM_DECL and is incrementally decomposed toward its type-speci= fier - until a match is found. NULL_TREE is returned if PRED does not match a= ny - part of T. +/* 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 t= oward + its type-specifier until a match is found. NULL is returned if PRED do= es not + match any part of *TP. =20 - This is primarily intended for detecting whether T uses `auto' or a con= cept + This is primarily intended for detecting whether *TP uses `auto' or a c= oncept identifier. Since either of these can only appear as a type-specifier = for the declaration in question, only top-level qualifications are traverse= d; find_type_usage does not look through the whole type. */ =20 -inline tree -find_type_usage (tree t, bool (*pred) (const_tree)) +inline tree * +find_type_usage (tree *tp, bool (*pred) (const_tree)) { - enum tree_code code; + tree t =3D *tp; if (pred (t)) - return t; + return tp; =20 - code =3D TREE_CODE (t); + enum tree_code code =3D TREE_CODE (t); =20 if (code =3D=3D POINTER_TYPE || code =3D=3D REFERENCE_TYPE || code =3D=3D PARM_DECL || code =3D=3D OFFSET_TYPE || code =3D=3D FUNCTION_TYPE || code =3D=3D METHOD_TYPE || code =3D=3D ARRAY_TYPE) - return find_type_usage (TREE_TYPE (t), pred); + return find_type_usage (&TREE_TYPE (t), pred); =20 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); =20 - return NULL_TREE; + return NULL; } =20 #endif // GCC_CP_TYPE_UTILS_H diff --git a/gcc/testsuite/g++.dg/concepts/abbrev3.C b/gcc/testsuite/g++.dg= /concepts/abbrev3.C new file mode 100644 index 00000000000..ba2a648156e --- /dev/null +++ b/gcc/testsuite/g++.dg/concepts/abbrev3.C @@ -0,0 +1,11 @@ +// PR c++/69448 +// { dg-do compile { target c++14 } } +// { dg-additional-options "-fconcepts" } + +long x; + +auto& f(auto) { return x; } +auto* g(auto) { return &x; } + +long& r =3D f(1); +long* p =3D g(1); diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-pr80471.C b/gcc/testsuite/= g++.dg/cpp2a/concepts-pr80471.C index d5fa5a536d3..6ea6164b417 100644 --- a/gcc/testsuite/g++.dg/cpp2a/concepts-pr80471.C +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-pr80471.C @@ -18,6 +18,6 @@ int main() { int i; static_assert(is_same< decltype(f(i)), int& >, ""); - static_assert(is_same< decltype(g(i)), int >, ""); + static_assert(is_same< decltype(g(i)), int& >, ""); static_assert(is_same< decltype(z(i)), int& >, ""); } --=20 2.25.0.191.gde93cc14ab