Skip to content

Commit e61d37a

Browse files
braw-leeP-E-P
authored andcommitted
Move errors with locations
gcc/rust/ChangeLog: * checks/errors/borrowck/rust-borrow-checker-diagnostics.cc (BorrowCheckerDiagnostics::report_move_errors): Specify locations for code causing errors and related moves. gcc/testsuite/ChangeLog: * rust/borrowck/test_move.rs: Test rich-errors related to moves. * rust/borrowck/test_move_conditional.rs: Likewise. Signed-off-by: Kushal Pal <[email protected]>
1 parent c6d0824 commit e61d37a

File tree

3 files changed

+96
-10
lines changed

3 files changed

+96
-10
lines changed

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,42 @@ BorrowCheckerDiagnostics::report_errors ()
3434
void
3535
BorrowCheckerDiagnostics::report_move_errors ()
3636
{
37-
if (!move_errors.empty ())
37+
for (const auto &pair : move_errors)
3838
{
39-
rust_error_at (hir_function->get_locus (),
40-
"Found move errors in function %s",
41-
hir_function->get_function_name ().as_string ().c_str ());
39+
auto error_location = get_statement (pair.first).get_location ();
40+
41+
// in future, we can use the assigned at location to hint the
42+
// user to implement copy trait for the type
43+
/*
44+
for (auto it : facts.path_assigned_at_base)
45+
{
46+
if (pair.second[0] == it.first)
47+
{
48+
auto point_assigned_at = it.second;
49+
auto assigned_at_location
50+
= get_statement (point_assigned_at).get_location ();
51+
}
52+
}
53+
*/
54+
55+
std::vector<LabelLocationPair> labels{
56+
{"moved value used here", error_location}};
57+
// add labels to all the moves for the given path
58+
for (auto it : facts.path_moved_at_base)
59+
{
60+
if (pair.second[0] == it.first)
61+
{
62+
auto point_moved_at = it.second;
63+
// don't label the move location where the error occured
64+
if (pair.first != point_moved_at)
65+
{
66+
auto move_at_location
67+
= get_statement (point_moved_at).get_location ();
68+
labels.push_back ({"value moved here", move_at_location});
69+
}
70+
}
71+
}
72+
multi_label_error ("use of moved value", error_location, labels);
4273
}
4374
}
4475

gcc/testsuite/rust/borrowck/test_move.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1-
// { dg-additional-options "-frust-compile-until=compilation -frust-borrowcheck" }
2-
fn test_move() { // { dg-error "Found move errors in function test_move" }
1+
// { dg-additional-options "-frust-compile-until=compilation -frust-borrowcheck -fdiagnostics-show-caret -fdiagnostics-show-line-numbers" }
2+
// { dg-enable-nn-line-numbers "" }
3+
4+
fn test_move() {
35
struct A {
46
i: i32,
57
}
68
let a = A { i: 1 };
79
let b = a;
8-
let c = a;
10+
let c = a; //~ ERROR
11+
// { dg-error "use of moved value" "" { target *-*-* } .-1 }
12+
/*
13+
{ dg-begin-multiline-output "" }
14+
NN | let b = a;
15+
| ~
16+
| |
17+
| value moved here
18+
NN | let c = a; //~ ERROR
19+
| ^
20+
| |
21+
| moved value used here
22+
{ dg-end-multiline-output "" }
23+
*/
24+
925
}
1026

1127
fn test_move_fixed() {

gcc/testsuite/rust/borrowck/test_move_conditional.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// { dg-additional-options "-frust-compile-until=compilation -frust-borrowcheck" }
1+
// { dg-additional-options "-frust-compile-until=compilation -frust-borrowcheck -fdiagnostics-show-caret -fdiagnostics-show-line-numbers" }
2+
// { dg-enable-nn-line-numbers "" }
23

3-
fn test_move_conditional(b1: bool, b2:bool) { // { dg-error "Found move errors in function test_move" }
4+
fn test_move_conditional(b1: bool, b2:bool) {
45
struct A {
56
i: i32,
67
}
@@ -9,9 +10,47 @@ fn test_move_conditional(b1: bool, b2:bool) { // { dg-error "Found move errors i
910
let b = a;
1011
if b1 {
1112
let b = a;
13+
// { dg-error "use of moved value" "" { target *-*-* } .-1 }
14+
/*
15+
{ dg-begin-multiline-output "" }
16+
NN | let b = a;
17+
| ~
18+
| |
19+
| value moved here
20+
NN | if b1 {
21+
NN | let b = a;
22+
| ~
23+
| |
24+
| value moved here
25+
......
26+
NN | let c = a;
27+
| ^
28+
| |
29+
| moved value used here
30+
{ dg-end-multiline-output "" }
31+
*/
1232
}
1333
if b2 {
1434
let c = a;
35+
// { dg-error "use of moved value" "" { target *-*-* } .-1 }
36+
/*
37+
{ dg-begin-multiline-output "" }
38+
NN | let b = a;
39+
| ~
40+
| |
41+
| value moved here
42+
NN | if b1 {
43+
NN | let b = a;
44+
| ^
45+
| |
46+
| moved value used here
47+
......
48+
NN | let c = a;
49+
| ~
50+
| |
51+
| value moved here
52+
{ dg-end-multiline-output "" }
53+
*/
1554
}
1655
}
1756

@@ -25,4 +64,4 @@ fn test_move_fixed(b1: bool, b2:bool) {
2564
if b2 {
2665
let c = a;
2766
}
28-
}
67+
}

0 commit comments

Comments
 (0)