[gcc r13-10474] c++: fix propagating REF_PARENTHESIZED_P [PR116379]

Marek Polacek via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:385482a2d70c13f2741f0fede95de93eefd63f1d

commit r13-10474-g385482a2d70c13f2741f0fede95de93eefd63f1d
Author: Marek Polacek <[email protected]>
Date:   Thu Aug 20 14:59:46 2026 -0400

    c++: fix propagating REF_PARENTHESIZED_P [PR116379]
    
    Here we have:
    
      template<typename T>
      struct X{
          T val;
          decltype(auto) value(){
              return (val);
          }
      };
    
    where the return type of value should be 'int &' since '(val)' is an
    expression, not a name, and decltype(auto) performs the type deduction
    using the decltype rules.
    
    The problem is that we weren't propagating REF_PARENTHESIZED_P
    correctly: the return value of finish_non_static_data_member in this
    test was a REFERENCE_REF_P, so we didn't set the flag.  We should
    use force_paren_expr like below.
    
            PR c++/116379
    
    gcc/cp/ChangeLog:
    
            * pt.cc (tsubst_expr) <COMPONENT_REF>: Use force_paren_expr to set
            REF_PARENTHESIZED_P.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp1y/decltype-auto9.C: New test.
    
    Reviewed-by: Jason Merrill <[email protected]>

Diff:
---
 gcc/cp/pt.cc                                |  4 ++--
 gcc/testsuite/g++.dg/cpp1y/decltype-auto9.C | 15 +++++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 9159c043cdc6..b01284ac12dd 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -21538,8 +21538,8 @@ tsubst_copy_and_build (tree t,
 	    r = finish_non_static_data_member (member, object, NULL_TREE,
 					       complain);
 	    pop_deferring_access_checks ();
-	    if (TREE_CODE (r) == COMPONENT_REF)
-	      REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
+	    if (REF_PARENTHESIZED_P (t))
+	      r = force_paren_expr (r);
 	    RETURN (r);
 	  }
 	else if (type_dependent_expression_p (object))
diff --git a/gcc/testsuite/g++.dg/cpp1y/decltype-auto9.C b/gcc/testsuite/g++.dg/cpp1y/decltype-auto9.C
new file mode 100644
index 000000000000..1ccf95a01702
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/decltype-auto9.C
@@ -0,0 +1,15 @@
+// PR c++/116379
+// { dg-do compile { target c++14 } }
+
+template<typename T>
+struct X {
+  T val;
+  decltype(auto) value() { return (val); }
+};
+
+int main() {
+  int i = 0;
+  X<int&&> x{ static_cast<int&&>(i) };
+  using type = decltype(x.value());
+  using type = int&;
+}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.