Skip to content

Commit fb1702f

Browse files
committed
compute liveness later
1 parent ccb550f commit fb1702f

File tree

3 files changed

+55
-40
lines changed

3 files changed

+55
-40
lines changed

src/librustc_mir/borrow_check/nll/mod.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use borrow_check::borrow_set::BorrowSet;
1212
use borrow_check::location::{LocationIndex, LocationTable};
1313
use borrow_check::nll::facts::AllFactsExt;
14-
use borrow_check::nll::type_check::MirTypeckRegionConstraints;
14+
use borrow_check::nll::type_check::{MirTypeckResults, MirTypeckRegionConstraints};
1515
use borrow_check::nll::region_infer::values::RegionValueElements;
1616
use borrow_check::nll::liveness_map::{NllLivenessMap, LocalWithRegion};
1717
use dataflow::indexes::BorrowIndex;
@@ -109,17 +109,19 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
109109
let elements = &Rc::new(RegionValueElements::new(mir));
110110

111111
// Run the MIR type-checker.
112-
let liveness_map = NllLivenessMap::compute(&mir);
113-
let liveness = LivenessResults::compute(mir, &liveness_map);
114-
let (constraint_sets, universal_region_relations) = type_check::type_check(
112+
let MirTypeckResults {
113+
constraints,
114+
universal_region_relations,
115+
liveness,
116+
liveness_map,
117+
} = type_check::type_check(
115118
infcx,
116119
param_env,
117120
mir,
118121
def_id,
119122
&universal_regions,
120123
location_table,
121124
borrow_set,
122-
&liveness,
123125
&mut all_facts,
124126
flow_inits,
125127
move_data,
@@ -141,7 +143,7 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
141143
mut liveness_constraints,
142144
outlives_constraints,
143145
type_tests,
144-
} = constraint_sets;
146+
} = constraints;
145147

