Account for x @ y and suggest ref x @ ref y

This commit is contained in:
Esteban Küber 2022-11-15 09:01:20 -08:00
parent 3c905d4ccd
commit 3a471b5fd8
13 changed files with 362 additions and 30 deletions

View File

@ -315,6 +315,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
expr_span: Span,
expr: Option<&'hir hir::Expr<'hir>>,
pat: Option<&'hir hir::Pat<'hir>>,
parent_pat: Option<&'hir hir::Pat<'hir>>,
}
impl<'hir> Visitor<'hir> for ExpressionFinder<'hir> {
fn visit_expr(&mut self, e: &'hir hir::Expr<'hir>) {
@ -327,11 +328,20 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
if p.span == self.expr_span {
self.pat = Some(p);
}
if let hir::PatKind::Binding(hir::BindingAnnotation::NONE, _, i, _) = p.kind
&& i.span == self.expr_span
{
if let hir::PatKind::Binding(hir::BindingAnnotation::NONE, _, i, sub) = p.kind {
if i.span == self.expr_span || p.span == self.expr_span {
self.pat = Some(p);
}
// Check if we are in a situation of `ident @ ident` where we want to suggest
// `ref ident @ ref ident` or `ref ident @ Struct { ref ident }`.
if let Some(subpat) = sub && self.pat.is_none() {
self.visit_pat(subpat);
if self.pat.is_some() {
self.parent_pat = Some(p);
}
return;
}
}
hir::intravisit::walk_pat(self, p);
}
}
@ -349,6 +359,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
expr_span: move_span,
expr: None,
pat: None,
parent_pat: None,
};
finder.visit_expr(expr);
if let Some(span) = span && let Some(expr) = finder.expr {
@ -414,7 +425,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
span,
format!(
"consider changing this parameter type in {descr} `{ident}` to \
borrow instead if ownering the value isn't necessary",
borrow instead if owning the value isn't necessary",
),
);
}
@ -434,10 +445,13 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
}
if let Some(pat) = finder.pat {
*in_pattern = true;
err.span_suggestion_verbose(
pat.span.shrink_to_lo(),
let mut sugg = vec![(pat.span.shrink_to_lo(), "ref ".to_string())];
if let Some(pat) = finder.parent_pat {
sugg.insert(0, (pat.span.shrink_to_lo(), "ref ".to_string()));
}
err.multipart_suggestion_verbose(
"borrow this binding in the pattern to avoid moving the value",
"ref ".to_string(),
sugg,
Applicability::MachineApplicable,
);
}

View File

@ -1044,11 +1044,19 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_, '_>, pat: &Pa
name,
typeck_results.node_type(pat.hir_id),
);
sess.struct_span_err(pat.span, "borrow of moved value")
.span_label(binding_span, format!("value moved into `{}` here", name))
let mut err = sess.struct_span_err(pat.span, "borrow of moved value");
err.span_label(binding_span, format!("value moved into `{}` here", name))
.span_label(binding_span, occurs_because)
.span_labels(conflicts_ref, "value borrowed here after move")
.emit();
.span_labels(conflicts_ref, "value borrowed here after move");
if pat.span.contains(binding_span) {
err.span_suggestion_verbose(
binding_span.shrink_to_lo(),
"borrow this binding in the pattern to avoid moving the value",
"ref ".to_string(),
Applicability::MachineApplicable,
);
}
err.emit();
}
return;
}

View File

@ -16,6 +16,11 @@ LL | Some(_z @ ref _y) => {}
| | value borrowed here after move
| value moved into `_z` here
| move occurs because `_z` has type `X` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | Some(ref _z @ ref _y) => {}
| +++
error: cannot move out of value because it is borrowed
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:26:14
@ -35,6 +40,11 @@ LL | Some(_z @ ref mut _y) => {}
| | value borrowed here after move
| value moved into `_z` here
| move occurs because `_z` has type `X` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | Some(ref _z @ ref mut _y) => {}
| +++
error[E0382]: borrow of moved value
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:12:14

View File

