Use better spans for cannot-move errors

This commit is contained in:
Matthew Jasper 2018-07-22 21:38:31 +01:00
parent 71637c2937
commit b32caef1ad
5 changed files with 210 additions and 28 deletions

View File

@ -87,6 +87,10 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
cannot_move_out_of: IllegalMoveOrigin { location, kind },
} => {
let stmt_source_info = self.mir.source_info(location);
// Note: that the only time we assign a place isn't a temporary
// to a user variable is when initializing it.
// If that ever stops being the case, then the ever initialized
// flow could be used.
if let Some(StatementKind::Assign(
Place::Local(local),
Rvalue::Use(Operand::Move(move_from)),
@ -109,26 +113,16 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
opt_ty_info: _,
}))) = local_decl.is_user_variable
{
// HACK use scopes to determine if this assignment is
// the initialization of a variable.
// FIXME(matthewjasper) This would probably be more
// reliable if it used the ever initialized dataflow
// but move errors are currently reported before the
// rest of borrowck has run.
if self
.mir
.is_sub_scope(local_decl.source_info.scope, stmt_source_info.scope)
{
self.append_binding_error(
grouped_errors,
kind,
move_from,
*local,
opt_match_place,
match_span,
);
return;
}
self.append_binding_error(
grouped_errors,
kind,
move_from,
*local,
opt_match_place,
match_span,
stmt_source_info.span,
);
return;
}
}
grouped_errors.push(GroupedMoveError::OtherIllegalMove {
@ -147,6 +141,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
bind_to: Local,
match_place: &Option<Place<'tcx>>,
match_span: Span,
statement_span: Span,
) {
debug!(
"append_to_grouped_errors(match_place={:?}, match_span={:?})",
@ -173,13 +168,13 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
debug!("found a new move error location");
// Don't need to point to x in let x = ... .
let binds_to = if from_simple_let {
vec![]
let (binds_to, span) = if from_simple_let {
(vec![], statement_span)
} else {
vec![bind_to]
(vec![bind_to], match_span)
};
grouped_errors.push(GroupedMoveError::MovesFromMatchPlace {
span: match_span,
span,
move_from: match_place.clone(),
kind,
binds_to,

View File

@ -2,25 +2,37 @@ error[E0507]: cannot move out of borrowed content
--> $DIR/issue-20801.rs:36:22
|
LL | let a = unsafe { *mut_ref() };
| ^^^^^^^^^^ cannot move out of borrowed content
| ^^^^^^^^^^
| |
| cannot move out of borrowed content
| help: consider using a reference instead: `&*mut_ref()`
error[E0507]: cannot move out of borrowed content
--> $DIR/issue-20801.rs:39:22
|
LL | let b = unsafe { *imm_ref() };
| ^^^^^^^^^^ cannot move out of borrowed content
| ^^^^^^^^^^
| |
| cannot move out of borrowed content
| help: consider using a reference instead: `&*imm_ref()`
error[E0507]: cannot move out of borrowed content
--> $DIR/issue-20801.rs:42:22
|
LL | let c = unsafe { *mut_ptr() };
| ^^^^^^^^^^ cannot move out of borrowed content
| ^^^^^^^^^^
| |
| cannot move out of borrowed content
| help: consider using a reference instead: `&*mut_ptr()`
error[E0507]: cannot move out of borrowed content
--> $DIR/issue-20801.rs:45:22
|
LL | let d = unsafe { *const_ptr() };
| ^^^^^^^^^^^^ cannot move out of borrowed content
| ^^^^^^^^^^^^
| |
| cannot move out of borrowed content
| help: consider using a reference instead: `&*const_ptr()`
error: aborting due to 4 previous errors

View File

@ -0,0 +1,85 @@
error[E0507]: cannot move out of borrowed content
--> $DIR/cannot-move-block-spans.rs:15:15
|
LL | let x = { *r }; //~ ERROR
| ^^
| |
| cannot move out of borrowed content
| help: consider removing this dereference operator: `r`
error[E0507]: cannot move out of borrowed content
--> $DIR/cannot-move-block-spans.rs:16:22
|
LL | let y = unsafe { *r }; //~ ERROR
| ^^
| |
| cannot move out of borrowed content
| help: consider removing this dereference operator: `r`
error[E0507]: cannot move out of borrowed content
--> $DIR/cannot-move-block-spans.rs:17:26
|
LL | let z = loop { break *r; }; //~ ERROR
| ^^
| |
| cannot move out of borrowed content
| help: consider removing this dereference operator: `r`
error[E0508]: cannot move out of type `[std::string::String; 2]`, a non-copy array
--> $DIR/cannot-move-block-spans.rs:21:15
|
LL | let x = { arr[0] }; //~ ERROR
| ^^^^^^
| |
| cannot move out of here
| help: consider using a reference instead: `&arr[0]`
error[E0508]: cannot move out of type `[std::string::String; 2]`, a non-copy array
--> $DIR/cannot-move-block-spans.rs:22:22
|
LL | let y = unsafe { arr[0] }; //~ ERROR
| ^^^^^^
| |
| cannot move out of here
| help: consider using a reference instead: `&arr[0]`
error[E0508]: cannot move out of type `[std::string::String; 2]`, a non-copy array
--> $DIR/cannot-move-block-spans.rs:23:26
|
LL | let z = loop { break arr[0]; }; //~ ERROR
| ^^^^^^
| |
| cannot move out of here
| help: consider using a reference instead: `&arr[0]`
error[E0507]: cannot move out of borrowed content
--> $DIR/cannot-move-block-spans.rs:27:38
|
LL | let x = { let mut u = 0; u += 1; *r }; //~ ERROR
| ^^
| |
| cannot move out of borrowed content
| help: consider removing this dereference operator: `r`
error[E0507]: cannot move out of borrowed content
--> $DIR/cannot-move-block-spans.rs:28:45
|
LL | let y = unsafe { let mut u = 0; u += 1; *r }; //~ ERROR
| ^^
| |
| cannot move out of borrowed content
| help: consider removing this dereference operator: `r`
error[E0507]: cannot move out of borrowed content
--> $DIR/cannot-move-block-spans.rs:29:49
|
LL | let z = loop { let mut u = 0; u += 1; break *r; u += 2; }; //~ ERROR
| ^^
| |
| cannot move out of borrowed content
| help: consider removing this dereference operator: `r`
error: aborting due to 9 previous errors
Some errors occurred: E0507, E0508.
For more information about an error, try `rustc --explain E0507`.

View File

@ -0,0 +1,32 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Test that the we point to the inner expression when moving out to initialize
// a variable, and that we don't give a useless suggestion such as &{ *r }.
pub fn deref(r: &String) {
let x = { *r }; //~ ERROR
let y = unsafe { *r }; //~ ERROR
let z = loop { break *r; }; //~ ERROR
}
pub fn index(arr: [String; 2]) {
let x = { arr[0] }; //~ ERROR
let y = unsafe { arr[0] }; //~ ERROR
let z = loop { break arr[0]; }; //~ ERROR
}
pub fn additional_statement_cases(r: &String) {
let x = { let mut u = 0; u += 1; *r }; //~ ERROR
let y = unsafe { let mut u = 0; u += 1; *r }; //~ ERROR
let z = loop { let mut u = 0; u += 1; break *r; u += 2; }; //~ ERROR
}
fn main() {}

View File

@ -0,0 +1,58 @@
error[E0507]: cannot move out of borrowed content
--> $DIR/cannot-move-block-spans.rs:15:15
|
LL | let x = { *r }; //~ ERROR
| ^^ cannot move out of borrowed content
error[E0507]: cannot move out of borrowed content
--> $DIR/cannot-move-block-spans.rs:16:22
|
LL | let y = unsafe { *r }; //~ ERROR
| ^^ cannot move out of borrowed content
error[E0507]: cannot move out of borrowed content
--> $DIR/cannot-move-block-spans.rs:17:26
|
LL | let z = loop { break *r; }; //~ ERROR
| ^^ cannot move out of borrowed content
error[E0508]: cannot move out of type `[std::string::String; 2]`, a non-copy array
--> $DIR/cannot-move-block-spans.rs:21:15
|
LL | let x = { arr[0] }; //~ ERROR
| ^^^^^^ cannot move out of here
error[E0508]: cannot move out of type `[std::string::String; 2]`, a non-copy array
--> $DIR/cannot-move-block-spans.rs:22:22
|
LL | let y = unsafe { arr[0] }; //~ ERROR
| ^^^^^^ cannot move out of here
error[E0508]: cannot move out of type `[std::string::String; 2]`, a non-copy array
--> $DIR/cannot-move-block-spans.rs:23:26
|
LL | let z = loop { break arr[0]; }; //~ ERROR
| ^^^^^^ cannot move out of here
error[E0507]: cannot move out of borrowed content
--> $DIR/cannot-move-block-spans.rs:27:38
|
LL | let x = { let mut u = 0; u += 1; *r }; //~ ERROR
| ^^ cannot move out of borrowed content
error[E0507]: cannot move out of borrowed content
--> $DIR/cannot-move-block-spans.rs:28:45
|
LL | let y = unsafe { let mut u = 0; u += 1; *r }; //~ ERROR
| ^^ cannot move out of borrowed content
error[E0507]: cannot move out of borrowed content
--> $DIR/cannot-move-block-spans.rs:29:49
|
LL | let z = loop { let mut u = 0; u += 1; break *r; u += 2; }; //~ ERROR
| ^^ cannot move out of borrowed content
error: aborting due to 9 previous errors
Some errors occurred: E0507, E0508.
For more information about an error, try `rustc --explain E0507`.