@@ -110,20 +110,31 @@ pub(crate) fn type_check<'tcx>(
110110 location_map : Rc < DenseLocationMap > ,
111111) -> MirTypeckResults < ' tcx > {
112112 let mut constraints = MirTypeckRegionConstraints {
113- placeholder_to_region : FxHashMap :: default ( ) ,
114113 liveness_constraints : LivenessValues :: with_specific_points ( Rc :: clone ( & location_map) ) ,
115114 outlives_constraints : OutlivesConstraintSet :: default ( ) ,
116115 member_constraints : MemberConstraintSet :: default ( ) ,
117116 type_tests : Vec :: default ( ) ,
118117 universe_causes : FxIndexMap :: default ( ) ,
119118 } ;
120119
120+ // FIXME: I strongly suspect this follows the case of being mutated for a while
121+ // and then settling, but I don't know enough about the type inference parts to know
122+ // when this happens and if this can be exploited to simplify some of the downstream
123+ // code. -- @amandasystems.
124+ let mut placeholder_to_region = Default :: default ( ) ;
125+
121126 let CreateResult {
122127 universal_region_relations,
123128 region_bound_pairs,
124129 normalized_inputs_and_output,
125130 known_type_outlives_obligations,
126- } = free_region_relations:: create ( infcx, infcx. param_env , universal_regions, & mut constraints) ;
131+ } = free_region_relations:: create (
132+ infcx,
133+ infcx. param_env ,
134+ universal_regions,
135+ & mut constraints,
136+ & mut placeholder_to_region,
137+ ) ;
127138
128139 let pre_obligations = infcx. take_registered_region_obligations ( ) ;
129140 assert ! (
@@ -155,6 +166,7 @@ pub(crate) fn type_check<'tcx>(
155166 borrow_set,
156167 constraints : & mut constraints,
157168 polonius_liveness,
169+ placeholder_to_region,
158170 } ;
159171
160172 typeck. check_user_type_annotations ( ) ;
@@ -221,6 +233,7 @@ struct TypeChecker<'a, 'tcx> {
221233 constraints : & ' a mut MirTypeckRegionConstraints < ' tcx > ,
222234 /// When using `-Zpolonius=next`, the liveness helper data used to create polonius constraints.
223235 polonius_liveness : Option < PoloniusLivenessContext > ,
236+ placeholder_to_region : PlaceholderToRegion < ' tcx > ,
224237}
225238
226239/// Holder struct for passing results from MIR typeck to the rest of the non-lexical regions
@@ -232,14 +245,30 @@ pub(crate) struct MirTypeckResults<'tcx> {
232245 pub ( crate ) polonius_context : Option < PoloniusContext > ,
233246}
234247
248+ /// For each placeholder we create a corresponding representative region vid.
249+ /// This map tracks those. This way, when we convert the same `ty::RePlaceholder(p)`
250+ /// twice, we can map to the same underlying `RegionVid`.
251+ #[ derive( Default ) ]
252+ pub ( crate ) struct PlaceholderToRegion < ' tcx > ( FxHashMap < ty:: PlaceholderRegion , ty:: Region < ' tcx > > ) ;
253+
254+ impl < ' tcx > PlaceholderToRegion < ' tcx > {
255+ /// Creates a `Region` for a given `PlaceholderRegion`, or returns the
256+ /// region that corresponds to a previously created one.
257+ fn placeholder_region (
258+ & mut self ,
259+ infcx : & InferCtxt < ' tcx > ,
260+ placeholder : ty:: PlaceholderRegion ,
261+ ) -> ty:: Region < ' tcx > {
262+ * self . 0 . entry ( placeholder) . or_insert_with ( || {
263+ let origin = NllRegionVariableOrigin :: Placeholder ( placeholder) ;
264+ infcx. next_nll_region_var_in_universe ( origin, placeholder. universe )
265+ } )
266+ }
267+ }
268+
235269/// A collection of region constraints that must be satisfied for the
236270/// program to be considered well-typed.
237271pub ( crate ) struct MirTypeckRegionConstraints < ' tcx > {
238- /// For each placeholder we create a corresponding representative region vid.
239- /// This map tracks those. This way, when we convert the same `ty::RePlaceholder(p)`
240- /// twice, we can map to the same underlying `RegionVid`.
241- placeholder_to_region : FxHashMap < ty:: PlaceholderRegion , ty:: Region < ' tcx > > ,
242-
243272 /// In general, the type-checker is not responsible for enforcing
244273 /// liveness constraints; this job falls to the region inferencer,
245274 /// which performs a liveness analysis. However, in some limited
@@ -258,21 +287,6 @@ pub(crate) struct MirTypeckRegionConstraints<'tcx> {
258287 pub ( crate ) type_tests : Vec < TypeTest < ' tcx > > ,
259288}
260289
261- impl < ' tcx > MirTypeckRegionConstraints < ' tcx > {
262- /// Creates a `Region` for a given `PlaceholderRegion`, or returns the
263- /// region that corresponds to a previously created one.
264- fn placeholder_region (
265- & mut self ,
266- infcx : & InferCtxt < ' tcx > ,
267- placeholder : ty:: PlaceholderRegion ,
268- ) -> ty:: Region < ' tcx > {
269- * self . placeholder_to_region . entry ( placeholder) . or_insert_with ( || {
270- let origin = NllRegionVariableOrigin :: Placeholder ( placeholder) ;
271- infcx. next_nll_region_var_in_universe ( origin, placeholder. universe )
272- } )
273- }
274- }
275-
276290/// The `Locations` type summarizes *where* region constraints are
277291/// required to hold. Normally, this is at a particular point which
278292/// created the obligation, but for constraints that the user gave, we
@@ -350,7 +364,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
350364
351365 fn to_region_vid ( & mut self , r : ty:: Region < ' tcx > ) -> RegionVid {
352366 if let ty:: RePlaceholder ( placeholder) = r. kind ( ) {
353- self . constraints . placeholder_region ( self . infcx , placeholder) . as_var ( )
367+ self . placeholder_to_region . placeholder_region ( self . infcx , placeholder) . as_var ( )
354368 } else {
355369 self . universal_regions . to_region_vid ( r)
356370 }
@@ -398,6 +412,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
398412 locations. span ( self . body ) ,
399413 category,
400414 self . constraints ,
415+ & mut self . placeholder_to_region ,
401416 )
402417 . convert_all ( data) ;
403418 }
@@ -2487,6 +2502,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
24872502 self . body . span , // irrelevant; will be overridden.
24882503 ConstraintCategory :: Boring , // same as above.
24892504 self . constraints ,
2505+ & mut self . placeholder_to_region ,
24902506 )
24912507 . apply_closure_requirements ( closure_requirements, def_id, args) ;
24922508 }
0 commit comments