Extend closure special-casing for generators.

This commit extends existing special-casing of closures to highlight the
use of variables within generators that are causing the generator to
borrow them.
This commit is contained in:
David Wood 2018-10-10 21:56:17 +02:00
parent 121320d523
commit aa701154f0
No known key found for this signature in database
GPG key ID: 01760B4F9F53F154
7 changed files with 228 additions and 155 deletions

View file

@ -1,10 +1,11 @@
error[E0597]: `a` does not live long enough
--> $DIR/borrowing.rs:18:18
--> $DIR/borrowing.rs:18:29
|
LL | unsafe { (|| yield &a).resume() }
| ^^^^^^^^^^^^^
| |
| borrowed value does not live long enough
| -----------^-
| || |
| || borrowed value does not live long enough
| |value captured here by generator
| a temporary with access to the borrow is created here ...
LL | //~^ ERROR: `a` does not live long enough
LL | };
@ -15,18 +16,18 @@ LL | };
= note: The temporary is part of an expression at the end of a block. Consider forcing this temporary to be dropped sooner, before the block's local variables are dropped. For example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block.
error[E0597]: `a` does not live long enough
--> $DIR/borrowing.rs:24:9
--> $DIR/borrowing.rs:25:20
|
LL | let _b = {
| -- borrow later stored here
LL | let a = 3;
LL | / || {
LL | | yield &a
LL | | //~^ ERROR: `a` does not live long enough
LL | | }
| |_________^ borrowed value does not live long enough
LL | };
| - `a` dropped here while still borrowed
LL | let _b = {
| -- borrow later stored here
LL | let a = 3;
LL | || {
| -- value captured here by generator
LL | yield &a
| ^ borrowed value does not live long enough
...
LL | };
| - `a` dropped here while still borrowed
error: aborting due to 2 previous errors

View file

@ -13,21 +13,19 @@ LL | }
= note: values in a scope are dropped in the opposite order they are defined
error[E0597]: `ref_` does not live long enough
--> $DIR/dropck.rs:22:11
--> $DIR/dropck.rs:24:18
|
LL | gen = || {
| ___________^
LL | | // but the generator can use it to drop a `Ref<'a, i32>`.
LL | | let _d = ref_.take(); //~ ERROR `ref_` does not live long enough
LL | | yield;
LL | | };
| |_____^ borrowed value does not live long enough
LL | gen = || {
| -- value captured here by generator
LL | // but the generator can use it to drop a `Ref<'a, i32>`.
LL | let _d = ref_.take(); //~ ERROR `ref_` does not live long enough
| ^^^^ borrowed value does not live long enough
...
LL | }
| -
| |
| `ref_` dropped here while still borrowed
| borrow might be used here, when `gen` is dropped and runs the destructor for generator
LL | }
| -
| |
| `ref_` dropped here while still borrowed
| borrow might be used here, when `gen` is dropped and runs the destructor for generator
|
= note: values in a scope are dropped in the opposite order they are defined

View file

@ -9,17 +9,15 @@ LL | yield();
error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable
--> $DIR/yield-while-iterating.rs:67:20
|
LL | let mut b = || {
| _________________-
LL | | for p in &mut x {
LL | | yield p;
LL | | }
LL | | };
| |_____- mutable borrow occurs here
LL | println!("{}", x[0]); //~ ERROR
| ^ immutable borrow occurs here
LL | b.resume();
| - mutable borrow later used here
LL | let mut b = || {
| -- mutable borrow occurs here
LL | for p in &mut x {
| - first borrow occurs due to use of `x` in generator
...
LL | println!("{}", x[0]); //~ ERROR
| ^ immutable borrow occurs here
LL | b.resume();
| - mutable borrow later used here
error: aborting due to 2 previous errors

View file

@ -1,17 +1,15 @@
error[E0501]: cannot borrow `x` as immutable because previous closure requires unique access
--> $DIR/yield-while-ref-reborrowed.rs:45:20
|
LL | let mut b = || {
| _________________-
LL | | let a = &mut *x;
LL | | yield();
LL | | println!("{}", a);
LL | | };
| |_____- closure construction occurs here
LL | println!("{}", x); //~ ERROR
| ^ borrow occurs here
LL | b.resume();
| - first borrow later used here
LL | let mut b = || {
| -- generator construction occurs here
LL | let a = &mut *x;
| - first borrow occurs due to use of `x` in generator
...
LL | println!("{}", x); //~ ERROR
| ^ borrow occurs here
LL | b.resume();
| - first borrow later used here
error: aborting due to previous error