@@ -144,8 +144,13 @@ pub struct LoweringContext<'a> {
144
144
}
145
145
146
146
pub trait Resolver {
147
- /// Resolve a hir path generated by the lowerer when expanding `for`, `if let`, etc.
148
- fn resolve_hir_path ( & mut self , path : & mut hir:: Path , is_value : bool ) ;
147
+ /// Resolve a path generated by the lowerer when expanding `for`, `if let`, etc.
148
+ fn resolve_hir_path (
149
+ & mut self ,
150
+ path : & ast:: Path ,
151
+ args : Option < P < hir:: GenericArgs > > ,
152
+ is_value : bool ,
153
+ ) -> hir:: Path ;
149
154
150
155
/// Obtain the resolution for a node id
151
156
fn get_resolution ( & mut self , id : NodeId ) -> Option < PathResolution > ;
@@ -164,7 +169,7 @@ pub trait Resolver {
164
169
span : Span ,
165
170
crate_root : Option < & str > ,
166
171
components : & [ & str ] ,
167
- params : Option < P < hir:: GenericArgs > > ,
172
+ args : Option < P < hir:: GenericArgs > > ,
168
173
is_value : bool ,
169
174
) -> hir:: Path ;
170
175
}
@@ -1060,6 +1065,9 @@ impl<'a> LoweringContext<'a> {
1060
1065
}
1061
1066
1062
1067
fn lower_attr ( & mut self , attr : & Attribute ) -> Attribute {
1068
+ // Note that we explicitly do not walk the path. Since we don't really
1069
+ // lower attributes (we use the AST version) there is nowhere to keep
1070
+ // the HirIds. We don't actually need HIR version of attributes anyway.
1063
1071
Attribute {
1064
1072
id : attr. id ,
1065
1073
style : attr. style ,
@@ -1673,6 +1681,7 @@ impl<'a> LoweringContext<'a> {
1673
1681
num_lifetimes,
1674
1682
parenthesized_generic_args,
1675
1683
itctx. reborrow ( ) ,
1684
+ None ,
1676
1685
)
1677
1686
} )
1678
1687
. collect ( ) ,
@@ -1716,6 +1725,7 @@ impl<'a> LoweringContext<'a> {
1716
1725
0 ,
1717
1726
ParenthesizedGenericArgs :: Warn ,
1718
1727
itctx. reborrow ( ) ,
1728
+ None ,
1719
1729
) ) ;
1720
1730
let qpath = hir:: QPath :: TypeRelative ( ty, segment) ;
1721
1731
@@ -1744,6 +1754,7 @@ impl<'a> LoweringContext<'a> {
1744
1754
p : & Path ,
1745
1755
ident : Option < Ident > ,
1746
1756
param_mode : ParamMode ,
1757
+ explicit_owner : Option < NodeId > ,
1747
1758
) -> hir:: Path {
1748
1759
hir:: Path {
1749
1760
def,
@@ -1757,6 +1768,7 @@ impl<'a> LoweringContext<'a> {
1757
1768
0 ,
1758
1769
ParenthesizedGenericArgs :: Err ,
1759
1770
ImplTraitContext :: disallowed ( ) ,
1771
+ explicit_owner,
1760
1772
)
1761
1773
} )
1762
1774
. chain ( ident. map ( |ident| hir:: PathSegment :: from_ident ( ident) ) )
@@ -1767,7 +1779,7 @@ impl<'a> LoweringContext<'a> {
1767
1779
1768
1780
fn lower_path ( & mut self , id : NodeId , p : & Path , param_mode : ParamMode ) -> hir:: Path {
1769
1781
let def = self . expect_full_def ( id) ;
1770
- self . lower_path_extra ( def, p, None , param_mode)
1782
+ self . lower_path_extra ( def, p, None , param_mode, None )
1771
1783
}
1772
1784
1773
1785
fn lower_path_segment (
@@ -1778,6 +1790,7 @@ impl<'a> LoweringContext<'a> {
1778
1790
expected_lifetimes : usize ,
1779
1791
parenthesized_generic_args : ParenthesizedGenericArgs ,
1780
1792
itctx : ImplTraitContext < ' _ > ,
1793
+ explicit_owner : Option < NodeId > ,
1781
1794
) -> hir:: PathSegment {
1782
1795
let ( mut generic_args, infer_types) = if let Some ( ref generic_args) = segment. args {
1783
1796
let msg = "parenthesized parameters may only be used with a trait" ;
@@ -1848,8 +1861,17 @@ impl<'a> LoweringContext<'a> {
1848
1861
}
1849
1862
}
1850
1863
1864
+ let def = self . expect_full_def ( segment. id ) ;
1865
+ let id = if let Some ( owner) = explicit_owner {
1866
+ self . lower_node_id_with_owner ( segment. id , owner)
1867
+ } else {
1868
+ self . lower_node_id ( segment. id )
1869
+ } ;
1870
+
1851
1871
hir:: PathSegment :: new (
1852
1872
segment. ident ,
1873
+ Some ( id. node_id ) ,
1874
+ Some ( def) ,
1853
1875
generic_args,
1854
1876
infer_types,
1855
1877
)
@@ -2913,19 +2935,20 @@ impl<'a> LoweringContext<'a> {
2913
2935
attrs : & hir:: HirVec < Attribute > ,
2914
2936
) -> hir:: ItemKind {
2915
2937
let path = & tree. prefix ;
2938
+ let segments = prefix
2939
+ . segments
2940
+ . iter ( )
2941
+ . chain ( path. segments . iter ( ) )
2942
+ . cloned ( )
2943
+ . collect ( ) ;
2916
2944
2917
2945
match tree. kind {
2918
2946
UseTreeKind :: Simple ( rename, id1, id2) => {
2919
2947
* name = tree. ident ( ) . name ;
2920
2948
2921
2949
// First apply the prefix to the path
2922
2950
let mut path = Path {
2923
- segments : prefix
2924
- . segments
2925
- . iter ( )
2926
- . chain ( path. segments . iter ( ) )
2927
- . cloned ( )
2928
- . collect ( ) ,
2951
+ segments,
2929
2952
span : path. span ,
2930
2953
} ;
2931
2954
@@ -2945,9 +2968,18 @@ impl<'a> LoweringContext<'a> {
2945
2968
// for later
2946
2969
let ret_def = defs. next ( ) . unwrap_or ( Def :: Err ) ;
2947
2970
2971
+ // Here, we are looping over namespaces, if they exist for the definition
2972
+ // being imported. We only handle type and value namespaces because we
2973
+ // won't be dealing with macros in the rest of the compiler.
2974
+ // Essentially a single `use` which imports two names is desugared into
2975
+ // two imports.
2948
2976
for ( def, & new_node_id) in defs. zip ( [ id1, id2] . iter ( ) ) {
2949
2977
let vis = vis. clone ( ) ;
2950
2978
let name = name. clone ( ) ;
2979
+ let mut path = path. clone ( ) ;
2980
+ for seg in & mut path. segments {
2981
+ seg. id = self . sess . next_node_id ( ) ;
2982
+ }
2951
2983
let span = path. span ;
2952
2984
self . resolver . definitions ( ) . create_def_with_parent (
2953
2985
parent_def_index,
@@ -2960,7 +2992,8 @@ impl<'a> LoweringContext<'a> {
2960
2992
2961
2993
self . with_hir_id_owner ( new_node_id, |this| {
2962
2994
let new_id = this. lower_node_id ( new_node_id) ;
2963
- let path = this. lower_path_extra ( def, & path, None , ParamMode :: Explicit ) ;
2995
+ let path =
2996
+ this. lower_path_extra ( def, & path, None , ParamMode :: Explicit , None ) ;
2964
2997
let item = hir:: ItemKind :: Use ( P ( path) , hir:: UseKind :: Single ) ;
2965
2998
let vis_kind = match vis. node {
2966
2999
hir:: VisibilityKind :: Public => hir:: VisibilityKind :: Public ,
@@ -2970,7 +3003,6 @@ impl<'a> LoweringContext<'a> {
2970
3003
let id = this. next_id ( ) ;
2971
3004
hir:: VisibilityKind :: Restricted {
2972
3005
path : path. clone ( ) ,
2973
- // We are allocating a new NodeId here
2974
3006
id : id. node_id ,
2975
3007
hir_id : id. hir_id ,
2976
3008
}
@@ -2993,50 +3025,60 @@ impl<'a> LoweringContext<'a> {
2993
3025
} ) ;
2994
3026
}
2995
3027
2996
- let path = P ( self . lower_path_extra ( ret_def, & path, None , ParamMode :: Explicit ) ) ;
3028
+ let path =
3029
+ P ( self . lower_path_extra ( ret_def, & path, None , ParamMode :: Explicit , None ) ) ;
2997
3030
hir:: ItemKind :: Use ( path, hir:: UseKind :: Single )
2998
3031
}
2999
3032
UseTreeKind :: Glob => {
3000
3033
let path = P ( self . lower_path (
3001
3034
id,
3002
3035
& Path {
3003
- segments : prefix
3004
- . segments
3005
- . iter ( )
3006
- . chain ( path. segments . iter ( ) )
3007
- . cloned ( )
3008
- . collect ( ) ,
3036
+ segments,
3009
3037
span : path. span ,
3010
3038
} ,
3011
3039
ParamMode :: Explicit ,
3012
3040
) ) ;
3013
3041
hir:: ItemKind :: Use ( path, hir:: UseKind :: Glob )
3014
3042
}
3015
3043
UseTreeKind :: Nested ( ref trees) => {
3044
+ // Nested imports are desugared into simple imports.
3045
+
3016
3046
let prefix = Path {
3017
- segments : prefix
3018
- . segments
3019
- . iter ( )
3020
- . chain ( path. segments . iter ( ) )
3021
- . cloned ( )
3022
- . collect ( ) ,
3047
+ segments,
3023
3048
span : prefix. span . to ( path. span ) ,
3024
3049
} ;
3025
3050
3026
- // Add all the nested PathListItems in the HIR
3051
+ // Add all the nested PathListItems to the HIR.
3027
3052
for & ( ref use_tree, id) in trees {
3028
3053
self . allocate_hir_id_counter ( id, & use_tree) ;
3054
+
3029
3055
let LoweredNodeId {
3030
3056
node_id : new_id,
3031
3057
hir_id : new_hir_id,
3032
3058
} = self . lower_node_id ( id) ;
3033
3059
3034
3060
let mut vis = vis. clone ( ) ;
3035
3061
let mut name = name. clone ( ) ;
3036
- let item =
3037
- self . lower_use_tree ( use_tree, & prefix, new_id, & mut vis, & mut name, & attrs) ;
3062
+ let mut prefix = prefix. clone ( ) ;
3063
+
3064
+ // Give the segments new ids since they are being cloned.
3065
+ for seg in & mut prefix. segments {
3066
+ seg. id = self . sess . next_node_id ( ) ;
3067
+ }
3038
3068
3069
+ // Each `use` import is an item and thus are owners of the
3070
+ // names in the path. Up to this point the nested import is
3071
+ // the current owner, since we want each desugared import to
3072
+ // own its own names, we have to adjust the owner before
3073
+ // lowering the rest of the import.
3039
3074
self . with_hir_id_owner ( new_id, |this| {
3075
+ let item = this. lower_use_tree ( use_tree,
3076
+ & prefix,
3077
+ new_id,
3078
+ & mut vis,
3079
+ & mut name,
3080
+ attrs) ;
3081
+
3040
3082
let vis_kind = match vis. node {
3041
3083
hir:: VisibilityKind :: Public => hir:: VisibilityKind :: Public ,
3042
3084
hir:: VisibilityKind :: Crate ( sugar) => hir:: VisibilityKind :: Crate ( sugar) ,
@@ -3045,7 +3087,6 @@ impl<'a> LoweringContext<'a> {
3045
3087
let id = this. next_id ( ) ;
3046
3088
hir:: VisibilityKind :: Restricted {
3047
3089
path : path. clone ( ) ,
3048
- // We are allocating a new NodeId here
3049
3090
id : id. node_id ,
3050
3091
hir_id : id. hir_id ,
3051
3092
}
@@ -3058,7 +3099,7 @@ impl<'a> LoweringContext<'a> {
3058
3099
hir:: Item {
3059
3100
id : new_id,
3060
3101
hir_id : new_hir_id,
3061
- name : name ,
3102
+ name,
3062
3103
attrs : attrs. clone ( ) ,
3063
3104
node : item,
3064
3105
vis,
@@ -3622,6 +3663,7 @@ impl<'a> LoweringContext<'a> {
3622
3663
0 ,
3623
3664
ParenthesizedGenericArgs :: Err ,
3624
3665
ImplTraitContext :: disallowed ( ) ,
3666
+ None ,
3625
3667
) ;
3626
3668
let args = args. iter ( ) . map ( |x| self . lower_expr ( x) ) . collect ( ) ;
3627
3669
hir:: ExprKind :: MethodCall ( hir_seg, seg. ident . span , args)
@@ -4475,8 +4517,15 @@ impl<'a> LoweringContext<'a> {
4475
4517
} else {
4476
4518
self . lower_node_id ( id)
4477
4519
} ;
4520
+ let def = self . expect_full_def ( id) ;
4478
4521
hir:: VisibilityKind :: Restricted {
4479
- path : P ( self . lower_path ( id, path, ParamMode :: Explicit ) ) ,
4522
+ path : P ( self . lower_path_extra (
4523
+ def,
4524
+ path,
4525
+ None ,
4526
+ ParamMode :: Explicit ,
4527
+ explicit_owner,
4528
+ ) ) ,
4480
4529
id : lowered_id. node_id ,
4481
4530
hir_id : lowered_id. hir_id ,
4482
4531
}
@@ -4783,8 +4832,15 @@ impl<'a> LoweringContext<'a> {
4783
4832
params : Option < P < hir:: GenericArgs > > ,
4784
4833
is_value : bool
4785
4834
) -> hir:: Path {
4786
- self . resolver
4787
- . resolve_str_path ( span, self . crate_root , components, params, is_value)
4835
+ let mut path = self . resolver
4836
+ . resolve_str_path ( span, self . crate_root , components, params, is_value) ;
4837
+
4838
+ for seg in path. segments . iter_mut ( ) {
4839
+ if let Some ( id) = seg. id {
4840
+ seg. id = Some ( self . lower_node_id ( id) . node_id ) ;
4841
+ }
4842
+ }
4843
+ path
4788
4844
}
4789
4845
4790
4846
fn ty_path ( & mut self , id : LoweredNodeId , span : Span , qpath : hir:: QPath ) -> hir:: Ty {
0 commit comments