Skip to content

Commit 68c2a39

Browse files
nikomatsakispnkfelix
authored andcommitted
free RegionBoundPairs earlier and avoid normalizing twice
Normalization results are memoized, so this may not be worth it, but it seems easy enough to do.
1 parent 490928f commit 68c2a39

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ crate struct UniversalRegionRelations<'tcx> {
5353
/// our special inference variable there, we would mess that up.
5454
type RegionBoundPairs<'tcx> = Vec<(ty::Region<'tcx>, GenericKind<'tcx>)>;
5555

56+
/// As part of computing the free region relations, we also have to
57+
/// normalize the input-output types, which we then need later. So we
58+
/// return those. This vector consists of first the input types and
59+
/// then the output type as the last element.
60+
type NormalizedInputsAndOutput<'tcx> = Vec<Ty<'tcx>>;
61+
62+
crate struct CreateResult<'tcx> {
63+
crate universal_region_relations: Rc<UniversalRegionRelations<'tcx>>,
64+
crate region_bound_pairs: RegionBoundPairs<'tcx>,
65+
crate normalized_inputs_and_output: NormalizedInputsAndOutput<'tcx>,
66+
}
67+
5668
crate fn create(
5769
infcx: &InferCtxt<'_, '_, 'tcx>,
5870
mir_def_id: DefId,
@@ -62,7 +74,7 @@ crate fn create(
6274
universal_regions: &Rc<UniversalRegions<'tcx>>,
6375
constraints: &mut MirTypeckRegionConstraints<'tcx>,
6476
all_facts: &mut Option<AllFacts>,
65-
) -> (Rc<UniversalRegionRelations<'tcx>>, RegionBoundPairs<'tcx>) {
77+
) -> CreateResult<'tcx> {
6678
let mir_node_id = infcx.tcx.hir.as_local_node_id(mir_def_id).unwrap();
6779
UniversalRegionRelationsBuilder {
6880
infcx,
@@ -215,7 +227,7 @@ struct UniversalRegionRelationsBuilder<'this, 'gcx: 'tcx, 'tcx: 'this> {
215227
}
216228

217229
impl UniversalRegionRelationsBuilder<'cx, 'gcx, 'tcx> {
218-
crate fn create(mut self) -> (Rc<UniversalRegionRelations<'tcx>>, RegionBoundPairs<'tcx>) {
230+
crate fn create(mut self) -> CreateResult<'tcx> {
219231
let unnormalized_input_output_tys = self
220232
.universal_regions
221233
.unnormalized_input_tys
@@ -231,6 +243,8 @@ impl UniversalRegionRelationsBuilder<'cx, 'gcx, 'tcx> {
231243
// the `region_bound_pairs` and so forth.
232244
// - After this is done, we'll process the constraints, once
233245
// the `relations` is built.
246+
let mut normalized_inputs_and_output =
247+
Vec::with_capacity(self.universal_regions.unnormalized_input_tys.len() + 1);
234248
let constraint_sets: Vec<_> = unnormalized_input_output_tys
235249
.flat_map(|ty| {
236250
debug!("build: input_or_output={:?}", ty);
@@ -240,6 +254,7 @@ impl UniversalRegionRelationsBuilder<'cx, 'gcx, 'tcx> {
240254
.fully_perform(self.infcx)
241255
.unwrap_or_else(|_| bug!("failed to normalize {:?}", ty));
242256
self.add_implied_bounds(ty);
257+
normalized_inputs_and_output.push(ty);
243258
constraints
244259
})
245260
.collect();
@@ -280,7 +295,11 @@ impl UniversalRegionRelationsBuilder<'cx, 'gcx, 'tcx> {
280295
).convert_all(&data);
281296
}
282297

283-
(Rc::new(self.relations), self.region_bound_pairs)
298+
CreateResult {
299+
universal_region_relations: Rc::new(self.relations),
300+
region_bound_pairs: self.region_bound_pairs,
301+
normalized_inputs_and_output,
302+
}
284303
}
285304

