]> git.lizzy.rs Git - rust.git/commitdiff
Store generator interior in MIR literals
authorJohn Kåre Alsaker <john.kare.alsaker@gmail.com>
Fri, 11 Aug 2017 05:15:33 +0000 (07:15 +0200)
committerJohn Kåre Alsaker <john.kare.alsaker@gmail.com>
Fri, 11 Aug 2017 05:15:33 +0000 (07:15 +0200)
src/librustc/ich/impls_mir.rs
src/librustc/mir/mod.rs
src/librustc/mir/tcx.rs
src/librustc/mir/visit.rs
src/librustc_mir/build/expr/as_rvalue.rs
src/librustc_mir/hair/cx/expr.rs
src/librustc_mir/hair/mod.rs
src/librustc_mir/transform/generator.rs

index e277f40a26d9c619d3962f17f7d60cec2158bb52..3d5de5579f0f910d0c5fafbf6e1981433d0990fc 100644 (file)
@@ -442,11 +442,15 @@ fn hash_stable<W: StableHasherResult>(&self,
                 substs.hash_stable(hcx, hasher);
                 active_field.hash_stable(hcx, hasher);
             }
-            mir::AggregateKind::Closure(def_id, ref substs) |
-            mir::AggregateKind::Generator(def_id, ref substs) => {
+            mir::AggregateKind::Closure(def_id, ref substs) => {
                 def_id.hash_stable(hcx, hasher);
                 substs.hash_stable(hcx, hasher);
             }
+            mir::AggregateKind::Generator(def_id, ref substs, ref interior) => {
+                def_id.hash_stable(hcx, hasher);
+                substs.hash_stable(hcx, hasher);
+                interior.hash_stable(hcx, hasher);
+            }
         }
     }
 }
index 20e74f47c9e2965ad1e60a68ebf9b235b864d711..0d3629a8e5feeaecc1bef7179a3f9da6bec454c2 100644 (file)
@@ -21,7 +21,7 @@
 use hir::def::CtorKind;
 use hir::def_id::DefId;
 use ty::subst::{Subst, Substs};
-use ty::{self, AdtDef, ClosureSubsts, Region, Ty};
+use ty::{self, AdtDef, ClosureSubsts, Region, Ty, GeneratorInterior};
 use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
 use util::ppaux;
 use rustc_back::slice;
@@ -1260,7 +1260,7 @@ pub enum AggregateKind<'tcx> {
     /// number and is present only for union expressions.
     Adt(&'tcx AdtDef, usize, &'tcx Substs<'tcx>, Option<usize>),
     Closure(DefId, ClosureSubsts<'tcx>),
-    Generator(DefId, ClosureSubsts<'tcx>),
+    Generator(DefId, ClosureSubsts<'tcx>, GeneratorInterior<'tcx>),
 }
 
 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
@@ -1423,7 +1423,7 @@ fn fmt_tuple(fmt: &mut Formatter, lvs: &[Operand]) -> fmt::Result {
                         }
                     }),
 
-                    AggregateKind::Generator(def_id, _) => ty::tls::with(|tcx| {
+                    AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| {
                         if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
                             let name = format!("[generator@{:?}]", tcx.hir.span(node_id));
                             let mut struct_fmt = fmt.debug_struct(&name);
@@ -1892,8 +1892,8 @@ fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F)
                         AggregateKind::Adt(def, v, substs.fold_with(folder), n),
                     AggregateKind::Closure(id, substs) =>
                         AggregateKind::Closure(id, substs.fold_with(folder)),
-                    AggregateKind::Generator(id, substs) =>
-                        AggregateKind::Generator(id, substs.fold_with(folder)),
+                    AggregateKind::Generator(id, substs, interior) =>
+                        AggregateKind::Generator(id, substs.fold_with(folder), interior.fold_with(folder)),
                 };
                 Aggregate(kind, fields.fold_with(folder))
             }
@@ -1920,7 +1920,8 @@ fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
                     AggregateKind::Tuple => false,
                     AggregateKind::Adt(_, _, substs, _) => substs.visit_with(visitor),
                     AggregateKind::Closure(_, substs) => substs.visit_with(visitor),
-                    AggregateKind::Generator(_, substs) => substs.visit_with(visitor),
+                    AggregateKind::Generator(_, substs, interior) => substs.visit_with(visitor) ||
+                        interior.visit_with(visitor),
                 }) || fields.visit_with(visitor)
             }
         }
index 12588edbea9c0c22633e0f3e51b17be26c346728..14c53ee2dd7820b2cd791405351adddc6ebc07d5 100644 (file)
@@ -202,13 +202,8 @@ pub fn ty<'a, 'gcx, D>(&self, local_decls: &D, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> T
                     AggregateKind::Closure(did, substs) => {
                         tcx.mk_closure_from_closure_substs(did, substs)
                     }
-                    AggregateKind::Generator(did, substs) => {
-                        let node_id = tcx.hir.as_local_node_id(did).unwrap();
-                        let interior = *tcx.typeck_tables_of(did)
-                            .generator_interiors
-                            .get(&node_id)
-                            .unwrap();
-                        tcx.mk_generator(did, substs, interior.subst(tcx, substs.substs))
+                    AggregateKind::Generator(did, substs, interior) => {
+                        tcx.mk_generator(did, substs, interior)
                     }
                 }
             }
index 8cc5b6cab116e58b4e38f9dea411241502ed9d26..30ffa73019c960b3ee6e8b2ae753177b9d228d33 100644 (file)
@@ -11,7 +11,7 @@
 use middle::const_val::ConstVal;
 use hir::def_id::DefId;
 use ty::subst::Substs;
