Skip to content

Commit ca7933e

Browse files
smallp-o-pdyung
authored andcommitted
[libc++] Disable mistakenly enabled optional<T&> constructors for optional<T> (llvm#194446)
Resolves llvm#194415 - A constructor specifically meant for `optional<T&>` was left enabled for `optional<T>` - Fix it, and add a test to check for regression. - This patch also corrects the constraints for `optional(optional<U>&)` and `optional(const optional<U>&)` , as they were incorrectly disallowing [valid conversions](https://godbolt.org/z/1r5Ea7z5M) - Also, correct the `noexcept` specification. - Add tests for both corrections. (cherry picked from commit 239189c)
1 parent 208ef91 commit ca7933e

5 files changed

Lines changed: 122 additions & 17 deletions

File tree

libcxx/include/optional

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,10 @@ private:
828828
template <class _Up>
829829
constexpr static bool __libcpp_opt_ref_ctor_deleted =
830830
is_lvalue_reference_v<_Tp> && reference_constructs_from_temporary_v<_Tp, _Up>;
831+
832+
template <class _Up>
833+
constexpr static bool __ref_ctor_enabled =
834+
is_lvalue_reference_v<_Tp> && !reference_constructs_from_temporary_v<_Tp, _Up>;
831835
# endif
832836

833837
// LWG2756: conditionally explicit conversion from _Up
@@ -935,15 +939,15 @@ public:
935939
template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>(), int> = 0>
936940
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(const optional<_Up>& __v)
937941
# if _LIBCPP_STD_VER >= 26
938-
noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp&, _Up&>)
942+
noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp&, const _Up&>)
939943
# endif
940944
{
941945
this->__construct_from(__v);
942946
}
943947
template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>(), int> = 0>
944948
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(const optional<_Up>& __v)
945949
# if _LIBCPP_STD_VER >= 26
946-
noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp&, _Up&>)
950+
noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp&, const _Up&>)
947951
# endif
948952
{
949953
this->__construct_from(__v);
@@ -981,25 +985,25 @@ public:
981985

982986
// optional(optional<U>& rhs)
983987
template <class _Up>
984-
requires(!__libcpp_opt_ref_ctor_deleted<_Up>) && (!is_same_v<remove_cvref_t<_Tp>, optional<_Up>>) &&
985-
(!is_same_v<_Tp&, _Up>) && is_constructible_v<_Tp&, _Up&>
988+
requires __ref_ctor_enabled<_Up&> && (!is_same_v<remove_cvref_t<_Tp>, optional<_Up>>) && (!is_same_v<_Tp&, _Up>) &&
989+
is_constructible_v<_Tp&, _Up&>
986990
_LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_Up&, _Tp&>)
987991
optional(optional<_Up>& __rhs) noexcept(is_nothrow_constructible_v<_Tp&, _Up&>) {
988992
this->__construct_from(__rhs);
989993
}
990994

991995
template <class _Up>
992-
requires __libcpp_opt_ref_ctor_deleted<_Up> && (!is_same_v<remove_cvref_t<_Tp>, optional<_Up>>) &&
996+
requires __libcpp_opt_ref_ctor_deleted<_Up&> && (!is_same_v<remove_cvref_t<_Tp>, optional<_Up>>) &&
993997
(!is_same_v<_Tp&, _Up>) && is_constructible_v<_Tp&, _Up&>
994998
constexpr explicit optional(optional<_Up>& __rhs) noexcept = delete;
995999

9961000
// optional(const optional<U>&)
9971001
template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>(), int> = 0>
998-
requires __libcpp_opt_ref_ctor_deleted<_Up>
1002+
requires __libcpp_opt_ref_ctor_deleted<const _Up&>
9991003
optional(const optional<_Up>&) = delete;
10001004

10011005
template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>(), int> = 0>
1002-
requires __libcpp_opt_ref_ctor_deleted<_Up>
1006+
requires __libcpp_opt_ref_ctor_deleted<const _Up&>
10031007
explicit optional(const optional<_Up>&) = delete;
10041008

