Index: include/tuple =================================================================== --- include/tuple +++ include/tuple @@ -173,16 +173,9 @@ template static constexpr bool __can_bind_reference() { - using _RawTp = typename remove_reference<_Tp>::type; - using _RawHp = typename remove_reference<_Hp>::type; - using _CheckLValueArg = integral_constant::value - || is_same<_RawTp, reference_wrapper<_RawHp>>::value - || is_same<_RawTp, reference_wrapper::type>>::value - >; - return !is_reference<_Hp>::value - || (is_lvalue_reference<_Hp>::value && _CheckLValueArg::value) - || (is_rvalue_reference<_Hp>::value && !is_lvalue_reference<_Tp>::value); +#if __has_keyword(__reference_binds_to_temporary) + return !__reference_binds_to_temporary(_Hp, _Tp); +#endif } __tuple_leaf& operator=(const __tuple_leaf&); @@ -224,15 +217,15 @@ _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 explicit __tuple_leaf(_Tp&& __t) _NOEXCEPT_((is_nothrow_constructible<_Hp, _Tp>::value)) : __value_(_VSTD::forward<_Tp>(__t)) - {static_assert(__can_bind_reference<_Tp>(), - "Attempted to construct a reference element in a tuple with an rvalue");} + {static_assert(__can_bind_reference<_Tp&&>(), + "Attempted construction of reference element binds to a temporary whose lifetime has ended");} template _LIBCPP_INLINE_VISIBILITY explicit __tuple_leaf(integral_constant, const _Alloc&, _Tp&& __t) : __value_(_VSTD::forward<_Tp>(__t)) - {static_assert(__can_bind_reference<_Tp>(), - "Attempted to construct a reference element in a tuple with an rvalue");} + {static_assert(__can_bind_reference<_Tp&&>(), + "Attempted construction of reference element binds to a temporary whose lifetime has ended");} template _LIBCPP_INLINE_VISIBILITY Index: test/libcxx/utilities/tuple/tuple.tuple/diagnose_reference_binding.fail.cpp =================================================================== --- test/libcxx/utilities/tuple/tuple.tuple/diagnose_reference_binding.fail.cpp +++ test/libcxx/utilities/tuple/tuple.tuple/diagnose_reference_binding.fail.cpp @@ -1,40 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is dual licensed under the MIT and the University of Illinois Open -// Source Licenses. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -// UNSUPPORTED: c++98, c++03 - -// - -// Test the diagnostics libc++ generates for invalid reference binding. -// Libc++ attempts to diagnose the following cases: -// * Constructing an lvalue reference from an rvalue. -// * Constructing an rvalue reference from an lvalue. - -#include -#include - -int main() { - std::allocator alloc; - - // expected-error-re@tuple:* 4 {{static_assert failed{{.*}} "Attempted to construct a reference element in a tuple with an rvalue"}} - - // bind lvalue to rvalue - std::tuple t(42); // expected-note {{requested here}} - std::tuple t1(std::allocator_arg, alloc, 42); // expected-note {{requested here}} - // bind rvalue to constructed non-rvalue - std::tuple t2("hello"); // expected-note {{requested here}} - std::tuple t3(std::allocator_arg, alloc, "hello"); // expected-note {{requested here}} - - // FIXME: The below warnings may get emitted as an error, a warning, or not emitted at all - // depending on the flags used to compile this test. - { - // expected-warning@tuple:* 0+ {{binding reference member '__value_' to a temporary value}} - // expected-error@tuple:* 0+ {{binding reference member '__value_' to a temporary value}} - } -} Index: test/libcxx/utilities/tuple/tuple.tuple/diagnose_reference_binding.pass.cpp =================================================================== --- test/libcxx/utilities/tuple/tuple.tuple/diagnose_reference_binding.pass.cpp +++ test/libcxx/utilities/tuple/tuple.tuple/diagnose_reference_binding.pass.cpp @@ -1,71 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is dual licensed under the MIT and the University of Illinois Open -// Source Licenses. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -// UNSUPPORTED: c++98, c++03 - -// - -// Test the diagnostics libc++ generates for invalid reference binding. -// Libc++ attempts to diagnose the following cases: -// * Constructing an lvalue reference from an rvalue. -// * Constructing an rvalue reference from an lvalue. - -#include -#include -#include -#include - -static_assert(std::is_constructible>::value, ""); -static_assert(std::is_constructible>::value, ""); - - -int main() { - std::allocator alloc; - int x = 42; - { - std::tuple t(std::ref(x)); - assert(&std::get<0>(t) == &x); - std::tuple t1(std::allocator_arg, alloc, std::ref(x)); - assert(&std::get<0>(t1) == &x); - } - { - auto r = std::ref(x); - auto const& cr = r; - std::tuple t(r); - assert(&std::get<0>(t) == &x); - std::tuple t1(cr); - assert(&std::get<0>(t1) == &x); - std::tuple t2(std::allocator_arg, alloc, r); - assert(&std::get<0>(t2) == &x); - std::tuple t3(std::allocator_arg, alloc, cr); - assert(&std::get<0>(t3) == &x); - } - { - std::tuple t(std::ref(x)); - assert(&std::get<0>(t) == &x); - std::tuple t2(std::cref(x)); - assert(&std::get<0>(t2) == &x); - std::tuple t3(std::allocator_arg, alloc, std::ref(x)); - assert(&std::get<0>(t3) == &x); - std::tuple t4(std::allocator_arg, alloc, std::cref(x)); - assert(&std::get<0>(t4) == &x); - } - { - auto r = std::ref(x); - auto cr = std::cref(x); - std::tuple t(r); - assert(&std::get<0>(t) == &x); - std::tuple t2(cr); - assert(&std::get<0>(t2) == &x); - std::tuple t3(std::allocator_arg, alloc, r); - assert(&std::get<0>(t3) == &x); - std::tuple t4(std::allocator_arg, alloc, cr); - assert(&std::get<0>(t4) == &x); - } -} Index: test/libcxx/utilities/tuple/tuple.tuple/tuple.cnstr/PR20855_tuple_ref_binding_diagnostics.fail.cpp =================================================================== --- test/libcxx/utilities/tuple/tuple.tuple/tuple.cnstr/PR20855_tuple_ref_binding_diagnostics.fail.cpp +++ test/libcxx/utilities/tuple/tuple.tuple/tuple.cnstr/PR20855_tuple_ref_binding_diagnostics.fail.cpp @@ -0,0 +1,80 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// + +// See llvm.org/PR20855 + +#ifdef __clang__ +#pragma clang diagnostic ignored "-Wdangling-field" +#endif + +#include +#include +#include "test_macros.h" + +template +struct ConvertsTo { + using RawTp = typename std::remove_cv< typename std::remove_reference::type>::type; + + operator Tp() const { + return static_cast(value); + } + + mutable RawTp value; +}; + +struct Base {}; +struct Derived : Base {}; + +template struct CannotDeduce { + using type = T; +}; + +template +void F(typename CannotDeduce>::type const&) {} + + +int main() { +#if TEST_HAS_BUILTIN_IDENTIFIER(__reference_binds_to_temporary) + // expected-error@tuple:* 8 {{"Attempted construction of reference element binds to a temporary whose lifetime has ended"}} + { + F(std::make_tuple(1, "abc")); // expected-note 1 {{requested here}} + } + { + std::tuple t(1, "a"); // expected-note 1 {{requested here}} + } + { + F(std::tuple(1, "abc")); // expected-note 1 {{requested here}} + } + { + ConvertsTo ct; + std::tuple t(ct, 42); // expected-note {{requested here}} + } + { + ConvertsTo ct; + std::tuple t(ct, nullptr); // expected-note {{requested here}} + } + { + ConvertsTo ct; + std::tuple t(ct, 42); // expected-note {{requested here}} + } + { + std::allocator alloc; + std::tuple t2("hello"); // expected-note {{requested here}} + std::tuple t3(std::allocator_arg, alloc, "hello"); // expected-note {{requested here}} + } +#else +#error force failure +// expected-error@-1 {{force failure}} +#endif +} Index: test/std/utilities/tuple/tuple.tuple/tuple.cnstr/PR20855_tuple_ref_binding_diagnostics.pass.cpp =================================================================== --- test/std/utilities/tuple/tuple.tuple/tuple.cnstr/PR20855_tuple_ref_binding_diagnostics.pass.cpp +++ test/std/utilities/tuple/tuple.tuple/tuple.cnstr/PR20855_tuple_ref_binding_diagnostics.pass.cpp @@ -0,0 +1,136 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// + +// See llvm.org/PR20855 + +#include +#include +#include "test_macros.h" + +#if TEST_HAS_BUILTIN_IDENTIFIER(__reference_binds_to_temporary) +# define ASSERT_REFERENCE_BINDS_TEMPORARY(...) static_assert(__reference_binds_to_temporary(__VA_ARGS__), "") +# define ASSERT_NOT_REFERENCE_BINDS_TEMPORARY(...) static_assert(!__reference_binds_to_temporary(__VA_ARGS__), "") +#else +# define ASSERT_REFERENCE_BINDS_TEMPORARY(...) static_assert(true, "") +# define ASSERT_NOT_REFERENCE_BINDS_TEMPORARY(...) static_assert(true, "") +#endif + +template +struct ConvertsTo { + using RawTp = typename std::remove_cv< typename std::remove_reference::type>::type; + + operator Tp() const { + return static_cast(value); + } + + mutable RawTp value; +}; + +struct Base {}; +struct Derived : Base {}; + + +static_assert(std::is_same::value, ""); +ASSERT_REFERENCE_BINDS_TEMPORARY(std::string const&, decltype("abc")); +ASSERT_REFERENCE_BINDS_TEMPORARY(std::string const&, decltype(("abc"))); +ASSERT_REFERENCE_BINDS_TEMPORARY(std::string const&, const char*&&); + +ASSERT_NOT_REFERENCE_BINDS_TEMPORARY(int&, const ConvertsTo&); +ASSERT_NOT_REFERENCE_BINDS_TEMPORARY(const int&, ConvertsTo&); +ASSERT_NOT_REFERENCE_BINDS_TEMPORARY(Base&, Derived&); + + +static_assert(std::is_constructible>::value, ""); +static_assert(std::is_constructible>::value, ""); + +template struct CannotDeduce { + using type = T; +}; + +template +void F(typename CannotDeduce>::type const&) {} + +void compile_tests() { + { + F(std::make_tuple(42, 42)); + } + { + F(std::make_tuple(42, 42)); + std::tuple t(std::make_tuple(42, 42)); + } + { + auto fn = &F; + fn(std::tuple(42, std::string("a"))); + fn(std::make_tuple(42, std::string("a"))); + } + { + Derived d; + std::tuple t(d, d); + } + { + ConvertsTo ct; + std::tuple t(42, ct); + } +} + +void allocator_tests() { + std::allocator alloc; + int x = 42; + { + std::tuple t(std::ref(x)); + assert(&std::get<0>(t) == &x); + std::tuple t1(std::allocator_arg, alloc, std::ref(x)); + assert(&std::get<0>(t1) == &x); + } + { + auto r = std::ref(x); + auto const& cr = r; + std::tuple t(r); + assert(&std::get<0>(t) == &x); + std::tuple t1(cr); + assert(&std::get<0>(t1) == &x); + std::tuple t2(std::allocator_arg, alloc, r); + assert(&std::get<0>(t2) == &x); + std::tuple t3(std::allocator_arg, alloc, cr); + assert(&std::get<0>(t3) == &x); + } + { + std::tuple t(std::ref(x)); + assert(&std::get<0>(t) == &x); + std::tuple t2(std::cref(x)); + assert(&std::get<0>(t2) == &x); + std::tuple t3(std::allocator_arg, alloc, std::ref(x)); + assert(&std::get<0>(t3) == &x); + std::tuple t4(std::allocator_arg, alloc, std::cref(x)); + assert(&std::get<0>(t4) == &x); + } + { + auto r = std::ref(x); + auto cr = std::cref(x); + std::tuple t(r); + assert(&std::get<0>(t) == &x); + std::tuple t2(cr); + assert(&std::get<0>(t2) == &x); + std::tuple t3(std::allocator_arg, alloc, r); + assert(&std::get<0>(t3) == &x); + std::tuple t4(std::allocator_arg, alloc, cr); + assert(&std::get<0>(t4) == &x); + } +} + + +int main() { + compile_tests(); + allocator_tests(); +}