From patchwork Wed Dec 1 15:16:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 48370 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7B0DB385842A for ; Wed, 1 Dec 2021 15:29:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7B0DB385842A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1638372581; bh=XkGM9XOnxefWUr5j1JUGmnle3iwS1utXRrCTDQVX97c=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=YGqi372QtTrcHhe7wfHT0AKHZ1J0JlJ21v/VZf19cx9TZ6eItkvXbgIS2MNgXQ6/U RFw7zU3+9DM7LWmjPqoFmwxoUBL7Xz3DaWd4teQ9HPW7rkL/mV/et/IdKeGgYdgBau TNp3yCoT5jqbyQ3bccbyAup7ZMkMZjoESC1eIcuw= 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 9B6C0385BF83 for ; Wed, 1 Dec 2021 15:17:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9B6C0385BF83 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-444-U9SKUIZzNHCq69Y7nssFhw-1; Wed, 01 Dec 2021 10:17:05 -0500 X-MC-Unique: U9SKUIZzNHCq69Y7nssFhw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8A7381932482 for ; Wed, 1 Dec 2021 15:17:04 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.17.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id 35E266060F; Wed, 1 Dec 2021 15:17:04 +0000 (UTC) To: GCC Patches , Jason Merrill Subject: [PATCH] c++: Fix for decltype(auto) and parenthesized expr [PR103403] Date: Wed, 1 Dec 2021 10:16:59 -0500 Message-Id: <20211201151659.1497996-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_EU, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" In r11-4758, I tried to fix this problem: int &&i = 0; decltype(auto) j = i; // should behave like int &&j = i; error wherein do_auto_deduction was getting confused with a REFERENCE_REF_P and it didn't realize its operand was a name, not an expression, and deduced the wrong type. Unfortunately that fix broke this: int&& r = 1; decltype(auto) rr = (r); where 'rr' should be 'int &' since '(r)' is an expression, not a name. But because I stripped the INDIRECT_REF with the r11-4758 change, we deduced 'rr's type as if decltype had gotten a name, resulting in 'int &&'. I suspect I thought that the REF_PARENTHESIZED_P check when setting 'bool id' in do_auto_deduction would handle the (r) case, but that's not the case; while the documentation for REF_PARENTHESIZED_P specifically says it can be set in INDIRECT_REF, we don't actually do so. This patch sets REF_PARENTHESIZED_P even on REFERENCE_REF_P, so that do_auto_deduction can use it. It also removes code in maybe_undo_parenthesized_ref that I think is dead -- and we don't hit it while running dg.exp. To adduce more data, it also looks dead here: https://splichal.eu/lcov/gcc/cp/semantics.c.gcov.html Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk and 11? PR c++/103403 gcc/cp/ChangeLog: * cp-gimplify.c (cp_fold): Don't recurse if maybe_undo_parenthesized_ref doesn't change its argument. * pt.c (do_auto_deduction): Don't strip REFERENCE_REF_P trees. Also REF_PARENTHESIZED_P for REFERENCE_REF_P. * semantics.c (force_paren_expr): Set REF_PARENTHESIZED_P on REFERENCE_REF_P trees too. (maybe_undo_parenthesized_ref): Remove dead code. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/decltype-auto2.C: New test. * g++.dg/cpp1y/decltype-auto3.C: New test. --- gcc/cp/cp-gimplify.c | 3 ++- gcc/cp/pt.c | 5 ++--- gcc/cp/semantics.c | 18 ++++-------------- gcc/testsuite/g++.dg/cpp1y/decltype-auto2.C | 12 ++++++++++++ gcc/testsuite/g++.dg/cpp1y/decltype-auto3.C | 12 ++++++++++++ 5 files changed, 32 insertions(+), 18 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/decltype-auto2.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/decltype-auto3.C base-commit: 1e625a44f6f3001cea31e0f7c563943ecba92b68 diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c index 0a002db14e7..e3ede02a48e 100644 --- a/gcc/cp/cp-gimplify.c +++ b/gcc/cp/cp-gimplify.c @@ -2421,7 +2421,8 @@ cp_fold (tree x) if (REF_PARENTHESIZED_P (x)) { tree p = maybe_undo_parenthesized_ref (x); - return cp_fold (p); + if (p != x) + return cp_fold (p); } goto unary; diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index f4b9d9673fb..c5b41b57028 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -29889,11 +29889,10 @@ do_auto_deduction (tree type, tree init, tree auto_node, else if (AUTO_IS_DECLTYPE (auto_node)) { tree stripped_init = tree_strip_any_location_wrapper (init); - if (REFERENCE_REF_P (stripped_init)) - stripped_init = TREE_OPERAND (stripped_init, 0); bool id = (DECL_P (stripped_init) || ((TREE_CODE (init) == COMPONENT_REF - || TREE_CODE (init) == SCOPE_REF) + || TREE_CODE (init) == SCOPE_REF + || REFERENCE_REF_P (init)) && !REF_PARENTHESIZED_P (init))); tree deduced = finish_decltype_type (init, id, complain); deduced = canonicalize_type_argument (deduced, complain); diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index cd1956497f8..edba4b60e10 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -2049,7 +2049,8 @@ force_paren_expr (tree expr, bool even_uneval) return expr; if (TREE_CODE (expr) == COMPONENT_REF - || TREE_CODE (expr) == SCOPE_REF) + || TREE_CODE (expr) == SCOPE_REF + || REFERENCE_REF_P (expr)) REF_PARENTHESIZED_P (expr) = true; else if (DECL_P (tree_strip_any_location_wrapper (expr))) { @@ -2072,19 +2073,8 @@ maybe_undo_parenthesized_ref (tree t) if (cxx_dialect < cxx14) return t; - if (INDIRECT_REF_P (t) && REF_PARENTHESIZED_P (t)) - { - t = TREE_OPERAND (t, 0); - while (TREE_CODE (t) == NON_LVALUE_EXPR - || TREE_CODE (t) == NOP_EXPR) - t = TREE_OPERAND (t, 0); - - gcc_assert (TREE_CODE (t) == ADDR_EXPR - || TREE_CODE (t) == STATIC_CAST_EXPR); - t = TREE_OPERAND (t, 0); - } - else if ((TREE_CODE (t) == PAREN_EXPR || TREE_CODE (t) == VIEW_CONVERT_EXPR) - && REF_PARENTHESIZED_P (t)) + if ((TREE_CODE (t) == PAREN_EXPR || TREE_CODE (t) == VIEW_CONVERT_EXPR) + && REF_PARENTHESIZED_P (t)) t = TREE_OPERAND (t, 0); return t; diff --git a/gcc/testsuite/g++.dg/cpp1y/decltype-auto2.C b/gcc/testsuite/g++.dg/cpp1y/decltype-auto2.C new file mode 100644 index 00000000000..56e011e36f4 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/decltype-auto2.C @@ -0,0 +1,12 @@ +// PR c++/103403 +// { dg-do compile { target c++14 } } + +template +auto constexpr RtoL1(T&& r) -> decltype(auto) { + return (r); +}; +int main() { + int t; + int x{3}; + decltype (RtoL1(x+0)) y = t; +} diff --git a/gcc/testsuite/g++.dg/cpp1y/decltype-auto3.C b/gcc/testsuite/g++.dg/cpp1y/decltype-auto3.C new file mode 100644 index 00000000000..914e87f5b79 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/decltype-auto3.C @@ -0,0 +1,12 @@ +// PR c++/103403 +// { dg-do compile { target c++14 } } + +int main() +{ + int i = 1; + int&& r = 1; + + decltype(auto) ri = (i); + decltype(auto) rr = (r); + decltype((r)) rr2 = (r); +}