]> git.lizzy.rs Git - rust.git/commitdiff
compute liveness later
authorNiko Matsakis <niko@alum.mit.edu>
Tue, 7 Aug 2018 14:00:04 +0000 (10:00 -0400)
committerNiko Matsakis <niko@alum.mit.edu>
Tue, 7 Aug 2018 18:52:59 +0000 (14:52 -0400)
src/librustc_mir/borrow_check/nll/mod.rs
src/librustc_mir/borrow_check/nll/type_check/liveness.rs
src/librustc_mir/borrow_check/nll/type_check/mod.rs

index 973568a67f030e8fdf04c89d45a8e59cfc5376c2..262d4a5cd3dabc09d91113af81d143d0d969afd5 100644 (file)
@@ -11,7 +11,7 @@
 use borrow_check::borrow_set::BorrowSet;
 use borrow_check::location::{LocationIndex, LocationTable};
 use borrow_check::nll::facts::AllFactsExt;
-use borrow_check::nll::type_check::MirTypeckRegionConstraints;
+use borrow_check::nll::type_check::{MirTypeckResults, MirTypeckRegionConstraints};
 use borrow_check::nll::region_infer::values::RegionValueElements;
 use borrow_check::nll::liveness_map::{NllLivenessMap, LocalWithRegion};
 use dataflow::indexes::BorrowIndex;
@@ -109,9 +109,12 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
     let elements = &Rc::new(RegionValueElements::new(mir));
 
     // Run the MIR type-checker.
