Skip to content

Commit 03618d6

Browse files
committed
Always require Drop for generators.
1 parent 9259da5 commit 03618d6

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

compiler/rustc_ty_utils/src/needs_drop.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ where
109109

110110
for component in components {
111111
match *component.kind() {
112+
// The information required to determine whether a generator has drop is
113+
// computed on MIR, while this very method is used to build MIR. To avoid
114+
// cycles, we consider that generators always require drop.
115+
ty::Generator(..) if tcx.sess.opts.unstable_opts.drop_tracking_mir => {
116+
return Some(Err(AlwaysRequiresDrop));
117+
}
118+
112119
_ if component.is_copy_modulo_regions(tcx, self.param_env) => (),
113120

114121
ty::Closure(_, substs) => {

tests/ui/generator/borrowing.drop_tracking_mir.stderr

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
error[E0597]: `a` does not live long enough
22
--> $DIR/borrowing.rs:13:33
33
|
4-
LL | let _b = {
5-
| -- borrow later stored here
6-
LL | let a = 3;
74
LL | Pin::new(&mut || yield &a).resume(())
8-
| -- ^ borrowed value does not live long enough
9-
| |
5+
| ----------^
6+
| | |
7+
| | borrowed value does not live long enough
108
| value captured here by generator
9+
| a temporary with access to the borrow is created here ...
1110
LL |
1211
LL | };
13-
| - `a` dropped here while still borrowed
12+
| -- ... and the borrow might be used here, when that temporary is dropped and runs the destructor for generator
13+
| |
14+
| `a` dropped here while still borrowed
15+
|
16+
= note: the temporary is part of an expression at the end of a block;
17+
consider forcing this temporary to be dropped sooner, before the block's local variables are dropped
18+
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
19+
|
20+
LL | let x = Pin::new(&mut || yield &a).resume(()); x
21+
| +++++++ +++
1422

1523
error[E0597]: `a` does not live long enough
1624
--> $DIR/borrowing.rs:20:20

tests/ui/generator/retain-resume-ref.drop_tracking_mir.stderr

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ error[E0499]: cannot borrow `thing` as mutable more than once at a time
44
LL | gen.as_mut().resume(&mut thing);
55
| ---------- first mutable borrow occurs here
66
LL | gen.as_mut().resume(&mut thing);
7-
| ------ ^^^^^^^^^^ second mutable borrow occurs here
8-
| |
9-
| first borrow later used by call
7+
| ^^^^^^^^^^ second mutable borrow occurs here
8+
LL |
9+
LL | }
10+
| - first borrow might be used here, when `gen` is dropped and runs the destructor for generator
1011

1112
error: aborting due to previous error
1213

0 commit comments

Comments
 (0)