Skip to content

Commit c1ba898

Browse files
braw-leeCohenArthur
authored andcommitted
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 <kushalpal109@gmail.com>
1 parent ff59d15 commit c1ba898

File tree

4 files changed

+142
-17
lines changed

4 files changed

+142
-17
lines changed

gcc/rust/Make-lang.in

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ GRS_OBJS = \
169169
rust/rust-hir-type-check-enumitem.o \
170170
rust/rust-hir-type-check-implitem.o \
171171
rust/rust-borrow-checker.o \
172+
rust/rust-borrow-checker-diagnostics.o\
172173
rust/rust-bir-builder-expr-stmt.o \
173174
rust/rust-bir-dump.o \
174175
rust/rust-polonius.o\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (C) 2020-2024 Free Software Foundation, Inc.
2+
3+
// This file is part of GCC.
4+
5+
// GCC is free software; you can redistribute it and/or modify it under
6+
// the terms of the GNU General Public License as published by the Free
7+
// Software Foundation; either version 3, or (at your option) any later
8+
// version.
9+
10+
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11+
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
// for more details.
14+
15+
// You should have received a copy of the GNU General Public License
16+
// along with GCC; see the file COPYING3. If not see
17+
// <http://www.gnu.org/licenses/>.
18+
19+
#include "rust-borrow-checker-diagnostics.h"
20+
21+
namespace Rust {
22+
namespace BIR {
23+
24+
void
25+
BorrowCheckerDiagnostics::report_errors ()
26+
{
27+
report_move_errors ();
28+
report_loan_errors ();
29+
report_subset_errors ();
30+
}
31+
32+
void
33+
BorrowCheckerDiagnostics::report_move_errors ()
34+
{
35+
if (!move_errors.empty ())
36+
{
37+
rust_error_at (hir_function->get_locus (),
38+
"Found move errors in function %s",
39+
hir_function->get_function_name ().as_string ().c_str ());
40+
}
41+
}
42+
43+
void
44+
BorrowCheckerDiagnostics::report_loan_errors ()
45+
{
46+
if (!loan_errors.empty ())
47+
{
48+
rust_error_at (hir_function->get_locus (),
49+
"Found loan errors in function %s",
50+
hir_function->get_function_name ().as_string ().c_str ());
51+
}
52+
}
53+
54+
void
55+
BorrowCheckerDiagnostics::report_subset_errors ()
56+
{
57+
if (!subset_errors.empty ())
58+
{
59+
rust_error_at (hir_function->get_locus (),
60+
"Found subset errors in function %s. Some lifetime "
61+
"constraints need to be added.",
62+
hir_function->get_function_name ().as_string ().c_str ());
63+
}
64+
}
65+
66+
} // namespace BIR
67+
} // namespace Rust
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (C) 2020-2024 Free Software Foundation, Inc.
2+
3+
// This file is part of GCC.
4+
5+
// GCC is free software; you can redistribute it and/or modify it under
6+
// the terms of the GNU General Public License as published by the Free
7+
// Software Foundation; either version 3, or (at your option) any later
8+
// version.
9+
10+
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11+
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
// for more details.
14+
15+
// You should have received a copy of the GNU General Public License
16+
// along with GCC; see the file COPYING3. If not see
17+
// <http://www.gnu.org/licenses/>.
18+
19+
#ifndef RUST_BORROW_CHECKER_DIAGNOSTICS_H
20+
#define RUST_BORROW_CHECKER_DIAGNOSTICS_H
21+
22+
#include "polonius/rust-polonius.h"
23+
#include "rust-bir.h"
24+
#include "rust-hir-item.h"
25+
26+
namespace Rust {
27+
namespace BIR {
28+
class BorrowCheckerDiagnostics
29+
{
30+
// HIR representation of Rust function
31+
const HIR::Function *hir_function;
32+
// BIR representation of Rust function
33+
const Function &bir_function;
34+
// Some facts related to this function
35+
const Polonius::Facts &facts;
36+
// Polonius output
37+
// Point - vector<Path>
38+
const std::vector<std::pair<size_t, std::vector<size_t>>> &move_errors;
39+
// Point - vector<Loan>
40+
const std::vector<std::pair<size_t, std::vector<size_t>>> &loan_errors;
41+
// Point - pair<Origin, Origin>
42+
const std::vector<std::pair<size_t, std::pair<size_t, size_t>>>
43+
&subset_errors;
44+
45+
public:
46+
BorrowCheckerDiagnostics (
47+
const HIR::Function *hir_function, const Function &bir_function,
48+
const Polonius::Facts &facts,
49+
const std::vector<std::pair<size_t, std::vector<size_t>>> &move_errors,
50+
const std::vector<std::pair<size_t, std::vector<size_t>>> &loan_errors,
51+
const std::vector<std::pair<size_t, std::pair<size_t, size_t>>>
52+
&subset_errors)
53+
54+
: hir_function (hir_function), bir_function (bir_function), facts (facts),
55+
move_errors (move_errors), loan_errors (loan_errors),
56+
subset_errors (subset_errors)
57+
{}
58+
59+
void report_errors ();
60+
61+
private:
62+
void report_move_errors ();
63+
void report_loan_errors ();
64+
void report_subset_errors ();
65+
};
66+
67+
} // namespace BIR
68+
} // namespace Rust
69+
70+
#endif // RUST_BORROW_CHECKER_DIAGNOSTICS_H

gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc

+4-17
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// <http://www.gnu.org/licenses/>.
1818

1919
#include "rust-borrow-checker.h"
20+
#include "rust-borrow-checker-diagnostics.h"
2021
#include "rust-function-collector.h"
2122
#include "rust-bir-fact-collector.h"
2223
#include "rust-bir-builder.h"
@@ -165,23 +166,9 @@ BorrowChecker::go (HIR::Crate &crate)
165166
delete result.move_errors;
166167
delete result.subset_errors;
167168

168-
if (!loan_errors.empty ())
169-
{
170-
rust_error_at (func->get_locus (), "Found loan errors in function %s",
171-
func->get_function_name ().as_string ().c_str ());
172-
}
173-
if (!subset_errors.empty ())
174-
{
175-
rust_error_at (func->get_locus (),
176-
"Found subset errors in function %s. Some lifetime "
177-
"constraints need to be added.",
178-
func->get_function_name ().as_string ().c_str ());
179-
}
180-
if (!move_errors.empty ())
181-
{
182-
rust_error_at (func->get_locus (), "Found move errors in function %s",
183-
func->get_function_name ().as_string ().c_str ());
184-
}
169+
BIR::BorrowCheckerDiagnostics (func, bir, facts, move_errors, loan_errors,
170+
subset_errors)
171+
.report_errors ();
185172
}
186173

187174
for (auto closure ATTRIBUTE_UNUSED : collector.get_closures ())

0 commit comments

Comments
 (0)