1
Fork 0

Modify find_expr from Span to better account for closures

Start pointing to where bindings were declared when they are captured in closures:

```
error[E0597]: `x` does not live long enough
  --> $DIR/suggest-return-closure.rs:23:9
   |
LL |     let x = String::new();
   |         - binding `x` declared here
...
LL |     |c| {
   |     --- value captured here
LL |         x.push(c);
   |         ^ borrowed value does not live long enough
...
LL | }
   | -- borrow later used here
   | |
   | `x` dropped here while still borrowed
```

Suggest cloning in more cases involving closures:

```
error[E0507]: cannot move out of `foo` in pattern guard
  --> $DIR/issue-27282-move-ref-mut-into-guard.rs:11:19
   |
LL |             if { (|| { let mut bar = foo; bar.take() })(); false } => {},
   |                   ^^                 --- move occurs because `foo` has type `&mut Option<&i32>`, which does not implement the `Copy` trait
   |                   |
   |                   `foo` is moved here
   |
   = note: variables bound in patterns cannot be moved from until after the end of the pattern guard
help: consider cloning the value if the performance cost is acceptable
   |
LL |             if { (|| { let mut bar = foo.clone(); bar.take() })(); false } => {},
   |                                         ++++++++
```
This commit is contained in:
Esteban Küber 2024-04-18 20:41:43 +00:00
parent ef8b9dcf23
commit 4aba2c55e6
31 changed files with 202 additions and 31 deletions

View file

