From e897008449a52dce32e917d67620c4511754dba6 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 7 Oct 2022 08:52:47 -0400 Subject: [PATCH] AK: Change ErrorOr to contain a Variant rather than inherit from it GCC seems to get tripped up over this inheritance when converting from an ErrorOr to the partially specialized ErrorOr. See the following snippet: NEVER_INLINE ErrorOr foo() { auto string = "abc"sv; outln("{:p}", string.characters_without_null_termination()); return string; } NEVER_INLINE ErrorOr bar() { auto string = TRY(foo()); outln("{:p}", string.characters_without_null_termination()); VERIFY(!string.starts_with('#')); return {}; } int main() { MUST(bar()); } On some machines, bar() will contain a StringView whose pointer has had its upper bits set to 0: 0x000000010cafd6f8 0x000000000cafd6f8 I'm not 100% clear on what's happening in the default-generated Variant destructor that causes this. Probably worth investigating further. The error would also be alleviated by making the Variant destructor virtual, but rather than that, let's make ErrorOr simply contain a Variant rather than inherit from it. Fixes #15449. --- AK/Error.h | 59 ++++++++++++++++-------------------------------------- 1 file changed, 17 insertions(+), 42 deletions(-) diff --git a/AK/Error.h b/AK/Error.h index decae33475..0ab143d6e7 100644 --- a/AK/Error.h +++ b/AK/Error.h @@ -6,7 +6,6 @@ #pragma once -#include #include #include #include @@ -74,32 +73,36 @@ private: }; template -class [[nodiscard]] ErrorOr final : public Variant { +class [[nodiscard]] ErrorOr { public: - using Variant::Variant; + ErrorOr() requires(IsSame) + : m_value_or_error(Empty {}) + { + } template - ALWAYS_INLINE ErrorOr(U&& value) requires(!IsSame, ErrorOr>) - : Variant(forward(value)) + ALWAYS_INLINE ErrorOr(U&& value) requires(!IsSame, ErrorOr>) + : m_value_or_error(forward(value)) { } #ifdef __serenity__ ErrorOr(ErrnoCode code) - : Variant(Error::from_errno(code)) + : m_value_or_error(Error::from_errno(code)) { } #endif T& value() { - return this->template get(); + return m_value_or_error.template get(); } - T const& value() const { return this->template get(); } - ErrorType& error() { return this->template get(); } - ErrorType const& error() const { return this->template get(); } + T const& value() const { return m_value_or_error.template get(); } - bool is_error() const { return this->template has(); } + ErrorType& error() { return m_value_or_error.template get(); } + ErrorType const& error() const { return m_value_or_error.template get(); } + + bool is_error() const { return m_value_or_error.template has(); } T release_value() { return move(value()); } ErrorType release_error() { return move(error()); } @@ -111,41 +114,13 @@ public: } private: - // 'downcast' is fishy in this context. Let's hide it by making it private. - using Variant::downcast; + Variant m_value_or_error; }; -// Partial specialization for void value type template -class [[nodiscard]] ErrorOr { +class [[nodiscard]] ErrorOr : public ErrorOr { public: - ErrorOr(ErrorType error) - : m_error(move(error)) - { - } - -#ifdef __serenity__ - ErrorOr(ErrnoCode code) - : m_error(Error::from_errno(code)) - { - } -#endif - - ErrorOr() = default; - ErrorOr(ErrorOr&& other) = default; - ErrorOr(ErrorOr const& other) = default; - ~ErrorOr() = default; - - ErrorOr& operator=(ErrorOr&& other) = default; - ErrorOr& operator=(ErrorOr const& other) = default; - - ErrorType const& error() const { return m_error.value(); } - bool is_error() const { return m_error.has_value(); } - ErrorType release_error() { return m_error.release_value(); } - void release_value() { } - -private: - Optional m_error; + using ErrorOr::ErrorOr; }; }