From abd7712f91c99690f8b0046ea168b2782afbac69 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Tue, 30 Nov 2021 21:07:25 -0500 Subject: c++: Fix for decltype(auto) and parenthesized expr [PR103403] 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 (It's dead since r9-1417.) Also add a fixed test for c++/81176. 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 if they are REF_PARENTHESIZED_P. Use stripped_init when checking for id-expression. * 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. * g++.dg/cpp1y/decltype-auto4.C: New test. * g++.dg/cpp1z/decomp-decltype1.C: New test. --- gcc/cp/cp-gimplify.c | 3 ++- gcc/cp/pt.c | 15 ++++++++++----- gcc/cp/semantics.c | 18 ++++-------------- 3 files changed, 16 insertions(+), 20 deletions(-) (limited to 'gcc/cp') diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c index 0a002db..e3ede02 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 6b56095..d3efc6e 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -29889,13 +29889,18 @@ do_auto_deduction (tree type, tree init, tree auto_node, } else if (AUTO_IS_DECLTYPE (auto_node)) { + /* Figure out if INIT is an unparenthesized id-expression or an + unparenthesized class member access. */ tree stripped_init = tree_strip_any_location_wrapper (init); - if (REFERENCE_REF_P (stripped_init)) + /* We need to be able to tell '(r)' and 'r' apart (when it's of + reference type). Only the latter is an id-expression. */ + if (REFERENCE_REF_P (stripped_init) + && !REF_PARENTHESIZED_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) - && !REF_PARENTHESIZED_P (init))); + const bool id = (DECL_P (stripped_init) + || ((TREE_CODE (stripped_init) == COMPONENT_REF + || TREE_CODE (stripped_init) == SCOPE_REF) + && !REF_PARENTHESIZED_P (stripped_init))); tree deduced = finish_decltype_type (init, id, complain); deduced = canonicalize_type_argument (deduced, complain); if (deduced == error_mark_node) diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index cd19564..edba4b6 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; -- cgit v1.1