[libc++] Fix QoI bug with construction of std::tuple involving std::any
In std::tuple, we should try to avoid calling std::is_copy_constructible
whenever we can to avoid surprising interactions with (I believe) compiler
builtins. This bug was reported in https://reviews.llvm.org/D96523#2730953.
The issue was that when tuple<_Up...> was the same as tuple<_Tp...>, we
would short-circuit the _Or (because sizeof...(_Tp) != 1) and go evaluate
the following `is_constructible<_Tp, const _Up&>...`. That shouldn't
actually be a problem, but see the analysis in https://reviews.llvm.org/D101770#2736470
for why it is with Clang and GCC.
Instead, after this patch, we check whether the constructed-from tuple
is the same as the current tuple regardless of the number of elements,
since we should always prefer the normal copy constructor in that case
anyway.
Differential Revision: https://reviews.llvm.org/D101770
NOKEYCHECK=True
GitOrigin-RevId: 17f2d1cb9b93d336d4187cd14307bef1ab535808
diff --git a/include/tuple b/include/tuple
index fa0b6a0..0a131f3 100644
--- a/include/tuple
+++ b/include/tuple
@@ -675,12 +675,12 @@
// tuple(const tuple<U...>&) constructors (including allocator_arg_t variants)
template <class ..._Up>
struct _EnableCopyFromOtherTuple : _And<
- _Or<
+ _Not<is_same<tuple<_Tp...>, tuple<_Up...> > >,
+ _Lazy<_Or,
_BoolConstant<sizeof...(_Tp) != 1>,
// _Tp and _Up are 1-element packs - the pack expansions look
// weird to avoid tripping up the type traits in degenerate cases
_Lazy<_And,
- _Not<is_same<_Tp, _Up> >...,
_Not<is_convertible<const tuple<_Up>&, _Tp> >...,
_Not<is_constructible<_Tp, const tuple<_Up>&> >...
>
@@ -741,12 +741,12 @@
// tuple(tuple<U...>&&) constructors (including allocator_arg_t variants)
template <class ..._Up>
struct _EnableMoveFromOtherTuple : _And<
- _Or<
+ _Not<is_same<tuple<_Tp...>, tuple<_Up...> > >,
+ _Lazy<_Or,
_BoolConstant<sizeof...(_Tp) != 1>,
// _Tp and _Up are 1-element packs - the pack expansions look
// weird to avoid tripping up the type traits in degenerate cases
_Lazy<_And,
- _Not<is_same<_Tp, _Up> >...,
_Not<is_convertible<tuple<_Up>, _Tp> >...,
_Not<is_constructible<_Tp, tuple<_Up> > >...
>