Skip to content

Commit 8085a8b

Browse files
authored
Rollup merge of #134378 - lqd:polonius-next-episode-2, r=jackh726
An octuple of polonius fact generation cleanups This PR is extracted from #134268 for easier review and contains its first 8 commits. They have already been reviewed by `@jackh726` over there. r? `@jackh726`
2 parents a49c303 + 8562497 commit 8085a8b

File tree

9 files changed

+240
-260
lines changed

9 files changed

+240
-260
lines changed

compiler/rustc_borrowck/src/nll.rs

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ pub(crate) fn compute_regions<'a, 'tcx>(
124124
borrow_set,
125125
move_data,
126126
&universal_region_relations,
127+
&constraints,
127128
);
128129

129130
let mut regioncx = RegionInferenceContext::new(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
2+
use rustc_middle::mir::{Body, Local, Location, Place};
3+
use rustc_middle::ty::TyCtxt;
4+
use rustc_mir_dataflow::move_paths::{LookupResult, MoveData};
5+
use tracing::debug;
6+
7+
use crate::def_use::{self, DefUse};
8+
use crate::facts::AllFacts;
9+
use crate::location::{LocationIndex, LocationTable};
10+
use crate::universal_regions::UniversalRegions;
11+
12+
/// Emit polonius facts for variable defs, uses, drops, and path accesses.
13+
pub(crate) fn emit_access_facts<'tcx>(
14+
tcx: TyCtxt<'tcx>,
15+
facts: &mut AllFacts,
16+
body: &Body<'tcx>,
17+
location_table: &LocationTable,
18+
move_data: &MoveData<'tcx>,
19+
universal_regions: &UniversalRegions<'tcx>,
20+
) {
21+
let mut extractor = AccessFactsExtractor { facts, move_data, location_table };
22+
extractor.visit_body(body);
23+
24+
for (local, local_decl) in body.local_decls.iter_enumerated() {
25+
debug!("add use_of_var_derefs_origin facts - local={:?}, type={:?}", local, local_decl.ty);
26+
tcx.for_each_free_region(&local_decl.ty, |region| {
27+
let region_vid = universal_regions.to_region_vid(region);
28+
facts.use_of_var_derefs_origin.push((local, region_vid.into()));
29+
});
30+
}
31+
}
32+
33+
/// MIR visitor extracting point-wise facts about accesses.
34+
struct AccessFactsExtractor<'a, 'tcx> {
35+
facts: &'a mut AllFacts,
36+
move_data: &'a MoveData<'tcx>,
37+
location_table: &'a LocationTable,
38+
}
39+
40+
impl<'tcx> AccessFactsExtractor<'_, 'tcx> {
41+
fn location_to_index(&self, location: Location) -> LocationIndex {
42+
self.location_table.mid_index(location)
43+
}
44+
}
45+
46+
impl<'a, 'tcx> Visitor<'tcx> for AccessFactsExtractor<'a, 'tcx> {
47+
fn visit_local(&mut self, local: Local, context: PlaceContext, location: Location) {
48+
match def_use::categorize(context) {
49+
Some(DefUse::Def) => {
50+
debug!("AccessFactsExtractor - emit def");
51+
self.facts.var_defined_at.push((local, self.location_to_index(location)));
52+
}
53+
Some(DefUse::Use) => {
54+
debug!("AccessFactsExtractor - emit use");
55+
self.facts.var_used_at.push((local, self.location_to_index(location)));
56+
}
57+
Some(DefUse::Drop) => {
58+
debug!("AccessFactsExtractor - emit drop");
59+
self.facts.var_dropped_at.push((local, self.location_to_index(location)));
60+
}
61+
_ => (),
62+
}
63+
}
64+
65+
fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
66+
self.super_place(place, context, location);
67+
68+
match context {
69+
PlaceContext::NonMutatingUse(_)
70+
| PlaceContext::MutatingUse(MutatingUseContext::Borrow) => {
71+
let path = match self.move_data.rev_lookup.find(place.as_ref()) {
72+
LookupResult::Exact(path) | LookupResult::Parent(Some(path)) => path,
73+
_ => {
74+
// There's no path access to emit.
75+
return;
76+
}
77+
};
78+
debug!("AccessFactsExtractor - emit path access ({path:?}, {location:?})");
79+
self.facts.path_accessed_at_base.push((path, self.location_to_index(location)));
80+
}
81+
82+
_ => {}
83+
}
84+
}
85+
}

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ use crate::{
2121
/// Emit `loan_invalidated_at` facts.
2222
pub(super) fn emit_loan_invalidations<'tcx>(
2323
tcx: TyCtxt<'tcx>,
24-
all_facts: &mut AllFacts,
25-
location_table: &LocationTable,
24+
facts: &mut AllFacts,
2625
body: &Body<'tcx>,
26+
location_table: &LocationTable,
2727
borrow_set: &BorrowSet<'tcx>,
2828
) {
2929
let dominators = body.basic_blocks.dominators();
3030
let mut visitor =
31-
LoanInvalidationsGenerator { all_facts, borrow_set, tcx, location_table, body, dominators };
31+
LoanInvalidationsGenerator { facts, borrow_set, tcx, location_table, body, dominators };
3232
visitor.visit_body(body);
3333
}
3434

