public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Marek Polacek <mpolacek@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r11-4758] c++: Fix decltype(auto) deduction with rvalue ref [PR78209]
Date: Thu,  5 Nov 2020 21:34:44 +0000 (GMT)	[thread overview]
Message-ID: <20201105213444.D37133857C75@sourceware.org> (raw)

https://gcc.gnu.org/g:e6fd02cc6d874c523466250a1cb724e0c7af9d75

commit r11-4758-ge6fd02cc6d874c523466250a1cb724e0c7af9d75
Author: Marek Polacek <polacek@redhat.com>
Date:   Tue Nov 3 15:10:31 2020 -0500

    c++: Fix decltype(auto) deduction with rvalue ref [PR78209]
    
    Here's a small deficiency in decltype(auto).  [dcl.type.auto.deduct]/5:
    If the placeholder-type-specifier is of the form decltype(auto), [...]
    the type deduced for T is determined [...] as though E had been the operand
    of the decltype.  So:
    
      int &&i = 0;
      decltype(auto) j = i; // should behave like int &&j = i; error
    
    We deduce j's type in do_auto_deduction via finish_decltype_type which
    takes an 'id' argument.  Currently we compute 'id' as false, because
    stripped_init is *i (a REFERENCE_REF_P).  But it seems to me we should
    rather set 'id' to true here, by looking through the REFERENCE_REF_P,
    so that finish_decltype_type DTRT.
    
    gcc/cp/ChangeLog:
    
            PR c++/78209
            * pt.c (do_auto_deduction): If init is REFERENCE_REF_P, use its
            first operand.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/78209
            * g++.dg/cpp1y/decltype-auto1.C: New test.

Diff:
---
 gcc/cp/pt.c                                 | 2 ++
 gcc/testsuite/g++.dg/cpp1y/decltype-auto1.C | 8 ++++++++
 2 files changed, 10 insertions(+)

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index f401c75b9e5..c033a286407 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -29278,6 +29278,8 @@ 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)
diff --git a/gcc/testsuite/g++.dg/cpp1y/decltype-auto1.C b/gcc/testsuite/g++.dg/cpp1y/decltype-auto1.C
new file mode 100644
index 00000000000..13baf8eba06
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/decltype-auto1.C
@@ -0,0 +1,8 @@
+// PR c++/78209
+// { dg-do compile { target c++14 } }
+
+int main()
+{
+  int &&i = 0;
+  decltype(auto) j = i; // { dg-error "cannot bind rvalue reference" }
+}


                 reply	other threads:[~2020-11-05 21:34 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201105213444.D37133857C75@sourceware.org \
    --to=mpolacek@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).