@@ -143,8 +143,12 @@ pub struct LoweringContext<'a> {
143
143
}
144
144
145
145
pub trait Resolver {
146
- /// Resolve a hir path generated by the lowerer when expanding `for`, `if let`, etc.
147
- fn resolve_hir_path ( & mut self , path : & mut hir:: Path , is_value : bool ) ;
146
+ /// Resolve a path generated by the lowerer when expanding `for`, `if let`, etc.
147
+ fn resolve_hir_path (
148
+ & mut self ,
149
+ path : & ast:: Path ,
150
+ is_value : bool ,
151
+ ) -> hir:: Path ;
148
152
149
153
/// Obtain the resolution for a node id
150
154
fn get_resolution ( & mut self , id : NodeId ) -> Option < PathResolution > ;
@@ -163,7 +167,6 @@ pub trait Resolver {
163
167
span : Span ,
164
168
crate_root : Option < & str > ,
165
169
components : & [ & str ] ,
166
- params : Option < P < hir:: GenericArgs > > ,
167
170
is_value : bool ,
168
171
) -> hir:: Path ;
169
172
}
@@ -1064,6 +1067,9 @@ impl<'a> LoweringContext<'a> {
1064
1067
}
1065
1068
1066
1069
fn lower_attr ( & mut self , attr : & Attribute ) -> Attribute {
1070
+ // Note that we explicitly do not walk the path. Since we don't really
1071
+ // lower attributes (we use the AST version) there is nowhere to keep
1072
+ // the HirIds. We don't actually need HIR version of attributes anyway.
1067
1073
Attribute {
1068
1074
id : attr. id ,
1069
1075
style : attr. style ,
@@ -1677,6 +1683,7 @@ impl<'a> LoweringContext<'a> {
1677
1683
num_lifetimes,
1678
1684
parenthesized_generic_args,
1679
1685
itctx. reborrow ( ) ,
1686
+ None ,
1680
1687
)
1681
1688
} )
1682
1689
. collect ( ) ,
@@ -1720,6 +1727,7 @@ impl<'a> LoweringContext<'a> {
1720
1727
0 ,
1721
1728
ParenthesizedGenericArgs :: Warn ,
1722
1729
itctx. reborrow ( ) ,
1730
+ None ,
1723
1731
) ) ;
1724
1732
let qpath = hir:: QPath :: TypeRelative ( ty, segment) ;
1725
1733
@@ -1748,6 +1756,7 @@ impl<'a> LoweringContext<'a> {
1748
1756
p : & Path ,
1749
1757
ident : Option < Ident > ,
1750
1758
param_mode : ParamMode ,
1759
+ explicit_owner : Option < NodeId > ,
1751
1760
) -> hir:: Path {
1752
1761
hir:: Path {
1753
1762
def,
@@ -1761,6 +1770,7 @@ impl<'a> LoweringContext<'a> {
1761
1770
0 ,
1762
1771
ParenthesizedGenericArgs :: Err ,
1763
1772
ImplTraitContext :: disallowed ( ) ,
1773
+ explicit_owner,
1764
1774
)
1765
1775
} )
1766
1776
. chain ( ident. map ( |ident| hir:: PathSegment :: from_ident ( ident) ) )
@@ -1771,7 +1781,7 @@ impl<'a> LoweringContext<'a> {
1771
1781
1772
1782
fn lower_path ( & mut self , id : NodeId , p : & Path , param_mode : ParamMode ) -> hir:: Path {
1773
1783
let def = self . expect_full_def ( id) ;
1774
- self . lower_path_extra ( def, p, None , param_mode)
1784
+ self . lower_path_extra ( def, p, None , param_mode, None )
1775
1785
}
1776
1786
1777
1787
fn lower_path_segment (
@@ -1782,6 +1792,7 @@ impl<'a> LoweringContext<'a> {
1782
1792
expected_lifetimes : usize ,
1783
1793
parenthesized_generic_args : ParenthesizedGenericArgs ,
1784
1794
itctx : ImplTraitContext < ' _ > ,
1795
+ explicit_owner : Option < NodeId > ,
1785
1796
) -> hir:: PathSegment {
1786
1797
let ( mut generic_args, infer_types) = if let Some ( ref generic_args) = segment. args {
1787
1798
let msg = "parenthesized parameters may only be used with a trait" ;
@@ -1852,8 +1863,17 @@ impl<'a> LoweringContext<'a> {
1852
1863
}
1853
1864
}
1854
1865
1866
+ let def = self . expect_full_def ( segment. id ) ;
1867
+ let id = if let Some ( owner) = explicit_owner {
1868
+ self . lower_node_id_with_owner ( segment. id , owner)
1869
+ } else {
1870
+ self . lower_node_id ( segment. id )
1871
+ } ;
1872
+
1855
1873
hir:: PathSegment :: new (
1856
1874
segment. ident ,
1875
+ Some ( id. node_id ) ,
1876
+ Some ( def) ,
1857
1877
generic_args,
1858
1878
infer_types,
1859
1879
)
@@ -2936,19 +2956,20 @@ impl<'a> LoweringContext<'a> {
2936
2956
attrs : & hir:: HirVec < Attribute > ,
2937
2957
) -> hir:: ItemKind {
2938
2958
let path = & tree. prefix ;
2959
+ let segments = prefix
2960
+ . segments
2961
+ . iter ( )
2962
+ . chain ( path. segments . iter ( ) )
2963
+ . cloned ( )
2964
+ . collect ( ) ;
2939
2965
2940
2966
match tree. kind {
2941
2967
UseTreeKind :: Simple ( rename, id1, id2) => {
2942
2968
* name = tree. ident ( ) . name ;
2943
2969
2944
2970
// First apply the prefix to the path
2945
2971
let mut path = Path {
2946
- segments : prefix
2947
- . segments
2948
- . iter ( )
2949
- . chain ( path. segments . iter ( ) )
2950
- . cloned ( )
2951
- . collect ( ) ,
2972
+ segments,
2952
2973
span : path. span ,
2953
2974
} ;
2954
2975
@@ -2968,9 +2989,18 @@ impl<'a> LoweringContext<'a> {
2968
2989
// for later
2969
2990
let ret_def = defs. next ( ) . unwrap_or ( Def :: Err ) ;
2970
2991
2992
+ // Here, we are looping over namespaces, if they exist for the definition
2993
+ // being imported. We only handle type and value namespaces because we
2994
+ // won't be dealing with macros in the rest of the compiler.
2995
+ // Essentially a single `use` which imports two names is desugared into
2996
+ // two imports.
2971
2997
for ( def, & new_node_id) in defs. zip ( [ id1, id2] . iter ( ) ) {
2972
2998
let vis = vis. clone ( ) ;
2973
2999
let name = name. clone ( ) ;
3000
+ let mut path = path. clone ( ) ;
3001
+ for seg in & mut path. segments {
3002
+ seg. id = self . sess . next_node_id ( ) ;
3003
+ }
2974
3004
let span = path. span ;
2975
3005
self . resolver . definitions ( ) . create_def_with_parent (
2976
3006
parent_def_index,
@@ -2983,7 +3013,8 @@ impl<'a> LoweringContext<'a> {
2983
3013
2984
3014
self . with_hir_id_owner ( new_node_id, |this| {
2985
3015
let new_id = this. lower_node_id ( new_node_id) ;
2986
- let path = this. lower_path_extra ( def, & path, None , ParamMode :: Explicit ) ;
3016
+ let path =
3017
+ this. lower_path_extra ( def, & path, None , ParamMode :: Explicit , None ) ;
2987
3018
let item = hir:: ItemKind :: Use ( P ( path) , hir:: UseKind :: Single ) ;
2988
3019
let vis_kind = match vis. node {
2989
3020
hir:: VisibilityKind :: Public => hir:: VisibilityKind :: Public ,
@@ -2993,7 +3024,6 @@ impl<'a> LoweringContext<'a> {
2993
3024
let id = this. next_id ( ) ;
2994
3025
hir:: VisibilityKind :: Restricted {
2995
3026
path : path. clone ( ) ,
2996
- // We are allocating a new NodeId here
2997
3027
id : id. node_id ,
2998
3028
hir_id : id. hir_id ,
2999
3029
}
@@ -3016,50 +3046,60 @@ impl<'a> LoweringContext<'a> {
3016
3046
} ) ;
3017
3047
}
3018
3048
3019
- let path = P ( self . lower_path_extra ( ret_def, & path, None , ParamMode :: Explicit ) ) ;
3049
+ let path =
3050
+ P ( self . lower_path_extra ( ret_def, & path, None , ParamMode :: Explicit , None ) ) ;
3020
3051
hir:: ItemKind :: Use ( path, hir:: UseKind :: Single )
3021
3052
}
3022
3053
UseTreeKind :: Glob => {
3023
3054
let path = P ( self . lower_path (
3024
3055
id,
3025
3056
& Path {
3026
- segments : prefix
3027
- . segments
3028
- . iter ( )
3029
- . chain ( path. segments . iter ( ) )
3030
- . cloned ( )
3031
- . collect ( ) ,
3057
+ segments,
3032
3058
span : path. span ,
3033
3059
} ,
3034
3060
ParamMode :: Explicit ,
3035
3061
) ) ;
3036
3062
hir:: ItemKind :: Use ( path, hir:: UseKind :: Glob )
3037
3063
}
3038
3064
UseTreeKind :: Nested ( ref trees) => {
3065
+ // Nested imports are desugared into simple imports.
3066
+
3039
3067
let prefix = Path {
3040
- segments : prefix
3041
- . segments
3042
- . iter ( )
3043
- . chain ( path. segments . iter ( ) )
3044
- . cloned ( )
3045
- . collect ( ) ,
3068
+ segments,
3046
3069
span : prefix. span . to ( path. span ) ,
3047
3070
} ;
3048
3071
3049
- // Add all the nested PathListItems in the HIR
3072
+ // Add all the nested PathListItems to the HIR.
3050
3073
for & ( ref use_tree, id) in trees {
3051
3074
self . allocate_hir_id_counter ( id, & use_tree) ;
3075
+
3052
3076
let LoweredNodeId {
3053
3077
node_id : new_id,
3054
3078
hir_id : new_hir_id,
3055
3079
} = self . lower_node_id ( id) ;
3056
3080
3057
3081
let mut vis = vis. clone ( ) ;
3058
3082
let mut name = name. clone ( ) ;
3059
- let item =
3060
- self . lower_use_tree ( use_tree, & prefix, new_id, & mut vis, & mut name, & attrs) ;
3083
+ let mut prefix = prefix. clone ( ) ;
3061
3084
3085
+ // Give the segments new ids since they are being cloned.
3086
+ for seg in & mut prefix. segments {
3087
+ seg. id = self . sess . next_node_id ( ) ;
3088
+ }
3089
+
3090
+ // Each `use` import is an item and thus are owners of the
3091
+ // names in the path. Up to this point the nested import is
3092
+ // the current owner, since we want each desugared import to
3093
+ // own its own names, we have to adjust the owner before
3094
+ // lowering the rest of the import.
3062
3095
self . with_hir_id_owner ( new_id, |this| {
3096
+ let item = this. lower_use_tree ( use_tree,
3097
+ & prefix,
3098
+ new_id,
3099
+ & mut vis,
3100
+ & mut name,
3101
+ attrs) ;
3102
+
3063
3103
let vis_kind = match vis. node {
3064
3104
hir:: VisibilityKind :: Public => hir:: VisibilityKind :: Public ,
3065
3105
hir:: VisibilityKind :: Crate ( sugar) => hir:: VisibilityKind :: Crate ( sugar) ,
@@ -3068,7 +3108,6 @@ impl<'a> LoweringContext<'a> {
3068
3108
let id = this. next_id ( ) ;
3069
3109
hir:: VisibilityKind :: Restricted {
3070
3110
path : path. clone ( ) ,
3071
- // We are allocating a new NodeId here
3072
3111
id : id. node_id ,
3073
3112
hir_id : id. hir_id ,
3074
3113
}
@@ -3081,7 +3120,7 @@ impl<'a> LoweringContext<'a> {
3081
3120
hir:: Item {
3082
3121
id : new_id,
3083
3122
hir_id : new_hir_id,
3084
- name : name ,
3123
+ name,
3085
3124
attrs : attrs. clone ( ) ,
3086
3125
node : item,
3087
3126
vis,
@@ -3645,6 +3684,7 @@ impl<'a> LoweringContext<'a> {
3645
3684
0 ,
3646
3685
ParenthesizedGenericArgs :: Err ,
3647
3686
ImplTraitContext :: disallowed ( ) ,
3687
+ None ,
3648
3688
) ;
3649
3689
let args = args. iter ( ) . map ( |x| self . lower_expr ( x) ) . collect ( ) ;
3650
3690
hir:: ExprKind :: MethodCall ( hir_seg, seg. ident . span , args)
@@ -4498,8 +4538,15 @@ impl<'a> LoweringContext<'a> {
4498
4538
} else {
4499
4539
self . lower_node_id ( id)
4500
4540
} ;
4541
+ let def = self . expect_full_def ( id) ;
4501
4542
hir:: VisibilityKind :: Restricted {
4502
- path : P ( self . lower_path ( id, path, ParamMode :: Explicit ) ) ,
4543
+ path : P ( self . lower_path_extra (
4544
+ def,
4545
+ path,
4546
+ None ,
4547
+ ParamMode :: Explicit ,
4548
+ explicit_owner,
4549
+ ) ) ,
4503
4550
id : lowered_id. node_id ,
4504
4551
hir_id : lowered_id. hir_id ,
4505
4552
}
@@ -4806,8 +4853,17 @@ impl<'a> LoweringContext<'a> {
4806
4853
params : Option < P < hir:: GenericArgs > > ,
4807
4854
is_value : bool
4808
4855
) -> hir:: Path {
4809
- self . resolver
4810
- . resolve_str_path ( span, self . crate_root , components, params, is_value)
4856
+ let mut path = self . resolver
4857
+ . resolve_str_path ( span, self . crate_root , components, is_value) ;
4858
+ path. segments . last_mut ( ) . unwrap ( ) . args = params;
4859
+
4860
+
4861
+ for seg in path. segments . iter_mut ( ) {
4862
+ if let Some ( id) = seg. id {
4863
+ seg. id = Some ( self . lower_node_id ( id) . node_id ) ;
4864
+ }
4865
+ }
4866
+ path
4811
4867
}
4812
4868
4813
4869
fn ty_path ( & mut self , id : LoweredNodeId , span : Span , qpath : hir:: QPath ) -> hir:: Ty {
0 commit comments