]> git.lizzy.rs Git - rust.git/commitdiff
Propagate bounds from generators
authorMatthew Jasper <mjjasper1@gmail.com>
Fri, 12 Oct 2018 14:16:29 +0000 (15:16 +0100)
committerMatthew Jasper <mjjasper1@gmail.com>
Thu, 18 Oct 2018 21:47:49 +0000 (22:47 +0100)
src/librustc_mir/borrow_check/nll/region_infer/mod.rs
src/librustc_mir/borrow_check/nll/type_check/mod.rs
src/librustc_mir/borrow_check/nll/universal_regions.rs
src/test/ui/generator/generator-region-requirements.ast.stderr [new file with mode: 0644]
src/test/ui/generator/generator-region-requirements.nll.stderr [new file with mode: 0644]
src/test/ui/generator/generator-region-requirements.rs [new file with mode: 0644]

index dd8a7f19a63bd1aba572714cd01fc38ae9bb36be..0fabcfe4564494e839a55d81ec78d72fb929d17f 100644 (file)
@@ -1268,7 +1268,7 @@ fn apply_requirements(
         tcx: TyCtxt<'_, 'gcx, 'tcx>,
         location: Location,
         closure_def_id: DefId,
-        closure_substs: ty::ClosureSubsts<'tcx>,
+        closure_substs: &'tcx ty::subst::Substs<'tcx>,
     ) -> Vec<QueryRegionConstraint<'tcx>>;
 
     fn subst_closure_mapping<T>(
@@ -1299,23 +1299,19 @@ fn apply_requirements(
         tcx: TyCtxt<'_, 'gcx, 'tcx>,
         location: Location,
         closure_def_id: DefId,
-        closure_substs: ty::ClosureSubsts<'tcx>,
+        closure_substs: &'tcx ty::subst::Substs<'tcx>,
     ) -> Vec<QueryRegionConstraint<'tcx>> {
         debug!(
             "apply_requirements(location={:?}, closure_def_id={:?}, closure_substs={:?})",
             location, closure_def_id, closure_substs
         );
 
-        // Get Tu.
-        let user_closure_ty = tcx.mk_closure(closure_def_id, closure_substs);
-        debug!("apply_requirements: user_closure_ty={:?}", user_closure_ty);
-
-        // Extract the values of the free regions in `user_closure_ty`
+        // Extract the values of the free regions in `closure_substs`
         // into a vector.  These are the regions that we will be
         // relating to one another.
         let closure_mapping = &UniversalRegions::closure_mapping(
             tcx,
-            user_closure_ty,
+            closure_substs,
             self.num_external_vids,
             tcx.closure_base_def_id(closure_def_id),
         );
index 1e79bc272e4c0ba687dc58fe02a83375fcefc63a..c5758cde9494d92aeb7202f17c2ac039c5058bd6 100644 (file)
@@ -42,7 +42,7 @@
 use rustc::traits::query::{Fallible, NoSolution};
 use rustc::traits::{ObligationCause, PredicateObligations};
 use rustc::ty::fold::TypeFoldable;
-use rustc::ty::subst::{Subst, UnpackedKind};
+use rustc::ty::subst::{Subst, Substs, UnpackedKind};
 use rustc::ty::{self, RegionVid, ToPolyTraitRef, Ty, TyCtxt, TyKind};
 use std::rc::Rc;
 use std::{fmt, iter};
@@ -2075,12 +2075,9 @@ fn prove_aggregate_predicates(
             // desugaring. A closure gets desugared to a struct, and
             // these extra requirements are basically like where
             // clauses on the struct.
-            AggregateKind::Closure(def_id, substs) => {
-                self.prove_closure_bounds(tcx, *def_id, *substs, location)
-            }
-
-            AggregateKind::Generator(def_id, substs, _) => {
-                tcx.predicates_of(*def_id).instantiate(tcx, substs.substs)
+            AggregateKind::Closure(def_id, ty::ClosureSubsts { substs })
+            | AggregateKind::Generator(def_id, ty::GeneratorSubsts { substs }, _) => {
+                self.prove_closure_bounds(tcx, *def_id, substs, location)
             }
 
             AggregateKind::Array(_) | AggregateKind::Tuple => ty::InstantiatedPredicates::empty(),
@@ -2096,7 +2093,7 @@ fn prove_closure_bounds(
         &mut self,
         tcx: TyCtxt<'a, 'gcx, 'tcx>,
         def_id: DefId,
-        substs: ty::ClosureSubsts<'tcx>,
+        substs: &'tcx Substs<'tcx>,
         location: Location,
     ) -> ty::InstantiatedPredicates<'tcx> {
         if let Some(closure_region_requirements) =
@@ -2155,7 +2152,7 @@ fn prove_closure_bounds(
             );
         }
 
-        tcx.predicates_of(def_id).instantiate(tcx, substs.substs)
+        tcx.predicates_of(def_id).instantiate(tcx, substs)
     }
 
     fn prove_trait_ref(
index eb6f1a0677b2c6f02045d1e377e7c4d83308eeaa..c54a4f96b7dcf12cbe9aa702cce8d7a3f5cae4c8 100644 (file)
@@ -232,13 +232,13 @@ pub fn new(
     /// `V[1]: V[2]`.
     pub fn closure_mapping(
         tcx: TyCtxt<'_, '_, 'tcx>,
-        closure_ty: Ty<'tcx>,
+        closure_substs: &'tcx Substs<'tcx>,
         expected_num_vars: usize,
         closure_base_def_id: DefId,
     ) -> IndexVec<RegionVid, ty::Region<'tcx>> {
         let mut region_mapping = IndexVec::with_capacity(expected_num_vars);
         region_mapping.push(tcx.types.re_static);
-        tcx.for_each_free_region(&closure_ty, |fr| {
+        tcx.for_each_free_region(&closure_substs, |fr| {
             region_mapping.push(fr);
         });
 
diff --git a/src/test/ui/generator/generator-region-requirements.ast.stderr b/src/test/ui/generator/generator-region-requirements.ast.stderr
new file mode 100644 (file)
index 0000000..6a423ae
--- /dev/null
@@ -0,0 +1,12 @@
+error[E0621]: explicit lifetime required in the type of `x`
+  --> $DIR/generator-region-requirements.rs:15:51
+   |
+LL | fn dangle(x: &mut i32) -> &'static mut i32 {
+   |              -------- help: add explicit lifetime `'static` to the type of `x`: `&'static mut i32`
+...
+LL |             GeneratorState::Complete(c) => return c,
+   |                                                   ^ lifetime `'static` required
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0621`.
diff --git a/src/test/ui/generator/generator-region-requirements.nll.stderr b/src/test/ui/generator/generator-region-requirements.nll.stderr
new file mode 100644 (file)
index 0000000..5d1050d
--- /dev/null
@@ -0,0 +1,12 @@
+error[E0621]: explicit lifetime required in the type of `x`
+  --> $DIR/generator-region-requirements.rs:11:9
+   |
+LL | fn dangle(x: &mut i32) -> &'static mut i32 {
+   |              -------- help: add explicit lifetime `'static` to the type of `x`: `&'static mut i32`
+...
+LL |         x
+   |         ^ lifetime `'static` required
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0621`.
diff --git a/src/test/ui/generator/generator-region-requirements.rs b/src/test/ui/generator/generator-region-requirements.rs
new file mode 100644 (file)
index 0000000..59e7841
--- /dev/null
@@ -0,0 +1,21 @@
+// revisions: ast nll
+// ignore-compare-mode-nll
+
+#![feature(generators, generator_trait)]
+#![cfg_attr(nll, feature(nll))]
+use std::ops::{Generator, GeneratorState};
+
+fn dangle(x: &mut i32) -> &'static mut i32 {
+    let mut g = || {
+        yield;
+        x
+    };
+    loop {
+        match unsafe { g.resume() } {
+            GeneratorState::Complete(c) => return c,
+            GeneratorState::Yielded(_) => (),
+        }
+    }
+}
+
+fn main() {}