@@ -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
}
@@ -1069,6 +1074,9 @@ impl<'a> LoweringContext<'a> {
1069
1074
}
1070
1075
1071
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.
1072
1080
Attribute {
1073
1081
id : attr. id ,
1074
1082
style : attr. style ,
@@ -1682,6 +1690,7 @@ impl<'a> LoweringContext<'a> {
1682
1690
num_lifetimes,
1683
1691
parenthesized_generic_args,
1684
1692
itctx. reborrow ( ) ,
1693
+ None ,
1685
1694
)
1686
1695
} )
1687
1696
. collect ( ) ,
@@ -1725,6 +1734,7 @@ impl<'a> LoweringContext<'a> {
1725
1734
0 ,
1726
1735
ParenthesizedGenericArgs :: Warn ,
1727
1736
itctx. reborrow ( ) ,
1737
+ None ,
1728
1738
) ) ;
1729
1739
let qpath = hir:: QPath :: TypeRelative ( ty, segment) ;
1730
1740
@@ -1753,6 +1763,7 @@ impl<'a> LoweringContext<'a> {
1753
1763
p : & Path ,
1754
1764
ident : Option < Ident > ,
1755
1765
param_mode : ParamMode ,
1766
+ explicit_owner : Option < NodeId > ,
1756
1767
) -> hir:: Path {
1757
1768
hir:: Path {
1758
1769
def,
@@ -1766,6 +1777,7 @@ impl<'a> LoweringContext<'a> {
1766
1777
0 ,
1767
1778
ParenthesizedGenericArgs :: Err ,
1768
1779
ImplTraitContext :: disallowed ( ) ,
1780
+ explicit_owner,
1769
1781
)
1770
1782
} )
1771
1783
. chain ( ident. map ( |ident| hir:: PathSegment :: from_ident ( ident) ) )
@@ -1776,7 +1788,7 @@ impl<'a> LoweringContext<'a> {
1776
1788
1777
1789
fn lower_path ( & mut self , id : NodeId , p : & Path , param_mode : ParamMode ) -> hir:: Path {
1778
1790
let def = self . expect_full_def ( id) ;
1779
- self . lower_path_extra ( def, p, None , param_mode)
1791
+ self . lower_path_extra ( def, p, None , param_mode, None )
1780
1792
}
1781
1793
1782
1794
fn lower_path_segment (
@@ -1787,6 +1799,7 @@ impl<'a> LoweringContext<'a> {
1787
1799
expected_lifetimes : usize ,
1788
1800
parenthesized_generic_args : ParenthesizedGenericArgs ,
1789
1801
itctx : ImplTraitContext < ' _ > ,
1802
+ explicit_owner : Option < NodeId > ,
1790
1803
) -> hir:: PathSegment {
1791
1804
let ( mut generic_args, infer_types) = if let Some ( ref generic_args) = segment. args {
1792
1805
let msg = "parenthesized parameters may only be used with a trait" ;
@@ -1857,8 +1870,17 @@ impl<'a> LoweringContext<'a> {
1857
1870
}
1858
1871
}
1859
1872
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
+
1860
1880
hir:: PathSegment :: new (
1861
1881
segment. ident ,
1882
+ Some ( id. node_id ) ,
1883
+ Some ( def) ,
1862
1884
generic_args,
1863
1885
infer_types,
1864
1886
)
@@ -2941,19 +2963,20 @@ impl<'a> LoweringContext<'a> {
2941
2963
attrs : & hir:: HirVec < Attribute > ,
2942
2964
) -> hir:: ItemKind {
2943
2965
let path = & tree. prefix ;
2966
+ let segments = prefix
2967
+ . segments
2968
+ . iter ( )
2969
+ . chain ( path. segments . iter ( ) )
2970
+ . cloned ( )
2971
+ . collect ( ) ;
2944
2972
2945
2973
match tree. kind {
2946
2974
UseTreeKind :: Simple ( rename, id1, id2) => {
2947
2975
* name = tree. ident ( ) . name ;
2948
2976
2949
2977
// First apply the prefix to the path
2950
2978
let mut path = Path {
2951
- segments : prefix
2952
- . segments
2953
- . iter ( )
2954
- . chain ( path. segments . iter ( ) )
2955
- . cloned ( )
2956
- . collect ( ) ,
2979
+ segments,
2957
2980
span : path. span ,
2958
2981
} ;
2959
2982
@@ -2973,9 +2996,18 @@ impl<'a> LoweringContext<'a> {
2973
2996
// for later
2974
2997
let ret_def = defs. next ( ) . unwrap_or ( Def :: Err ) ;
2975
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.
2976
3004
for ( def, & new_node_id) in defs. zip ( [ id1, id2] . iter ( ) ) {
2977
3005
let vis = vis. clone ( ) ;
2978
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
+ }
2979
3011
let span = path. span ;
2980
3012
self . resolver . definitions ( ) . create_def_with_parent (
2981
3013
parent_def_index,
@@ -2988,7 +3020,8 @@ impl<'a> LoweringContext<'a> {
2988
3020
2989
3021
self . with_hir_id_owner ( new_node_id, |this| {
2990
3022
let new_id = this. lower_node_id ( new_node_id) ;
2991
- 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 ) ;
2992
3025
let item = hir:: ItemKind :: Use ( P ( path) , hir:: UseKind :: Single ) ;
2993
3026
let vis_kind = match vis. node {
2994
3027
hir:: VisibilityKind :: Public => hir:: VisibilityKind :: Public ,
@@ -2998,7 +3031,6 @@ impl<'a> LoweringContext<'a> {
2998
3031
let id = this. next_id ( ) ;
2999
3032
hir:: VisibilityKind :: Restricted {
3000
3033
path : path. clone ( ) ,
3001
- // We are allocating a new NodeId here
3002
3034
id : id. node_id ,
3003
3035
hir_id : id. hir_id ,
3004
3036
}
@@ -3021,50 +3053,60 @@ impl<'a> LoweringContext<'a> {
3021
3053
} ) ;
3022
3054
}
3023
3055
3024
- 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 ) ) ;
3025
3058
hir:: ItemKind :: Use ( path, hir:: UseKind :: Single )
3026
3059
}
3027
3060
UseTreeKind :: Glob => {
3028
3061
let path = P ( self . lower_path (
3029
3062
id,
3030
3063
& Path {
3031
- segments : prefix
3032
- . segments
3033
- . iter ( )
3034
- . chain ( path. segments . iter ( ) )
3035
- . cloned ( )
3036
- . collect ( ) ,
3064
+ segments,
3037
3065
span : path. span ,
3038
3066
} ,
3039
3067
ParamMode :: Explicit ,
3040
3068
) ) ;
3041
3069
hir:: ItemKind :: Use ( path, hir:: UseKind :: Glob )
3042
3070
}
3043
3071
UseTreeKind :: Nested ( ref trees) => {
3072
+ // Nested imports are desugared into simple imports.
3073
+
3044
3074
let prefix = Path {
3045
- segments : prefix
3046
- . segments
3047
- . iter ( )
3048
- . chain ( path. segments . iter ( ) )
3049
- . cloned ( )
3050
- . collect ( ) ,
3075
+ segments,
3051
3076
span : prefix. span . to ( path. span ) ,
3052
3077
} ;
3053
3078
3054
- // Add all the nested PathListItems in the HIR
3079
+ // Add all the nested PathListItems to the HIR.
3055
3080
for & ( ref use_tree, id) in trees {
3056
3081
self . allocate_hir_id_counter ( id, & use_tree) ;
3082
+
3057
3083
let LoweredNodeId {
3058
3084
node_id : new_id,
3059
3085
hir_id : new_hir_id,
3060
3086
} = self . lower_node_id ( id) ;
3061
3087
3062
3088
let mut vis = vis. clone ( ) ;
3063
3089
let mut name = name. clone ( ) ;
3064
- let item =
3065
- self . lower_use_tree ( use_tree, & prefix, new_id, & mut vis, & mut name, & attrs) ;
3090
+ let mut prefix = prefix. clone ( ) ;
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
+ }
3066
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.
3067
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
+
3068
3110
let vis_kind = match vis. node {
3069
3111
hir:: VisibilityKind :: Public => hir:: VisibilityKind :: Public ,
3070
3112
hir:: VisibilityKind :: Crate ( sugar) => hir:: VisibilityKind :: Crate ( sugar) ,
@@ -3073,7 +3115,6 @@ impl<'a> LoweringContext<'a> {
3073
3115
let id = this. next_id ( ) ;
3074
3116
hir:: VisibilityKind :: Restricted {
3075
3117
path : path. clone ( ) ,
3076
- // We are allocating a new NodeId here
3077
3118
id : id. node_id ,
3078
3119
hir_id : id. hir_id ,
3079
3120
}
@@ -3086,7 +3127,7 @@ impl<'a> LoweringContext<'a> {
3086
3127
hir:: Item {
3087
3128
id : new_id,
3088
3129
hir_id : new_hir_id,
3089
- name : name ,
3130
+ name,
3090
3131
attrs : attrs. clone ( ) ,
3091
3132
node : item,
3092
3133
vis,
@@ -3650,6 +3691,7 @@ impl<'a> LoweringContext<'a> {
3650
3691
0 ,
3651
3692
ParenthesizedGenericArgs :: Err ,
3652
3693
ImplTraitContext :: disallowed ( ) ,
3694
+ None ,
3653
3695
) ;
3654
3696
let args = args. iter ( ) . map ( |x| self . lower_expr ( x) ) . collect ( ) ;
3655
3697
hir:: ExprKind :: MethodCall ( hir_seg, seg. ident . span , args)
@@ -4503,8 +4545,15 @@ impl<'a> LoweringContext<'a> {
4503
4545
} else {
4504
4546
self . lower_node_id ( id)
4505
4547
} ;
4548
+ let def = self . expect_full_def ( id) ;
4506
4549
hir:: VisibilityKind :: Restricted {
4507
- 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
+ ) ) ,
4508
4557
id : lowered_id. node_id ,
4509
4558
hir_id : lowered_id. hir_id ,
4510
4559
}
@@ -4811,8 +4860,15 @@ impl<'a> LoweringContext<'a> {
4811
4860
params : Option < P < hir:: GenericArgs > > ,
4812
4861
is_value : bool
4813
4862
) -> hir:: Path {
4814
- self . resolver
4815
- . 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
4816
4872
}
4817
4873
4818
4874
fn ty_path ( & mut self , id : LoweredNodeId , span : Span , qpath : hir:: QPath ) -> hir:: Ty {
0 commit comments