]> git.lizzy.rs Git - rust.git/commitdiff
Make `needs_drop` less pessimistic on generators
authorJonas Schievink <jonasschievink@gmail.com>
Sat, 14 Mar 2020 23:24:47 +0000 (00:24 +0100)
committerJonas Schievink <jonasschievink@gmail.com>
Fri, 17 Apr 2020 18:30:23 +0000 (20:30 +0200)
src/librustc_middle/ty/util.rs
src/librustc_ty/needs_drop.rs
src/test/ui/generator/borrowing.stderr
src/test/ui/generator/retain-resume-ref.stderr

index 86575b013335c7f9434c46d21094de05da1fe358..239507e19e00136b0243d683d2c8e385076f827b 100644 (file)
@@ -1047,10 +1047,7 @@ pub fn needs_drop_components(
         // Foreign types can never have destructors.
         ty::Foreign(..) => Ok(SmallVec::new()),
 
-        // Pessimistically assume that all generators will require destructors
-        // as we don't know if a destructor is a noop or not until after the MIR
-        // state transformation pass.
-        ty::Generator(..) | ty::Dynamic(..) | ty::Error => Err(AlwaysRequiresDrop),
+        ty::Dynamic(..) | ty::Error => Err(AlwaysRequiresDrop),
 
         ty::Slice(ty) => needs_drop_components(ty, target_layout),
         ty::Array(elem_ty, size) => {
@@ -1083,7 +1080,8 @@ pub fn needs_drop_components(
         | ty::Placeholder(..)
         | ty::Opaque(..)
         | ty::Infer(_)
-        | ty::Closure(..) => Ok(smallvec![ty]),
+        | ty::Closure(..)
+        | ty::Generator(..) => Ok(smallvec![ty]),
     }
 }
 
index 6b0104e164bca8de9a5534aa795cefa87bc5e569..515cf6f29116431868d999b60e740fe44d4faa65 100644 (file)
@@ -99,6 +99,23 @@ fn next(&mut self) -> Option<NeedsDropResult<Ty<'tcx>>> {
                         }
                     }
 
+                    ty::Generator(def_id, substs, _) => {
+                        let substs = substs.as_generator();
+                        for upvar_ty in substs.upvar_tys(def_id, tcx) {
+                            queue_type(self, upvar_ty);
+                        }
+
+                        let witness = substs.witness(def_id, tcx);
+                        let interior_tys = match &witness.kind {
+                            ty::GeneratorWitness(tys) => tcx.erase_late_bound_regions(tys),
+                            _ => bug!(),
+                        };
+
+                        for interior_ty in interior_tys {
+                            queue_type(self, interior_ty);
+                        }
+                    }
+
                     // Check for a `Drop` impl and whether this is a union or
                     // `ManuallyDrop`. If it's a struct or enum without a `Drop`
                     // impl then check whether the field types need `Drop`.
index 83987e19839ce81ca82b2d90c3303ece764af3fc..38e1ace8c4efbc358033fce0f69245851ee47dd6 100644 (file)
@@ -1,19 +1,16 @@
 error[E0597]: `a` does not live long enough
   --> $DIR/borrowing.rs:9: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 |     };
-   |     -- ... 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. 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.
+   |     - `a` dropped here while still borrowed
 
 error[E0597]: `a` does not live long enough
   --> $DIR/borrowing.rs:16:20
index bc715c7030eb397e617376c4bb455e74f945d54f..e33310d12d9ef762c7bf9ba2f02fe3fa5d371fe0 100644 (file)
@@ -4,10 +4,9 @@ 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
-LL |
-LL | }
-   | - first borrow might be used here, when `gen` is dropped and runs the destructor for generator
+   |                  ------ ^^^^^^^^^^ second mutable borrow occurs here
+   |                  |
+   |                  first borrow later used by call
 
 error: aborting due to previous error