3535
struct LoanInvalidationsGenerator<'a, 'tcx> {
3636
tcx: TyCtxt<'tcx>,
37-
all_facts: &'a mut AllFacts,
38-
location_table: &'a LocationTable,
37+
facts: &'a mut AllFacts,
3938
body: &'a Body<'tcx>,
39+
location_table: &'a LocationTable,
4040
dominators: &'a Dominators<BasicBlock>,
4141
borrow_set: &'a BorrowSet<'tcx>,
4242
}
@@ -151,7 +151,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LoanInvalidationsGenerator<'a, 'tcx> {
151151
let resume = self.location_table.start_index(resume.start_location());
152152
for (i, data) in borrow_set.iter_enumerated() {
153153
if borrow_of_local_data(data.borrowed_place) {
154-
self.all_facts.loan_invalidated_at.push((resume, i));
154+
self.facts.loan_invalidated_at.push((resume, i));
155155
}
156156
}
157157

@@ -165,7 +165,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LoanInvalidationsGenerator<'a, 'tcx> {
165165
let start = self.location_table.start_index(location);
166166
for (i, data) in borrow_set.iter_enumerated() {
167167
if borrow_of_local_data(data.borrowed_place) {
168-
self.all_facts.loan_invalidated_at.push((start, i));
168+
self.facts.loan_invalidated_at.push((start, i));
169169
}
170170
}
171171
}
@@ -409,7 +409,7 @@ impl<'a, 'tcx> LoanInvalidationsGenerator<'a, 'tcx> {
409409
/// Generates a new `loan_invalidated_at(L, B)` fact.
410410
fn emit_loan_invalidated_at(&mut self, b: BorrowIndex, l: Location) {
411411
let lidx = self.location_table.start_index(l);
412-
self.all_facts.loan_invalidated_at.push((lidx, b));
412+
self.facts.loan_invalidated_at.push((lidx, b));
413413
}
414414

415415
fn check_activations(&mut self, location: Location) {

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ use crate::places_conflict;
1414
/// Emit `loan_killed_at` and `cfg_edge` facts at the same time.
1515
pub(super) fn emit_loan_kills<'tcx>(
1616
tcx: TyCtxt<'tcx>,
17-
all_facts: &mut AllFacts,
18-
location_table: &LocationTable,
17+
facts: &mut AllFacts,
1918
body: &Body<'tcx>,
19+
location_table: &LocationTable,
2020
borrow_set: &BorrowSet<'tcx>,
2121
) {
22-
let mut visitor = LoanKillsGenerator { borrow_set, tcx, location_table, all_facts, body };
22+
let mut visitor = LoanKillsGenerator { borrow_set, tcx, location_table, facts, body };
2323
for (bb, data) in body.basic_blocks.iter_enumerated() {
2424
visitor.visit_basic_block_data(bb, data);
2525
}
2626
}
2727

2828
struct LoanKillsGenerator<'a, 'tcx> {
2929
tcx: TyCtxt<'tcx>,
30-
all_facts: &'a mut AllFacts,
30+
facts: &'a mut AllFacts,
3131
location_table: &'a LocationTable,
3232
borrow_set: &'a BorrowSet<'tcx>,
3333
body: &'a Body<'tcx>,
@@ -36,12 +36,12 @@ struct LoanKillsGenerator<'a, 'tcx> {
3636
impl<'a, 'tcx> Visitor<'tcx> for LoanKillsGenerator<'a, 'tcx> {
3737
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
3838
// Also record CFG facts here.
39-
self.all_facts.cfg_edge.push((
39+
self.facts.cfg_edge.push((
4040
self.location_table.start_index(location),
4141
self.location_table.mid_index(location),
4242
));
4343

44-
self.all_facts.cfg_edge.push((
44+
self.facts.cfg_edge.push((
4545
self.location_table.mid_index(location),
4646
self.location_table.start_index(location.successor_within_block()),
4747
));
@@ -63,15 +63,15 @@ impl<'a, 'tcx> Visitor<'tcx> for LoanKillsGenerator<'a, 'tcx> {
6363

6464
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
6565
// Also record CFG facts here.
66-
self.all_facts.cfg_edge.push((
66+
self.facts.cfg_edge.push((
6767
self.location_table.start_index(location),
6868
self.location_table.mid_index(location),
6969
));
7070

7171
let successor_blocks = terminator.successors();
72-
self.all_facts.cfg_edge.reserve(successor_blocks.size_hint().0);
72+
self.facts.cfg_edge.reserve(successor_blocks.size_hint().0);
7373
for successor_block in successor_blocks {
74-
self.all_facts.cfg_edge.push((
74+
self.facts.cfg_edge.push((
7575
self.location_table.mid_index(location),
7676
self.location_table.start_index(successor_block.start_location()),
7777
));
@@ -128,7 +128,7 @@ impl<'tcx> LoanKillsGenerator<'_, 'tcx> {
128128

129129
if places_conflict {
130130
let location_index = self.location_table.mid_index(location);
131-
self.all_facts.loan_killed_at.push((borrow_index, location_index));
131+
self.facts.loan_killed_at.push((borrow_index, location_index));
132132
}
133133
}
134134
}
@@ -140,9 +140,9 @@ impl<'tcx> LoanKillsGenerator<'_, 'tcx> {
140140
fn record_killed_borrows_for_local(&mut self, local: Local, location: Location) {
141141
if let Some(borrow_indices) = self.borrow_set.local_map.get(&local) {
142142
let location_index = self.location_table.mid_index(location);
143-
self.all_facts.loan_killed_at.reserve(borrow_indices.len());
143+
self.facts.loan_killed_at.reserve(borrow_indices.len());
144144
for &borrow_index in borrow_indices {
145-
self.all_facts.loan_killed_at.push((borrow_index, location_index));
145+
self.facts.loan_killed_at.push((borrow_index, location_index));
146146
}
147147
}
148148
}

0 commit comments

Comments
 (0)