10051009
// optional(optional<U>&&)
@@ -1013,16 +1017,16 @@ public:
10131017

10141018
// optional(const optional<U>&&)
10151019
template <class _Up>
1016-
requires(!__libcpp_opt_ref_ctor_deleted<_Up>) && (!is_same_v<remove_cvref_t<_Tp>, optional<_Up>>) &&
1017-
(!is_same_v<_Tp&, _Up>) && is_constructible_v<_Tp&, _Up>
1020+
requires __ref_ctor_enabled<const _Up> && (!is_same_v<remove_cvref_t<_Tp>, optional<_Up>>) &&
1021+
(!is_same_v<_Tp&, _Up>) && is_constructible_v<_Tp&, const _Up>
10181022
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit(!is_convertible_v<const _Up, _Tp&>)
10191023
optional(const optional<_Up>&& __v) noexcept(is_nothrow_constructible_v<_Tp&, const _Up>) {
10201024
this->__construct_from(std::move(__v));
10211025
}
10221026

10231027
template <class _Up>
1024-
requires __libcpp_opt_ref_ctor_deleted<_Up> && (!is_same_v<remove_cvref_t<_Tp>, optional<_Up>>) &&
1025-
(!is_same_v<_Tp&, _Up>) && is_constructible_v<_Tp&, _Up>
1028+
requires __libcpp_opt_ref_ctor_deleted<const _Up> && (!is_same_v<remove_cvref_t<_Tp>, optional<_Up>>) &&
1029+
(!is_same_v<_Tp&, _Up>) && is_constructible_v<_Tp&, const _Up>
10261030
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(const optional<_Up>&& __v) noexcept = delete;
10271031
# endif
10281032

libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/const_optional_U.pass.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,37 @@ constexpr bool test_ref() {
151151
assert(*o2 == 1);
152152
}
153153

154+
{
155+
const std::optional<int> o1{2};
156+
std::optional<const int&> o2(o1);
157+
assert(*o2 == 2);
158+
assert(&(*o2) == &(*o1));
159+
}
160+
161+
{
162+
const std::optional<ReferenceConversion<int>> o1({1, 2});
163+
std::optional<const int&> o2(o1);
164+
assert(o2.has_value());
165+
assert(&(*o2) == &o1->lvalue);
166+
assert(*o2 == 1);
167+
}
168+
169+
{
170+
const std::optional<ReferenceConversion<int>> o1({1, 2});
171+
std::optional<const int&> o2(std::move(o1));
172+
assert(o2.has_value());
173+
assert(&(*o2) == &o1->rvalue);
174+
assert(*o2 == 2);
175+
}
176+
177+
{
178+
const std::optional<ReferenceConversionThrows<int>> o1({1, 2});
179+
ASSERT_NOT_NOEXCEPT(std::optional<const int&>(o1));
180+
ASSERT_NOT_NOEXCEPT(std::optional<const int&>(std::move(o1)));
181+
}
182+
183+
static_assert(!std::is_constructible_v<std::optional<int>, const std::optional<ConstRValueOnly<int>>&&>);
184+
154185
return true;
155186
}
156187
#endif

libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/optional_U.pass.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,36 @@ constexpr bool test_ref() {
144144
assert(*o2 == 1);
145145
}
146146

147+
{
148+
std::optional<int> o1(1);
149+
std::optional<const int&> o2(o1);
150+
assert(o2.has_value());
151+
assert(*o2 == 1);
152+
assert(&(*o2) == &(*o1));
153+
}
154+
155+
{
156+
std::optional<ReferenceConversion<int>> o1({1, 2});
157+
std::optional<const int&> o2(o1);
158+
assert(o2.has_value());
159+
assert(*o2 == 1);
160+
assert(&(*o2) == &o1->lvalue);
161+
}
162+
163+
{
164+
std::optional<ReferenceConversion<int>> o1({1, 2});
165+
std::optional<const int&> o2(std::move(o1));
166+
assert(o2.has_value());
167+
assert(*o2 == 2);
168+
assert(&(*o2) == &o1->rvalue);
169+
}
170+
171+
{
172+
std::optional<ReferenceConversionThrows<int>> o1({1, 2});
173+
ASSERT_NOT_NOEXCEPT(std::optional<const int&>(o1));
174+
ASSERT_NOT_NOEXCEPT(std::optional<const int&>(std::move(o1)));
175+
}
176+
147177
return true;
148178
}
149179
#endif
@@ -164,6 +194,11 @@ int main(int, char**) {
164194
test<Z>(std::move(rhs), true);
165195
}
166196

