Always require Drop for generators.

This commit is contained in:
Camille GILLOT 2022-09-11 17:22:38 +02:00
parent 9259da51ed
commit 03618d6afd
3 changed files with 25 additions and 9 deletions

View File

@ -109,6 +109,13 @@ fn next(&mut self) -> Option<NeedsDropResult<Ty<'tcx>>> {
for component in components {
match *component.kind() {
// The information required to determine whether a generator has drop is
// computed on MIR, while this very method is used to build MIR. To avoid
// cycles, we consider that generators always require drop.
ty::Generator(..) if tcx.sess.opts.unstable_opts.drop_tracking_mir => {
return Some(Err(AlwaysRequiresDrop));
}
_ if component.is_copy_modulo_regions(tcx, self.param_env) => (),
ty::Closure(_, substs) => {

View File

@ -1,16 +1,24 @@
error[E0597]: `a` does not live long enough
--> $DIR/borrowing.rs:13:33
|
LL | let _b = {
| -- borrow later stored here
LL | let a = 3;
LL | Pin::new(&mut || 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 |
LL | };
| - `a` dropped here while still borrowed
| -- ... and the borrow might be used here, when that temporary is dropped and runs the destructor for generator
| |
| `a` dropped here while still borrowed
|
= 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
help: 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
|
LL | let x = Pin::new(&mut || yield &a).resume(()); x
| +++++++ +++
error[E0597]: `a` does not live long enough
--> $DIR/borrowing.rs:20:20

View File

@ -4,9 +4,10 @@ error[E0499]: cannot borrow `thing` as mutable more than once at a time
LL | gen.as_mut().resume(&mut thing);
| ---------- first mutable borrow occurs here
LL | gen.as_mut().resume(&mut thing);
| ------ ^^^^^^^^^^ second mutable borrow occurs here
| |
| first borrow later used by call
| ^^^^^^^^^^ second mutable borrow occurs here
LL |
LL | }
| - first borrow might be used here, when `gen` is dropped and runs the destructor for generator
error: aborting due to previous error