Skip to content

Commit cdb95b0

Browse files
committed
build up the placeholder indices as we go
Avoids a linear walk over the regions at the end.
1 parent 0887456 commit cdb95b0

File tree

5 files changed

+43
-31
lines changed

5 files changed

+43
-31
lines changed

src/librustc_mir/borrow_check/nll/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
107107
// Run the MIR type-checker.
108108
let MirTypeckResults {
109109
constraints,
110+
placeholder_indices,
110111
universal_region_relations,
111112
} = type_check::type_check(
112113
infcx,
@@ -122,6 +123,8 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
122123
elements,
123124
);
124125

126+
let placeholder_indices = Rc::new(placeholder_indices);
127+
125128
if let Some(all_facts) = &mut all_facts {
126129
all_facts
127130
.universal_region
@@ -150,6 +153,7 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
150153
let mut regioncx = RegionInferenceContext::new(
151154
var_origins,
152155
universal_regions,
156+
placeholder_indices,
153157
universal_region_relations,
154158
mir,
155159
outlives_constraints,

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
183183
pub(crate) fn new(
184184
var_infos: VarInfos,
185185
universal_regions: Rc<UniversalRegions<'tcx>>,
186+
placeholder_indices: Rc<PlaceholderIndices>,
186187
universal_region_relations: Rc<UniversalRegionRelations<'tcx>>,
187188
_mir: &Mir<'tcx>,
188189
outlives_constraints: ConstraintSet,
@@ -196,22 +197,13 @@ impl<'tcx> RegionInferenceContext<'tcx> {
196197
.map(|info| RegionDefinition::new(info.universe, info.origin))
197198
.collect();
198199

199-
// Compute the max universe used anywhere amongst the regions.
200-
let placeholder_indices: PlaceholderIndices = definitions
201-
.iter()
202-
.filter_map(|d| match d.origin {
203-
NLLRegionVariableOrigin::Placeholder(placeholder) => Some(placeholder),
204-
_ => None,
205-
})
206-
.collect();
207-
208200
let constraints = Rc::new(outlives_constraints); // freeze constraints
209201
let constraint_graph = Rc::new(constraints.graph(definitions.len()));
210202
let fr_static = universal_regions.fr_static;
211203
let constraint_sccs = Rc::new(constraints.compute_sccs(&constraint_graph, fr_static));
212204

213205
let mut scc_values =
214-
RegionValues::new(elements, universal_regions.len(), placeholder_indices);
206+
RegionValues::new(elements, universal_regions.len(), &placeholder_indices);
215207

216208
for region in liveness_constraints.rows() {
217209
let scc = constraint_sccs.scc(region);

src/librustc_mir/borrow_check/nll/region_infer/values.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,13 @@ impl<N: Idx> RegionValues<N> {
302302
crate fn new(
303303
elements: &Rc<RegionValueElements>,
304304
num_universal_regions: usize,
305-
placeholder_indices: PlaceholderIndices,
305+
placeholder_indices: &Rc<PlaceholderIndices>,
306306
) -> Self {
307307
let num_placeholders = placeholder_indices.len();
308308
Self {
309309
elements: elements.clone(),
310310
points: SparseBitMatrix::new(elements.num_points),
311-
placeholder_indices: Rc::new(placeholder_indices),
311+
placeholder_indices: placeholder_indices.clone(),
312312
free_regions: SparseBitMatrix::new(num_universal_regions),
313313
placeholders: SparseBitMatrix::new(num_placeholders),
314314
}

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

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use borrow_check::borrow_set::BorrowSet;
1515
use borrow_check::location::LocationTable;
1616
use borrow_check::nll::constraints::{ConstraintCategory, ConstraintSet, OutlivesConstraint};
1717
use borrow_check::nll::facts::AllFacts;
18-
use borrow_check::nll::region_infer::values::{LivenessValues, RegionValueElements};
18+
use borrow_check::nll::region_infer::values::LivenessValues;
19+
use borrow_check::nll::region_infer::values::PlaceholderIndices;
20+
use borrow_check::nll::region_infer::values::RegionValueElements;
1921
use borrow_check::nll::region_infer::{ClosureRegionRequirementsExt, TypeTest};
2022
use borrow_check::nll::renumber;
2123
use borrow_check::nll::type_check::free_region_relations::{
@@ -42,13 +44,13 @@ use rustc::traits::{ObligationCause, PredicateObligations};
4244
use rustc::ty::fold::TypeFoldable;
4345
use rustc::ty::subst::Subst;
4446
use rustc::ty::{self, CanonicalTy, RegionVid, ToPolyTraitRef, Ty, TyCtxt, TyKind};
45-
use std::{fmt, iter};
4647
use std::rc::Rc;
48+
use std::{fmt, iter};
4749
use syntax_pos::{Span, DUMMY_SP};
4850
use transform::{MirPass, MirSource};
4951

50-
use rustc_data_structures::fx::FxHashSet;
5152
use either::Either;
53+
use rustc_data_structures::fx::FxHashSet;
5254

5355
macro_rules! span_mirbug {
5456
($context:expr, $elem:expr, $($message:tt)*) => ({
@@ -128,6 +130,7 @@ pub(crate) fn type_check<'gcx, 'tcx>(
128130
outlives_constraints: ConstraintSet::default(),
129131
type_tests: Vec::default(),
130132
};
133+
let mut placeholder_indices = PlaceholderIndices::default();
131134

132135
let CreateResult {
133136
universal_region_relations,
@@ -147,6 +150,7 @@ pub(crate) fn type_check<'gcx, 'tcx>(
147150
borrow_set,
148151
all_facts,
149152
constraints: &mut constraints,
153+
placeholder_indices: &mut placeholder_indices,
150154
};
151155

152156
type_check_internal(
@@ -162,12 +166,15 @@ pub(crate) fn type_check<'gcx, 'tcx>(
162166
cx.equate_inputs_and_outputs(mir, universal_regions, &normalized_inputs_and_output);
163167
liveness::generate(cx, mir, elements, flow_inits, move_data, location_table);
164168

165-
cx.borrowck_context.as_mut().map(|bcx| translate_outlives_facts(bcx));
169+
cx.borrowck_context
170+
.as_mut()
171+
.map(|bcx| translate_outlives_facts(bcx));
166172
},
167173
);
168174

169175
MirTypeckResults {
170176
constraints,
177+
placeholder_indices,
171178
universal_region_relations,
172179
}
173180
}
@@ -210,21 +217,25 @@ fn type_check_internal<'a, 'gcx, 'tcx, R>(
210217
fn translate_outlives_facts(cx: &mut BorrowCheckContext) {
211218
if let Some(facts) = cx.all_facts {
212219
let location_table = cx.location_table;
213-
facts.outlives.extend(
214-
cx.constraints.outlives_constraints.iter().flat_map(|constraint: &OutlivesConstraint| {
215-
if let Some(from_location) = constraint.locations.from_location() {
216-
Either::Left(iter::once((
217-
constraint.sup,
218-
constraint.sub,
219-
location_table.mid_index(from_location),
220-
)))
221-
} else {
222-
Either::Right(location_table.all_points().map(move |location| {
223-
(constraint.sup, constraint.sub, location)
224-
}))
225-
}
226-
})
227-
);
220+
facts
221+
.outlives
222+
.extend(cx.constraints.outlives_constraints.iter().flat_map(
223+
|constraint: &OutlivesConstraint| {
224+
if let Some(from_location) = constraint.locations.from_location() {
225+
Either::Left(iter::once((
226+
constraint.sup,
227+
constraint.sub,
228+
location_table.mid_index(from_location),
229+
)))
230+
} else {
231+
Either::Right(
232+
location_table
233+
.all_points()
234+
.map(move |location| (constraint.sup, constraint.sub, location)),
235+
)
236+
}
237+
},
238+
));
228239
}
229240
}
230241

@@ -718,10 +729,12 @@ struct BorrowCheckContext<'a, 'tcx: 'a> {
718729
all_facts: &'a mut Option<AllFacts>,
719730
borrow_set: &'a BorrowSet<'tcx>,
720731
constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
732+
placeholder_indices: &'a mut PlaceholderIndices,
721733
}
722734

723735
crate struct MirTypeckResults<'tcx> {
724736
crate constraints: MirTypeckRegionConstraints<'tcx>,
737+
crate placeholder_indices: PlaceholderIndices,
725738
crate universal_region_relations: Rc<UniversalRegionRelations<'tcx>>,
726739
}
727740

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, '_, 'tcx> {
217217

218218
fn next_placeholder_region(&mut self, placeholder: ty::Placeholder) -> ty::Region<'tcx> {
219219
let origin = NLLRegionVariableOrigin::Placeholder(placeholder);
220+
if let Some(borrowck_context) = &mut self.borrowck_context {
221+
borrowck_context.placeholder_indices.insert(placeholder);
222+
}
220223
self.infcx.next_nll_region_var(origin)
221224
}
222225

0 commit comments

Comments
 (0)