Skip to content

Commit

Permalink
Change heuristic and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Jun 20, 2020
1 parent a10e07c commit 5faf657
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 9 deletions.
13 changes: 6 additions & 7 deletions src/librustc_mir/borrow_check/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,13 +776,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
}
}

let normal_ret = if let [ProjectionElem::Downcast(..), ProjectionElem::Field(_, _)] =
moved_place.projection
{
PatUse(stmt.source_info.span)
} else {
OtherUse(stmt.source_info.span)
};
let normal_ret =
if moved_place.projection.iter().any(|p| matches!(p, ProjectionElem::Downcast(..))) {
PatUse(stmt.source_info.span)
} else {
OtherUse(stmt.source_info.span)
};

// We are trying to find MIR of the form:
// ```
Expand Down
23 changes: 23 additions & 0 deletions src/test/ui/borrowck/move-in-pattern-mut.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Issue #63988
#[derive(Debug)]
struct S;
fn foo(_: Option<S>) {}

enum E {
V {
s: S,
}
}
fn bar(_: E) {}

fn main() {
let s = Some(S);
if let Some(mut x) = s {
x = S;
}
foo(s); //~ ERROR use of moved value: `s`
let mut e = E::V { s: S };
let E::V { s: mut x } = e;
x = S;
bar(e); //~ ERROR use of moved value: `e`
}
33 changes: 33 additions & 0 deletions src/test/ui/borrowck/move-in-pattern-mut.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
error[E0382]: use of moved value: `s`
--> $DIR/move-in-pattern-mut.rs:18:9
|
LL | if let Some(mut x) = s {
| ----- value moved here
...
LL | foo(s);
| ^ value used here after partial move
|
= note: move occurs because value has type `S`, which does not implement the `Copy` trait
help: borrow this field in the pattern to avoid moving `s.0`
|
LL | if let Some(ref mut x) = s {
| ^^^

error[E0382]: use of moved value: `e`
--> $DIR/move-in-pattern-mut.rs:22:9
|
LL | let E::V { s: mut x } = e;
| ----- value moved here
LL | x = S;
LL | bar(e);
| ^ value used here after partial move
|
= note: move occurs because value has type `S`, which does not implement the `Copy` trait
help: borrow this field in the pattern to avoid moving `e.s`
|
LL | let E::V { s: ref mut x } = e;
| ^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0382`.
11 changes: 11 additions & 0 deletions src/test/ui/borrowck/move-in-pattern.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@
struct S;
fn foo(_: Option<S>) {}

enum E {
V {
s: S,
}
}
fn bar(_: E) {}

fn main() {
let s = Some(S);
if let Some(ref x) = s {
let _ = x;
}
foo(s); //~ ERROR use of moved value: `s`
let e = E::V { s: S };
let E::V { s: ref x } = e;
let _ = x;
bar(e); //~ ERROR use of moved value: `e`
}
11 changes: 11 additions & 0 deletions src/test/ui/borrowck/move-in-pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@
struct S;
fn foo(_: Option<S>) {}

enum E {
V {
s: S,
}
}
fn bar(_: E) {}

fn main() {
let s = Some(S);
if let Some(x) = s {
let _ = x;
}
foo(s); //~ ERROR use of moved value: `s`
let e = E::V { s: S };
let E::V { s: x } = e;
let _ = x;
bar(e); //~ ERROR use of moved value: `e`
}
19 changes: 17 additions & 2 deletions src/test/ui/borrowck/move-in-pattern.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0382]: use of moved value: `s`
--> $DIR/move-in-pattern.rs:12:9
--> $DIR/move-in-pattern.rs:19:9
|
LL | if let Some(x) = s {
| - value moved here
Expand All @@ -13,6 +13,21 @@ help: borrow this field in the pattern to avoid moving `s.0`
LL | if let Some(ref x) = s {
| ^^^

error: aborting due to previous error
error[E0382]: use of moved value: `e`
--> $DIR/move-in-pattern.rs:23:9
|
LL | let E::V { s: x } = e;
| - value moved here
LL | let _ = x;
LL | bar(e);
| ^ value used here after partial move
|
= note: move occurs because value has type `S`, which does not implement the `Copy` trait
help: borrow this field in the pattern to avoid moving `e.s`
|
LL | let E::V { s: ref x } = e;
| ^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0382`.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ LL | consume(node) + r
| ^^^^ value used here after partial move
|
= note: move occurs because value has type `std::boxed::Box<List>`, which does not implement the `Copy` trait
help: borrow this field in the pattern to avoid moving `node.next.0`
|
LL | Some(ref right) => consume(right),
| ^^^

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ LL | a @ Some((mut b @ ref mut c, d @ ref e)) => {}
| value moved here
|
= note: move occurs because value has type `main::U`, which does not implement the `Copy` trait
help: borrow this field in the pattern to avoid moving the value
|
LL | a @ Some((ref mut b @ ref mut c, d @ ref e)) => {}
| ^^^

error[E0382]: use of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:61:38
Expand All @@ -379,6 +383,10 @@ LL | a @ Some((mut b @ ref mut c, d @ ref e)) => {}
| value moved here
|
= note: move occurs because value has type `main::U`, which does not implement the `Copy` trait
help: borrow this field in the pattern to avoid moving the value
|
LL | a @ Some((mut b @ ref mut c, ref d @ ref e)) => {}
| ^^^

error[E0382]: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:71:30
Expand Down Expand Up @@ -412,6 +420,10 @@ LL | a @ Some((mut b @ ref mut c, d @ ref e)) => {}
| value moved here
|
= note: move occurs because value has type `main::U`, which does not implement the `Copy` trait
help: borrow this field in the pattern to avoid moving the value
|
LL | a @ Some((ref mut b @ ref mut c, d @ ref e)) => {}
| ^^^

error[E0382]: use of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:83:38
Expand All @@ -434,6 +446,10 @@ LL | a @ Some((mut b @ ref mut c, d @ ref e)) => {}
| value moved here
|
= note: move occurs because value has type `main::U`, which does not implement the `Copy` trait
help: borrow this field in the pattern to avoid moving the value
|
LL | a @ Some((mut b @ ref mut c, ref d @ ref e)) => {}
| ^^^

error[E0382]: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:93:30
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/ref-suggestion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ LL | x;
| ^ value used here after partial move
|
= note: move occurs because value has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait
help: borrow this field in the pattern to avoid moving `x.0.0`
|
LL | (Some(ref y), ()) => {},
| ^^^

error: aborting due to 3 previous errors

Expand Down

0 comments on commit 5faf657

Please sign in to comment.