rust/tests/ui/regions/regions-nested-fns.stderr
Esteban Küber 4aba2c55e6 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 } => {},
   |                                         ++++++++
```
2024-04-24 22:21:13 +00:00

58 lines
1.9 KiB
Plaintext

error[E0521]: borrowed data escapes outside of closure
--> $DIR/regions-nested-fns.rs:12:9
|
LL | let mut ay = &y;
| ------ `ay` declared here, outside of the closure body
...
LL | ignore::<Box<dyn for<'z> FnMut(&'z isize)>>(Box::new(|z| {
| - `z` is a reference that is only valid in the closure body
...
LL | ay = z;
| ^^^^^^ `z` escapes the closure body here
error[E0597]: `y` does not live long enough
--> $DIR/regions-nested-fns.rs:5:18
|
LL | let y = 3;
| - binding `y` declared here
LL | let mut ay = &y;
| ^^ borrowed value does not live long enough
...
LL | if false { return ay; }
| -- returning this value requires that `y` is borrowed for `'static`
...
LL | }
| - `y` dropped here while still borrowed
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;
LL | ay = &y;
| ^ borrowed value does not live long enough
...
LL | if false { return ay; }
| -- returning this value requires that `y` is borrowed for `'static`
...
LL | }
| - `y` dropped here while still borrowed
error: lifetime may not live long enough
--> $DIR/regions-nested-fns.rs:17:27
|
LL | fn nested<'x>(x: &'x isize) {
| -- lifetime `'x` defined here
...
LL | if false { return x; }
| ^ returning this value requires that `'x` must outlive `'static`
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0521, E0597.
For more information about an error, try `rustc --explain E0521`.