Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions compiler/rustc_borrowck/src/diagnostics/move_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,30 +990,25 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
let bind_to = &self.body.local_decls[*local];
let binding_span = bind_to.source_info.span;

if j == 0 {
err.span_label(binding_span, "data moved here");
} else {
err.span_label(binding_span, "...and here");
}

if binds_to.len() == 1 {
let place_desc = self.local_name(*local).map(|sym| format!("`{sym}`"));

err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::LabelMovedHere {
ty: bind_to.ty,
place: place_desc.as_deref().unwrap_or("the place"),
span: binding_span,
});

if !desugar_spans.contains(&binding_span)
&& let Some(expr) = self.find_expr(binding_span)
{
// The binding_span doesn't correspond to a let binding desugaring
// and is an expression where calling `.clone()` would be valid.
let local_place: PlaceRef<'tcx> = (*local).into();
self.suggest_cloning(err, local_place, bind_to.ty, expr, None);
}

err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
is_partial_move: false,
ty: bind_to.ty,
place: place_desc.as_deref().unwrap_or("the place"),
span: binding_span,
});
} else if j == 0 {
err.span_label(binding_span, "data moved here");
} else {
err.span_label(binding_span, "...and here");
}
}

Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_borrowck/src/session_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,16 @@ pub(crate) enum TypeNoCopy<'a, 'tcx> {
#[primary_span]
span: Span,
},
#[label(
"data moved here because {$place} has type `{$ty}`, which does not implement the `Copy` \
trait"
)]
LabelMovedHere {
ty: Ty<'tcx>,
place: &'a str,
#[primary_span]
span: Span,
},
#[note(
"{$is_partial_move ->
[true] partial move
Expand Down
5 changes: 1 addition & 4 deletions tests/ui/borrowck/access-mode-in-closures.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ error[E0507]: cannot move out of `s` which is behind a shared reference
--> $DIR/access-mode-in-closures.rs:8:15
|
LL | match *s { S(v) => v }
| ^^ -
| |
| data moved here
| move occurs because `v` has type `Vec<isize>`, which does not implement the `Copy` trait
| ^^ - data moved here because `v` has type `Vec<isize>`, which does not implement the `Copy` trait
|
help: consider removing the dereference here
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ error[E0507]: cannot move out of a shared reference
LL | for &a in x.iter() {
| - ^^^^^^^^
| |
| data moved here
| move occurs because `a` has type `&mut i32`, which does not implement the `Copy` trait
| data moved here because `a` has type `&mut i32`, which does not implement the `Copy` trait
|
help: consider removing the borrow
|
Expand All @@ -19,8 +18,7 @@ error[E0507]: cannot move out of a shared reference
LL | for &a in &f.a {
| - ^^^^
| |
| data moved here
| move occurs because `a` has type `Box<isize>`, which does not implement the `Copy` trait
| data moved here because `a` has type `Box<isize>`, which does not implement the `Copy` trait
|
help: consider removing the borrow
|
Expand All @@ -34,8 +32,7 @@ error[E0507]: cannot move out of a shared reference
LL | for &a in x.iter() {
| - ^^^^^^^^
| |
| data moved here
| move occurs because `a` has type `Box<i32>`, which does not implement the `Copy` trait
| data moved here because `a` has type `Box<i32>`, which does not implement the `Copy` trait
|
help: consider removing the borrow
|
Expand Down
5 changes: 1 addition & 4 deletions tests/ui/borrowck/borrowck-move-error-with-note.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ error[E0507]: cannot move out of `a.a` which is behind a shared reference
LL | match a.a {
| ^^^
LL | n => {
| -
| |
| data moved here
| move occurs because `n` has type `Box<isize>`, which does not implement the `Copy` trait
| - data moved here because `n` has type `Box<isize>`, which does not implement the `Copy` trait
|
help: consider borrowing here
|
Expand Down
9 changes: 3 additions & 6 deletions tests/ui/borrowck/borrowck-move-in-irrefut-pat.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ error[E0507]: cannot move out of a shared reference
LL | fn arg_item(&_x: &String) {}
| ^--
| |
| data moved here
| move occurs because `_x` has type `String`, which does not implement the `Copy` trait
| data moved here because `_x` has type `String`, which does not implement the `Copy` trait
|
help: consider removing the borrow
|
Expand All @@ -19,8 +18,7 @@ error[E0507]: cannot move out of a shared reference
LL | with(|&_x| ())
| ^--
| |
| data moved here
| move occurs because `_x` has type `String`, which does not implement the `Copy` trait
| data moved here because `_x` has type `String`, which does not implement the `Copy` trait
|
help: consider removing the borrow
|
Expand All @@ -34,8 +32,7 @@ error[E0507]: cannot move out of a shared reference
LL | let &_x = &"hi".to_string();
| -- ^^^^^^^^^^^^^^^^^
| |
| data moved here
| move occurs because `_x` has type `String`, which does not implement the `Copy` trait
| data moved here because `_x` has type `String`, which does not implement the `Copy` trait
|
help: consider removing the borrow
|
Expand Down
11 changes: 3 additions & 8 deletions tests/ui/borrowck/borrowck-move-out-of-struct-with-dtor.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ LL | match (S {f:"foo".to_string()}) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cannot move out of here
LL |
LL | S {f:_s} => {}
| --
| |
| data moved here
| move occurs because `_s` has type `String`, which does not implement the `Copy` trait
| -- data moved here because `_s` has type `String`, which does not implement the `Copy` trait
|
help: consider borrowing the pattern binding
|
Expand All @@ -21,8 +18,7 @@ error[E0509]: cannot move out of type `S`, which implements the `Drop` trait
LL | let S {f:_s} = S {f:"foo".to_string()};
| -- ^^^^^^^^^^^^^^^^^^^^^^^ cannot move out of here
| |
| data moved here
| move occurs because `_s` has type `String`, which does not implement the `Copy` trait
| data moved here because `_s` has type `String`, which does not implement the `Copy` trait
|
help: consider borrowing the pattern binding
|
Expand All @@ -35,8 +31,7 @@ error[E0509]: cannot move out of type `S`, which implements the `Drop` trait
LL | fn move_in_fn_arg(S {f:_s}: S) {
| ^^^^^--^
| | |
| | data moved here
| | move occurs because `_s` has type `String`, which does not implement the `Copy` trait
| | data moved here because `_s` has type `String`, which does not implement the `Copy` trait
| cannot move out of here
|
help: consider borrowing the pattern binding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ LL | match S("foo".to_string()) {
| ^^^^^^^^^^^^^^^^^^^^ cannot move out of here
LL |
LL | S(_s) => {}
| --
| |
| data moved here
| move occurs because `_s` has type `String`, which does not implement the `Copy` trait
| -- data moved here because `_s` has type `String`, which does not implement the `Copy` trait
|
help: consider borrowing the pattern binding
|
Expand All @@ -21,8 +18,7 @@ error[E0509]: cannot move out of type `S`, which implements the `Drop` trait
LL | let S(_s) = S("foo".to_string());
| -- ^^^^^^^^^^^^^^^^^^^^ cannot move out of here
| |
| data moved here
| move occurs because `_s` has type `String`, which does not implement the `Copy` trait
| data moved here because `_s` has type `String`, which does not implement the `Copy` trait
|
help: consider borrowing the pattern binding
|
Expand All @@ -35,8 +31,7 @@ error[E0509]: cannot move out of type `S`, which implements the `Drop` trait
LL | fn move_in_fn_arg(S(_s): S) {
| ^^--^
| | |
| | data moved here
| | move occurs because `_s` has type `String`, which does not implement the `Copy` trait
| | data moved here because `_s` has type `String`, which does not implement the `Copy` trait
| cannot move out of here
|
help: consider borrowing the pattern binding
Expand Down
6 changes: 2 additions & 4 deletions tests/ui/borrowck/borrowck-vec-pattern-nesting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ fn c() {
//~^ ERROR cannot move out
//~| NOTE cannot move out
&mut [_a,
//~^ NOTE data moved here
//~| NOTE move occurs because `_a` has type
//~^ NOTE data moved here because `_a` has type
//~| HELP consider removing the mutable borrow
..
] => {
Expand All @@ -59,8 +58,7 @@ fn d() {
&mut [
//~^ HELP consider removing the mutable borrow
_b] => {}
//~^ NOTE data moved here
//~| NOTE move occurs because `_b` has type
//~^ NOTE data moved here because `_b` has type
_ => {}
}
let a = vec[0]; //~ ERROR cannot move out
Expand Down
20 changes: 7 additions & 13 deletions tests/ui/borrowck/borrowck-vec-pattern-nesting.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ LL | match vec {
| ^^^ cannot move out of here
...
LL | &mut [_a,
| --
| |
| data moved here
| move occurs because `_a` has type `Box<isize>`, which does not implement the `Copy` trait
| -- data moved here because `_a` has type `Box<isize>`, which does not implement the `Copy` trait
|
help: consider removing the mutable borrow
|
Expand All @@ -41,7 +38,7 @@ LL + [_a,
|

error[E0508]: cannot move out of type `[Box<isize>]`, a non-copy slice
--> $DIR/borrowck-vec-pattern-nesting.rs:46:13
--> $DIR/borrowck-vec-pattern-nesting.rs:45:13
|
LL | let a = vec[0];
| ^^^^^^
Expand All @@ -59,16 +56,13 @@ LL | let a = vec[0].clone();
| ++++++++

error[E0508]: cannot move out of type `[Box<isize>]`, a non-copy slice
--> $DIR/borrowck-vec-pattern-nesting.rs:56:11
--> $DIR/borrowck-vec-pattern-nesting.rs:55:11
|
LL | match vec {
| ^^^ cannot move out of here
...
LL | _b] => {}
| --
| |
| data moved here
| move occurs because `_b` has type `Box<isize>`, which does not implement the `Copy` trait
| -- data moved here because `_b` has type `Box<isize>`, which does not implement the `Copy` trait
|
help: consider removing the mutable borrow
|
Expand All @@ -77,7 +71,7 @@ LL + [
|

error[E0508]: cannot move out of type `[Box<isize>]`, a non-copy slice
--> $DIR/borrowck-vec-pattern-nesting.rs:66:13
--> $DIR/borrowck-vec-pattern-nesting.rs:64:13
|
LL | let a = vec[0];
| ^^^^^^
Expand All @@ -95,7 +89,7 @@ LL | let a = vec[0].clone();
| ++++++++

error[E0508]: cannot move out of type `[Box<isize>]`, a non-copy slice
--> $DIR/borrowck-vec-pattern-nesting.rs:76:11
--> $DIR/borrowck-vec-pattern-nesting.rs:74:11
|
LL | match vec {
| ^^^ cannot move out of here
Expand All @@ -114,7 +108,7 @@ LL + [_a, _b, _c] => {}
|

error[E0508]: cannot move out of type `[Box<isize>]`, a non-copy slice
--> $DIR/borrowck-vec-pattern-nesting.rs:87:13
--> $DIR/borrowck-vec-pattern-nesting.rs:85:13
|
LL | let a = vec[0];
| ^^^^^^
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/borrowck/issue-51301.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ error[E0507]: cannot move out of a shared reference
LL | .find(|(&event_type, _)| event == event_type)
| ^^----------^^^^
| |
| data moved here
| move occurs because `event_type` has type `EventType`, which does not implement the `Copy` trait
| data moved here because `event_type` has type `EventType`, which does not implement the `Copy` trait
|
help: consider borrowing the pattern binding
|
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/borrowck/issue-51415.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ error[E0507]: cannot move out of a shared reference
LL | let opt = a.iter().enumerate().find(|(_, &s)| {
| ^^^^^-^
| |
| data moved here
| move occurs because `s` has type `String`, which does not implement the `Copy` trait
| data moved here because `s` has type `String`, which does not implement the `Copy` trait
|
help: consider borrowing the pattern binding
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ error[E0509]: cannot move out of type `X`, which implements the `Drop` trait
LL | let X { x: y } = x;
| - ^ cannot move out of here
| |
| data moved here
| move occurs because `y` has type `String`, which does not implement the `Copy` trait
| data moved here because `y` has type `String`, which does not implement the `Copy` trait
|
help: consider borrowing the pattern binding
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ LL | match x {
| ^ cannot move out of here
LL |
LL | X { x: y } => println!("contents: {}", y)
| -
| |
| data moved here
| move occurs because `y` has type `String`, which does not implement the `Copy` trait
| - data moved here because `y` has type `String`, which does not implement the `Copy` trait
|
help: consider borrowing the pattern binding
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ error[E0507]: cannot move out of a shared reference
LL | let _ = HashMap::<String, i32>::new().iter().filter(|&(&_k, &_v)| { true });
| ^^^--^^^^^^
| |
| data moved here
| move occurs because `_k` has type `String`, which does not implement the `Copy` trait
| data moved here because `_k` has type `String`, which does not implement the `Copy` trait
|
help: consider removing the borrow
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ error[E0509]: cannot move out of type `Thing`, which implements the `Drop` trait
LL | Thing(*&mut String::new()) = Thing(String::new());
| ------------------- ^^^^^^^^^^^^^^^^^^^^ cannot move out of here
| |
| data moved here
| move occurs because the place has type `String`, which does not implement the `Copy` trait
| data moved here because the place has type `String`, which does not implement the `Copy` trait

error: aborting due to 1 previous error

Expand Down
5 changes: 1 addition & 4 deletions tests/ui/moves/issue-99470-move-out-of-some.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ LL | match x {
| ^
LL |
LL | &Some(_y) => (),
| --
| |
| data moved here
| move occurs because `_y` has type `Box<i32>`, which does not implement the `Copy` trait
| -- data moved here because `_y` has type `Box<i32>`, which does not implement the `Copy` trait
|
help: consider removing the borrow
|
Expand Down
Loading
Loading