@ -9,8 +9,8 @@ LL | let a @ b = U;
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let a @ ref b = U;
| +++
LL | let ref a @ ref b = U;
| +++ +++
error[E0382]: use of partially moved value
--> $DIR/borrowck-move-and-move.rs:13:9
@ -23,8 +23,8 @@ LL | let a @ (b, c) = (U, U);
= note: partial move occurs because value has type `U`, which does not implement the `Copy` trait
help: borrow this binding in the pattern to avoid moving the value
|
LL | let a @ (b, ref c) = (U, U);
| +++
LL | let ref a @ (b, ref c) = (U, U);
| +++ +++
error[E0382]: use of partially moved value
--> $DIR/borrowck-move-and-move.rs:15:9
@ -37,8 +37,8 @@ LL | let a @ (b, c) = (u(), u());
= note: partial move occurs because value has type `U`, which does not implement the `Copy` trait
help: borrow this binding in the pattern to avoid moving the value
|
LL | let a @ (b, ref c) = (u(), u());
| +++
LL | let ref a @ (b, ref c) = (u(), u());
| +++ +++
error[E0382]: use of moved value
--> $DIR/borrowck-move-and-move.rs:18:16
@ -81,8 +81,8 @@ LL | xs @ [a, .., b] => {}
= note: partial move occurs because value has type `U`, which does not implement the `Copy` trait
help: borrow this binding in the pattern to avoid moving the value
|
LL | xs @ [a, .., ref b] => {}
| +++
LL | ref xs @ [a, .., ref b] => {}
| +++ +++
error[E0382]: use of partially moved value
--> $DIR/borrowck-move-and-move.rs:29:9
@ -95,8 +95,8 @@ LL | xs @ [_, ys @ .., _] => {}
= note: partial move occurs because value has type `U`, which does not implement the `Copy` trait
help: borrow this binding in the pattern to avoid moving the value
|
LL | xs @ [_, ref ys @ .., _] => {}
| +++
LL | ref xs @ [_, ref ys @ .., _] => {}
| +++ +++
error[E0382]: use of moved value
--> $DIR/borrowck-move-and-move.rs:22:12

View File

@ -7,6 +7,11 @@ LL | let a @ ref b = U;
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `U` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ref a @ ref b = U;
| +++
error: aborting due to previous error

View File

