@@ -1065,6 +1065,9 @@ impl<'a> LoweringContext<'a> {
1065
1065
}
1066
1066
1067
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.
1068
1071
Attribute {
1069
1072
id : attr. id ,
1070
1073
style : attr. style ,
@@ -1678,6 +1681,7 @@ impl<'a> LoweringContext<'a> {
1678
1681
num_lifetimes,
1679
1682
parenthesized_generic_args,
1680
1683
itctx. reborrow ( ) ,
1684
+ None ,
1681
1685
)
1682
1686
} )
1683
1687
. collect ( ) ,
@@ -1721,6 +1725,7 @@ impl<'a> LoweringContext<'a> {
1721
1725
0 ,
1722
1726
ParenthesizedGenericArgs :: Warn ,
1723
1727
itctx. reborrow ( ) ,
1728
+ None ,
1724
1729
) ) ;
1725
1730
let qpath = hir:: QPath :: TypeRelative ( ty, segment) ;
1726
1731
@@ -1749,6 +1754,7 @@ impl<'a> LoweringContext<'a> {
1749
1754
p : & Path ,
1750
1755
ident : Option < Ident > ,
1751
1756
param_mode : ParamMode ,
1757
+ explicit_owner : Option < NodeId > ,
1752
1758
) -> hir:: Path {
1753
1759
hir:: Path {
1754
1760
def,
@@ -1762,6 +1768,7 @@ impl<'a> LoweringContext<'a> {
1762
1768
0 ,
1763
1769
ParenthesizedGenericArgs :: Err ,
1764
1770
ImplTraitContext :: disallowed ( ) ,
1771
+ explicit_owner,
1765
1772
)
1766
1773
} )
1767
1774
. chain ( ident. map ( |ident| hir:: PathSegment :: from_ident ( ident) ) )
@@ -1772,7 +1779,7 @@ impl<'a> LoweringContext<'a> {
1772
1779
1773
1780
fn lower_path ( & mut self , id : NodeId , p : & Path , param_mode : ParamMode ) -> hir:: Path {
1774
1781
let def = self . expect_full_def ( id) ;
1775
- self . lower_path_extra ( def, p, None , param_mode)
1782
+ self . lower_path_extra ( def, p, None , param_mode, None )
1776
1783
}
1777
1784
1778
1785
fn lower_path_segment (
@@ -1783,6 +1790,7 @@ impl<'a> LoweringContext<'a> {
1783
1790
expected_lifetimes : usize ,
1784
1791
parenthesized_generic_args : ParenthesizedGenericArgs ,
1785
1792
itctx : ImplTraitContext < ' _ > ,
1793
+ explicit_owner : Option < NodeId > ,
1786
1794
) -> hir:: PathSegment {
1787
1795
let ( mut generic_args, infer_types) = if let Some ( ref generic_args) = segment. args {
1788
1796
let msg = "parenthesized parameters may only be used with a trait" ;
@@ -1854,9 +1862,15 @@ impl<'a> LoweringContext<'a> {
1854
1862
}
1855
1863
1856
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
+
1857
1871
hir:: PathSegment :: new (
1858
1872
segment. ident ,
1859
- Some ( segment . id ) ,
1873
+ Some ( id . node_id ) ,
1860
1874
Some ( def) ,
1861
1875
generic_args,
1862
1876
infer_types,
@@ -2921,19 +2935,20 @@ impl<'a> LoweringContext<'a> {
2921
2935
attrs : & hir:: HirVec < Attribute > ,
2922
2936
) -> hir:: ItemKind {
2923
2937
let path = & tree. prefix ;
2938
+ let segments = prefix
2939
+ . segments
2940
+ . iter ( )
2941
+ . chain ( path. segments . iter ( ) )
2942
+ . cloned ( )
2943
+ . collect ( ) ;
2924
2944
2925
2945
match tree. kind {
2926
2946
UseTreeKind :: Simple ( rename, id1, id2) => {
2927
2947
* name = tree. ident ( ) . name ;
2928
2948
2929
2949
// First apply the prefix to the path
2930
2950
let mut path = Path {
2931
- segments : prefix
2932
- . segments
2933
- . iter ( )
2934
- . chain ( path. segments . iter ( ) )
2935
- . cloned ( )
2936
- . collect ( ) ,
2951
+ segments,
2937
2952
span : path. span ,
2938
2953
} ;
2939
2954
@@ -2953,9 +2968,18 @@ impl<'a> LoweringContext<'a> {
2953
2968
// for later
2954
2969
let ret_def = defs. next ( ) . unwrap_or ( Def :: Err ) ;
2955
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.
2956
2976
for ( def, & new_node_id) in defs. zip ( [ id1, id2] . iter ( ) ) {
2957
2977
let vis = vis. clone ( ) ;
2958
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
+ }
2959
2983
let span = path. span ;
2960
2984
self . resolver . definitions ( ) . create_def_with_parent (
2961
2985
parent_def_index,
@@ -2968,7 +2992,8 @@ impl<'a> LoweringContext<'a> {
2968
2992
2969
2993
self . with_hir_id_owner ( new_node_id, |this| {
2970
2994
let new_id = this. lower_node_id ( new_node_id) ;
2971
- 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 ) ;
2972
2997
let item = hir:: ItemKind :: Use ( P ( path) , hir:: UseKind :: Single ) ;
2973
2998
let vis_kind = match vis. node {
2974
2999
hir:: VisibilityKind :: Public => hir:: VisibilityKind :: Public ,
@@ -2978,7 +3003,6 @@ impl<'a> LoweringContext<'a> {
2978
3003
let id = this. next_id ( ) ;
2979
3004
hir:: VisibilityKind :: Restricted {
2980
3005
path : path. clone ( ) ,
2981
- // We are allocating a new NodeId here
2982
3006
id : id. node_id ,
2983
3007
hir_id : id. hir_id ,
2984
3008
}
@@ -3001,50 +3025,60 @@ impl<'a> LoweringContext<'a> {
3001
3025
} ) ;
3002
3026
}
3003
3027
3004
- 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 ) ) ;
3005
3030
hir:: ItemKind :: Use ( path, hir:: UseKind :: Single )
3006
3031
}
3007
3032
UseTreeKind :: Glob => {
3008
3033
let path = P ( self . lower_path (
3009
3034
id,
3010
3035
& Path {
3011
- segments : prefix
3012
- . segments
3013
- . iter ( )
3014
- . chain ( path. segments . iter ( ) )
3015
- . cloned ( )
3016
- . collect ( ) ,
3036
+ segments,
3017
3037
span : path. span ,
3018
3038
} ,
3019
3039
ParamMode :: Explicit ,
3020
3040
) ) ;
3021
3041
hir:: ItemKind :: Use ( path, hir:: UseKind :: Glob )
3022
3042
}
3023
3043
UseTreeKind :: Nested ( ref trees) => {
3044
+ // Nested imports are desugared into simple imports.
3045
+
3024
3046
let prefix = Path {
3025
- segments : prefix
3026
- . segments
3027
- . iter ( )
3028
- . chain ( path. segments . iter ( ) )
3029
- . cloned ( )
3030
- . collect ( ) ,
3047
+ segments,
3031
3048
span : prefix. span . to ( path. span ) ,
3032
3049
} ;
3033
3050
3034
- // Add all the nested PathListItems in the HIR
3051
+ // Add all the nested PathListItems to the HIR.
3035
3052
for & ( ref use_tree, id) in trees {
3036
3053
self . allocate_hir_id_counter ( id, & use_tree) ;
3054
+
3037
3055
let LoweredNodeId {
3038
3056
node_id : new_id,
3039
3057
hir_id : new_hir_id,
3040
3058
} = self . lower_node_id ( id) ;
3041
3059
3042
3060
let mut vis = vis. clone ( ) ;
3043
3061
let mut name = name. clone ( ) ;
3044
- let item =
3045
- self . lower_use_tree ( use_tree, & prefix, new_id, & mut vis, & mut name, & attrs) ;
3062
+ let mut prefix = prefix. clone ( ) ;
3046
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
+ }
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.
3047
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
+
3048
3082
let vis_kind = match vis. node {
3049
3083
hir:: VisibilityKind :: Public => hir:: VisibilityKind :: Public ,
3050
3084
hir:: VisibilityKind :: Crate ( sugar) => hir:: VisibilityKind :: Crate ( sugar) ,
@@ -3053,7 +3087,6 @@ impl<'a> LoweringContext<'a> {
3053
3087
let id = this. next_id ( ) ;
3054
3088
hir:: VisibilityKind :: Restricted {
3055
3089
path : path. clone ( ) ,
3056
- // We are allocating a new NodeId here
3057
3090
id : id. node_id ,
3058
3091
hir_id : id. hir_id ,
3059
3092
}
@@ -3066,7 +3099,7 @@ impl<'a> LoweringContext<'a> {
3066
3099
hir:: Item {
3067
3100
id : new_id,
3068
3101
hir_id : new_hir_id,
3069
- name : name ,
3102
+ name,
3070
3103
attrs : attrs. clone ( ) ,
3071
3104
node : item,
3072
3105
vis,
@@ -3630,6 +3663,7 @@ impl<'a> LoweringContext<'a> {
3630
3663
0 ,
3631
3664
ParenthesizedGenericArgs :: Err ,
3632
3665
ImplTraitContext :: disallowed ( ) ,
3666
+ None ,
3633
3667
) ;
3634
3668
let args = args. iter ( ) . map ( |x| self . lower_expr ( x) ) . collect ( ) ;
3635
3669
hir:: ExprKind :: MethodCall ( hir_seg, seg. ident . span , args)
@@ -4483,8 +4517,15 @@ impl<'a> LoweringContext<'a> {
4483
4517
} else {
4484
4518
self . lower_node_id ( id)
4485
4519
} ;
4520
+ let def = self . expect_full_def ( id) ;
4486
4521
hir:: VisibilityKind :: Restricted {
4487
- 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
+ ) ) ,
4488
4529
id : lowered_id. node_id ,
4489
4530
hir_id : lowered_id. hir_id ,
4490
4531
}
@@ -4791,8 +4832,15 @@ impl<'a> LoweringContext<'a> {
4791
4832
params : Option < P < hir:: GenericArgs > > ,
4792
4833
is_value : bool
4793
4834
) -> hir:: Path {
4794
- self . resolver
4795
- . 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
4796
4844
}
4797
4845
4798
4846
fn ty_path ( & mut self , id : LoweredNodeId , span : Span , qpath : hir:: QPath ) -> hir:: Ty {
0 commit comments