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