@ -7,6 +7,11 @@ LL | let a @ ref b = U;
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `U` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ref a @ ref b = U;
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:24:9
@ -18,6 +23,11 @@ LL | let a @ (mut b @ ref mut c, d @ ref e) = (U, U);
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `(U, U)` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ref a @ (mut b @ ref mut c, d @ ref e) = (U, U);
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:24:14
@ -28,6 +38,11 @@ LL | let a @ (mut b @ ref mut c, d @ ref e) = (U, U);
| | value borrowed here after move
| value moved into `b` here
| move occurs because `b` has type `U` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let a @ (ref mut b @ ref mut c, d @ ref e) = (U, U);
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:24:33
@ -38,6 +53,11 @@ LL | let a @ (mut b @ ref mut c, d @ ref e) = (U, U);
| | value borrowed here after move
| value moved into `d` here
| move occurs because `d` has type `U` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let a @ (mut b @ ref mut c, ref d @ ref e) = (U, U);
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:29:9
@ -49,6 +69,11 @@ LL | let a @ [ref mut b, ref c] = [U, U];
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `[U; 2]` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ref a @ [ref mut b, ref c] = [U, U];
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:31:9
@ -59,6 +84,11 @@ LL | let a @ ref b = u();
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `U` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ref a @ ref b = u();
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:33:9
@ -70,6 +100,11 @@ LL | let a @ (mut b @ ref mut c, d @ ref e) = (u(), u());
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `(U, U)` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ref a @ (mut b @ ref mut c, d @ ref e) = (u(), u());
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:33:14
@ -80,6 +115,11 @@ LL | let a @ (mut b @ ref mut c, d @ ref e) = (u(), u());
| | value borrowed here after move
| value moved into `b` here
| move occurs because `b` has type `U` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let a @ (ref mut b @ ref mut c, d @ ref e) = (u(), u());
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:33:33
@ -90,6 +130,11 @@ LL | let a @ (mut b @ ref mut c, d @ ref e) = (u(), u());
| | value borrowed here after move
| value moved into `d` here
| move occurs because `d` has type `U` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let a @ (mut b @ ref mut c, ref d @ ref e) = (u(), u());
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:38:9
@ -101,6 +146,11 @@ LL | let a @ [ref mut b, ref c] = [u(), u()];
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `[U; 2]` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ref a @ [ref mut b, ref c] = [u(), u()];
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:42:9
@ -111,6 +161,11 @@ LL | a @ Some(ref b) => {}
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `Option<U>` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | ref a @ Some(ref b) => {}
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:47:9
@ -122,6 +177,11 @@ LL | a @ Some((mut b @ ref mut c, d @ ref e)) => {}
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `Option<(U, U)>` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | ref a @ Some((mut b @ ref mut c, d @ ref e)) => {}
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:47:19
@ -132,6 +192,11 @@ LL | a @ Some((mut b @ ref mut c, d @ ref e)) => {}
| | value borrowed here after move
| value moved into `b` here
| move occurs because `b` has type `U` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | a @ Some((ref mut b @ ref mut c, d @ ref e)) => {}
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:47:38
@ -142,6 +207,11 @@ LL | a @ Some((mut b @ ref mut c, d @ ref e)) => {}
| | value borrowed here after move
| value moved into `d` here
| move occurs because `d` has type `U` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | a @ Some((mut b @ ref mut c, ref d @ ref e)) => {}
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:55:9
@ -153,6 +223,11 @@ LL | mut a @ Some([ref b, ref mut c]) => {}
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `Option<[U; 2]>` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | ref mut a @ Some([ref b, ref mut c]) => {}
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:61:9
@ -163,6 +238,11 @@ LL | a @ Some(ref b) => {}
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `Option<U>` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | ref a @ Some(ref b) => {}
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:67:9
@ -174,6 +254,11 @@ LL | a @ Some((mut b @ ref mut c, d @ ref e)) => {}
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `Option<(U, U)>` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | ref a @ Some((mut b @ ref mut c, d @ ref e)) => {}
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:67:19
@ -184,6 +269,11 @@ LL | a @ Some((mut b @ ref mut c, d @ ref e)) => {}
| | value borrowed here after move
| value moved into `b` here
| move occurs because `b` has type `U` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | a @ Some((ref mut b @ ref mut c, d @ ref e)) => {}
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:67:38
@ -194,6 +284,11 @@ LL | a @ Some((mut b @ ref mut c, d @ ref e)) => {}
| | value borrowed here after move
| value moved into `d` here
| move occurs because `d` has type `U` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | a @ Some((mut b @ ref mut c, ref d @ ref e)) => {}
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:75:9
@ -205,6 +300,11 @@ LL | mut a @ Some([ref b, ref mut c]) => {}
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `Option<[U; 2]>` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | ref mut a @ Some([ref b, ref mut c]) => {}
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:11:11
@ -215,6 +315,11 @@ LL | fn f1(a @ ref b: U) {}
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `U` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | fn f1(ref a @ ref b: U) {}
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:14:11
@ -226,6 +331,11 @@ LL | fn f2(mut a @ (b @ ref c, mut d @ ref e): (U, U)) {}
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `(U, U)` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | fn f2(ref mut a @ (b @ ref c, mut d @ ref e): (U, U)) {}
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:14:20
@ -236,6 +346,11 @@ LL | fn f2(mut a @ (b @ ref c, mut d @ ref e): (U, U)) {}
| | value borrowed here after move
| value moved into `b` here
| move occurs because `b` has type `U` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | fn f2(mut a @ (ref b @ ref c, mut d @ ref e): (U, U)) {}
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:14:31
@ -246,6 +361,11 @@ LL | fn f2(mut a @ (b @ ref c, mut d @ ref e): (U, U)) {}
| | value borrowed here after move
| value moved into `d` here
| move occurs because `d` has type `U` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | fn f2(mut a @ (b @ ref c, ref mut d @ ref e): (U, U)) {}
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:19:11
@ -257,6 +377,11 @@ LL | fn f3(a @ [ref mut b, ref c]: [U; 2]) {}
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `[U; 2]` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | fn f3(ref a @ [ref mut b, ref c]: [U; 2]) {}
| +++
error[E0382]: use of partially moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:24:9
@ -269,8 +394,8 @@ LL | let a @ (mut b @ ref mut c, d @ ref e) = (U, U);
= note: partial move occurs because value has type `U`, which does not implement the `Copy` trait
help: borrow this binding in the pattern to avoid moving the value
|
LL | let a @ (mut b @ ref mut c, ref d @ ref e) = (U, U);
| +++
LL | let ref a @ (mut b @ ref mut c, ref d @ ref e) = (U, U);
| +++ +++
error[E0382]: use of partially moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:33:9
@ -283,8 +408,8 @@ LL | let a @ (mut b @ ref mut c, d @ ref e) = (u(), u());
= note: partial move occurs because value has type `U`, which does not implement the `Copy` trait
help: borrow this binding in the pattern to avoid moving the value
|
LL | let a @ (mut b @ ref mut c, ref d @ ref e) = (u(), u());
| +++
LL | let ref a @ (mut b @ ref mut c, ref d @ ref e) = (u(), u());
| +++ +++
error[E0382]: use of moved value
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:47:38

