diff --git a/llvm/include/llvm/Support/ErrorOr.h b/llvm/include/llvm/Support/ErrorOr.h index 0fdafa09d58f..d5b11cbe5223 100644 --- a/llvm/include/llvm/Support/ErrorOr.h +++ b/llvm/include/llvm/Support/ErrorOr.h @@ -108,30 +108,28 @@ private: typedef typename remove_reference::type *pointer; public: - ErrorOr() : IsValid(false) {} - template ErrorOr(E ErrorCode, typename enable_if_c::value || is_error_condition_enum::value, void *>::type = 0) - : HasError(true), IsValid(true) { + : HasError(true) { new (getError()) error_code(make_error_code(ErrorCode)); } - ErrorOr(llvm::error_code EC) : HasError(true), IsValid(true) { + ErrorOr(llvm::error_code EC) : HasError(true) { new (getError()) error_code(EC); } - ErrorOr(T Val) : HasError(false), IsValid(true) { + ErrorOr(T Val) : HasError(false) { new (get()) storage_type(moveIfMoveConstructible(Val)); } - ErrorOr(const ErrorOr &Other) : IsValid(false) { + ErrorOr(const ErrorOr &Other) { copyConstruct(Other); } template - ErrorOr(const ErrorOr &Other) : IsValid(false) { + ErrorOr(const ErrorOr &Other) { copyConstruct(Other); } @@ -147,12 +145,12 @@ public: } #if LLVM_HAS_RVALUE_REFERENCES - ErrorOr(ErrorOr &&Other) : IsValid(false) { + ErrorOr(ErrorOr &&Other) { moveConstruct(std::move(Other)); } template - ErrorOr(ErrorOr &&Other) : IsValid(false) { + ErrorOr(ErrorOr &&Other) { moveConstruct(std::move(Other)); } @@ -169,8 +167,6 @@ public: #endif ~ErrorOr() { - if (!IsValid) - return; if (!HasError) get()->~storage_type(); } @@ -180,12 +176,10 @@ public: /// \brief Return false if there is an error. operator unspecified_bool_type() const { - assert(IsValid && "Can't do anything on a default constructed ErrorOr!"); return HasError ? 0 : unspecified_bool_true; } operator llvm::error_code() const { - assert(IsValid && "Can't do anything on a default constructed ErrorOr!"); return HasError ? *getError() : llvm::error_code::success(); } @@ -200,10 +194,6 @@ public: private: template void copyConstruct(const ErrorOr &Other) { - // Construct an invalid ErrorOr if other is invalid. - if (!Other.IsValid) - return; - IsValid = true; if (!Other.HasError) { // Get the other value. HasError = false; @@ -237,22 +227,14 @@ private: #if LLVM_HAS_RVALUE_REFERENCES template void moveConstruct(ErrorOr &&Other) { - // Construct an invalid ErrorOr if other is invalid. - if (!Other.IsValid) - return; - IsValid = true; if (!Other.HasError) { // Get the other value. HasError = false; new (get()) storage_type(std::move(*Other.get())); - // Tell other not to do any destruction. - Other.IsValid = false; } else { // Get other's error. HasError = true; new (getError()) error_code(Other); - // Tell other not to do any destruction. - Other.IsValid = false; } } @@ -275,19 +257,16 @@ private: } storage_type *get() { - assert(IsValid && "Can't do anything on a default constructed ErrorOr!"); assert(!HasError && "Cannot get value when an error exists!"); return reinterpret_cast(TStorage.buffer); } const storage_type *get() const { - assert(IsValid && "Can't do anything on a default constructed ErrorOr!"); assert(!HasError && "Cannot get value when an error exists!"); return reinterpret_cast(TStorage.buffer); } error_code *getError() { - assert(IsValid && "Can't do anything on a default constructed ErrorOr!"); assert(HasError && "Cannot get error when a value exists!"); return reinterpret_cast(ErrorStorage.buffer); } @@ -302,7 +281,6 @@ private: AlignedCharArrayUnion ErrorStorage; }; bool HasError : 1; - bool IsValid : 1; }; template