146148
constraint_generation::generate_constraints(
147149
infcx,
@@ -205,6 +207,7 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
205207
dump_mir_results(
206208
infcx,
207209
&liveness,
210+
&liveness_map,
208211
MirSource::item(def_id),
209212
&mir,
210213
&regioncx,
@@ -221,6 +224,7 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
221224
fn dump_mir_results<'a, 'gcx, 'tcx>(
222225
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
223226
liveness: &LivenessResults<LocalWithRegion>,
227+
liveness_map: &NllLivenessMap,
224228
source: MirSource,
225229
mir: &Mir<'tcx>,
226230
regioncx: &RegionInferenceContext,
@@ -230,16 +234,14 @@ fn dump_mir_results<'a, 'gcx, 'tcx>(
230234
return;
231235
}
232236

233-
let map = &NllLivenessMap::compute(mir);
234-
235237
let regular_liveness_per_location: FxHashMap<_, _> = mir
236238
.basic_blocks()
237239
.indices()
238240
.flat_map(|bb| {
239241
let mut results = vec![];
240242
liveness
241243
.regular
242-
.simulate_block(&mir, bb, map, |location, local_set| {
244+
.simulate_block(&mir, bb, liveness_map, |location, local_set| {
243245
results.push((location, local_set.clone()));
244246
});
245247
results
@@ -253,7 +255,7 @@ fn dump_mir_results<'a, 'gcx, 'tcx>(
253255
let mut results = vec![];
254256
liveness
255257
.drop
256-
.simulate_block(&mir, bb, map, |location, local_set| {
258+
.simulate_block(&mir, bb, liveness_map, |location, local_set| {
257259
results.push((location, local_set.clone()));
258260
});
259261
results

src/librustc_mir/borrow_check/nll/type_check/liveness.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,29 @@ use super::TypeChecker;
3636
pub(super) fn generate<'gcx, 'tcx>(
3737
cx: &mut TypeChecker<'_, 'gcx, 'tcx>,
3838
mir: &Mir<'tcx>,
39-
liveness: &LivenessResults<LocalWithRegion>,
4039
flow_inits: &mut FlowAtLocation<MaybeInitializedPlaces<'_, 'gcx, 'tcx>>,
4140
move_data: &MoveData<'tcx>,
42-
) {
43-
let mut generator = TypeLivenessGenerator {
44-
cx,
45-
mir,
46-
liveness,
47-
flow_inits,
48-
move_data,
49-
drop_data: FxHashMap(),
50-
map: &NllLivenessMap::compute(mir),
51-
};
52-
53-
for bb in mir.basic_blocks().indices() {
54-
generator.add_liveness_constraints(bb);
41+
) -> (LivenessResults<LocalWithRegion>, NllLivenessMap) {
42+
let liveness_map = NllLivenessMap::compute(&mir);
43+
let liveness = LivenessResults::compute(mir, &liveness_map);
44+
45+
{
46+
let mut generator = TypeLivenessGenerator {
47+
cx,
48+
mir,
49+
liveness: &liveness,
50+
flow_inits,
51+
move_data,
52+
drop_data: FxHashMap(),
53+
map: &liveness_map,
54+
};
55+
56+
for bb in mir.basic_blocks().indices() {
57+
generator.add_liveness_constraints(bb);
58+
}
5559
}
60+
61+
(liveness, liveness_map)
5662
}
5763

5864
struct TypeLivenessGenerator<'gen, 'typeck, 'flow, 'gcx, 'tcx>

src/librustc_mir/borrow_check/nll/type_check/mod.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use borrow_check::borrow_set::BorrowSet;
1515
use borrow_check::location::LocationTable;
1616
use borrow_check::nll::constraints::{ConstraintSet, OutlivesConstraint};
1717
use borrow_check::nll::facts::AllFacts;
18+
use borrow_check::nll::liveness_map::NllLivenessMap;
1819
use borrow_check::nll::region_infer::values::{RegionValueElements, LivenessValues};
1920
use borrow_check::nll::region_infer::{ClosureRegionRequirementsExt, TypeTest};
2021
use borrow_check::nll::type_check::free_region_relations::{CreateResult, UniversalRegionRelations};
@@ -115,16 +116,12 @@ pub(crate) fn type_check<'gcx, 'tcx>(
115116
universal_regions: &Rc<UniversalRegions<'tcx>>,
116117
location_table: &LocationTable,
117118
borrow_set: &BorrowSet<'tcx>,
118-
liveness: &LivenessResults<LocalWithRegion>,
119119
all_facts: &mut Option<AllFacts>,
120120
flow_inits: &mut FlowAtLocation<MaybeInitializedPlaces<'_, 'gcx, 'tcx>>,
121121
move_data: &MoveData<'tcx>,
122122
elements: &Rc<RegionValueElements>,
123123
errors_buffer: &mut Vec<Diagnostic>,
124-
) -> (
125-
MirTypeckRegionConstraints<'tcx>,
126-
Rc<UniversalRegionRelations<'tcx>>,
127-
) {
124+
) -> MirTypeckResults<'tcx> {
128125
let implicit_region_bound = infcx.tcx.mk_region(ty::ReVar(universal_regions.fr_fn_body));
129126
let mut constraints = MirTypeckRegionConstraints {
130127
liveness_constraints: LivenessValues::new(elements),
@@ -147,7 +144,7 @@ pub(crate) fn type_check<'gcx, 'tcx>(
147144
all_facts,
148145
);
149146

150-
{
147+
let (liveness, liveness_map) = {
151148
let mut borrowck_context = BorrowCheckContext {
152149
universal_regions,
153150
location_table,
@@ -166,22 +163,27 @@ pub(crate) fn type_check<'gcx, 'tcx>(
166163
Some(&mut borrowck_context),
167164
Some(errors_buffer),
168165
|cx| {
169-
liveness::generate(cx, mir, liveness, flow_inits, move_data);
170166
cx.equate_inputs_and_outputs(
171167
mir,
172168
mir_def_id,
173169
universal_regions,
174170
&universal_region_relations,
175171
&normalized_inputs_and_output,
176172
);
173+
liveness::generate(cx, mir, flow_inits, move_data)
177174
},
178-
);
179-
}
175+
)
176+
};
180177

181-
(constraints, universal_region_relations)
178+
MirTypeckResults {
179+
constraints,
180+
universal_region_relations,
181+
liveness,
182+
liveness_map,
183+
}
182184
}
183185

184-
fn type_check_internal<'a, 'gcx, 'tcx, F>(
186+
fn type_check_internal<'a, 'gcx, 'tcx, R>(
185187
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
186188
mir_def_id: DefId,
187189
param_env: ty::ParamEnv<'gcx>,
@@ -190,10 +192,8 @@ fn type_check_internal<'a, 'gcx, 'tcx, F>(
190192
implicit_region_bound: Option<ty::Region<'tcx>>,
191193
borrowck_context: Option<&'a mut BorrowCheckContext<'a, 'tcx>>,
192194
errors_buffer: Option<&mut Vec<Diagnostic>>,
193-
mut extra: F,
194-
) where
195-
F: FnMut(&mut TypeChecker<'a, 'gcx, 'tcx>),
196-
{
195+
mut extra: impl FnMut(&mut TypeChecker<'a, 'gcx, 'tcx>) -> R,
196+
) -> R where {
197197
let mut checker = TypeChecker::new(
198198
infcx,
199199
mir,
@@ -214,7 +214,7 @@ fn type_check_internal<'a, 'gcx, 'tcx, F>(
214214
checker.typeck_mir(mir, errors_buffer);
215215
}
216216

217-
extra(&mut checker);
217+
extra(&mut checker)
218218
}
219219

220220
fn mirbug(tcx: TyCtxt, span: Span, msg: &str) {
@@ -655,6 +655,13 @@ struct BorrowCheckContext<'a, 'tcx: 'a> {
655655
constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
656656
}
657657

658+
crate struct MirTypeckResults<'tcx> {
659+
crate constraints: MirTypeckRegionConstraints<'tcx>,
660+
crate universal_region_relations: Rc<UniversalRegionRelations<'tcx>>,
661+
crate liveness: LivenessResults<LocalWithRegion>,
662+
crate liveness_map: NllLivenessMap,
663+
}
664+
658665
/// A collection of region constraints that must be satisfied for the
659666
/// program to be considered well-typed.
660667
crate struct MirTypeckRegionConstraints<'tcx> {

0 commit comments

Comments
 (0)