View File

@ -97,6 +97,11 @@ LL | let a @ (ref mut b, ref mut c) = (U, U);
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `(U, U)` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ref a @ (ref mut b, ref mut c) = (U, U);
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-ref-mut-twice.rs:67:9
@ -109,6 +114,11 @@ LL | let a @ (b, [c, d]) = &mut val; // Same as ^--
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `&mut (U, [U; 2])` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ref a @ (b, [c, d]) = &mut val; // Same as ^--
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-ref-mut-twice.rs:70:9
@ -119,6 +129,11 @@ LL | let a @ &mut ref mut b = &mut U;
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `&mut U` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ref a @ &mut ref mut b = &mut U;
| +++
error: borrow of moved value
--> $DIR/borrowck-pat-ref-mut-twice.rs:72:9
@ -130,6 +145,11 @@ LL | let a @ &mut (ref mut b, ref mut c) = &mut (U, U);
| | value borrowed here after move
| value moved into `a` here
| move occurs because `a` has type `&mut (U, U)` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ref a @ &mut (ref mut b, ref mut c) = &mut (U, U);
| +++
error: cannot borrow value as mutable more than once at a time
--> $DIR/borrowck-pat-ref-mut-twice.rs:76:9

View File

@ -9,8 +9,8 @@ LL | let a @ NC(b, c @ NC(d, e)) = NC(C, NC(C, C));
= note: partial move occurs because value has type `NC<C, C>`, which does not implement the `Copy` trait
help: borrow this binding in the pattern to avoid moving the value
|
LL | let a @ NC(b, ref c @ NC(d, e)) = NC(C, NC(C, C));
| +++
LL | let ref a @ NC(b, ref c @ NC(d, e)) = NC(C, NC(C, C));
| +++ +++
error: aborting due to previous error

View File

