Skip to content

Commit b32caef

Browse files
committedJul 26, 2018
Use better spans for cannot-move errors
1 parent 71637c2 commit b32caef

File tree

5 files changed

+210
-28
lines changed

5 files changed

+210
-28
lines changed
 

‎src/librustc_mir/borrow_check/move_errors.rs

+19-24
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
8787
cannot_move_out_of: IllegalMoveOrigin { location, kind },
8888
} => {
8989
let stmt_source_info = self.mir.source_info(location);
90+
// Note: that the only time we assign a place isn't a temporary
91+
// to a user variable is when initializing it.
92+
// If that ever stops being the case, then the ever initialized
93+
// flow could be used.
9094
if let Some(StatementKind::Assign(
9195
Place::Local(local),
9296
Rvalue::Use(Operand::Move(move_from)),
@@ -109,26 +113,16 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
109113
opt_ty_info: _,
110114
}))) = local_decl.is_user_variable
111115
{
112-
// HACK use scopes to determine if this assignment is
113-
// the initialization of a variable.
114-
// FIXME(matthewjasper) This would probably be more
115-
// reliable if it used the ever initialized dataflow
116-
// but move errors are currently reported before the
117-
// rest of borrowck has run.
118-
if self
119-
.mir
120-
.is_sub_scope(local_decl.source_info.scope, stmt_source_info.scope)
121-
{
122-
self.append_binding_error(
123-
grouped_errors,
124-
kind,
125-
move_from,
126-
*local,
127-
opt_match_place,
128-
match_span,
129-
);
130-
return;
131-
}
116+
self.append_binding_error(
117+
grouped_errors,
118+
kind,
119+
move_from,
120+
*local,
121+
opt_match_place,
122+
match_span,
123+
stmt_source_info.span,
124+
);
125+
return;
132126
}
133127
}
134128
grouped_errors.push(GroupedMoveError::OtherIllegalMove {
@@ -147,6 +141,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
147141
bind_to: Local,
148142
match_place: &Option<Place<'tcx>>,
149143
match_span: Span,
144+
statement_span: Span,
150145
) {
151146
debug!(
152147
"append_to_grouped_errors(match_place={:?}, match_span={:?})",
@@ -173,13 +168,13 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
173168
debug!("found a new move error location");
174169

175170
// Don't need to point to x in let x = ... .
176-
let binds_to = if from_simple_let {
177-
vec![]
171+
let (binds_to, span) = if from_simple_let {
172+
(vec![], statement_span)
178173
} else {
179-
vec![bind_to]
174+
(vec![bind_to], match_span)
180175
};
181176
grouped_errors.push(GroupedMoveError::MovesFromMatchPlace {
182-
span: match_span,
177+
span,
183178
move_from: match_place.clone(),
184179
kind,
185180
binds_to,

‎src/test/ui/issue-20801.nll.stderr

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,37 @@ error[E0507]: cannot move out of borrowed content
22
--> $DIR/issue-20801.rs:36:22
33
|
44
LL | let a = unsafe { *mut_ref() };
5-
| ^^^^^^^^^^ cannot move out of borrowed content
5+
| ^^^^^^^^^^
6+
| |
7+
| cannot move out of borrowed content
8+
| help: consider using a reference instead: `&*mut_ref()`
69

710
error[E0507]: cannot move out of borrowed content
811
--> $DIR/issue-20801.rs:39:22
912
|
1013
LL | let b = unsafe { *imm_ref() };
11-
| ^^^^^^^^^^ cannot move out of borrowed content
14+
| ^^^^^^^^^^
15+
| |
16+
| cannot move out of borrowed content
17+
| help: consider using a reference instead: `&*imm_ref()`
1218

1319
error[E0507]: cannot move out of borrowed content
1420
--> $DIR/issue-20801.rs:42:22
1521
|
1622
LL | let c = unsafe { *mut_ptr() };
17-
| ^^^^^^^^^^ cannot move out of borrowed content
23+
| ^^^^^^^^^^
24+
| |
25+
| cannot move out of borrowed content
26+
| help: consider using a reference instead: `&*mut_ptr()`
1827

1928
error[E0507]: cannot move out of borrowed content
2029
--> $DIR/issue-20801.rs:45:22
2130
|
2231
LL | let d = unsafe { *const_ptr() };
23-
| ^^^^^^^^^^^^ cannot move out of borrowed content
32+
| ^^^^^^^^^^^^
33+
| |
34+
| cannot move out of borrowed content
35+
| help: consider using a reference instead: `&*const_ptr()`
2436

2537
error: aborting due to 4 previous errors
2638

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
error[E0507]: cannot move out of borrowed content
2+
--> $DIR/cannot-move-block-spans.rs:15:15
3+
|
4+
LL | let x = { *r }; //~ ERROR
5+
| ^^
6+
| |
7+
| cannot move out of borrowed content
8+
| help: consider removing this dereference operator: `r`
9+
10+
error[E0507]: cannot move out of borrowed content
11+
--> $DIR/cannot-move-block-spans.rs:16:22
12+
|
13+
LL | let y = unsafe { *r }; //~ ERROR
14+
| ^^
15+
| |
16+
| cannot move out of borrowed content
17+
| help: consider removing this dereference operator: `r`
18+
19+
error[E0507]: cannot move out of borrowed content
20+
--> $DIR/cannot-move-block-spans.rs:17:26
21+
|
22+
LL | let z = loop { break *r; }; //~ ERROR
23+
| ^^
24+
| |
25+
| cannot move out of borrowed content
26+
| help: consider removing this dereference operator: `r`
27+
28+
error[E0508]: cannot move out of type `[std::string::String; 2]`, a non-copy array
29+
--> $DIR/cannot-move-block-spans.rs:21:15
30+
|
31+
LL | let x = { arr[0] }; //~ ERROR
32+
| ^^^^^^
33+
| |
34+
| cannot move out of here
35+
| help: consider using a reference instead: `&arr[0]`
36+
37+
error[E0508]: cannot move out of type `[std::string::String; 2]`, a non-copy array
38+
--> $DIR/cannot-move-block-spans.rs:22:22
39+
|
40+
LL | let y = unsafe { arr[0] }; //~ ERROR
41+
| ^^^^^^
42+
| |
43+
| cannot move out of here
44+
| help: consider using a reference instead: `&arr[0]`
45+
46+
error[E0508]: cannot move out of type `[std::string::String; 2]`, a non-copy array
47+
--> $DIR/cannot-move-block-spans.rs:23:26
48+
|
49+
LL | let z = loop { break arr[0]; }; //~ ERROR
50+
| ^^^^^^
51+
| |
52+
| cannot move out of here
53+
| help: consider using a reference instead: `&arr[0]`
54+
55+
error[E0507]: cannot move out of borrowed content
56+
--> $DIR/cannot-move-block-spans.rs:27:38
57+
|
58+
LL | let x = { let mut u = 0; u += 1; *r }; //~ ERROR
59+
| ^^
60+
| |
61+
| cannot move out of borrowed content
62+
| help: consider removing this dereference operator: `r`
63+
64+
error[E0507]: cannot move out of borrowed content
65+
--> $DIR/cannot-move-block-spans.rs:28:45
66+
|
67+
LL | let y = unsafe { let mut u = 0; u += 1; *r }; //~ ERROR
68+
| ^^
69+
| |
70+
| cannot move out of borrowed content
71+
| help: consider removing this dereference operator: `r`
72+
73+
error[E0507]: cannot move out of borrowed content
74+
--> $DIR/cannot-move-block-spans.rs:29:49
75+
|
76+
LL | let z = loop { let mut u = 0; u += 1; break *r; u += 2; }; //~ ERROR
77+
| ^^
78+
| |
79+
| cannot move out of borrowed content
80+
| help: consider removing this dereference operator: `r`
81+
82+
error: aborting due to 9 previous errors
83+
84+
Some errors occurred: E0507, E0508.
85+
For more information about an error, try `rustc --explain E0507`.
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test that the we point to the inner expression when moving out to initialize
12+
// a variable, and that we don't give a useless suggestion such as &{ *r }.
13+
14+
pub fn deref(r: &String) {
15+
let x = { *r }; //~ ERROR
16+
let y = unsafe { *r }; //~ ERROR
17+
let z = loop { break *r; }; //~ ERROR
18+
}
19+
20+
pub fn index(arr: [String; 2]) {
21+
let x = { arr[0] }; //~ ERROR
22+
let y = unsafe { arr[0] }; //~ ERROR
23+
let z = loop { break arr[0]; }; //~ ERROR
24+
}
25+
26+
pub fn additional_statement_cases(r: &String) {
27+
let x = { let mut u = 0; u += 1; *r }; //~ ERROR
28+
let y = unsafe { let mut u = 0; u += 1; *r }; //~ ERROR
29+
let z = loop { let mut u = 0; u += 1; break *r; u += 2; }; //~ ERROR
30+
}
31+
32+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
error[E0507]: cannot move out of borrowed content
2+
--> $DIR/cannot-move-block-spans.rs:15:15
3+
|
4+
LL | let x = { *r }; //~ ERROR
5+
| ^^ cannot move out of borrowed content
6+
7+
error[E0507]: cannot move out of borrowed content
8+
--> $DIR/cannot-move-block-spans.rs:16:22
9+
|
10+
LL | let y = unsafe { *r }; //~ ERROR
11+
| ^^ cannot move out of borrowed content
12+
13+
error[E0507]: cannot move out of borrowed content
14+
--> $DIR/cannot-move-block-spans.rs:17:26
15+
|
16+
LL | let z = loop { break *r; }; //~ ERROR
17+
| ^^ cannot move out of borrowed content
18+
19+
error[E0508]: cannot move out of type `[std::string::String; 2]`, a non-copy array
20+
--> $DIR/cannot-move-block-spans.rs:21:15
21+
|
22+
LL | let x = { arr[0] }; //~ ERROR
23+
| ^^^^^^ cannot move out of here
24+
25+
error[E0508]: cannot move out of type `[std::string::String; 2]`, a non-copy array
26+
--> $DIR/cannot-move-block-spans.rs:22:22
27+
|
28+
LL | let y = unsafe { arr[0] }; //~ ERROR
29+
| ^^^^^^ cannot move out of here
30+
31+
error[E0508]: cannot move out of type `[std::string::String; 2]`, a non-copy array
32+
--> $DIR/cannot-move-block-spans.rs:23:26
33+
|
34+
LL | let z = loop { break arr[0]; }; //~ ERROR
35+
| ^^^^^^ cannot move out of here
36+
37+
error[E0507]: cannot move out of borrowed content
38+
--> $DIR/cannot-move-block-spans.rs:27:38
39+
|
40+
LL | let x = { let mut u = 0; u += 1; *r }; //~ ERROR
41+
| ^^ cannot move out of borrowed content
42+
43+
error[E0507]: cannot move out of borrowed content
44+
--> $DIR/cannot-move-block-spans.rs:28:45
45+
|
46+
LL | let y = unsafe { let mut u = 0; u += 1; *r }; //~ ERROR
47+
| ^^ cannot move out of borrowed content
48+
49+
error[E0507]: cannot move out of borrowed content
50+
--> $DIR/cannot-move-block-spans.rs:29:49
51+
|
52+
LL | let z = loop { let mut u = 0; u += 1; break *r; u += 2; }; //~ ERROR
53+
| ^^ cannot move out of borrowed content
54+
55+
error: aborting due to 9 previous errors
56+
57+
Some errors occurred: E0507, E0508.
58+
For more information about an error, try `rustc --explain E0507`.

0 commit comments

Comments
 (0)