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<'_>> {
let tcx = self.infcx.tcx;
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.result
}
@ -1998,14 +1998,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
};
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);
let Some(index1) = expr_finder.result else {
note_default_suggestion();
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);
let Some(index2) = expr_finder.result else {
note_default_suggestion();

View File

@ -76,7 +76,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
&& let Some(body_id) = node.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);
if let Some(mut expr) = expr_finder.result {
while let hir::ExprKind::AddrOf(_, _, inner)

View File

@ -50,15 +50,22 @@ pub struct FindExprBySpan<'hir> {
pub span: Span,
pub result: Option<&'hir hir::Expr<'hir>>,
pub ty_result: Option<&'hir hir::Ty<'hir>>,
pub tcx: TyCtxt<'hir>,
}
impl<'hir> FindExprBySpan<'hir> {
pub fn new(span: Span) -> Self {
Self { span, result: None, ty_result: None }
pub fn new(span: Span, tcx: TyCtxt<'hir>) -> Self {
Self { span, result: None, ty_result: None, tcx }
}
}
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>) {
if self.span == ex.span {
self.result = Some(ex);

View File

@ -901,7 +901,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
// Remove all the desugaring and macro contexts.
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 {
return;
};
@ -1367,7 +1367,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
return false;
};
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);
let Some(expr) = expr_finder.result else {
return false;
@ -1469,7 +1469,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
// Remove all the hir desugaring contexts while maintaining the macro contexts.
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 {
return false;
};

View File

@ -2457,7 +2457,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
&& let Some(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);
if let Some(hir::Expr {

View File

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

View File

@ -18,6 +18,9 @@ LL | }
error[E0597]: `ref_` does not live long enough
--> $DIR/dropck.rs:16:18
|
LL | let ref_ = Box::leak(Box::new(Some(cell.borrow_mut())));
| ---- binding `ref_` declared here
...
LL | || {
| -- value captured here by coroutine
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
let x = String::new();
//~^ HELP: consider changing this to be mutable
//~| NOTE binding `x` declared here
|c| { //~ NOTE: value captured here
x.push(c);
//~^ 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
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() -> _ {
| ^
@ -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
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();
| - help: consider changing this to be mutable: `mut x`
@ -41,8 +41,11 @@ LL | x.push(c);
| ^ cannot borrow as mutable
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| {
| --- value captured here
LL | x.push(c);

View File

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

View File

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

View File

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

View File

@ -1,6 +1,8 @@
error[E0597]: `local_arr` does not live long enough
--> $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 _);
| ------------------- type annotation requires that `local_arr` is borrowed for `'static`
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
// 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 bar = foo; bar.take() })(); false } => {},
if { (|| { let mut bar = foo; bar.take() })(); false } => {},
//~^ ERROR cannot move out of `foo` in pattern guard [E0507]
Some(s) => std::process::exit(*s),
}
@ -16,7 +16,7 @@ fn main() {
match Some(&4) {
None => {},
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]
Some(s) => std::process::exit(*s),
}

View File

@ -1,22 +1,30 @@
error[E0507]: cannot move out of `foo` in pattern guard
--> $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
| |
| `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 } => {},
| ++++++++
error[E0507]: cannot move out of `foo` in pattern guard
--> $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
| |
| `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 Some(()) = { (|| { let mut bar = foo.clone(); bar.take() })(); None } => {},
| ++++++++
error: aborting due to 2 previous errors

View File

@ -7,6 +7,10 @@ LL | (|| { let bar = foo; bar.take() })();
| `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 | (|| { let bar = foo.clone(); bar.take() })();
| ++++++++
error[E0507]: cannot move out of `foo` in pattern guard
--> $DIR/issue-27282-mutation-in-guard.rs:20:18
@ -17,6 +21,10 @@ LL | (|| { let bar = foo; bar.take() })();
| `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 | (|| { let bar = foo.clone(); bar.take() })();
| ++++++++
error: aborting due to 2 previous errors

View File

@ -11,6 +11,8 @@ LL | || doit(data);
error[E0597]: `data` does not live long enough
--> $DIR/issue-42574-diagnostic-in-nested-closure.rs:6:13
|
LL | fn doit(data: &'static mut ()) {
| ---- binding `data` declared here
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)]
#![allow(unused_mut)]
//@ run-rustfix
// Here is arielb1's basic example from rust-lang/rust#27282
// that AST borrowck is flummoxed by:
@ -7,7 +9,7 @@ fn should_reject_destructive_mutate_in_guard() {
match Some(&4) {
None => {},
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]
false } => { },
Some(s) => std::process::exit(*s),
@ -16,7 +18,7 @@ fn should_reject_destructive_mutate_in_guard() {
match Some(&4) {
None => {},
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]
None } => { },
Some(s) => std::process::exit(*s),

View File

@ -1,22 +1,30 @@
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
| |
| `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 | (|| { let mut bar = foo.clone(); bar.take() })();
| ++++++++
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
| |
| `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 | (|| { let mut bar = foo.clone(); bar.take() })();
| ++++++++
error: aborting due to 2 previous errors

View File

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

View File

@ -1,6 +1,8 @@
error[E0597]: `a` does not live long enough
--> $DIR/location-insensitive-scopes-issue-117146.rs:10:18
|
LL | let a = ();
| - binding `a` declared here
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) {
| -- lifetime `'a` defined here
...
LL | let a = 22;
| - binding `a` declared here
LL | let b = 44;
LL | let _closure = || {
| -- value captured here
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) {
| -- lifetime `'a` defined here
...
LL | let a = 22;
LL | let b = 44;
| - binding `b` declared here
LL | let _closure = || {
| -- value captured here
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
--> $DIR/regions-addr-of-upvar-self.rs:8:46
|
LL | pub fn chase_cat(&mut self) {
| --------- binding `self` declared here
LL | let _f = || {
| -- value captured here
LL | let p: &'static mut usize = &mut self.food;

View File

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

View File

@ -27,6 +27,9 @@ LL | }
error[E0597]: `y` does not live long enough
--> $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| {
| --- value captured here
LL | ay = x;

View File

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

View File

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

View File

@ -1,6 +1,9 @@
error[E0597]: `factorial` does not live long enough
--> $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 {
| --------------- value captured here
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
|
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 | let f = |x: u32| -> u32 {
| --------------- value captured here