-    let liveness_map = NllLivenessMap::compute(&mir);
-    let liveness = LivenessResults::compute(mir, &liveness_map);
-    let (constraint_sets, universal_region_relations) = type_check::type_check(
+    let MirTypeckResults {
+        constraints,
+        universal_region_relations,
+        liveness,
+        liveness_map,
+    } = type_check::type_check(
         infcx,
         param_env,
         mir,
@@ -119,7 +122,6 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
         &universal_regions,
         location_table,
         borrow_set,
-        &liveness,
         &mut all_facts,
         flow_inits,
         move_data,
@@ -141,7 +143,7 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
         mut liveness_constraints,
         outlives_constraints,
         type_tests,
-    } = constraint_sets;
+    } = constraints;
 
     constraint_generation::generate_constraints(
         infcx,
@@ -205,6 +207,7 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
     dump_mir_results(
         infcx,
         &liveness,
+        &liveness_map,
         MirSource::item(def_id),
         &mir,
         &regioncx,
@@ -221,6 +224,7 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
 fn dump_mir_results<'a, 'gcx, 'tcx>(
     infcx: &InferCtxt<'a, 'gcx, 'tcx>,
     liveness: &LivenessResults<LocalWithRegion>,
+    liveness_map: &NllLivenessMap,
     source: MirSource,
     mir: &Mir<'tcx>,
     regioncx: &RegionInferenceContext,
@@ -230,8 +234,6 @@ fn dump_mir_results<'a, 'gcx, 'tcx>(
         return;
     }
 
-    let map = &NllLivenessMap::compute(mir);
-
     let regular_liveness_per_location: FxHashMap<_, _> = mir
         .basic_blocks()
         .indices()
@@ -239,7 +241,7 @@ fn dump_mir_results<'a, 'gcx, 'tcx>(
             let mut results = vec![];
             liveness
                 .regular
-                .simulate_block(&mir, bb, map, |location, local_set| {
+                .simulate_block(&mir, bb, liveness_map, |location, local_set| {
                     results.push((location, local_set.clone()));
                 });
             results
@@ -253,7 +255,7 @@ fn dump_mir_results<'a, 'gcx, 'tcx>(
             let mut results = vec![];
             liveness
                 .drop
-                .simulate_block(&mir, bb, map, |location, local_set| {
+                .simulate_block(&mir, bb, liveness_map, |location, local_set| {
                     results.push((location, local_set.clone()));
                 });
             results
index 2b9307db59af9feb3821760f2c52f059845d3d39..f15881ee3c89f668b3d3b492e2a0e6a0b837aa6e 100644 (file)
 pub(super) fn generate<'gcx, 'tcx>(
     cx: &mut TypeChecker<'_, 'gcx, 'tcx>,
     mir: &Mir<'tcx>,
-    liveness: &LivenessResults<LocalWithRegion>,
     flow_inits: &mut FlowAtLocation<MaybeInitializedPlaces<'_, 'gcx, 'tcx>>,
     move_data: &MoveData<'tcx>,
-) {
-    let mut generator = TypeLivenessGenerator {
-        cx,
-        mir,
-        liveness,
-        flow_inits,
-        move_data,
-        drop_data: FxHashMap(),
-        map: &NllLivenessMap::compute(mir),
-    };
-
-    for bb in mir.basic_blocks().indices() {
-        generator.add_liveness_constraints(bb);
+) -> (LivenessResults<LocalWithRegion>, NllLivenessMap) {
+    let liveness_map = NllLivenessMap::compute(&mir);
+    let liveness = LivenessResults::compute(mir, &liveness_map);
+
+    {
+        let mut generator = TypeLivenessGenerator {
+            cx,
+            mir,
+            liveness: &liveness,
+            flow_inits,
+            move_data,
+            drop_data: FxHashMap(),
+            map: &liveness_map,
+        };
+
+        for bb in mir.basic_blocks().indices() {
+            generator.add_liveness_constraints(bb);
+        }
     }
+
+    (liveness, liveness_map)
 }
 
 struct TypeLivenessGenerator<'gen, 'typeck, 'flow, 'gcx, 'tcx>
index a18e2368bf724d55b81f5f5afba76711e8d149d3..4ca84df445e79dd47aab9f4de3e7234d1176c5c3 100644 (file)
@@ -15,6 +15,7 @@
 use borrow_check::location::LocationTable;
 use borrow_check::nll::constraints::{ConstraintSet, OutlivesConstraint};
 use borrow_check::nll::facts::AllFacts;
+use borrow_check::nll::liveness_map::NllLivenessMap;
 use borrow_check::nll::region_infer::values::{RegionValueElements, LivenessValues};
 use borrow_check::nll::region_infer::{ClosureRegionRequirementsExt, TypeTest};
 use borrow_check::nll::type_check::free_region_relations::{CreateResult, UniversalRegionRelations};
@@ -115,16 +116,12 @@ pub(crate) fn type_check<'gcx, 'tcx>(
     universal_regions: &Rc<UniversalRegions<'tcx>>,
     location_table: &LocationTable,
     borrow_set: &BorrowSet<'tcx>,
-    liveness: &LivenessResults<LocalWithRegion>,
     all_facts: &mut Option<AllFacts>,
     flow_inits: &mut FlowAtLocation<MaybeInitializedPlaces<'_, 'gcx, 'tcx>>,
     move_data: &MoveData<'tcx>,
     elements: &Rc<RegionValueElements>,
     errors_buffer: &mut Vec<Diagnostic>,
-) -> (
-    MirTypeckRegionConstraints<'tcx>,
-    Rc<UniversalRegionRelations<'tcx>>,
-) {
+) -> MirTypeckResults<'tcx> {
     let implicit_region_bound = infcx.tcx.mk_region(ty::ReVar(universal_regions.fr_fn_body));
     let mut constraints = MirTypeckRegionConstraints {
         liveness_constraints: LivenessValues::new(elements),
@@ -147,7 +144,7 @@ pub(crate) fn type_check<'gcx, 'tcx>(
         all_facts,
     );
 
-    {
+    let (liveness, liveness_map) = {
         let mut borrowck_context = BorrowCheckContext {
             universal_regions,
             location_table,
@@ -166,7 +163,6 @@ pub(crate) fn type_check<'gcx, 'tcx>(
             Some(&mut borrowck_context),
             Some(errors_buffer),
             |cx| {
-                liveness::generate(cx, mir, liveness, flow_inits, move_data);
                 cx.equate_inputs_and_outputs(
                     mir,
                     mir_def_id,
@@ -174,14 +170,20 @@ pub(crate) fn type_check<'gcx, 'tcx>(
                     &universal_region_relations,
                     &normalized_inputs_and_output,
                 );
+                liveness::generate(cx, mir, flow_inits, move_data)
             },
-        );
-    }
+        )
+    };
 
-    (constraints, universal_region_relations)
+    MirTypeckResults {
+        constraints,
+        universal_region_relations,
+        liveness,
+        liveness_map,
+    }
 }
 
-fn type_check_internal<'a, 'gcx, 'tcx, F>(
+fn type_check_internal<'a, 'gcx, 'tcx, R>(
     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
     mir_def_id: DefId,
     param_env: ty::ParamEnv<'gcx>,
@@ -190,10 +192,8 @@ fn type_check_internal<'a, 'gcx, 'tcx, F>(
     implicit_region_bound: Option<ty::Region<'tcx>>,
     borrowck_context: Option<&'a mut BorrowCheckContext<'a, 'tcx>>,
     errors_buffer: Option<&mut Vec<Diagnostic>>,
-    mut extra: F,
-) where
-    F: FnMut(&mut TypeChecker<'a, 'gcx, 'tcx>),
-{
+    mut extra: impl FnMut(&mut TypeChecker<'a, 'gcx, 'tcx>) -> R,
+) -> R where {
     let mut checker = TypeChecker::new(
         infcx,
         mir,
@@ -214,7 +214,7 @@ fn type_check_internal<'a, 'gcx, 'tcx, F>(
         checker.typeck_mir(mir, errors_buffer);
     }
 
-    extra(&mut checker);
+    extra(&mut checker)
 }
 
 fn mirbug(tcx: TyCtxt, span: Span, msg: &str) {
@@ -655,6 +655,13 @@ struct BorrowCheckContext<'a, 'tcx: 'a> {
     constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
 }
 
+crate struct MirTypeckResults<'tcx> {
+    crate constraints: MirTypeckRegionConstraints<'tcx>,
+    crate universal_region_relations: Rc<UniversalRegionRelations<'tcx>>,
+    crate liveness: LivenessResults<LocalWithRegion>,
+    crate liveness_map: NllLivenessMap,
+}
+
 /// A collection of region constraints that must be satisfied for the
 /// program to be considered well-typed.
 crate struct MirTypeckRegionConstraints<'tcx> {