Bläddra i källkod

Remove location support from error (#5837)

Location support was probably there for explorer, which is deleted.
Remove support as a simplification.
Jon Ross-Perkins 9 månader sedan
förälder
incheckning
bcfaf1044e
3 ändrade filer med 9 tillägg och 41 borttagningar
  1. 8 33
      common/error.h
  2. 0 7
      common/error_test.cpp
  3. 1 1
      testing/file_test/test_file.cpp

+ 8 - 33
common/error.h

@@ -32,43 +32,21 @@ struct Success : public Printable<Success> {
 class [[nodiscard]] Error : public Printable<Error> {
 class [[nodiscard]] Error : public Printable<Error> {
  public:
  public:
   // Represents an error state.
   // 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.");
     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.
   // 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.
   // Returns the error message.
   auto message() const -> const std::string& { return message_; }
   auto message() const -> const std::string& { return message_; }
 
 
  private:
  private:
-  // The location associated with the error.
-  std::string location_;
   // The error message.
   // The error message.
   std::string message_;
   std::string message_;
 };
 };
@@ -210,9 +188,7 @@ class [[nodiscard]] ErrorOr {
 // `Error` and `ErrorOr<T>`.
 // `Error` and `ErrorOr<T>`.
 class ErrorBuilder {
 class ErrorBuilder {
  public:
  public:
-  explicit ErrorBuilder(std::string location = "")
-      : location_(std::move(location)),
-        out_(std::make_unique<RawStringOstream>()) {}
+  explicit ErrorBuilder() : out_(std::make_unique<RawStringOstream>()) {}
 
 
   ErrorBuilder(ErrorBuilder&&) = default;
   ErrorBuilder(ErrorBuilder&&) = default;
   auto operator=(ErrorBuilder&&) -> ErrorBuilder& = default;
   auto operator=(ErrorBuilder&&) -> ErrorBuilder& = default;
@@ -233,16 +209,15 @@ class ErrorBuilder {
   }
   }
 
 
   // NOLINTNEXTLINE(google-explicit-constructor): Implicit cast for returns.
   // NOLINTNEXTLINE(google-explicit-constructor): Implicit cast for returns.
-  operator Error() { return Error(location_, out_->TakeStr()); }
+  operator Error() { return Error(out_->TakeStr()); }
 
 
   template <typename T>
   template <typename T>
   // NOLINTNEXTLINE(google-explicit-constructor): Implicit cast for returns.
   // NOLINTNEXTLINE(google-explicit-constructor): Implicit cast for returns.
   operator ErrorOr<T>() {
   operator ErrorOr<T>() {
-    return Error(location_, out_->TakeStr());
+    return Error(out_->TakeStr());
   }
   }
 
 
  private:
  private:
-  std::string location_;
   std::unique_ptr<RawStringOstream> out_;
   std::unique_ptr<RawStringOstream> out_;
 };
 };
 
 

+ 0 - 7
common/error_test.cpp

@@ -36,13 +36,6 @@ TEST(ErrorTest, ErrorBuilderOperatorImplicitCast) {
   EXPECT_THAT(result, IsError("msg"));
   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<CustomError> {
 class CustomError : public ErrorBase<CustomError> {
  public:
  public:
   auto Print(llvm::raw_ostream& os) const -> void {
   auto Print(llvm::raw_ostream& os) const -> void {

+ 1 - 1
testing/file_test/test_file.cpp

@@ -108,7 +108,7 @@ static auto ExtractFilePathFromUri(llvm::StringRef uri)
     -> ErrorOr<llvm::StringRef> {
     -> ErrorOr<llvm::StringRef> {
   static constexpr llvm::StringRef FilePrefix = "file:/";
   static constexpr llvm::StringRef FilePrefix = "file:/";
   if (!uri.starts_with(FilePrefix)) {
   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());
   return uri.drop_front(FilePrefix.size());
 }
 }