diff --git a/compiler/rustc_ty_utils/src/needs_drop.rs b/compiler/rustc_ty_utils/src/needs_drop.rs index 0df060fc5fb..f519ea01a2c 100644 --- a/compiler/rustc_ty_utils/src/needs_drop.rs +++ b/compiler/rustc_ty_utils/src/needs_drop.rs @@ -109,6 +109,13 @@ where 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) => { diff --git a/tests/ui/generator/borrowing.drop_tracking_mir.stderr b/tests/ui/generator/borrowing.drop_tracking_mir.stderr index 96e3c327f8b..8fbad276db4 100644 --- a/tests/ui/generator/borrowing.drop_tracking_mir.stderr +++ b/tests/ui/generator/borrowing.drop_tracking_mir.stderr @@ -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 diff --git a/tests/ui/generator/retain-resume-ref.drop_tracking_mir.stderr b/tests/ui/generator/retain-resume-ref.drop_tracking_mir.stderr index 7122a951e80..736ed1fb608 100644 --- a/tests/ui/generator/retain-resume-ref.drop_tracking_mir.stderr +++ b/tests/ui/generator/retain-resume-ref.drop_tracking_mir.stderr @@ -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