286305
/// Update the type of a single local, which should represent

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

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,24 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
3939
mir_def_id: DefId,
4040
universal_regions: &UniversalRegions<'tcx>,
4141
universal_region_relations: &UniversalRegionRelations<'tcx>,
42+
normalized_inputs_and_output: &[Ty<'tcx>],
4243
) {
4344
let tcx = self.infcx.tcx;
4445

45-
let &UniversalRegions {
46-
unnormalized_output_ty,
47-
unnormalized_input_tys,
48-
..
49-
} = universal_regions;
46+
let (&normalized_output_ty, normalized_input_tys) =
47+
normalized_inputs_and_output.split_last().unwrap();
5048
let infcx = self.infcx;
5149

5250
// Equate expected input tys with those in the MIR.
5351
let argument_locals = (1..).map(Local::new);
54-
for (&unnormalized_input_ty, local) in unnormalized_input_tys.iter().zip(argument_locals) {
55-
let input_ty = self.normalize(unnormalized_input_ty, Locations::All);
52+
for (&normalized_input_ty, local) in normalized_input_tys.iter().zip(argument_locals) {
53+
debug!(
54+
"equate_inputs_and_outputs: normalized_input_ty = {:?}",
55+
normalized_input_ty
56+
);
57+
5658
let mir_input_ty = mir.local_decls[local].ty;
57-
self.equate_normalized_input_or_output(input_ty, mir_input_ty);
59+
self.equate_normalized_input_or_output(normalized_input_ty, mir_input_ty);
5860
}
5961

6062
assert!(
@@ -68,15 +70,6 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
6870

6971
// Return types are a bit more complex. They may contain existential `impl Trait`
7072
// types.
71-
debug!(
72-
"equate_inputs_and_outputs: unnormalized_output_ty={:?}",
73-
unnormalized_output_ty
74-
);
75-
let output_ty = self.normalize(unnormalized_output_ty, Locations::All);
76-
debug!(
77-
"equate_inputs_and_outputs: normalized output_ty={:?}",
78-
output_ty
79-
);
8073
let param_env = self.param_env;
8174
let mir_output_ty = mir.local_decls[RETURN_PLACE].ty;
8275
let anon_type_map =
@@ -92,7 +85,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
9285
mir_def_id,
9386
dummy_body_id,
9487
param_env,
95-
&output_ty,
88+
&normalized_output_ty,
9689
));
9790
debug!(
9891
"equate_inputs_and_outputs: instantiated output_ty={:?}",
@@ -146,7 +139,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
146139
self,
147140
Location::START,
148141
"equate_inputs_and_outputs: `{:?}=={:?}` failed with `{:?}`",
149-
output_ty,
142+
normalized_output_ty,
150143
mir_output_ty,
151144
terr
152145
);

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use borrow_check::nll::constraints::{ConstraintSet, OutlivesConstraint};
1717
use borrow_check::nll::facts::AllFacts;
1818
use borrow_check::nll::region_infer::values::{RegionValueElements, LivenessValues};
1919
use borrow_check::nll::region_infer::{ClosureRegionRequirementsExt, TypeTest};
20-
use borrow_check::nll::type_check::free_region_relations::UniversalRegionRelations;
20+
use borrow_check::nll::type_check::free_region_relations::{CreateResult, UniversalRegionRelations};
2121
use borrow_check::nll::universal_regions::UniversalRegions;
2222
use borrow_check::nll::LocalWithRegion;
2323
use borrow_check::nll::ToRegionVid;
@@ -132,7 +132,11 @@ pub(crate) fn type_check<'gcx, 'tcx>(
132132
type_tests: Vec::default(),
133133
};
134134

135-
let (universal_region_relations, region_bound_pairs) = free_region_relations::create(
135+
let CreateResult {
136+
universal_region_relations,
137+
region_bound_pairs,
138+
normalized_inputs_and_output,
139+
} = free_region_relations::create(
136140
infcx,
137141
mir_def_id,
138142
param_env,
@@ -168,6 +172,7 @@ pub(crate) fn type_check<'gcx, 'tcx>(
168172
mir_def_id,
169173
universal_regions,
170174
&universal_region_relations,
175+
&normalized_inputs_and_output,
171176
);
172177
},
173178
);

0 commit comments

Comments
 (0)