Skip to content

Commit

Permalink
Introduce new class to handle borrow errors
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
braw-lee authored and CohenArthur committed Jul 16, 2024
1 parent ff59d15 commit c1ba898
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 17 deletions.
1 change: 1 addition & 0 deletions gcc/rust/Make-lang.in
Original file line number Diff line number Diff line change
Expand Up @@ -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\
Expand Down
67 changes: 67 additions & 0 deletions gcc/rust/checks/errors/borrowck/rust-borrow-checker-diagnostics.cc
Original file line number Diff line number Diff line change
@@ -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
70 changes: 70 additions & 0 deletions gcc/rust/checks/errors/borrowck/rust-borrow-checker-diagnostics.h
Original file line number Diff line number Diff line change
@@ -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
21 changes: 4 additions & 17 deletions gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 ())
Expand Down

0 comments on commit c1ba898

Please sign in to comment.