Commit f1320274 authored by Kenton Varda's avatar Kenton Varda

Fix implicit conversions of Maybe in multi-step conversion cases.

parent 938d1e03
...@@ -37,6 +37,14 @@ KJ_TEST("kj::size() on native arrays") { ...@@ -37,6 +37,14 @@ KJ_TEST("kj::size() on native arrays") {
KJ_EXPECT(expected == 4u); KJ_EXPECT(expected == 4u);
} }
struct ImplicitToInt {
int i;
operator int() const {
return i;
}
};
TEST(Common, Maybe) { TEST(Common, Maybe) {
{ {
Maybe<int> m = 123; Maybe<int> m = 123;
...@@ -166,6 +174,23 @@ TEST(Common, Maybe) { ...@@ -166,6 +174,23 @@ TEST(Common, Maybe) {
EXPECT_EQ(0, *v); // avoid unused warning EXPECT_EQ(0, *v); // avoid unused warning
} }
} }
{
// Test a case where an implicit conversion didn't used to happen correctly.
Maybe<ImplicitToInt> m(ImplicitToInt { 123 });
Maybe<uint> m2(m);
Maybe<uint> m3(kj::mv(m));
KJ_IF_MAYBE(v, m2) {
EXPECT_EQ(123, *v);
} else {
ADD_FAILURE();
}
KJ_IF_MAYBE(v, m3) {
EXPECT_EQ(123, *v);
} else {
ADD_FAILURE();
}
}
} }
TEST(Common, MaybeConstness) { TEST(Common, MaybeConstness) {
......
...@@ -1031,13 +1031,13 @@ public: ...@@ -1031,13 +1031,13 @@ public:
template <typename U> template <typename U>
Maybe(Maybe<U>&& other) noexcept(noexcept(T(instance<U&&>()))) { Maybe(Maybe<U>&& other) noexcept(noexcept(T(instance<U&&>()))) {
KJ_IF_MAYBE(val, kj::mv(other)) { KJ_IF_MAYBE(val, kj::mv(other)) {
ptr = *val; ptr.emplace(kj::mv(*val));
} }
} }
template <typename U> template <typename U>
Maybe(const Maybe<U>& other) { Maybe(const Maybe<U>& other) {
KJ_IF_MAYBE(val, other) { KJ_IF_MAYBE(val, other) {
ptr = *val; ptr.emplace(*val);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment