Skip to content

Commit 1740a5f

Browse files
committed
simplify emit_access_facts and fact generation
- integrate it within existing fact generation instead of being called in typeck - simplify access fact extraction - also remove single use fact emit functions in root fact generation
1 parent 5486857 commit 1740a5f

File tree

3 files changed

+21
-58
lines changed

3 files changed

+21
-58
lines changed

compiler/rustc_borrowck/src/polonius/legacy/accesses.rs

+9-24
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,24 @@
11
use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
22
use rustc_middle::mir::{Body, Local, Location, Place};
33
use rustc_middle::ty::TyCtxt;
4-
use rustc_mir_dataflow::move_paths::{LookupResult, MoveData, MovePathIndex};
4+
use rustc_mir_dataflow::move_paths::{LookupResult, MoveData};
55
use tracing::debug;
66

77
use crate::def_use::{self, DefUse};
88
use crate::facts::AllFacts;
99
use crate::location::{LocationIndex, LocationTable};
1010
use crate::universal_regions::UniversalRegions;
1111

12-
type VarPointRelation = Vec<(Local, LocationIndex)>;
13-
type PathPointRelation = Vec<(MovePathIndex, LocationIndex)>;
14-
1512
/// Emit polonius facts for variable defs, uses, drops, and path accesses.
1613
pub(crate) fn emit_access_facts<'tcx>(
14+
facts: &mut AllFacts,
1715
tcx: TyCtxt<'tcx>,
1816
body: &Body<'tcx>,
1917
move_data: &MoveData<'tcx>,
2018
universal_regions: &UniversalRegions<'tcx>,
2119
location_table: &LocationTable,
22-
all_facts: &mut Option<AllFacts>,
2320
) {
24-
let Some(facts) = all_facts.as_mut() else { return };
25-
let _prof_timer = tcx.prof.generic_activity("polonius_fact_generation");
26-
let mut extractor = AccessFactsExtractor {
27-
var_defined_at: &mut facts.var_defined_at,
28-
var_used_at: &mut facts.var_used_at,
29-
var_dropped_at: &mut facts.var_dropped_at,
30-
path_accessed_at_base: &mut facts.path_accessed_at_base,
31-
location_table,
32-
move_data,
33-
};
21+
let mut extractor = AccessFactsExtractor { facts, move_data, location_table };
3422
extractor.visit_body(body);
3523

3624
for (local, local_decl) in body.local_decls.iter_enumerated() {
@@ -44,12 +32,9 @@ pub(crate) fn emit_access_facts<'tcx>(
4432

4533
/// MIR visitor extracting point-wise facts about accesses.
4634
struct AccessFactsExtractor<'a, 'tcx> {
47-
var_defined_at: &'a mut VarPointRelation,
48-
var_used_at: &'a mut VarPointRelation,
49-
location_table: &'a LocationTable,
50-
var_dropped_at: &'a mut VarPointRelation,
35+
facts: &'a mut AllFacts,
5136
move_data: &'a MoveData<'tcx>,
52-
path_accessed_at_base: &'a mut PathPointRelation,
37+
location_table: &'a LocationTable,
5338
}
5439

5540
impl<'tcx> AccessFactsExtractor<'_, 'tcx> {
@@ -63,15 +48,15 @@ impl<'a, 'tcx> Visitor<'tcx> for AccessFactsExtractor<'a, 'tcx> {
6348
match def_use::categorize(context) {
6449
Some(DefUse::Def) => {
6550
debug!("AccessFactsExtractor - emit def");
66-
self.var_defined_at.push((local, self.location_to_index(location)));
51+
self.facts.var_defined_at.push((local, self.location_to_index(location)));
6752
}
6853
Some(DefUse::Use) => {
6954
debug!("AccessFactsExtractor - emit use");
70-
self.var_used_at.push((local, self.location_to_index(location)));
55+
self.facts.var_used_at.push((local, self.location_to_index(location)));
7156
}
7257
Some(DefUse::Drop) => {
7358
debug!("AccessFactsExtractor - emit drop");
74-
self.var_dropped_at.push((local, self.location_to_index(location)));
59+
self.facts.var_dropped_at.push((local, self.location_to_index(location)));
7560
}
7661
_ => (),
7762
}
@@ -91,7 +76,7 @@ impl<'a, 'tcx> Visitor<'tcx> for AccessFactsExtractor<'a, 'tcx> {
9176
}
9277
};
9378
debug!("AccessFactsExtractor - emit path access ({path:?}, {location:?})");
94-
self.path_accessed_at_base.push((path, self.location_to_index(location)));
79+
self.facts.path_accessed_at_base.push((path, self.location_to_index(location)));
9580
}
9681

