@@ -506,6 +506,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
506
506
start
507
507
}
508
508
509
+ /// Given the id of some node in the AST, finds the `LocalDefId` associated with it by the name
510
+ /// resolver (if any).
511
+ fn orig_opt_local_def_id ( & self , node : NodeId ) -> Option < LocalDefId > {
512
+ self . resolver . node_id_to_def_id . get ( & node) . map ( |local_def_id| * local_def_id)
513
+ }
514
+
515
+ fn orig_local_def_id ( & self , node : NodeId ) -> LocalDefId {
516
+ self . orig_opt_local_def_id ( node)
517
+ . unwrap_or_else ( || panic ! ( "no entry for node id: `{:?}`" , node) )
518
+ }
519
+
509
520
/// Given the id of some node in the AST, finds the `LocalDefId` associated with it by the name
510
521
/// resolver (if any), after applying any remapping from `get_remapped_def_id`.
511
522
///
@@ -520,10 +531,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
520
531
/// we would create an opaque type `type FooReturn<'a1> = impl Debug + 'a1`.
521
532
/// When lowering the `Debug + 'a` bounds, we add a remapping to map `'a` to `'a1`.
522
533
fn opt_local_def_id ( & self , node : NodeId ) -> Option < LocalDefId > {
523
- self . resolver
524
- . node_id_to_def_id
525
- . get ( & node)
526
- . map ( |local_def_id| self . get_remapped_def_id ( * local_def_id) )
534
+ self . orig_opt_local_def_id ( node) . map ( |local_def_id| self . get_remapped_def_id ( local_def_id) )
527
535
}
528
536
529
537
fn local_def_id ( & self , node : NodeId ) -> LocalDefId {
@@ -532,9 +540,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
532
540
533
541
/// Get the previously recorded `to` local def id given the `from` local def id, obtained using
534
542
/// `generics_def_id_map` field.
535
- fn get_remapped_def_id ( & self , mut local_def_id : LocalDefId ) -> LocalDefId {
543
+ fn get_remapped_def_id ( & self , local_def_id : LocalDefId ) -> LocalDefId {
536
544
// `generics_def_id_map` is a stack of mappings. As we go deeper in impl traits nesting we
537
- // push new mappings so we need to try first the latest mappings, hence `iter().rev()`.
545
+ // push new mappings, so we first need to get the latest (innermost) mappings, hence `iter().rev()`.
538
546
//
539
547
// Consider:
540
548
//
@@ -544,18 +552,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
544
552
//
545
553
// `[[fn#'b -> impl_trait#'b], [fn#'b -> impl_sized#'b]]`
546
554
//
547
- // for the opaque type generated on `impl Sized + 'b`, We want the result to be:
548
- // impl_sized#'b, so iterating forward is the wrong thing to do.
549
- for map in self . generics_def_id_map . iter ( ) . rev ( ) {
550
- if let Some ( r) = map. get ( & local_def_id) {
551
- debug ! ( "def_id_remapper: remapping from `{local_def_id:?}` to `{r:?}`" ) ;
552
- local_def_id = * r;
553
- } else {
554
- debug ! ( "def_id_remapper: no remapping for `{local_def_id:?}` found in map" ) ;
555
- }
556
- }
557
-
558
- local_def_id
555
+ // for the opaque type generated on `impl Sized + 'b`, we want the result to be: impl_sized#'b.
556
+ // So, if we were trying to find first from the start (outermost) would give the wrong result, impl_trait#'b.
557
+ self . generics_def_id_map
558
+ . iter ( )
559
+ . rev ( )
560
+ . find_map ( |map| map. get ( & local_def_id) . map ( |local_def_id| * local_def_id) )
561
+ . unwrap_or ( local_def_id)
559
562
}
560
563
561
564
/// Freshen the `LoweringContext` and ready it to lower a nested item.
@@ -1633,7 +1636,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1633
1636
1634
1637
LifetimeRes :: Fresh { param, binder : _ } => {
1635
1638
debug_assert_eq ! ( lifetime. ident. name, kw:: UnderscoreLifetime ) ;
1636
- if let Some ( old_def_id) = self . opt_local_def_id ( param) && remapping. get ( & old_def_id) . is_none ( ) {
1639
+ if let Some ( old_def_id) = self . orig_opt_local_def_id ( param) && remapping. get ( & old_def_id) . is_none ( ) {
1637
1640
let node_id = self . next_node_id ( ) ;
1638
1641
1639
1642
let new_def_id = self . create_def (
@@ -1878,7 +1881,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1878
1881
let extra_lifetime_params = self . resolver . take_extra_lifetime_params ( opaque_ty_node_id) ;
1879
1882
debug ! ( ?extra_lifetime_params) ;
1880
1883
for ( ident, outer_node_id, outer_res) in extra_lifetime_params {
1881
- let outer_def_id = self . local_def_id ( outer_node_id) ;
1884
+ let outer_def_id = self . orig_local_def_id ( outer_node_id) ;
1882
1885
let inner_node_id = self . next_node_id ( ) ;
1883
1886
1884
1887
// Add a definition for the in scope lifetime def.
0 commit comments