From 5349972f4107b9e2ef975c98f0ce2c0a18e7b2a7 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 19 Jan 2023 17:02:26 -0500 Subject: [PATCH] LibJS: Add a special ThrowCompletionOr constructor for OptionalNone Currently, if you have a fallible function with an Optional return type and try to return OptionalNone like this: ThrowCompletionOr> { return OptionalNone {}; } Then ThrowCompletionOr's m_value (whose type is an Optional) will be set to empty, rather than containing an empty Optional. This is due to the ThrowCompletionOr's WrappedValueType constructor. This patch adds a constructor specifically for OptionalNone. If someone attempts to construct a ThrowCompletionOr with OptionalNone, but the ValueType is not an Optional, a compile error like the following will occur: ThrowCompletionOr foo() { return OptionalNone {}; } error: no matching constructor for initialization of 'AK::String' Otherwise, it will fill ThrowCompletionOr's m_value with an empty Optional. --- Userland/Libraries/LibJS/Runtime/Completion.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/Completion.h b/Userland/Libraries/LibJS/Runtime/Completion.h index 8fa7566b61..2d61864e5a 100644 --- a/Userland/Libraries/LibJS/Runtime/Completion.h +++ b/Userland/Libraries/LibJS/Runtime/Completion.h @@ -277,6 +277,11 @@ public: ThrowCompletionOr(ThrowCompletionOr&&) = default; ThrowCompletionOr& operator=(ThrowCompletionOr&&) = default; + ThrowCompletionOr(OptionalNone value) + : m_value(ValueType { value }) + { + } + // Allows implicit construction of ThrowCompletionOr from a type U if T(U) is a supported constructor. // Most commonly: Value from Object* or similar, so we can omit the curly braces from "return { TRY(...) };". // Disabled for POD types to avoid weird conversion shenanigans.