@@ -608,15 +608,7 @@ impl<'a> LoweringContext<'a> {
608
608
} ) ;
609
609
610
610
if let Some ( hir_id) = item_hir_id {
611
- let item_generics = match self . lctx . items . get ( & hir_id) . unwrap ( ) . node {
612
- hir:: ItemKind :: Impl ( _, _, _, ref generics, ..)
613
- | hir:: ItemKind :: Trait ( _, _, ref generics, ..) => {
614
- generics. params . clone ( )
615
- }
616
- _ => HirVec :: new ( ) ,
617
- } ;
618
-
619
- self . lctx . with_parent_impl_lifetime_defs ( & item_generics, |this| {
611
+ self . lctx . with_parent_item_lifetime_defs ( hir_id, |this| {
620
612
let this = & mut ItemLowerer { lctx : this } ;
621
613
if let ItemKind :: Impl ( .., ref opt_trait_ref, _, _) = item. node {
622
614
this. with_trait_impl_ref ( opt_trait_ref, |this| {
@@ -1054,14 +1046,22 @@ impl<'a> LoweringContext<'a> {
1054
1046
// This should only be used with generics that have already had their
1055
1047
// in-band lifetimes added. In practice, this means that this function is
1056
1048
// only used when lowering a child item of a trait or impl.
1057
- fn with_parent_impl_lifetime_defs < T , F > ( & mut self ,
1058
- params : & HirVec < hir:: GenericParam > ,
1049
+ fn with_parent_item_lifetime_defs < T , F > ( & mut self ,
1050
+ parent_hir_id : hir:: HirId ,
1059
1051
f : F
1060
1052
) -> T where
1061
1053
F : FnOnce ( & mut LoweringContext < ' _ > ) -> T ,
1062
1054
{
1063
1055
let old_len = self . in_scope_lifetimes . len ( ) ;
1064
- let lt_def_names = params. iter ( ) . filter_map ( |param| match param. kind {
1056
+
1057
+ let parent_generics = match self . items . get ( & parent_hir_id) . unwrap ( ) . node {
1058
+ hir:: ItemKind :: Impl ( _, _, _, ref generics, ..)
1059
+ | hir:: ItemKind :: Trait ( _, _, ref generics, ..) => {
1060
+ & generics. params [ ..]
1061
+ }
1062
+ _ => & [ ] ,
1063
+ } ;
1064
+ let lt_def_names = parent_generics. iter ( ) . filter_map ( |param| match param. kind {
1065
1065
hir:: GenericParamKind :: Lifetime { .. } => Some ( param. name . ident ( ) . modern ( ) ) ,
1066
1066
_ => None ,
1067
1067
} ) ;
@@ -1113,8 +1113,7 @@ impl<'a> LoweringContext<'a> {
1113
1113
1114
1114
lowered_generics. params = lowered_generics
1115
1115
. params
1116
- . iter ( )
1117
- . cloned ( )
1116
+ . into_iter ( )
1118
1117
. chain ( in_band_defs)
1119
1118
. collect ( ) ;
1120
1119
@@ -3114,8 +3113,8 @@ impl<'a> LoweringContext<'a> {
3114
3113
& NodeMap :: default ( ) ,
3115
3114
itctx. reborrow ( ) ,
3116
3115
) ;
3117
- let trait_ref = self . with_parent_impl_lifetime_defs (
3118
- & bound_generic_params,
3116
+ let trait_ref = self . with_in_scope_lifetime_defs (
3117
+ & p . bound_generic_params ,
3119
3118
|this| this. lower_trait_ref ( & p. trait_ref , itctx) ,
3120
3119
) ;
3121
3120
@@ -3602,8 +3601,7 @@ impl<'a> LoweringContext<'a> {
3602
3601
// Essentially a single `use` which imports two names is desugared into
3603
3602
// two imports.
3604
3603
for ( res, & new_node_id) in resolutions. zip ( [ id1, id2] . iter ( ) ) {
3605
- let vis = vis. clone ( ) ;
3606
- let ident = ident. clone ( ) ;
3604
+ let ident = * ident;
3607
3605
let mut path = path. clone ( ) ;
3608
3606
for seg in & mut path. segments {
3609
3607
seg. id = self . sess . next_node_id ( ) ;
@@ -3616,19 +3614,7 @@ impl<'a> LoweringContext<'a> {
3616
3614
let path =
3617
3615
this. lower_path_extra ( res, & path, ParamMode :: Explicit , None ) ;
3618
3616
let item = hir:: ItemKind :: Use ( P ( path) , hir:: UseKind :: Single ) ;
3619
- let vis_kind = match vis. node {
3620
- hir:: VisibilityKind :: Public => hir:: VisibilityKind :: Public ,
3621
- hir:: VisibilityKind :: Crate ( sugar) => hir:: VisibilityKind :: Crate ( sugar) ,
3622
- hir:: VisibilityKind :: Inherited => hir:: VisibilityKind :: Inherited ,
3623
- hir:: VisibilityKind :: Restricted { ref path, hir_id : _ } => {
3624
- let path = this. renumber_segment_ids ( path) ;
3625
- hir:: VisibilityKind :: Restricted {
3626
- path,
3627
- hir_id : this. next_id ( ) ,
3628
- }
3629
- }
3630
- } ;
3631
- let vis = respan ( vis. span , vis_kind) ;
3617
+ let vis = this. rebuild_vis ( & vis) ;
3632
3618
3633
3619
this. insert_item (
3634
3620
hir:: Item {
@@ -3692,8 +3678,6 @@ impl<'a> LoweringContext<'a> {
3692
3678
for & ( ref use_tree, id) in trees {
3693
3679
let new_hir_id = self . lower_node_id ( id) ;
3694
3680
3695
- let mut vis = vis. clone ( ) ;
3696
- let mut ident = ident. clone ( ) ;
3697
3681
let mut prefix = prefix. clone ( ) ;
3698
3682
3699
3683
// Give the segments new node-ids since they are being cloned.
@@ -3707,27 +3691,16 @@ impl<'a> LoweringContext<'a> {
3707
3691
// own its own names, we have to adjust the owner before
3708
3692
// lowering the rest of the import.
3709
3693
self . with_hir_id_owner ( id, |this| {
3694
+ let mut vis = this. rebuild_vis ( & vis) ;
3695
+ let mut ident = * ident;
3696
+
3710
3697
let item = this. lower_use_tree ( use_tree,
3711
3698
& prefix,
3712
3699
id,
3713
3700
& mut vis,
3714
3701
& mut ident,
3715
3702
attrs) ;
3716
3703
3717
- let vis_kind = match vis. node {
3718
- hir:: VisibilityKind :: Public => hir:: VisibilityKind :: Public ,
3719
- hir:: VisibilityKind :: Crate ( sugar) => hir:: VisibilityKind :: Crate ( sugar) ,
3720
- hir:: VisibilityKind :: Inherited => hir:: VisibilityKind :: Inherited ,
3721
- hir:: VisibilityKind :: Restricted { ref path, hir_id : _ } => {
3722
- let path = this. renumber_segment_ids ( path) ;
3723
- hir:: VisibilityKind :: Restricted {
3724
- path : path,
3725
- hir_id : this. next_id ( ) ,
3726
- }
3727
- }
3728
- } ;
3729
- let vis = respan ( vis. span , vis_kind) ;
3730
-
3731
3704
this. insert_item (
3732
3705
hir:: Item {
3733
3706
hir_id : new_hir_id,
@@ -3773,15 +3746,35 @@ impl<'a> LoweringContext<'a> {
3773
3746
/// Paths like the visibility path in `pub(super) use foo::{bar, baz}` are repeated
3774
3747
/// many times in the HIR tree; for each occurrence, we need to assign distinct
3775
3748
/// `NodeId`s. (See, e.g., #56128.)
3776
- fn renumber_segment_ids ( & mut self , path : & P < hir:: Path > ) -> P < hir:: Path > {
3777
- debug ! ( "renumber_segment_ids(path = {:?})" , path) ;
3778
- let mut path = path. clone ( ) ;
3779
- for seg in path. segments . iter_mut ( ) {
3780
- if seg. hir_id . is_some ( ) {
3781
- seg. hir_id = Some ( self . next_id ( ) ) ;
3782
- }
3749
+ fn rebuild_use_path ( & mut self , path : & hir:: Path ) -> hir:: Path {
3750
+ debug ! ( "rebuild_use_path(path = {:?})" , path) ;
3751
+ let segments = path. segments . iter ( ) . map ( |seg| hir:: PathSegment {
3752
+ ident : seg. ident ,
3753
+ hir_id : seg. hir_id . map ( |_| self . next_id ( ) ) ,
3754
+ res : seg. res ,
3755
+ args : None ,
3756
+ infer_args : seg. infer_args ,
3757
+ } ) . collect ( ) ;
3758
+ hir:: Path {
3759
+ span : path. span ,
3760
+ res : path. res ,
3761
+ segments,
3783
3762
}
3784
- path
3763
+ }
3764
+
3765
+ fn rebuild_vis ( & mut self , vis : & hir:: Visibility ) -> hir:: Visibility {
3766
+ let vis_kind = match vis. node {
3767
+ hir:: VisibilityKind :: Public => hir:: VisibilityKind :: Public ,
3768
+ hir:: VisibilityKind :: Crate ( sugar) => hir:: VisibilityKind :: Crate ( sugar) ,
3769
+ hir:: VisibilityKind :: Inherited => hir:: VisibilityKind :: Inherited ,
3770
+ hir:: VisibilityKind :: Restricted { ref path, hir_id : _ } => {
3771
+ hir:: VisibilityKind :: Restricted {
3772
+ path : P ( self . rebuild_use_path ( path) ) ,
3773
+ hir_id : self . next_id ( ) ,
3774
+ }
3775
+ }
3776
+ } ;
3777
+ respan ( vis. span , vis_kind)
3785
3778
}
3786
3779
3787
3780
fn lower_trait_item ( & mut self , i : & TraitItem ) -> hir:: TraitItem {
0 commit comments