@@ -1074,6 +1074,9 @@ impl<'a> LoweringContext<'a> {
1074
1074
}
1075
1075
1076
1076
fn lower_attr ( & mut self , attr : & Attribute ) -> Attribute {
1077
+ // Note that we explicitly do not walk the path. Since we don't really
1078
+ // lower attributes (we use the AST version) there is nowhere to keep
1079
+ // the HirIds. We don't actually need HIR version of attributes anyway.
1077
1080
Attribute {
1078
1081
id : attr. id ,
1079
1082
style : attr. style ,
@@ -1687,6 +1690,7 @@ impl<'a> LoweringContext<'a> {
1687
1690
num_lifetimes,
1688
1691
parenthesized_generic_args,
1689
1692
itctx. reborrow ( ) ,
1693
+ None ,
1690
1694
)
1691
1695
} )
1692
1696
. collect ( ) ,
@@ -1730,6 +1734,7 @@ impl<'a> LoweringContext<'a> {
1730
1734
0 ,
1731
1735
ParenthesizedGenericArgs :: Warn ,
1732
1736
itctx. reborrow ( ) ,
1737
+ None ,
1733
1738
) ) ;
1734
1739
let qpath = hir:: QPath :: TypeRelative ( ty, segment) ;
1735
1740
@@ -1758,6 +1763,7 @@ impl<'a> LoweringContext<'a> {
1758
1763
p : & Path ,
1759
1764
ident : Option < Ident > ,
1760
1765
param_mode : ParamMode ,
1766
+ explicit_owner : Option < NodeId > ,
1761
1767
) -> hir:: Path {
1762
1768
hir:: Path {
1763
1769
def,
@@ -1771,6 +1777,7 @@ impl<'a> LoweringContext<'a> {
1771
1777
0 ,
1772
1778
ParenthesizedGenericArgs :: Err ,
1773
1779
ImplTraitContext :: disallowed ( ) ,
1780
+ explicit_owner,
1774
1781
)
1775
1782
} )
1776
1783
. chain ( ident. map ( |ident| hir:: PathSegment :: from_ident ( ident) ) )
@@ -1781,7 +1788,7 @@ impl<'a> LoweringContext<'a> {
1781
1788
1782
1789
fn lower_path ( & mut self , id : NodeId , p : & Path , param_mode : ParamMode ) -> hir:: Path {
1783
1790
let def = self . expect_full_def ( id) ;
1784
- self . lower_path_extra ( def, p, None , param_mode)
1791
+ self . lower_path_extra ( def, p, None , param_mode, None )
1785
1792
}
1786
1793
1787
1794
fn lower_path_segment (
@@ -1792,6 +1799,7 @@ impl<'a> LoweringContext<'a> {
1792
1799
expected_lifetimes : usize ,
1793
1800
parenthesized_generic_args : ParenthesizedGenericArgs ,
1794
1801
itctx : ImplTraitContext < ' _ > ,
1802
+ explicit_owner : Option < NodeId > ,
1795
1803
) -> hir:: PathSegment {
1796
1804
let ( mut generic_args, infer_types) = if let Some ( ref generic_args) = segment. args {
1797
1805
let msg = "parenthesized parameters may only be used with a trait" ;
@@ -1863,9 +1871,15 @@ impl<'a> LoweringContext<'a> {
1863
1871
}
1864
1872
1865
1873
let def = self . expect_full_def ( segment. id ) ;
1874
+ let id = if let Some ( owner) = explicit_owner {
1875
+ self . lower_node_id_with_owner ( segment. id , owner)
1876
+ } else {
1877
+ self . lower_node_id ( segment. id )
1878
+ } ;
1879
+
1866
1880
hir:: PathSegment :: new (
1867
1881
segment. ident ,
1868
- Some ( segment . id ) ,
1882
+ Some ( id . node_id ) ,
1869
1883
Some ( def) ,
1870
1884
generic_args,
1871
1885
infer_types,
@@ -2949,19 +2963,20 @@ impl<'a> LoweringContext<'a> {
2949
2963
attrs : & hir:: HirVec < Attribute > ,
2950
2964
) -> hir:: ItemKind {
2951
2965
let path = & tree. prefix ;
2966
+ let segments = prefix
2967
+ . segments
2968
+ . iter ( )
2969
+ . chain ( path. segments . iter ( ) )
2970
+ . cloned ( )
2971
+ . collect ( ) ;
2952
2972
2953
2973
match tree. kind {
2954
2974
UseTreeKind :: Simple ( rename, id1, id2) => {
2955
2975
* name = tree. ident ( ) . name ;
2956
2976
2957
2977
// First apply the prefix to the path
2958
2978
let mut path = Path {
2959
- segments : prefix
2960
- . segments
2961
- . iter ( )
2962
- . chain ( path. segments . iter ( ) )
2963
- . cloned ( )
2964
- . collect ( ) ,
2979
+ segments,
2965
2980
span : path. span ,
2966
2981
} ;
2967
2982
@@ -2981,9 +2996,18 @@ impl<'a> LoweringContext<'a> {
2981
2996
// for later
2982
2997
let ret_def = defs. next ( ) . unwrap_or ( Def :: Err ) ;
2983
2998
2999
+ // Here, we are looping over namespaces, if they exist for the definition
3000
+ // being imported. We only handle type and value namespaces because we
3001
+ // won't be dealing with macros in the rest of the compiler.
3002
+ // Essentially a single `use` which imports two names is desugared into
3003
+ // two imports.
2984
3004
for ( def, & new_node_id) in defs. zip ( [ id1, id2] . iter ( ) ) {
2985
3005
let vis = vis. clone ( ) ;
2986
3006
let name = name. clone ( ) ;
3007
+ let mut path = path. clone ( ) ;
3008
+ for seg in & mut path. segments {
3009
+ seg. id = self . sess . next_node_id ( ) ;
3010
+ }
2987
3011
let span = path. span ;
2988
3012
self . resolver . definitions ( ) . create_def_with_parent (
2989
3013
parent_def_index,
@@ -2996,7 +3020,8 @@ impl<'a> LoweringContext<'a> {
2996
3020
2997
3021
self . with_hir_id_owner ( new_node_id, |this| {
2998
3022
let new_id = this. lower_node_id ( new_node_id) ;
2999
- let path = this. lower_path_extra ( def, & path, None , ParamMode :: Explicit ) ;
3023
+ let path =
3024
+ this. lower_path_extra ( def, & path, None , ParamMode :: Explicit , None ) ;
3000
3025
let item = hir:: ItemKind :: Use ( P ( path) , hir:: UseKind :: Single ) ;
3001
3026
let vis_kind = match vis. node {
3002
3027
hir:: VisibilityKind :: Public => hir:: VisibilityKind :: Public ,
@@ -3006,7 +3031,6 @@ impl<'a> LoweringContext<'a> {
3006
3031
let id = this. next_id ( ) ;
3007
3032
hir:: VisibilityKind :: Restricted {
3008
3033
path : path. clone ( ) ,
3009
- // We are allocating a new NodeId here
3010
3034
id : id. node_id ,
3011
3035
hir_id : id. hir_id ,
3012
3036
}
@@ -3029,50 +3053,60 @@ impl<'a> LoweringContext<'a> {
3029
3053
} ) ;
3030
3054
}
3031
3055
3032
- let path = P ( self . lower_path_extra ( ret_def, & path, None , ParamMode :: Explicit ) ) ;
3056
+ let path =
3057
+ P ( self . lower_path_extra ( ret_def, & path, None , ParamMode :: Explicit , None ) ) ;
3033
3058
hir:: ItemKind :: Use ( path, hir:: UseKind :: Single )
3034
3059
}
3035
3060
UseTreeKind :: Glob => {
3036
3061
let path = P ( self . lower_path (
3037
3062
id,
3038
3063
& Path {
3039
- segments : prefix
3040
- . segments
3041
- . iter ( )
3042
- . chain ( path. segments . iter ( ) )
3043
- . cloned ( )
3044
- . collect ( ) ,
3064
+ segments,
3045
3065
span : path. span ,
3046
3066
} ,
3047
3067
ParamMode :: Explicit ,
3048
3068
) ) ;
3049
3069
hir:: ItemKind :: Use ( path, hir:: UseKind :: Glob )
3050
3070
}
3051
3071
UseTreeKind :: Nested ( ref trees) => {
3072
+ // Nested imports are desugared into simple imports.
3073
+
3052
3074
let prefix = Path {
3053
- segments : prefix
3054
- . segments
3055
- . iter ( )
3056
- . chain ( path. segments . iter ( ) )
3057
- . cloned ( )
3058
- . collect ( ) ,
3075
+ segments,
3059
3076
span : prefix. span . to ( path. span ) ,
3060
3077
} ;
3061
3078
3062
- // Add all the nested PathListItems in the HIR
3079
+ // Add all the nested PathListItems to the HIR.
3063
3080
for & ( ref use_tree, id) in trees {
3064
3081
self . allocate_hir_id_counter ( id, & use_tree) ;
3082
+
3065
3083
let LoweredNodeId {
3066
3084
node_id : new_id,
3067
3085
hir_id : new_hir_id,
3068
3086
} = self . lower_node_id ( id) ;
3069
3087
3070
3088
let mut vis = vis. clone ( ) ;
3071
3089
let mut name = name. clone ( ) ;
3072
- let item =
3073
- self . lower_use_tree ( use_tree, & prefix, new_id, & mut vis, & mut name, & attrs) ;
3090
+ let mut prefix = prefix. clone ( ) ;
3074
3091
3092
+ // Give the segments new ids since they are being cloned.
3093
+ for seg in & mut prefix. segments {
3094
+ seg. id = self . sess . next_node_id ( ) ;
3095
+ }
3096
+
3097
+ // Each `use` import is an item and thus are owners of the
3098
+ // names in the path. Up to this point the nested import is
3099
+ // the current owner, since we want each desugared import to
3100
+ // own its own names, we have to adjust the owner before
3101
+ // lowering the rest of the import.
3075
3102
self . with_hir_id_owner ( new_id, |this| {
3103
+ let item = this. lower_use_tree ( use_tree,
3104
+ & prefix,
3105
+ new_id,
3106
+ & mut vis,
3107
+ & mut name,
3108
+ attrs) ;
3109
+
3076
3110
let vis_kind = match vis. node {
3077
3111
hir:: VisibilityKind :: Public => hir:: VisibilityKind :: Public ,
3078
3112
hir:: VisibilityKind :: Crate ( sugar) => hir:: VisibilityKind :: Crate ( sugar) ,
@@ -3081,7 +3115,6 @@ impl<'a> LoweringContext<'a> {
3081
3115
let id = this. next_id ( ) ;
3082
3116
hir:: VisibilityKind :: Restricted {
3083
3117
path : path. clone ( ) ,
3084
- // We are allocating a new NodeId here
3085
3118
id : id. node_id ,
3086
3119
hir_id : id. hir_id ,
3087
3120
}
@@ -3094,7 +3127,7 @@ impl<'a> LoweringContext<'a> {
3094
3127
hir:: Item {
3095
3128
id : new_id,
3096
3129
hir_id : new_hir_id,
3097
- name : name ,
3130
+ name,
3098
3131
attrs : attrs. clone ( ) ,
3099
3132
node : item,
3100
3133
vis,
@@ -3658,6 +3691,7 @@ impl<'a> LoweringContext<'a> {
3658
3691
0 ,
3659
3692
ParenthesizedGenericArgs :: Err ,
3660
3693
ImplTraitContext :: disallowed ( ) ,
3694
+ None ,
3661
3695
) ;
3662
3696
let args = args. iter ( ) . map ( |x| self . lower_expr ( x) ) . collect ( ) ;
3663
3697
hir:: ExprKind :: MethodCall ( hir_seg, seg. ident . span , args)
@@ -4511,8 +4545,15 @@ impl<'a> LoweringContext<'a> {
4511
4545
} else {
4512
4546
self . lower_node_id ( id)
4513
4547
} ;
4548
+ let def = self . expect_full_def ( id) ;
4514
4549
hir:: VisibilityKind :: Restricted {
4515
- path : P ( self . lower_path ( id, path, ParamMode :: Explicit ) ) ,
4550
+ path : P ( self . lower_path_extra (
4551
+ def,
4552
+ path,
4553
+ None ,
4554
+ ParamMode :: Explicit ,
4555
+ explicit_owner,
4556
+ ) ) ,
4516
4557
id : lowered_id. node_id ,
4517
4558
hir_id : lowered_id. hir_id ,
4518
4559
}
@@ -4819,8 +4860,15 @@ impl<'a> LoweringContext<'a> {
4819
4860
params : Option < P < hir:: GenericArgs > > ,
4820
4861
is_value : bool
4821
4862
) -> hir:: Path {
4822
- self . resolver
4823
- . resolve_str_path ( span, self . crate_root , components, params, is_value)
4863
+ let mut path = self . resolver
4864
+ . resolve_str_path ( span, self . crate_root , components, params, is_value) ;
4865
+
4866
+ for seg in path. segments . iter_mut ( ) {
4867
+ if let Some ( id) = seg. id {
4868
+ seg. id = Some ( self . lower_node_id ( id) . node_id ) ;
4869
+ }
4870
+ }
4871
+ path
4824
4872
}
4825
4873
4826
4874
fn ty_path ( & mut self , id : LoweredNodeId , span : Span , qpath : hir:: QPath ) -> hir:: Ty {
0 commit comments