197+
#if TEST_STD_VER >= 26
198+
// GH: #194415
199+
static_assert(!std::is_constructible_v<std::optional<int>, std::optional<LValueOnly<int>>&>);
200+
#endif
201+
167202
static_assert(!(std::is_constructible<optional<X>, optional<Z>>::value), "");
168203

169204
#if TEST_STD_VER >= 26

libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/ref_constructs_from_temporary.verify.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ void test() {
2929
const std::optional<int> co(1);
3030
std::optional<int> o0(1);
3131

32-
// expected-error-re@*:* 10 {{call to deleted constructor of 'std::optional<{{.*}}>'}}
32+
// expected-error-re@*:* 8 {{call to deleted constructor of 'std::optional<{{.*}}>'}}
3333
std::optional<const int&> o1{1}; // optional(U&&)
34-
std::optional<const int&> o2{o0}; // optional(optional<U>&)
35-
std::optional<const int&> o3{co}; // optional(const optional<U>&)
36-
std::optional<const int&> o4{std::move(o0)}; // optional(optional<U>&&&)
37-
std::optional<const int&> o5{std::move(co)}; // optional(optional<U>&&&)
34+
std::optional<const int&> o4{std::move(o0)}; // optional(optional<U>&&)
35+
std::optional<const int&> o5{std::move(co)}; // optional(optional<U>&&)
3836

3937
std::optional<const X&> o6{1}; // optional(U&&)
4038
std::optional<const X&> o7{o0}; // optional(optional<U>&)

libcxx/test/std/utilities/optional/optional.object/optional_helper_types.h

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ struct ReferenceConversion {
2121
constexpr ReferenceConversion(T lval, T rval) : lvalue(lval), rvalue(rval) {}
2222

2323
constexpr operator T&() & noexcept { return lvalue; }
24-
24+
constexpr operator const T&() const& noexcept { return lvalue; }
2525
constexpr operator T&() && noexcept { return rvalue; }
26+
constexpr operator const T&() const&& noexcept { return rvalue; }
2627
};
2728

2829
template <typename T>
@@ -42,13 +43,49 @@ struct ReferenceConversionThrows {
4243
return lvalue;
4344
}
4445

46+
constexpr operator const T&() const& {
47+
if (throws) {
48+
TEST_THROW(1);
49+
}
50+
51+
return lvalue;
52+
}
53+
4554
constexpr operator T&() && {
4655
if (throws) {
4756
TEST_THROW(2);
4857
}
4958

5059
return rvalue;
5160
}
61+
62+
constexpr operator const T&() const&& {
63+
if (throws) {
64+
TEST_THROW(2);
65+
}
66+
67+
return rvalue;
68+
}
69+
};
70+
71+
template <typename T>
72+
struct LValueOnly {
73+
T val{};
74+
75+
constexpr operator T&() & noexcept { return val; }
76+
constexpr operator T&() const& = delete;
77+
constexpr operator T&() && = delete;
78+
constexpr operator T&() const&& = delete;
79+
};
80+
81+
template <typename T>
82+
struct ConstRValueOnly {
83+
mutable T val{};
84+
85+
constexpr operator T&() & = delete;
86+
constexpr operator T&() const& = delete;
87+
constexpr operator T&() && = delete;
88+
constexpr operator T&() const&& { return val; };
5289
};
5390

5491
#endif

0 commit comments

Comments
 (0)