Skip to content

Commit 459a616

Browse files
committed
extract polonius universal regions fact generation
1 parent 3de68e0 commit 459a616

File tree

2 files changed

+50
-35
lines changed

2 files changed

+50
-35
lines changed

compiler/rustc_borrowck/src/nll.rs

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -127,42 +127,13 @@ pub(crate) fn compute_regions<'cx, 'tcx>(
127127

128128
if let Some(all_facts) = &mut all_facts {
129129
let _prof_timer = infcx.tcx.prof.generic_activity("polonius_fact_generation");
130-
all_facts.universal_region.extend(universal_regions.universal_regions());
131130
polonius::emit_move_facts(all_facts, move_data, location_table, body);
132-
133-
// Emit universal regions facts, and their relations, for Polonius.
134-
//
135-
// 1: universal regions are modeled in Polonius as a pair:
136-
// - the universal region vid itself.
137-
// - a "placeholder loan" associated to this universal region. Since they don't exist in
138-
// the `borrow_set`, their `BorrowIndex` are synthesized as the universal region index
139-
// added to the existing number of loans, as if they succeeded them in the set.
140-
//
141-
let borrow_count = borrow_set.len();
142-
debug!(
143-
"compute_regions: polonius placeholders, num_universals={}, borrow_count={}",
144-
universal_regions.len(),
145-
borrow_count
131+
polonius::emit_universal_region_facts(
132+
all_facts,
133+
borrow_set,
134+
&universal_regions,
135+
&universal_region_relations,
146136
);
147-
148-
for universal_region in universal_regions.universal_regions() {
149-
let universal_region_idx = universal_region.index();
150-
let placeholder_loan_idx = borrow_count + universal_region_idx;
151-
all_facts.placeholder.push((universal_region, placeholder_loan_idx.into()));
152-
}
153-
154-
// 2: the universal region relations `outlives` constraints are emitted as
155-
// `known_placeholder_subset` facts.
156-
for (fr1, fr2) in universal_region_relations.known_outlives() {
157-
if fr1 != fr2 {
158-
debug!(
159-
"compute_regions: emitting polonius `known_placeholder_subset` \
160-
fr1={:?}, fr2={:?}",
161-
fr1, fr2
162-
);
163-
all_facts.known_placeholder_subset.push((fr1, fr2));
164-
}
165-
}
166137
}
167138

168139
// Create the region inference context, taking ownership of the

compiler/rustc_borrowck/src/nll/polonius.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
use rustc_middle::mir::{Body, LocalKind, Location, START_BLOCK};
77
use rustc_mir_dataflow::move_paths::{InitKind, InitLocation, MoveData};
88

9+
use crate::borrow_set::BorrowSet;
910
use crate::facts::AllFacts;
1011
use crate::location::LocationTable;
12+
use crate::type_check::free_region_relations::UniversalRegionRelations;
13+
use crate::universal_regions::UniversalRegions;
1114

12-
/// Emit polonius facts needed for move/init analysis: moves and assignments.
15+
/// Emit facts needed for move/init analysis: moves and assignments.
1316
pub(crate) fn emit_move_facts(
1417
all_facts: &mut AllFacts,
1518
move_data: &MoveData<'_>,
@@ -82,3 +85,44 @@ pub(crate) fn emit_move_facts(
8285
.path_moved_at_base
8386
.extend(move_data.moves.iter().map(|mo| (mo.path, location_table.mid_index(mo.source))));
8487
}
88+
89+
/// Emit universal regions facts, and their relations.
90+
pub(crate) fn emit_universal_region_facts(
91+
all_facts: &mut AllFacts,
92+
borrow_set: &BorrowSet<'_>,
93+
universal_regions: &UniversalRegions<'_>,
94+
universal_region_relations: &UniversalRegionRelations<'_>,
95+
) {
96+
// 1: universal regions are modeled in Polonius as a pair:
97+
// - the universal region vid itself.
98+
// - a "placeholder loan" associated to this universal region. Since they don't exist in
99+
// the `borrow_set`, their `BorrowIndex` are synthesized as the universal region index
100+
// added to the existing number of loans, as if they succeeded them in the set.
101+
//
102+
all_facts.universal_region.extend(universal_regions.universal_regions());
103+
let borrow_count = borrow_set.len();
104+
debug!(
105+
"emit_universal_region_facts: polonius placeholders, num_universals={}, borrow_count={}",
106+
universal_regions.len(),
107+
borrow_count
108+
);
109+
110+
for universal_region in universal_regions.universal_regions() {
111+
let universal_region_idx = universal_region.index();
112+
let placeholder_loan_idx = borrow_count + universal_region_idx;
113+
all_facts.placeholder.push((universal_region, placeholder_loan_idx.into()));
114+
}
115+
116+
// 2: the universal region relations `outlives` constraints are emitted as
117+
// `known_placeholder_subset` facts.
118+
for (fr1, fr2) in universal_region_relations.known_outlives() {
119+
if fr1 != fr2 {
120+
debug!(
121+
"emit_universal_region_facts: emitting polonius `known_placeholder_subset` \
122+
fr1={:?}, fr2={:?}",
123+
fr1, fr2
124+
);
125+
all_facts.known_placeholder_subset.push((fr1, fr2));
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)