@ -1964,7 +1964,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
pub(crate) fn find_expr(&self, span: Span) -> Option<&hir::Expr<'_>> { pub(crate) fn find_expr(&self, span: Span) -> Option<&hir::Expr<'_>> {
let tcx = self.infcx.tcx; let tcx = self.infcx.tcx;
let body_id = tcx.hir_node(self.mir_hir_id()).body_id()?; let body_id = tcx.hir_node(self.mir_hir_id()).body_id()?;
let mut expr_finder = FindExprBySpan::new(span); let mut expr_finder = FindExprBySpan::new(span, tcx);
expr_finder.visit_expr(tcx.hir().body(body_id).value); expr_finder.visit_expr(tcx.hir().body(body_id).value);
expr_finder.result expr_finder.result
} }
@ -1998,14 +1998,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
}; };
let mut expr_finder = let mut expr_finder =
FindExprBySpan::new(self.body.local_decls[*index1].source_info.span); FindExprBySpan::new(self.body.local_decls[*index1].source_info.span, tcx);
expr_finder.visit_expr(hir.body(body_id).value); expr_finder.visit_expr(hir.body(body_id).value);
let Some(index1) = expr_finder.result else { let Some(index1) = expr_finder.result else {
note_default_suggestion(); note_default_suggestion();
return; return;
}; };
expr_finder = FindExprBySpan::new(self.body.local_decls[*index2].source_info.span); expr_finder = FindExprBySpan::new(self.body.local_decls[*index2].source_info.span, tcx);
expr_finder.visit_expr(hir.body(body_id).value); expr_finder.visit_expr(hir.body(body_id).value);
let Some(index2) = expr_finder.result else { let Some(index2) = expr_finder.result else {
note_default_suggestion(); note_default_suggestion();

View file

@ -76,7 +76,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
&& let Some(body_id) = node.body_id() && let Some(body_id) = node.body_id()
{ {
let body = tcx.hir().body(body_id); let body = tcx.hir().body(body_id);
let mut expr_finder = FindExprBySpan::new(span); let mut expr_finder = FindExprBySpan::new(span, tcx);
expr_finder.visit_expr(body.value); expr_finder.visit_expr(body.value);
if let Some(mut expr) = expr_finder.result { if let Some(mut expr) = expr_finder.result {
while let hir::ExprKind::AddrOf(_, _, inner) while let hir::ExprKind::AddrOf(_, _, inner)

View file

@ -50,15 +50,22 @@ pub struct FindExprBySpan<'hir> {
pub span: Span, pub span: Span,
pub result: Option<&'hir hir::Expr<'hir>>, pub result: Option<&'hir hir::Expr<'hir>>,
pub ty_result: Option<&'hir hir::Ty<'hir>>, pub ty_result: Option<&'hir hir::Ty<'hir>>,
pub tcx: TyCtxt<'hir>,
} }
impl<'hir> FindExprBySpan<'hir> { impl<'hir> FindExprBySpan<'hir> {
pub fn new(span: Span) -> Self { pub fn new(span: Span, tcx: TyCtxt<'hir>) -> Self {
Self { span, result: None, ty_result: None } Self { span, result: None, ty_result: None, tcx }
} }
} }
impl<'v> Visitor<'v> for FindExprBySpan<'v> { impl<'v> Visitor<'v> for FindExprBySpan<'v> {
type NestedFilter = rustc_middle::hir::nested_filter::OnlyBodies;
fn nested_visit_map(&mut self) -> Self::Map {
self.tcx.hir()
}
fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) { fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
if self.span == ex.span { if self.span == ex.span {
self.result = Some(ex); self.result = Some(ex);

View file

@ -901,7 +901,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
// Remove all the desugaring and macro contexts. // Remove all the desugaring and macro contexts.
span.remove_mark(); span.remove_mark();
} }
let mut expr_finder = FindExprBySpan::new(span); let mut expr_finder = FindExprBySpan::new(span, self.tcx);
let Some(body_id) = self.tcx.hir().maybe_body_owned_by(obligation.cause.body_id) else { let Some(body_id) = self.tcx.hir().maybe_body_owned_by(obligation.cause.body_id) else {
return; return;
}; };
@ -1367,7 +1367,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
return false; return false;
}; };
let body = self.tcx.hir().body(body_id); let body = self.tcx.hir().body(body_id);
let mut expr_finder = FindExprBySpan::new(span); let mut expr_finder = FindExprBySpan::new(span, self.tcx);
expr_finder.visit_expr(body.value); expr_finder.visit_expr(body.value);
let Some(expr) = expr_finder.result else { let Some(expr) = expr_finder.result else {
return false; return false;
@ -1469,7 +1469,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
// Remove all the hir desugaring contexts while maintaining the macro contexts. // Remove all the hir desugaring contexts while maintaining the macro contexts.
span.remove_mark(); span.remove_mark();
} }
let mut expr_finder = super::FindExprBySpan::new(span); let mut expr_finder = super::FindExprBySpan::new(span, self.tcx);
let Some(body_id) = self.tcx.hir().maybe_body_owned_by(obligation.cause.body_id) else { let Some(body_id) = self.tcx.hir().maybe_body_owned_by(obligation.cause.body_id) else {
return false; return false;
}; };

View file

@ -2457,7 +2457,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
&& let Some(body_id) = && let Some(body_id) =
self.tcx.hir().maybe_body_owned_by(obligation.cause.body_id) self.tcx.hir().maybe_body_owned_by(obligation.cause.body_id)
{ {
let mut expr_finder = FindExprBySpan::new(span); let mut expr_finder = FindExprBySpan::new(span, self.tcx);
expr_finder.visit_expr(self.tcx.hir().body(body_id).value); expr_finder.visit_expr(self.tcx.hir().body(body_id).value);
if let Some(hir::Expr { if let Some(hir::Expr {

View file

@ -4,6 +4,7 @@ error[E0597]: `a` does not live long enough
LL | let _b = { LL | let _b = {
| -- borrow later stored here | -- borrow later stored here
LL | let a = 3; LL | let a = 3;
| - binding `a` declared here
LL | Pin::new(&mut #[coroutine] || yield &a).resume(()) LL | Pin::new(&mut #[coroutine] || yield &a).resume(())
| -- ^ borrowed value does not live long enough | -- ^ borrowed value does not live long enough
| | | |
@ -18,6 +19,7 @@ error[E0597]: `a` does not live long enough
LL | let _b = { LL | let _b = {
| -- borrow later stored here | -- borrow later stored here
LL | let a = 3; LL | let a = 3;
| - binding `a` declared here
LL | #[coroutine] || { LL | #[coroutine] || {
| -- value captured here by coroutine | -- value captured here by coroutine
LL | yield &a LL | yield &a

View file

@ -18,6 +18,9 @@ LL | }
error[E0597]: `ref_` does not live long enough error[E0597]: `ref_` does not live long enough
--> $DIR/dropck.rs:16:18 --> $DIR/dropck.rs:16:18
| |
LL | let ref_ = Box::leak(Box::new(Some(cell.borrow_mut())));
| ---- binding `ref_` declared here
...
LL | || { LL | || {
| -- value captured here by coroutine | -- value captured here by coroutine
LL | // but the coroutine can use it to drop a `Ref<'a, i32>`. LL | // but the coroutine can use it to drop a `Ref<'a, i32>`.

View file

@ -18,6 +18,7 @@ fn fn_mut() -> _ {
//~| NOTE for more information on `Fn` traits and closure types //~| NOTE for more information on `Fn` traits and closure types
let x = String::new(); let x = String::new();
//~^ HELP: consider changing this to be mutable //~^ HELP: consider changing this to be mutable
//~| NOTE binding `x` declared here
|c| { //~ NOTE: value captured here |c| { //~ NOTE: value captured here
x.push(c); x.push(c);
//~^ ERROR: does not live long enough //~^ ERROR: does not live long enough

View file

@ -21,7 +21,7 @@ LL | fn fn_mut() -> _ {
= note: for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html = note: for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
--> $DIR/suggest-return-closure.rs:31:13 --> $DIR/suggest-return-closure.rs:32:13
| |
LL | fn fun() -> _ { LL | fn fun() -> _ {
| ^ | ^
@ -32,7 +32,7 @@ LL | fn fun() -> _ {
= note: for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html = note: for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
--> $DIR/suggest-return-closure.rs:22:9 --> $DIR/suggest-return-closure.rs:23:9
| |
LL | let x = String::new(); LL | let x = String::new();
| - help: consider changing this to be mutable: `mut x` | - help: consider changing this to be mutable: `mut x`
@ -41,8 +41,11 @@ LL | x.push(c);
| ^ cannot borrow as mutable | ^ cannot borrow as mutable
error[E0597]: `x` does not live long enough error[E0597]: `x` does not live long enough
--> $DIR/suggest-return-closure.rs:22:9 --> $DIR/suggest-return-closure.rs:23:9
| |
LL | let x = String::new();
| - binding `x` declared here
...
LL | |c| { LL | |c| {
| --- value captured here | --- value captured here
LL | x.push(c); LL | x.push(c);

View file

@ -25,6 +25,8 @@ LL | f.use_ref();
error[E0597]: `x` does not live long enough error[E0597]: `x` does not live long enough
--> $DIR/closure-borrow-spans.rs:19:16 --> $DIR/closure-borrow-spans.rs:19:16
| |
LL | let x = 1;
| - binding `x` declared here
LL | f = || x; LL | f = || x;
| -- ^ borrowed value does not live long enough | -- ^ borrowed value does not live long enough
| | | |
@ -85,6 +87,8 @@ LL | f.use_ref();
error[E0597]: `x` does not live long enough error[E0597]: `x` does not live long enough
--> $DIR/closure-borrow-spans.rs:52:16 --> $DIR/closure-borrow-spans.rs:52:16
| |
LL | let mut x = 1;
| ----- binding `x` declared here
LL | f = || x = 0; LL | f = || x = 0;
| -- ^ borrowed value does not live long enough | -- ^ borrowed value does not live long enough
| | | |
@ -145,6 +149,8 @@ LL | f.use_ref();
error[E0597]: `x` does not live long enough error[E0597]: `x` does not live long enough
--> $DIR/closure-borrow-spans.rs:86:16 --> $DIR/closure-borrow-spans.rs:86:16
| |
LL | let x = &mut z;
| - binding `x` declared here
LL | f = || *x = 0; LL | f = || *x = 0;
| -- ^^ borrowed value does not live long enough | -- ^^ borrowed value does not live long enough
| | | |

View file

@ -37,6 +37,9 @@ LL | fn test() {
error[E0597]: `y` does not live long enough error[E0597]: `y` does not live long enough
--> $DIR/escape-upvar-nested.rs:21:40 --> $DIR/escape-upvar-nested.rs:21:40
| |
LL | let y = 22;
| - binding `y` declared here
LL |
LL | let mut closure = || { LL | let mut closure = || {
| -- value captured here | -- value captured here
LL | let mut closure1 = || p = &y; LL | let mut closure1 = || p = &y;

View file

@ -23,6 +23,8 @@ LL | fn test() {
error[E0597]: `y` does not live long enough error[E0597]: `y` does not live long enough
--> $DIR/escape-upvar-ref.rs:23:35 --> $DIR/escape-upvar-ref.rs:23:35
| |
LL | let y = 22;
| - binding `y` declared here
LL | let mut closure = || p = &y; LL | let mut closure = || p = &y;
| -- ^ borrowed value does not live long enough | -- ^ borrowed value does not live long enough
| | | |

View file

@ -1,6 +1,8 @@
error[E0597]: `local_arr` does not live long enough error[E0597]: `local_arr` does not live long enough
--> $DIR/propagate-multiple-requirements.rs:15:14 --> $DIR/propagate-multiple-requirements.rs:15:14
| |
LL | let local_arr = other_local_arr;
| --------- binding `local_arr` declared here
LL | let mut out: &mut &'static [i32] = &mut (&[1] as _); LL | let mut out: &mut &'static [i32] = &mut (&[1] as _);
| ------------------- type annotation requires that `local_arr` is borrowed for `'static` | ------------------- type annotation requires that `local_arr` is borrowed for `'static`
LL | once(|mut z: &[i32], mut out_val: &mut &[i32]| { LL | once(|mut z: &[i32], mut out_val: &mut &[i32]| {

View file

@ -0,0 +1,23 @@
// Issue 27282: Example 1: This sidesteps the AST checks disallowing
// mutable borrows in match guards by hiding the mutable borrow in a
// guard behind a move (of the ref mut pattern id) within a closure.
//@ run-rustfix
#![feature(if_let_guard)]
fn main() {
match Some(&4) {
None => {},
ref mut foo
if { (|| { let mut bar = foo.clone(); bar.take() })(); false } => {},
//~^ ERROR cannot move out of `foo` in pattern guard [E0507]
Some(s) => std::process::exit(*s),
}
match Some(&4) {
None => {},
ref mut foo
if let Some(()) = { (|| { let mut bar = foo.clone(); bar.take() })(); None } => {},
//~^ ERROR cannot move out of `foo` in pattern guard [E0507]
Some(s) => std::process::exit(*s),
}
}

View file

@ -1,14 +1,14 @@
// Issue 27282: Example 1: This sidesteps the AST checks disallowing // Issue 27282: Example 1: This sidesteps the AST checks disallowing
// mutable borrows in match guards by hiding the mutable borrow in a // mutable borrows in match guards by hiding the mutable borrow in a
// guard behind a move (of the ref mut pattern id) within a closure. // guard behind a move (of the ref mut pattern id) within a closure.
//@ run-rustfix
#![feature(if_let_guard)] #![feature(if_let_guard)]
fn main() { fn main() {
match Some(&4) { match Some(&4) {
None => {}, None => {},
ref mut foo ref mut foo
if { (|| { let bar = foo; bar.take() })(); false } => {}, if { (|| { let mut bar = foo; bar.take() })(); false } => {},
//~^ ERROR cannot move out of `foo` in pattern guard [E0507] //~^ ERROR cannot move out of `foo` in pattern guard [E0507]
Some(s) => std::process::exit(*s), Some(s) => std::process::exit(*s),
} }
@ -16,7 +16,7 @@ fn main() {
match Some(&4) { match Some(&4) {
None => {}, None => {},
ref mut foo ref mut foo
if let Some(()) = { (|| { let bar = foo; bar.take() })(); None } => {}, if let Some(()) = { (|| { let mut bar = foo; bar.take() })(); None } => {},
//~^ ERROR cannot move out of `foo` in pattern guard [E0507] //~^ ERROR cannot move out of `foo` in pattern guard [E0507]
Some(s) => std::process::exit(*s), Some(s) => std::process::exit(*s),
} }

View file

@ -1,22 +1,30 @@
error[E0507]: cannot move out of `foo` in pattern guard error[E0507]: cannot move out of `foo` in pattern guard
--> $DIR/issue-27282-move-ref-mut-into-guard.rs:11:19 --> $DIR/issue-27282-move-ref-mut-into-guard.rs:11:19
| |
LL | if { (|| { let bar = foo; bar.take() })(); false } => {}, LL | if { (|| { let mut bar = foo; bar.take() })(); false } => {},
| ^^ --- move occurs because `foo` has type `&mut Option<&i32>`, which does not implement the `Copy` trait | ^^ --- move occurs because `foo` has type `&mut Option<&i32>`, which does not implement the `Copy` trait
| | | |
| `foo` is moved here | `foo` is moved here
| |
= note: variables bound in patterns cannot be moved from until after the end of the pattern guard = note: variables bound in patterns cannot be moved from until after the end of the pattern guard
help: consider cloning the value if the performance cost is acceptable
|
LL | if { (|| { let mut bar = foo.clone(); bar.take() })(); false } => {},
| ++++++++
error[E0507]: cannot move out of `foo` in pattern guard error[E0507]: cannot move out of `foo` in pattern guard
--> $DIR/issue-27282-move-ref-mut-into-guard.rs:19:34 --> $DIR/issue-27282-move-ref-mut-into-guard.rs:19:34
| |
LL | if let Some(()) = { (|| { let bar = foo; bar.take() })(); None } => {}, LL | if let Some(()) = { (|| { let mut bar = foo; bar.take() })(); None } => {},
| ^^ --- move occurs because `foo` has type `&mut Option<&i32>`, which does not implement the `Copy` trait | ^^ --- move occurs because `foo` has type `&mut Option<&i32>`, which does not implement the `Copy` trait
| | | |
| `foo` is moved here | `foo` is moved here
| |
= note: variables bound in patterns cannot be moved from until after the end of the pattern guard = note: variables bound in patterns cannot be moved from until after the end of the pattern guard
help: consider cloning the value if the performance cost is acceptable
|
LL | if let Some(()) = { (|| { let mut bar = foo.clone(); bar.take() })(); None } => {},
| ++++++++
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -7,6 +7,10 @@ LL | (|| { let bar = foo; bar.take() })();
| `foo` is moved here | `foo` is moved here
| |
= note: variables bound in patterns cannot be moved from until after the end of the pattern guard = note: variables bound in patterns cannot be moved from until after the end of the pattern guard
help: consider cloning the value if the performance cost is acceptable
|
LL | (|| { let bar = foo.clone(); bar.take() })();
| ++++++++
error[E0507]: cannot move out of `foo` in pattern guard error[E0507]: cannot move out of `foo` in pattern guard
--> $DIR/issue-27282-mutation-in-guard.rs:20:18 --> $DIR/issue-27282-mutation-in-guard.rs:20:18
@ -17,6 +21,10 @@ LL | (|| { let bar = foo; bar.take() })();
| `foo` is moved here | `foo` is moved here
| |
= note: variables bound in patterns cannot be moved from until after the end of the pattern guard = note: variables bound in patterns cannot be moved from until after the end of the pattern guard
help: consider cloning the value if the performance cost is acceptable
|
LL | (|| { let bar = foo.clone(); bar.take() })();
| ++++++++
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -11,6 +11,8 @@ LL | || doit(data);
error[E0597]: `data` does not live long enough error[E0597]: `data` does not live long enough
--> $DIR/issue-42574-diagnostic-in-nested-closure.rs:6:13 --> $DIR/issue-42574-diagnostic-in-nested-closure.rs:6:13
| |
LL | fn doit(data: &'static mut ()) {
| ---- binding `data` declared here
LL | || doit(data); LL | || doit(data);
| -- -----^^^^- | -- -----^^^^-
| | | | | | | |

View file

@ -0,0 +1,66 @@
#![feature(if_let_guard)]
#![allow(unused_mut)]
//@ run-rustfix
// Here is arielb1's basic example from rust-lang/rust#27282
// that AST borrowck is flummoxed by:
fn should_reject_destructive_mutate_in_guard() {
match Some(&4) {
None => {},
ref mut foo if {
(|| { let mut bar = foo.clone(); bar.take() })();
//~^ ERROR cannot move out of `foo` in pattern guard [E0507]
false } => { },
Some(s) => std::process::exit(*s),
}
match Some(&4) {
None => {},
ref mut foo if let Some(()) = {
(|| { let mut bar = foo.clone(); bar.take() })();
//~^ ERROR cannot move out of `foo` in pattern guard [E0507]
None } => { },
Some(s) => std::process::exit(*s),
}
}
// Here below is a case that needs to keep working: we only use the
// binding via immutable-borrow in the guard, and we mutate in the arm
// body.
fn allow_mutate_in_arm_body() {
match Some(&4) {
None => {},
ref mut foo if foo.is_some() => { foo.take(); () }
Some(s) => std::process::exit(*s),
}
match Some(&4) {
None => {},
ref mut foo if let Some(_) = foo => { foo.take(); () }
Some(s) => std::process::exit(*s),
}
}
// Here below is a case that needs to keep working: we only use the
// binding via immutable-borrow in the guard, and we move into the arm
// body.
fn allow_move_into_arm_body() {
match Some(&4) {
None => {},
mut foo if foo.is_some() => { foo.unwrap(); () }
Some(s) => std::process::exit(*s),
}
match Some(&4) {
None => {},
mut foo if let Some(_) = foo => { foo.unwrap(); () }
Some(s) => std::process::exit(*s),
}
}
fn main() {
should_reject_destructive_mutate_in_guard();
allow_mutate_in_arm_body();
allow_move_into_arm_body();
}

View file

@ -1,4 +1,6 @@
#![feature(if_let_guard)] #![feature(if_let_guard)]
#![allow(unused_mut)]
//@ run-rustfix
// Here is arielb1's basic example from rust-lang/rust#27282 // Here is arielb1's basic example from rust-lang/rust#27282
// that AST borrowck is flummoxed by: // that AST borrowck is flummoxed by:
@ -7,7 +9,7 @@ fn should_reject_destructive_mutate_in_guard() {
match Some(&4) { match Some(&4) {
None => {}, None => {},
ref mut foo if { ref mut foo if {
(|| { let bar = foo; bar.take() })(); (|| { let mut bar = foo; bar.take() })();
//~^ ERROR cannot move out of `foo` in pattern guard [E0507] //~^ ERROR cannot move out of `foo` in pattern guard [E0507]
false } => { }, false } => { },
Some(s) => std::process::exit(*s), Some(s) => std::process::exit(*s),
@ -16,7 +18,7 @@ fn should_reject_destructive_mutate_in_guard() {
match Some(&4) { match Some(&4) {
None => {}, None => {},
ref mut foo if let Some(()) = { ref mut foo if let Some(()) = {
(|| { let bar = foo; bar.take() })(); (|| { let mut bar = foo; bar.take() })();
//~^ ERROR cannot move out of `foo` in pattern guard [E0507] //~^ ERROR cannot move out of `foo` in pattern guard [E0507]
None } => { }, None } => { },
Some(s) => std::process::exit(*s), Some(s) => std::process::exit(*s),

View file

@ -1,22 +1,30 @@
error[E0507]: cannot move out of `foo` in pattern guard error[E0507]: cannot move out of `foo` in pattern guard
--> $DIR/match-guards-always-borrow.rs:10:14 --> $DIR/match-guards-always-borrow.rs:12:14
| |
LL | (|| { let bar = foo; bar.take() })(); LL | (|| { let mut bar = foo; bar.take() })();
| ^^ --- move occurs because `foo` has type `&mut Option<&i32>`, which does not implement the `Copy` trait | ^^ --- move occurs because `foo` has type `&mut Option<&i32>`, which does not implement the `Copy` trait
| | | |
| `foo` is moved here | `foo` is moved here
| |
= note: variables bound in patterns cannot be moved from until after the end of the pattern guard = note: variables bound in patterns cannot be moved from until after the end of the pattern guard
help: consider cloning the value if the performance cost is acceptable
|
LL | (|| { let mut bar = foo.clone(); bar.take() })();
| ++++++++
error[E0507]: cannot move out of `foo` in pattern guard error[E0507]: cannot move out of `foo` in pattern guard
--> $DIR/match-guards-always-borrow.rs:19:14 --> $DIR/match-guards-always-borrow.rs:21:14
| |
LL | (|| { let bar = foo; bar.take() })(); LL | (|| { let mut bar = foo; bar.take() })();
| ^^ --- move occurs because `foo` has type `&mut Option<&i32>`, which does not implement the `Copy` trait | ^^ --- move occurs because `foo` has type `&mut Option<&i32>`, which does not implement the `Copy` trait
| | | |
| `foo` is moved here | `foo` is moved here
| |
= note: variables bound in patterns cannot be moved from until after the end of the pattern guard = note: variables bound in patterns cannot be moved from until after the end of the pattern guard
help: consider cloning the value if the performance cost is acceptable
|
LL | (|| { let mut bar = foo.clone(); bar.take() })();
| ++++++++
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -1,6 +1,8 @@
error[E0597]: `a` does not live long enough error[E0597]: `a` does not live long enough
--> $DIR/location-insensitive-scopes-issue-117146.rs:10:18 --> $DIR/location-insensitive-scopes-issue-117146.rs:10:18
| |
LL | let a = ();
| - binding `a` declared here
LL | let b = |_| &a; LL | let b = |_| &a;
| --- -^ | --- -^
| | || | | ||

View file

@ -1,6 +1,8 @@
error[E0597]: `a` does not live long enough error[E0597]: `a` does not live long enough
--> $DIR/location-insensitive-scopes-issue-117146.rs:10:18 --> $DIR/location-insensitive-scopes-issue-117146.rs:10:18
| |
LL | let a = ();
| - binding `a` declared here
LL | let b = |_| &a; LL | let b = |_| &a;
| --- -^ | --- -^
| | || | | ||

View file

@ -33,7 +33,9 @@ error[E0597]: `a` does not live long enough
| |
LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) { LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
| -- lifetime `'a` defined here | -- lifetime `'a` defined here
... LL | let a = 22;
| - binding `a` declared here
LL | let b = 44;
LL | let _closure = || { LL | let _closure = || {
| -- value captured here | -- value captured here
LL | let c = 66; LL | let c = 66;

View file

@ -34,7 +34,9 @@ error[E0597]: `b` does not live long enough
| |
LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) { LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
| -- lifetime `'a` defined here | -- lifetime `'a` defined here
... LL | let a = 22;
LL | let b = 44;
| - binding `b` declared here
LL | let _closure = || { LL | let _closure = || {
| -- value captured here | -- value captured here
LL | let c = 66; LL | let c = 66;

View file

@ -20,6 +20,8 @@ LL | let p: &'static mut usize = &mut self.food;
error[E0597]: `self` does not live long enough error[E0597]: `self` does not live long enough
--> $DIR/regions-addr-of-upvar-self.rs:8:46 --> $DIR/regions-addr-of-upvar-self.rs:8:46
| |
LL | pub fn chase_cat(&mut self) {
| --------- binding `self` declared here
LL | let _f = || { LL | let _f = || {
| -- value captured here | -- value captured here
LL | let p: &'static mut usize = &mut self.food; LL | let p: &'static mut usize = &mut self.food;

View file

@ -1,6 +1,9 @@
error[E0597]: `y` does not live long enough error[E0597]: `y` does not live long enough
--> $DIR/regions-nested-fns-2.rs:7:25 --> $DIR/regions-nested-fns-2.rs:7:25
| |
LL | let y = 3;
| - binding `y` declared here
LL | ignore(
LL | |z| { LL | |z| {
| --- value captured here | --- value captured here
LL | if false { &y } else { z } LL | if false { &y } else { z }

View file

@ -27,6 +27,9 @@ LL | }
error[E0597]: `y` does not live long enough error[E0597]: `y` does not live long enough
--> $DIR/regions-nested-fns.rs:10:15 --> $DIR/regions-nested-fns.rs:10:15
| |
LL | let y = 3;
| - binding `y` declared here
...
LL | ignore::<Box<dyn for<'z> FnMut(&'z isize)>>(Box::new(|z| { LL | ignore::<Box<dyn for<'z> FnMut(&'z isize)>>(Box::new(|z| {
| --- value captured here | --- value captured here
LL | ay = x; LL | ay = x;

View file

@ -4,6 +4,7 @@ error[E0597]: `i` does not live long enough
LL | let mut cl_box = { LL | let mut cl_box = {
| ---------- borrow later stored here | ---------- borrow later stored here
LL | let mut i = 3; LL | let mut i = 3;
| ----- binding `i` declared here
LL | box_it(Box::new(|| i += 1)) LL | box_it(Box::new(|| i += 1))
| -- ^ borrowed value does not live long enough | -- ^ borrowed value does not live long enough
| | | |

View file

@ -16,6 +16,9 @@ error[E0597]: `y` does not live long enough
| |
LL | let bad = { LL | let bad = {
| --- borrow later stored here | --- borrow later stored here
LL | let x = 1;
LL | let y = &x;
| - binding `y` declared here
... ...
LL | scoped(|| { LL | scoped(|| {
| -- value captured here | -- value captured here

View file

@ -1,6 +1,9 @@
error[E0597]: `factorial` does not live long enough error[E0597]: `factorial` does not live long enough
--> $DIR/unboxed-closures-failed-recursive-fn-1.rs:15:17 --> $DIR/unboxed-closures-failed-recursive-fn-1.rs:15:17
| |
LL | let mut factorial: Option<Box<dyn Fn(u32) -> u32>> = None;
| ------------- binding `factorial` declared here
LL |
LL | let f = |x: u32| -> u32 { LL | let f = |x: u32| -> u32 {
| --------------- value captured here | --------------- value captured here
LL | let g = factorial.as_ref().unwrap(); LL | let g = factorial.as_ref().unwrap();
@ -30,7 +33,9 @@ error[E0597]: `factorial` does not live long enough
--> $DIR/unboxed-closures-failed-recursive-fn-1.rs:28:17 --> $DIR/unboxed-closures-failed-recursive-fn-1.rs:28:17
| |
LL | let mut factorial: Option<Box<dyn Fn(u32) -> u32 + 'static>> = None; LL | let mut factorial: Option<Box<dyn Fn(u32) -> u32 + 'static>> = None;
| ----------------------------------------- type annotation requires that `factorial` is borrowed for `'static` | ------------- ----------------------------------------- type annotation requires that `factorial` is borrowed for `'static`
| |
| binding `factorial` declared here
LL | LL |
LL | let f = |x: u32| -> u32 { LL | let f = |x: u32| -> u32 {
| --------------- value captured here | --------------- value captured here