public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r12-1437] libstdc++: Fix common_reference for non-reference results [PR100894]
Date: Mon, 14 Jun 2021 20:17:56 +0000 (GMT)	[thread overview]
Message-ID: <20210614201756.62B1B38515EC@sourceware.org> (raw)

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

commit r12-1437-gc37b5ddcc88e0cc0f6a4ad609eda51021df0f6bb
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Jun 14 20:31:00 2021 +0100

    libstdc++: Fix common_reference for non-reference results [PR100894]
    
    The result of COMMON-REF(A&, B&&) where they have no common reference
    type should not be a reference. The implementation of COMMON-REF fails
    to check that the result is a reference, so is well-formed when it
    shouldn't be. This means that common_reference uses that result when it
    shouldn't.
    
    The fix is to reject the result of COMMON-REF(A, B) if it's not a
    reference, so that common_reference falls through to the next case,
    which uses COND-RES, which yields a non-reference result.
    
    Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/100894
            * include/std/type_traits (__common_ref_impl<X&, Y&>): Only
            use the type if it's a reference.
            * testsuite/20_util/common_reference/100894.cc: New test.

Diff:
---
 libstdc++-v3/include/std/type_traits                      | 12 +++++++++---
 libstdc++-v3/testsuite/20_util/common_reference/100894.cc |  9 +++++++++
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index eaf06fcb036..d9068a06f08 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -3340,11 +3340,17 @@ template <typename _From, typename _To>
   template<typename _Ap, typename _Bp>
     using __common_ref = typename __common_ref_impl<_Ap, _Bp>::type;
 
+  // COND-RES(COPYCV(X, Y) &, COPYCV(Y, X) &)
+  template<typename _Xp, typename _Yp>
+    using __condres_cvref
+      = __cond_res<__copy_cv<_Xp, _Yp>&, __copy_cv<_Yp, _Xp>&>;
+
   // If A and B are both lvalue reference types, ...
   template<typename _Xp, typename _Yp>
-    struct __common_ref_impl<_Xp&, _Yp&,
-      __void_t<__cond_res<__copy_cv<_Xp, _Yp>&, __copy_cv<_Yp, _Xp>&>>>
-    { using type = __cond_res<__copy_cv<_Xp, _Yp>&, __copy_cv<_Yp, _Xp>&>; };
+    struct __common_ref_impl<_Xp&, _Yp&, __void_t<__condres_cvref<_Xp, _Yp>>>
+    : enable_if<is_reference_v<__condres_cvref<_Xp, _Yp>>,
+		__condres_cvref<_Xp, _Yp>>
+    { };
 
   // let C be remove_reference_t<COMMON-REF(X&, Y&)>&&
   template<typename _Xp, typename _Yp>
diff --git a/libstdc++-v3/testsuite/20_util/common_reference/100894.cc b/libstdc++-v3/testsuite/20_util/common_reference/100894.cc
new file mode 100644
index 00000000000..5e144768002
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/common_reference/100894.cc
@@ -0,0 +1,9 @@
+// { dg-options "-std=gnu++20" }
+// { dg-do compile { target c++20 } }
+// PR libstdc++/100894 - common_reference implementation seems to be wrong
+
+#include <type_traits>
+
+struct A {};
+struct B { B(A); };
+static_assert( std::is_same_v<std::common_reference_t<A&, B&&>, B> );


                 reply	other threads:[~2021-06-14 20:17 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=20210614201756.62B1B38515EC@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-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).