@@ -132,6 +132,12 @@ struct LoweringContext<'a, 'hir> {
132
132
allow_try_trait : Option < Lrc < [ Symbol ] > > ,
133
133
allow_gen_future : Option < Lrc < [ Symbol ] > > ,
134
134
allow_into_future : Option < Lrc < [ Symbol ] > > ,
135
+
136
+ /// Mapping from generics `def_id`s to TAIT generics `def_id`s.
137
+ /// For each captured lifetime (e.g., 'a), we create a new lifetime parameter that is a generic
138
+ /// defined on the TAIT, so we have type Foo<'a1> = ... and we establish a mapping in this
139
+ /// field from the original parameter 'a to the new parameter 'a1.
140
+ generics_def_id_map : Vec < FxHashMap < LocalDefId , LocalDefId > > ,
135
141
}
136
142
137
143
trait ResolverAstLoweringExt {
@@ -142,12 +148,6 @@ trait ResolverAstLoweringExt {
142
148
fn get_lifetime_res ( & self , id : NodeId ) -> Option < LifetimeRes > ;
143
149
fn take_extra_lifetime_params ( & mut self , id : NodeId ) -> Vec < ( Ident , NodeId , LifetimeRes ) > ;
144
150
fn decl_macro_kind ( & self , def_id : LocalDefId ) -> MacroKind ;
145
- /// Record the map from `from` local def id to `to` local def id, on `generics_def_id_map`
146
- /// field.
147
- fn record_def_id_remap ( & mut self , from : LocalDefId , to : LocalDefId ) ;
148
- /// Get the previously recorded `to` local def id given the `from` local def id, obtained using
149
- /// `generics_def_id_map` field.
150
- fn get_remapped_def_id ( & self , local_def_id : LocalDefId ) -> LocalDefId ;
151
151
}
152
152
153
153
impl ResolverAstLoweringExt for ResolverAstLowering {
@@ -215,41 +215,6 @@ impl ResolverAstLoweringExt for ResolverAstLowering {
215
215
fn decl_macro_kind ( & self , def_id : LocalDefId ) -> MacroKind {
216
216
self . builtin_macro_kinds . get ( & def_id) . copied ( ) . unwrap_or ( MacroKind :: Bang )
217
217
}
218
-
219
- /// Push a remapping into the top-most map.
220
- /// Panics if no map has been pushed.
221
- /// Remapping is used when creating lowering `-> impl Trait` return
222
- /// types to create the resulting opaque type.
223
- #[ instrument( level = "debug" , skip( self ) ) ]
224
- fn record_def_id_remap ( & mut self , from : LocalDefId , to : LocalDefId ) {
225
- self . generics_def_id_map . last_mut ( ) . expect ( "no map pushed" ) . insert ( from, to) ;
226
- }
227
-
228
- fn get_remapped_def_id ( & self , mut local_def_id : LocalDefId ) -> LocalDefId {
229
- // `generics_def_id_map` is a stack of mappings. As we go deeper in impl traits nesting we
230
- // push new mappings so we need to try first the latest mappings, hence `iter().rev()`.
231
- //
232
- // Consider:
233
- //
234
- // `fn test<'a, 'b>() -> impl Trait<&'a u8, Ty = impl Sized + 'b> {}`
235
- //
236
- // We would end with a generics_def_id_map like:
237
- //
238
- // `[[fn#'b -> impl_trait#'b], [fn#'b -> impl_sized#'b]]`
239
- //
240
- // for the opaque type generated on `impl Sized + 'b`, We want the result to be:
241
- // impl_sized#'b, so iterating forward is the wrong thing to do.
242
- for map in self . generics_def_id_map . iter ( ) . rev ( ) {
243
- if let Some ( r) = map. get ( & local_def_id) {
244
- debug ! ( "def_id_remapper: remapping from `{local_def_id:?}` to `{r:?}`" ) ;
245
- local_def_id = * r;
246
- } else {
247
- debug ! ( "def_id_remapper: no remapping for `{local_def_id:?}` found in map" ) ;
248
- }
249
- }
250
-
251
- local_def_id
252
- }
253
218
}
254
219
255
220
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
@@ -522,13 +487,41 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
522
487
self . resolver
523
488
. node_id_to_def_id
524
489
. get ( & node)
525
- . map ( |local_def_id| self . resolver . get_remapped_def_id ( * local_def_id) )
490
+ . map ( |local_def_id| self . get_remapped_def_id ( * local_def_id) )
526
491
}
527
492
528
493
fn local_def_id ( & self , node : NodeId ) -> LocalDefId {
529
494
self . opt_local_def_id ( node) . unwrap_or_else ( || panic ! ( "no entry for node id: `{:?}`" , node) )
530
495
}
531
496
497
+ /// Get the previously recorded `to` local def id given the `from` local def id, obtained using
498
+ /// `generics_def_id_map` field.
499
+ fn get_remapped_def_id ( & self , mut local_def_id : LocalDefId ) -> LocalDefId {
500
+ // `generics_def_id_map` is a stack of mappings. As we go deeper in impl traits nesting we
501
+ // push new mappings so we need to try first the latest mappings, hence `iter().rev()`.
502
+ //
503
+ // Consider:
504
+ //
505
+ // `fn test<'a, 'b>() -> impl Trait<&'a u8, Ty = impl Sized + 'b> {}`
506
+ //
507
+ // We would end with a generics_def_id_map like:
508
+ //
509
+ // `[[fn#'b -> impl_trait#'b], [fn#'b -> impl_sized#'b]]`
510
+ //
511
+ // for the opaque type generated on `impl Sized + 'b`, We want the result to be:
512
+ // impl_sized#'b, so iterating forward is the wrong thing to do.
513
+ for map in self . generics_def_id_map . iter ( ) . rev ( ) {
514
+ if let Some ( r) = map. get ( & local_def_id) {
515
+ debug ! ( "def_id_remapper: remapping from `{local_def_id:?}` to `{r:?}`" ) ;
516
+ local_def_id = * r;
517
+ } else {
518
+ debug ! ( "def_id_remapper: no remapping for `{local_def_id:?}` found in map" ) ;
519
+ }
520
+ }
521
+
522
+ local_def_id
523
+ }
524
+
532
525
/// Freshen the `LoweringContext` and ready it to lower a nested item.
533
526
/// The lowered item is registered into `self.children`.
534
527
///
@@ -597,9 +590,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
597
590
remap : FxHashMap < LocalDefId , LocalDefId > ,
598
591
f : impl FnOnce ( & mut Self ) -> R ,
599
592
) -> R {
600
- self . resolver . generics_def_id_map . push ( remap) ;
593
+ self . generics_def_id_map . push ( remap) ;
601
594
let res = f ( self ) ;
602
- self . resolver . generics_def_id_map . pop ( ) ;
595
+ self . generics_def_id_map . pop ( ) ;
603
596
res
604
597
}
605
598
@@ -2024,7 +2017,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2024
2017
let name = match res {
2025
2018
LifetimeRes :: Param { param, .. } => {
2026
2019
let p_name = ParamName :: Plain ( ident) ;
2027
- let param = self . resolver . get_remapped_def_id ( param) ;
2020
+ let param = self . get_remapped_def_id ( param) ;
2028
2021
2029
2022
hir:: LifetimeName :: Param ( param, p_name)
2030
2023
}
0 commit comments