]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Introduce new class to handle borrow errors
authorKushal Pal <kushalpal109@gmail.com>
Fri, 28 Jun 2024 06:25:52 +0000 (06:25 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 17 Mar 2025 15:35:52 +0000 (16:35 +0100)
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 <kushalpal109@gmail.com>
gcc/rust/Make-lang.in
gcc/rust/checks/errors/borrowck/rust-borrow-checker-diagnostics.cc [new file with mode: 0644]
gcc/rust/checks/errors/borrowck/rust-borrow-checker-diagnostics.h [new file with mode: 0644]
gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc

index 17f1feb6e46b775b995f478a697882fa0f7eca36..efa630927fabea0c6f0f9f0e6b0ca29339493ba6 100644 (file)
@@ -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 (file)
index 0000000..a8eaa80
--- /dev/null
@@ -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 (file)
index 0000000..90d5ed8
--- /dev/null
@@ -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
index 0ef4e5abf8c54d8a269857cf23d5817d05b94e8c..881d71fcc6da6d7609585ff399248f2f6a18d879 100644 (file)
@@ -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"
@@ -152,23 +153,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 ())