@ -34,6 +34,11 @@ LL | Ok(ref a @ b) | Err(b @ ref a) => {
| | value borrowed here after move
| value moved into `b` here
| move occurs because `b` has type `NotCopy` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | Ok(ref a @ b) | Err(ref b @ ref a) => {
| +++
error: cannot move out of value because it is borrowed
--> $DIR/default-binding-modes-both-sides-independent.rs:42:9

View File

@ -0,0 +1,19 @@
// run-rustfix
#![allow(unused)]
struct S {
f: String,
}
fn main() {
let ref _moved @ ref _from = String::from("foo"); //~ ERROR
let ref _moved @ ref _from = String::from("foo"); //~ ERROR
let ref _moved @ ref _from = String::from("foo"); //~ ERROR
//~^ ERROR
let ref _moved @ ref _from = String::from("foo"); // ok
let ref _moved @ S { ref f } = S { f: String::from("foo") }; //~ ERROR
let ref _moved @ S { ref f } = S { f: String::from("foo") }; //~ ERROR
//~^ ERROR
let ref _moved @ S { ref f } = S { f: String::from("foo") }; // ok
let ref _moved @ S { ref f } = S { f: String::from("foo") }; //~ ERROR
}

View File

@ -0,0 +1,19 @@
// run-rustfix
#![allow(unused)]
struct S {
f: String,
}
fn main() {
let _moved @ _from = String::from("foo"); //~ ERROR
let _moved @ ref _from = String::from("foo"); //~ ERROR
let ref _moved @ _from = String::from("foo"); //~ ERROR
//~^ ERROR
let ref _moved @ ref _from = String::from("foo"); // ok
let _moved @ S { f } = S { f: String::from("foo") }; //~ ERROR
let ref _moved @ S { f } = S { f: String::from("foo") }; //~ ERROR
//~^ ERROR
let ref _moved @ S { ref f } = S { f: String::from("foo") }; // ok
let _moved @ S { ref f } = S { f: String::from("foo") }; //~ ERROR
}

View File

@ -0,0 +1,107 @@
error: borrow of moved value
--> $DIR/ref-pattern-binding.rs:10:9
|
LL | let _moved @ ref _from = String::from("foo");
| ------^^^---------
| | |
| | value borrowed here after move
| value moved into `_moved` here
| move occurs because `_moved` has type `String` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ref _moved @ ref _from = String::from("foo");
| +++
error: cannot move out of value because it is borrowed
--> $DIR/ref-pattern-binding.rs:11:9
|
LL | let ref _moved @ _from = String::from("foo");
| ----------^^^-----
| | |
| | value moved into `_from` here
| value borrowed, by `_moved`, here
error: cannot move out of value because it is borrowed
--> $DIR/ref-pattern-binding.rs:15:9
|
LL | let ref _moved @ S { f } = S { f: String::from("foo") };
| ----------^^^^^^^-^^
| | |
| | value moved into `f` here
| value borrowed, by `_moved`, here
error: borrow of moved value
--> $DIR/ref-pattern-binding.rs:18:9
|
LL | let _moved @ S { ref f } = S { f: String::from("foo") };
| ------^^^^^^^-----^^
| | |
| | value borrowed here after move
| value moved into `_moved` here
| move occurs because `_moved` has type `S` which does not implement the `Copy` trait
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ref _moved @ S { ref f } = S { f: String::from("foo") };
| +++
error[E0382]: use of moved value
--> $DIR/ref-pattern-binding.rs:9:9
|
LL | let _moved @ _from = String::from("foo");
| ^^^^^^ ----- ------------------- move occurs because value has type `String`, which does not implement the `Copy` trait
| | |
| | value moved here
| value used here after move
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ref _moved @ ref _from = String::from("foo");
| +++ +++
error[E0382]: borrow of moved value
--> $DIR/ref-pattern-binding.rs:11:9
|
LL | let ref _moved @ _from = String::from("foo");
| ^^^^^^^^^^ ----- ------------------- move occurs because value has type `String`, which does not implement the `Copy` trait
| | |
| | value moved here
| value borrowed here after move
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ref _moved @ ref _from = String::from("foo");
| +++
error[E0382]: use of partially moved value
--> $DIR/ref-pattern-binding.rs:14:9
|
LL | let _moved @ S { f } = S { f: String::from("foo") };
| ^^^^^^ - value partially moved here
| |
| value used here after partial move
|
= note: partial move occurs because value has type `String`, which does not implement the `Copy` trait
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ref _moved @ S { ref f } = S { f: String::from("foo") };
| +++ +++
error[E0382]: borrow of partially moved value
--> $DIR/ref-pattern-binding.rs:15:9
|
LL | let ref _moved @ S { f } = S { f: String::from("foo") };
| ^^^^^^^^^^ - value partially moved here
| |
| value borrowed here after partial move
|
= note: partial move occurs because value has type `String`, which does not implement the `Copy` trait
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ref _moved @ S { ref f } = S { f: String::from("foo") };
| +++
error: aborting due to 8 previous errors
For more information about this error, try `rustc --explain E0382`.

View File

@ -10,8 +10,8 @@ LL | let _moved @ _from = String::from("foo");
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | let _moved @ ref _from = String::from("foo");
| +++
LL | let ref _moved @ ref _from = String::from("foo");
| +++ +++
error: aborting due to previous error