about summary refs log tree commit diff
path: root/tests/ui/coroutine
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2024-04-18 20:41:43 +0000
committerEsteban Küber <esteban@kuber.com.ar>2024-04-24 22:21:13 +0000
commit4aba2c55e6bbe6c09132ff19360d302269dca775 (patch)
treec7d83085f07dbdbc30cb34a437db3f6f85d2c5d7 /tests/ui/coroutine
parentef8b9dcf23700f2e2265317611460d3a65c19eff (diff)
downloadrust-4aba2c55e6bbe6c09132ff19360d302269dca775.tar.gz
rust-4aba2c55e6bbe6c09132ff19360d302269dca775.zip
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 } => {},
   |                                         ++++++++
```
Diffstat (limited to 'tests/ui/coroutine')
-rw-r--r--tests/ui/coroutine/borrowing.stderr2
-rw-r--r--tests/ui/coroutine/dropck.stderr3
2 files changed, 5 insertions, 0 deletions
diff --git a/tests/ui/coroutine/borrowing.stderr b/tests/ui/coroutine/borrowing.stderr
index 9132e5d84ed..4f8b9737777 100644
--- a/tests/ui/coroutine/borrowing.stderr
+++ b/tests/ui/coroutine/borrowing.stderr
@@ -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
diff --git a/tests/ui/coroutine/dropck.stderr b/tests/ui/coroutine/dropck.stderr
index d6cd7c4cd47..78fdeec972f 100644
--- a/tests/ui/coroutine/dropck.stderr
+++ b/tests/ui/coroutine/dropck.stderr
@@ -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>`.