https://gcc.gnu.org/g:c1ba8985c6599a8cde5c9462e38515f29e54f69e

commit c1ba8985c6599a8cde5c9462e38515f29e54f69e
Author: Kushal Pal <kushalpal...@gmail.com>
Date:   Fri Jun 28 06:25:52 2024 +0000

    Introduce new class to handle borrow errors
    
    gcc/rust/ChangeLog:
    
            * Make-lang.in: Compile new file.
            * checks/errors/borrowck/rust-borrow-checker.cc (BorrowChecker::go):
            Use new class to report errors.
            * checks/errors/borrowck/rust-borrow-checker-diagnostics.cc: New 
file.
            * checks/errors/borrowck/rust-borrow-checker-diagnostics.h:
            New file, adds new class.
    
    Signed-off-by: Kushal Pal <kushalpal...@gmail.com>

Diff:
---
 gcc/rust/Make-lang.in                              |  1 +
 .../borrowck/rust-borrow-checker-diagnostics.cc    | 67 +++++++++++++++++++++
 .../borrowck/rust-borrow-checker-diagnostics.h     | 70 ++++++++++++++++++++++
 .../checks/errors/borrowck/rust-borrow-checker.cc  | 21 ++-----
 4 files changed, 142 insertions(+), 17 deletions(-)

diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in
index 05ebc707569d..f9c6dcac3058 100644
--- a/gcc/rust/Make-lang.in
+++ b/gcc/rust/Make-lang.in
@@ -169,6 +169,7 @@ GRS_OBJS = \
     rust/rust-hir-type-check-enumitem.o \
     rust/rust-hir-type-check-implitem.o \
     rust/rust-borrow-checker.o \
+    rust/rust-borrow-checker-diagnostics.o\
     rust/rust-bir-builder-expr-stmt.o \
     rust/rust-bir-dump.o \
     rust/rust-polonius.o\
diff --git a/gcc/rust/checks/errors/borrowck/rust-borrow-checker-diagnostics.cc 
b/gcc/rust/checks/errors/borrowck/rust-borrow-checker-diagnostics.cc
new file mode 100644
index 000000000000..a8eaa807ebb0
--- /dev/null
+++ b/gcc/rust/checks/errors/borrowck/rust-borrow-checker-diagnostics.cc
@@ -0,0 +1,67 @@
+// Copyright (C) 2020-2024 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include "rust-borrow-checker-diagnostics.h"
+
+namespace Rust {
+namespace BIR {
+
+void
+BorrowCheckerDiagnostics::report_errors ()
+{
+  report_move_errors ();
+  report_loan_errors ();
+  report_subset_errors ();
+}
+
+void
+BorrowCheckerDiagnostics::report_move_errors ()
+{
+  if (!move_errors.empty ())
+    {
+      rust_error_at (hir_function->get_locus (),
+                    "Found move errors in function %s",
+                    hir_function->get_function_name ().as_string ().c_str ());
+    }
+}
+
+void
+BorrowCheckerDiagnostics::report_loan_errors ()
+{
+  if (!loan_errors.empty ())
+    {
+      rust_error_at (hir_function->get_locus (),
+                    "Found loan errors in function %s",
+                    hir_function->get_function_name ().as_string ().c_str ());
+    }
+}
+
+void
+BorrowCheckerDiagnostics::report_subset_errors ()
+{
+  if (!subset_errors.empty ())
+    {
+      rust_error_at (hir_function->get_locus (),
+                    "Found subset errors in function %s. Some lifetime "
+                    "constraints need to be added.",
+                    hir_function->get_function_name ().as_string ().c_str ());
+    }
+}
+
+} // namespace BIR
+} // namespace Rust
diff --git a/gcc/rust/checks/errors/borrowck/rust-borrow-checker-diagnostics.h 
b/gcc/rust/checks/errors/borrowck/rust-borrow-checker-diagnostics.h
new file mode 100644
index 000000000000..90d5ed8aaef7
--- /dev/null
+++ b/gcc/rust/checks/errors/borrowck/rust-borrow-checker-diagnostics.h
@@ -0,0 +1,70 @@
+// Copyright (C) 2020-2024 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#ifndef RUST_BORROW_CHECKER_DIAGNOSTICS_H
+#define RUST_BORROW_CHECKER_DIAGNOSTICS_H
+
+#include "polonius/rust-polonius.h"
+#include "rust-bir.h"
+#include "rust-hir-item.h"
+
+namespace Rust {
+namespace BIR {
+class BorrowCheckerDiagnostics
+{
+  // HIR representation of Rust function
+  const HIR::Function *hir_function;
+  // BIR representation of Rust function
+  const Function &bir_function;
+  // Some facts related to this function
+  const Polonius::Facts &facts;
+  // Polonius output
+  // Point - vector<Path>
+  const std::vector<std::pair<size_t, std::vector<size_t>>> &move_errors;
+  // Point - vector<Loan>
+  const std::vector<std::pair<size_t, std::vector<size_t>>> &loan_errors;
+  // Point - pair<Origin, Origin>
+  const std::vector<std::pair<size_t, std::pair<size_t, size_t>>>
+    &subset_errors;
+
+public:
+  BorrowCheckerDiagnostics (
+    const HIR::Function *hir_function, const Function &bir_function,
+    const Polonius::Facts &facts,
+    const std::vector<std::pair<size_t, std::vector<size_t>>> &move_errors,
+    const std::vector<std::pair<size_t, std::vector<size_t>>> &loan_errors,
+    const std::vector<std::pair<size_t, std::pair<size_t, size_t>>>
+      &subset_errors)
+
+    : hir_function (hir_function), bir_function (bir_function), facts (facts),
+      move_errors (move_errors), loan_errors (loan_errors),
+      subset_errors (subset_errors)
+  {}
+
+  void report_errors ();
+
+private:
+  void report_move_errors ();
+  void report_loan_errors ();
+  void report_subset_errors ();
+};
+
+} // namespace BIR
+} // namespace Rust
+
+#endif // RUST_BORROW_CHECKER_DIAGNOSTICS_H
diff --git a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc 
b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
index c0d20ed35a24..2b1d70229cb9 100644
--- a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
+++ b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
@@ -17,6 +17,7 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-borrow-checker.h"
+#include "rust-borrow-checker-diagnostics.h"
 #include "rust-function-collector.h"
 #include "rust-bir-fact-collector.h"
 #include "rust-bir-builder.h"
@@ -165,23 +166,9 @@ BorrowChecker::go (HIR::Crate &crate)
       delete result.move_errors;
       delete result.subset_errors;
 
-      if (!loan_errors.empty ())
-       {
-         rust_error_at (func->get_locus (), "Found loan errors in function %s",
-                        func->get_function_name ().as_string ().c_str ());
-       }
-      if (!subset_errors.empty ())
-       {
-         rust_error_at (func->get_locus (),
-                        "Found subset errors in function %s. Some lifetime "
-                        "constraints need to be added.",
-                        func->get_function_name ().as_string ().c_str ());
-       }
-      if (!move_errors.empty ())
-       {
-         rust_error_at (func->get_locus (), "Found move errors in function %s",
-                        func->get_function_name ().as_string ().c_str ());
-       }
+      BIR::BorrowCheckerDiagnostics (func, bir, facts, move_errors, 
loan_errors,
+                                    subset_errors)
+       .report_errors ();
     }
 
   for (auto closure ATTRIBUTE_UNUSED : collector.get_closures ())

Reply via email to