diff options
author | Erik Kundt <bitshift@posteo.org> | 2018-12-04 00:59:21 +0800 |
---|---|---|
committer | Erik Kundt <bitshift@posteo.org> | 2018-12-05 17:33:34 +0800 |
commit | e3accc6aa6857c8ae7c6f075ff50d9a17989cd0d (patch) | |
tree | 61f195edac6d80164098f98b3ec0aa640c59b388 | |
parent | 05e74d096ed5a891ae2c1c0d916727ebaca1109f (diff) | |
download | dexon-solidity-e3accc6aa6857c8ae7c6f075ff50d9a17989cd0d.tar.gz dexon-solidity-e3accc6aa6857c8ae7c6f075ff50d9a17989cd0d.tar.zst dexon-solidity-e3accc6aa6857c8ae7c6f075ff50d9a17989cd0d.zip |
Simplifies Result<T> and prevents undefined behaviour.
-rw-r--r-- | libdevcore/Result.h | 35 | ||||
-rw-r--r-- | libsolidity/ast/Types.cpp | 6 |
2 files changed, 12 insertions, 29 deletions
diff --git a/libdevcore/Result.h b/libdevcore/Result.h index c1abf380..cdff67c6 100644 --- a/libdevcore/Result.h +++ b/libdevcore/Result.h @@ -28,44 +28,22 @@ namespace dev /// Result<bool> check() /// { /// if (false) -/// return Result<bool>(false, "Error message.") +/// return Result<bool>("Error message.") /// return true; /// } /// -/// A result can also be instantiated using one of the factory methods it provides: -/// -/// using BoolResult = Result<bool>; -/// BoolResult check() -/// { -/// if (false) -/// return BoolResult::failure("Error message"); -/// return BoolResult::success(true); -/// } -/// -/// -struct ResultError { std::string reason; }; template <class ResultType> class Result { public: - /// @{ - /// @name Factory functions - /// Factory functions that provide a verbose way to create a result - static Result<ResultType> Ok(ResultType _value) { return Result(std::move(_value)); } - static Result<ResultType> Err() { return Result(ResultError{std::string()}); } - static Result<ResultType> Err(std::string _error) { return Result(ResultError{std::move(_error)}); } - /// @} - - Result(ResultType _value): m_value(std::move(_value)) {} - Result(ResultError _error): m_error(std::move(_error.reason)) {} - Result(ResultType _value, ResultError _error): m_value(std::move(_value)), m_error(std::move(_error.reason)) {} + Result(ResultType _value): Result(_value, std::string{}) {} + Result(std::string _error): Result(ResultType{}, _error) {} /// @{ /// @name Wrapper functions /// Wrapper functions that provide implicit conversions to and explicit retrieval of /// the value this result holds. - /// If the result is an error, accessing the value results in undefined behaviour. operator ResultType const&() const { return m_value; } ResultType& operator*() const { return m_value; } ResultType const& get() const { return m_value; } @@ -73,9 +51,14 @@ public: /// @} /// @returns the error message (can be empty). - std::string const& err() const { return m_error; } + std::string const& error() const { return m_error; } private: + explicit Result(ResultType _value, std::string _error): + m_value(std::move(_value)), + m_error(std::move(_error)) + {} + ResultType m_value; std::string m_error; }; diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index f67d0b12..c6b4211a 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -611,14 +611,14 @@ TypeResult IntegerType::unaryOperatorResult(Token _operator) const { // "delete" is ok for all integer types if (_operator == Token::Delete) - return TypeResult::Ok(make_shared<TupleType>()); + return TypeResult{make_shared<TupleType>()}; // we allow +, -, ++ and -- else if (_operator == Token::Add || _operator == Token::Sub || _operator == Token::Inc || _operator == Token::Dec || _operator == Token::BitNot) - return TypeResult::Ok(shared_from_this()); + return TypeResult{shared_from_this()}; else - return TypeResult::Err(); + return TypeResult{""}; } bool IntegerType::operator==(Type const& _other) const |