Skip to content

Commit e155646

Browse files
committed
Make drop-use fact collection simpler for polonius
This shunts all the complexity of siphoning off the drop-use facts into `LivenessResults::add_extra_drop_facts()`, which may or may not be a good approach.
1 parent 077a821 commit e155646

File tree

4 files changed

+34
-41
lines changed

4 files changed

+34
-41
lines changed

compiler/rustc_borrowck/src/type_check/liveness/mod.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use std::rc::Rc;
1414
use crate::{
1515
constraints::OutlivesConstraintSet,
1616
facts::{AllFacts, AllFactsExt},
17-
location::LocationTable,
1817
region_infer::values::LivenessValues,
1918
universal_regions::UniversalRegions,
2019
};
@@ -39,7 +38,6 @@ pub(super) fn generate<'mir, 'tcx>(
3938
elements: &Rc<DenseLocationMap>,
4039
flow_inits: &mut ResultsCursor<'mir, 'tcx, MaybeInitializedPlaces<'mir, 'tcx>>,
4140
move_data: &MoveData<'tcx>,
42-
location_table: &LocationTable,
4341
use_polonius: bool,
4442
) {
4543
debug!("liveness::generate");
@@ -53,11 +51,9 @@ pub(super) fn generate<'mir, 'tcx>(
5351
compute_relevant_live_locals(typeck.tcx(), &free_regions, body);
5452
let facts_enabled = use_polonius || AllFacts::enabled(typeck.tcx());
5553

56-
let polonius_drop_used = facts_enabled.then(|| {
57-
let mut drop_used = Vec::new();
58-
polonius::populate_access_facts(typeck, body, location_table, move_data, &mut drop_used);
59-
drop_used
60-
});
54+
if facts_enabled {
55+
polonius::populate_access_facts(typeck, body, move_data);
56+
};
6157

6258
trace::trace(
6359
typeck,
@@ -67,7 +63,6 @@ pub(super) fn generate<'mir, 'tcx>(
6763
move_data,
6864
relevant_live_locals,
6965
boring_locals,
70-
polonius_drop_used,
7166
);
7267

7368
// Mark regions that should be live where they appear within rvalues or within a call: like

compiler/rustc_borrowck/src/type_check/liveness/polonius.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct UseFactsExtractor<'me, 'tcx> {
1414
var_defined_at: &'me mut VarPointRelation,
1515
var_used_at: &'me mut VarPointRelation,
1616
location_table: &'me LocationTable,
17-
var_dropped_at: &'me mut Vec<(Local, Location)>,
17+
var_dropped_at: &'me mut VarPointRelation,
1818
move_data: &'me MoveData<'tcx>,
1919
path_accessed_at_base: &'me mut PathPointRelation,
2020
}
@@ -37,7 +37,7 @@ impl<'tcx> UseFactsExtractor<'_, 'tcx> {
3737

3838
fn insert_drop_use(&mut self, local: Local, location: Location) {
3939
debug!("UseFactsExtractor::insert_drop_use()");
40-
self.var_dropped_at.push((local, location));
40+
self.var_dropped_at.push((local, self.location_to_index(location)));
4141
}
4242

4343
fn insert_path_access(&mut self, path: MovePathIndex, location: Location) {
@@ -85,33 +85,22 @@ impl<'a, 'tcx> Visitor<'tcx> for UseFactsExtractor<'a, 'tcx> {
8585
pub(super) fn populate_access_facts<'a, 'tcx>(
8686
typeck: &mut TypeChecker<'a, 'tcx>,
8787
body: &Body<'tcx>,
88-
location_table: &LocationTable,
8988
move_data: &MoveData<'tcx>,
90-
// FIXME: this is an inelegant way of squirreling away a
91-
// copy of `var_dropped_at` in the original `Location` format
92-
// for later use in `trace::trace()`, which updates some liveness-
93-
// internal data based on what Polonius saw.
94-
// Ideally, that part would access the Polonius facts directly, and this
95-
// would be regular facts gathering.
96-
dropped_at: &mut Vec<(Local, Location)>,
9789
) {
9890
debug!("populate_access_facts()");
91+
let location_table = typeck.borrowck_context.location_table;
9992

10093
if let Some(facts) = typeck.borrowck_context.all_facts.as_mut() {
10194
let mut extractor = UseFactsExtractor {
10295
var_defined_at: &mut facts.var_defined_at,
10396
var_used_at: &mut facts.var_used_at,
104-
var_dropped_at: dropped_at,
97+
var_dropped_at: &mut facts.var_dropped_at,
10598
path_accessed_at_base: &mut facts.path_accessed_at_base,
10699
location_table,
107100
move_data,
108101
};
109102
extractor.visit_body(body);
110103

111-
facts.var_dropped_at.extend(
112-
dropped_at.iter().map(|&(local, location)| (local, location_table.mid_index(location))),
113-
);
114-
115104
for (local, local_decl) in body.local_decls.iter_enumerated() {
116105
debug!(
117106
"add use_of_var_derefs_origin facts - local={:?}, type={:?}",

compiler/rustc_borrowck/src/type_check/liveness/trace.rs

+26-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
1616
use rustc_mir_dataflow::move_paths::{HasMoveData, MoveData, MovePathIndex};
1717
use rustc_mir_dataflow::ResultsCursor;
1818

19+
use crate::location::RichLocation;
1920
use crate::{
2021
region_infer::values::{self, LiveLoans},
2122
type_check::liveness::local_use_map::LocalUseMap,
@@ -46,7 +47,6 @@ pub(super) fn trace<'mir, 'tcx>(
4647
move_data: &MoveData<'tcx>,
4748
relevant_live_locals: Vec<Local>,
4849
boring_locals: Vec<Local>,
49-
polonius_drop_used: Option<Vec<(Local, Location)>>,
5050
) {
5151
let local_use_map = &LocalUseMap::build(&relevant_live_locals, elements, body);
5252

@@ -81,6 +81,8 @@ pub(super) fn trace<'mir, 'tcx>(
8181
borrowck_context.constraints.liveness_constraints.loans = Some(live_loans);
8282
};
8383

84+
let polonius_facts_gathered = typeck.borrowck_context.all_facts.is_some();
85+
8486
let cx = LivenessContext {
8587
typeck,
8688
body,
@@ -93,8 +95,8 @@ pub(super) fn trace<'mir, 'tcx>(
9395

9496
let mut results = LivenessResults::new(cx);
9597

96-
if let Some(drop_used) = polonius_drop_used {
97-
results.add_extra_drop_facts(drop_used, relevant_live_locals.iter().copied().collect())
98+
if polonius_facts_gathered {
99+
results.add_extra_drop_facts(relevant_live_locals.iter().copied().collect());
98100
}
99101

100102
results.compute_for_all_locals(relevant_live_locals);
@@ -218,17 +220,32 @@ impl<'me, 'typeck, 'flow, 'tcx> LivenessResults<'me, 'typeck, 'flow, 'tcx> {
218220
///
219221
/// Add facts for all locals with free regions, since regions may outlive
220222
/// the function body only at certain nodes in the CFG.
221-
fn add_extra_drop_facts(
222-
&mut self,
223-
drop_used: Vec<(Local, Location)>,
224-
relevant_live_locals: FxIndexSet<Local>,
225-
) {
223+
fn add_extra_drop_facts(&mut self, relevant_live_locals: FxIndexSet<Local>) {
224+
let drop_used = self
225+
.cx
226+
.typeck
227+
.borrowck_context
228+
.all_facts
229+
.as_ref()
230+
.map(|facts| facts.var_dropped_at.clone())
231+
.into_iter()
232+
.flatten();
226233
let locations = IntervalSet::new(self.cx.elements.num_points());
227234

228-
for (local, location) in drop_used {
235+
for (local, location_index) in drop_used {
229236
if !relevant_live_locals.contains(&local) {
230237
let local_ty = self.cx.body.local_decls[local].ty;
231238
if local_ty.has_free_regions() {
239+
let location = match self
240+
.cx
241+
.typeck
242+
.borrowck_context
243+
.location_table
244+
.to_location(location_index)
245+
{
246+
RichLocation::Start(l) => l,
247+
RichLocation::Mid(l) => l,
248+
};
232249
self.cx.add_drop_live_facts_for(local, local_ty, &[location], &locations);
233250
}
234251
}

compiler/rustc_borrowck/src/type_check/mod.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,7 @@ pub(crate) fn type_check<'mir, 'tcx>(
188188
checker.equate_inputs_and_outputs(body, universal_regions, &normalized_inputs_and_output);
189189
checker.check_signature_annotation(body);
190190

191-
liveness::generate(
192-
&mut checker,
193-
body,
194-
elements,
195-
flow_inits,
196-
move_data,
197-
location_table,
198-
use_polonius,
199-
);
191+
liveness::generate(&mut checker, body, elements, flow_inits, move_data, use_polonius);
200192

201193
translate_outlives_facts(&mut checker);
202194
let opaque_type_values = infcx.take_opaque_types();

0 commit comments

Comments
 (0)