9782
_ => {}

compiler/rustc_borrowck/src/polonius/legacy/mod.rs

+12-26
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ mod accesses;
2323
mod loan_invalidations;
2424
mod loan_kills;
2525

26-
pub(crate) use accesses::emit_access_facts;
27-
2826
/// When requested, emit most of the facts needed by polonius:
2927
/// - moves and assignments
3028
/// - universal regions and their relations
3129
/// - CFG points and edges
3230
/// - loan kills
3331
/// - loan invalidations
32+
/// - access facts such as variable definitions, uses, drops, and path accesses
33+
/// - outlives constraints
3434
///
3535
/// The rest of the facts are emitted during typeck and liveness.
3636
pub(crate) fn emit_facts<'tcx>(
@@ -49,8 +49,16 @@ pub(crate) fn emit_facts<'tcx>(
4949
let _prof_timer = tcx.prof.generic_activity("polonius_fact_generation");
5050
emit_move_facts(all_facts, move_data, location_table, body);
5151
emit_universal_region_facts(all_facts, borrow_set, universal_region_relations);
52-
emit_cfg_and_loan_kills_facts(all_facts, tcx, location_table, body, borrow_set);
53-
emit_loan_invalidations_facts(all_facts, tcx, location_table, body, borrow_set);
52+
loan_kills::emit_loan_kills(tcx, all_facts, location_table, body, borrow_set);
53+
loan_invalidations::emit_loan_invalidations(tcx, all_facts, location_table, body, borrow_set);
54+
accesses::emit_access_facts(
55+
all_facts,
56+
tcx,
57+
body,
58+
move_data,
59+
&universal_region_relations.universal_regions,
60+
location_table,
61+
);
5462
}
5563

5664
/// Emit facts needed for move/init analysis: moves and assignments.
@@ -170,28 +178,6 @@ fn emit_universal_region_facts(
170178
}
171179
}
172180

173-
/// Emit facts about loan invalidations.
174-
fn emit_loan_invalidations_facts<'tcx>(
175-
all_facts: &mut AllFacts,
176-
tcx: TyCtxt<'tcx>,
177-
location_table: &LocationTable,
178-
body: &Body<'tcx>,
179-
borrow_set: &BorrowSet<'tcx>,
180-
) {
181-
loan_invalidations::emit_loan_invalidations(tcx, all_facts, location_table, body, borrow_set);
182-
}
183-
184-
/// Emit facts about CFG points and edges, as well as locations where loans are killed.
185-
fn emit_cfg_and_loan_kills_facts<'tcx>(
186-
all_facts: &mut AllFacts,
187-
tcx: TyCtxt<'tcx>,
188-
location_table: &LocationTable,
189-
body: &Body<'tcx>,
190-
borrow_set: &BorrowSet<'tcx>,
191-
) {
192-
loan_kills::emit_loan_kills(tcx, all_facts, location_table, body, borrow_set);
193-
}
194-
195181
/// For every potentially drop()-touched region `region` in `local`'s type
196182
/// (`kind`), emit a `drop_of_var_derefs_origin(local, origin)` fact.
197183
pub(crate) fn emit_drop_facts<'tcx>(

compiler/rustc_borrowck/src/type_check/mod.rs

-8
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,6 @@ pub(crate) fn type_check<'a, 'tcx>(
181181

182182
liveness::generate(&mut checker, body, &elements, flow_inits, move_data);
183183

184-
polonius::legacy::emit_access_facts(
185-
infcx.tcx,
186-
body,
187-
move_data,
188-
&universal_region_relations.universal_regions,
189-
location_table,
190-
checker.all_facts,
191-
);
192184
polonius::legacy::emit_outlives_facts(
193185
infcx.tcx,
194186
checker.constraints,

0 commit comments

Comments
 (0)