From bcfaf1044ea8293079c219e15302095a5980cec7 Mon Sep 17 00:00:00 2001 From: Jon Ross-Perkins Date: Fri, 25 Jul 2025 08:00:33 -0700 Subject: [PATCH] Remove location support from error (#5837) Location support was probably there for explorer, which is deleted. Remove support as a simplification. --- common/error.h | 41 +++++++-------------------------- common/error_test.cpp | 7 ------ testing/file_test/test_file.cpp | 2 +- 3 files changed, 9 insertions(+), 41 deletions(-) diff --git a/common/error.h b/common/error.h index 641f77fa771f..d08475d50b25 100644 --- a/common/error.h +++ b/common/error.h @@ -32,43 +32,21 @@ struct Success : public Printable { class [[nodiscard]] Error : public Printable { public: // Represents an error state. - explicit Error(llvm::Twine location, llvm::Twine message) - : location_(location.str()), message_(message.str()) { + explicit Error(llvm::Twine message) : message_(message.str()) { CARBON_CHECK(!message_.empty(), "Errors must have a message."); } - // Represents an error with no associated location. - // TODO: Consider using two different types. - explicit Error(llvm::Twine message) : Error("", message) {} - - Error(Error&& other) noexcept - : location_(std::move(other.location_)), - message_(std::move(other.message_)) {} - - auto operator=(Error&& other) noexcept -> Error& { - location_ = std::move(other.location_); - message_ = std::move(other.message_); - return *this; - } + // Move-only. + Error(Error&& other) noexcept = default; + auto operator=(Error&& other) noexcept -> Error& = default; // Prints the error string. - auto Print(llvm::raw_ostream& out) const -> void { - if (!location().empty()) { - out << location() << ": "; - } - out << message(); - } - - // Returns a string describing the location of the error, such as - // "file.cc:123". - auto location() const -> const std::string& { return location_; } + auto Print(llvm::raw_ostream& out) const -> void { out << message(); } // Returns the error message. auto message() const -> const std::string& { return message_; } private: - // The location associated with the error. - std::string location_; // The error message. std::string message_; }; @@ -210,9 +188,7 @@ class [[nodiscard]] ErrorOr { // `Error` and `ErrorOr`. class ErrorBuilder { public: - explicit ErrorBuilder(std::string location = "") - : location_(std::move(location)), - out_(std::make_unique()) {} + explicit ErrorBuilder() : out_(std::make_unique()) {} ErrorBuilder(ErrorBuilder&&) = default; auto operator=(ErrorBuilder&&) -> ErrorBuilder& = default; @@ -233,16 +209,15 @@ class ErrorBuilder { } // NOLINTNEXTLINE(google-explicit-constructor): Implicit cast for returns. - operator Error() { return Error(location_, out_->TakeStr()); } + operator Error() { return Error(out_->TakeStr()); } template // NOLINTNEXTLINE(google-explicit-constructor): Implicit cast for returns. operator ErrorOr() { - return Error(location_, out_->TakeStr()); + return Error(out_->TakeStr()); } private: - std::string location_; std::unique_ptr out_; }; diff --git a/common/error_test.cpp b/common/error_test.cpp index 67216f033ee0..a271b9a0fb73 100644 --- a/common/error_test.cpp +++ b/common/error_test.cpp @@ -36,13 +36,6 @@ TEST(ErrorTest, ErrorBuilderOperatorImplicitCast) { EXPECT_THAT(result, IsError("msg")); } -TEST(ErrorTest, StreamError) { - Error result = ErrorBuilder("TestFunc") << "msg"; - RawStringOstream result_stream; - result_stream << result; - EXPECT_EQ(result_stream.TakeStr(), "TestFunc: msg"); -} - class CustomError : public ErrorBase { public: auto Print(llvm::raw_ostream& os) const -> void { diff --git a/testing/file_test/test_file.cpp b/testing/file_test/test_file.cpp index c88242a9a65f..ad9ac1a44bfc 100644 --- a/testing/file_test/test_file.cpp +++ b/testing/file_test/test_file.cpp @@ -108,7 +108,7 @@ static auto ExtractFilePathFromUri(llvm::StringRef uri) -> ErrorOr { static constexpr llvm::StringRef FilePrefix = "file:/"; if (!uri.starts_with(FilePrefix)) { - return ErrorBuilder("uri `") << uri << "` is not a file uri"; + return ErrorBuilder() << "uri `" << uri << "` is not a file uri"; } return uri.drop_front(FilePrefix.size()); }