-use ty::{ClosureSubsts, Region, Ty};
+use ty::{ClosureSubsts, Region, Ty, GeneratorInterior};
 use mir::*;
 use rustc_const_math::ConstUsize;
 use syntax_pos::Span;
@@ -226,6 +226,12 @@ fn visit_closure_substs(&mut self,
                 self.super_closure_substs(substs);
             }
 
+            fn visit_generator_interior(&mut self,
+                                    interior: & $($mutability)* GeneratorInterior<'tcx>,
+                                    _: Location) {
+                self.super_generator_interior(interior);
+            }
+
             fn visit_const_val(&mut self,
                                const_val: & $($mutability)* ConstVal,
                                _: Location) {
@@ -570,9 +576,11 @@ fn super_rvalue(&mut self,
                                 self.visit_closure_substs(closure_substs, location);
                             }
                             AggregateKind::Generator(ref $($mutability)* def_id,
-                                                   ref $($mutability)* closure_substs) => {
+                                                   ref $($mutability)* closure_substs,
+                                                   ref $($mutability)* interior) => {
                                 self.visit_def_id(def_id, location);
                                 self.visit_closure_substs(closure_substs, location);
+                                self.visit_generator_interior(interior, location);
                             }
                         }
 
@@ -742,6 +750,10 @@ fn super_ty(&mut self, _ty: & $($mutability)* Ty<'tcx>) {
             fn super_substs(&mut self, _substs: & $($mutability)* &'tcx Substs<'tcx>) {
             }
 
+            fn super_generator_interior(&mut self,
+                                    _interior: & $($mutability)* GeneratorInterior<'tcx>) {
+            }
+
             fn super_closure_substs(&mut self,
                                     _substs: & $($mutability)* ClosureSubsts<'tcx>) {
             }
index d585672d6da23969ae296b9cdecc80c96eddd571..b2a38679240102291a755c10aba91df8e5159d8d 100644 (file)
@@ -179,12 +179,12 @@ fn expr_as_rvalue(&mut self,
 
                 block.and(Rvalue::Aggregate(box AggregateKind::Tuple, fields))
             }
-            ExprKind::Closure { closure_id, substs, upvars, generator } => { // see (*) above
+            ExprKind::Closure { closure_id, substs, upvars, interior } => { // see (*) above
                 let mut operands: Vec<_> =
                     upvars.into_iter()
                           .map(|upvar| unpack!(block = this.as_operand(block, scope, upvar)))
                           .collect();
-                let result = if generator {
+                let result = if let Some(interior) = interior {
                     // Add the state operand
                     operands.push(Operand::Constant(box Constant {
                         span: expr_span,
@@ -193,7 +193,7 @@ fn expr_as_rvalue(&mut self,
                             value: ConstVal::Integral(ConstInt::U32(0)),
                         },
                     }));
-                    box AggregateKind::Generator(closure_id, substs)
+                    box AggregateKind::Generator(closure_id, substs, interior)
                 } else {
                     box AggregateKind::Closure(closure_id, substs)
                 };
index a81103213b841f92d93c1da62d16d4bcd1a3f507..b8b69382d7d0de32eb7e183be090f6daa92232e1 100644 (file)
@@ -428,11 +428,11 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
             }
         }
 
-        hir::ExprClosure(.., is_generator) => {
+        hir::ExprClosure(..) => {
             let closure_ty = cx.tables().expr_ty(expr);
-            let (def_id, substs) = match closure_ty.sty {
-                ty::TyClosure(def_id, substs) |
-                ty::TyGenerator(def_id, substs, _) => (def_id, substs),
+            let (def_id, substs, interior) = match closure_ty.sty {
+                ty::TyClosure(def_id, substs) => (def_id, substs, None),
+                ty::TyGenerator(def_id, substs, interior) => (def_id, substs, Some(interior)),
                 _ => {
                     span_bug!(expr.span, "closure expr w/o closure type: {:?}", closure_ty);
                 }
@@ -447,7 +447,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
                 closure_id: def_id,
                 substs: substs,
                 upvars: upvars,
-                generator: is_generator,
+                interior,
             }
         }
 
index ff239a2adddd348dddadb9d9111988b56cd6fbe4..89d0260fb9f5dd14d92ad10a9e6a9e6b6734c322 100644 (file)
@@ -19,7 +19,7 @@
 use rustc::hir::def_id::DefId;
 use rustc::middle::region::CodeExtent;
 use rustc::ty::subst::Substs;
-use rustc::ty::{self, AdtDef, ClosureSubsts, Region, Ty};
+use rustc::ty::{self, AdtDef, ClosureSubsts, Region, Ty, GeneratorInterior};
 use rustc::hir;
 use syntax::ast;
 use syntax_pos::Span;
@@ -239,7 +239,7 @@ pub enum ExprKind<'tcx> {
         closure_id: DefId,
         substs: ClosureSubsts<'tcx>,
         upvars: Vec<ExprRef<'tcx>>,
-        generator: bool,
+        interior: Option<GeneratorInterior<'tcx>>,
     },
     Literal {
         literal: Literal<'tcx>,
index 90bcc87b0c4a0972462ff8f73c7760e824e81f42..651246ab4a5e379ae176f01c9faf5736a88e5005 100644 (file)
@@ -76,8 +76,8 @@ struct TransformVisitor<'a, 'tcx: 'a> {
     // The number of generator states. 0 is unresumed, 1 is poisoned. So this is initialized to 2
     bb_target_count: u32,
 
-    // Map from a (which block to resume execution at, which block to use to drop the generator) to a 
-    // genrator state
+    // Map from a (which block to resume execution at, which block to use to drop the generator)
+    // to a generator state
     bb_targets: HashMap<(BasicBlock, Option<BasicBlock>), u32>,
 
     // The original RETURN_POINTER local