@@ -1069,6 +1069,9 @@ impl<'a> LoweringContext<'a> {
1069
1069
}
1070
1070
1071
1071
fn lower_attr ( & mut self , attr : & Attribute ) -> Attribute {
1072
+ // Note that we explicitly do not walk the path. Since we don't really
1073
+ // lower attributes (we use the AST version) there is nowhere to keep
1074
+ // the HirIds. We don't actually need HIR version of attributes anyway.
1072
1075
Attribute {
1073
1076
id : attr. id ,
1074
1077
style : attr. style ,
@@ -1682,6 +1685,7 @@ impl<'a> LoweringContext<'a> {
1682
1685
num_lifetimes,
1683
1686
parenthesized_generic_args,
1684
1687
itctx. reborrow ( ) ,
1688
+ None ,
1685
1689
)
1686
1690
} )
1687
1691
. collect ( ) ,
@@ -1725,6 +1729,7 @@ impl<'a> LoweringContext<'a> {
1725
1729
0 ,
1726
1730
ParenthesizedGenericArgs :: Warn ,
1727
1731
itctx. reborrow ( ) ,
1732
+ None ,
1728
1733
) ) ;
1729
1734
let qpath = hir:: QPath :: TypeRelative ( ty, segment) ;
1730
1735
@@ -1753,6 +1758,7 @@ impl<'a> LoweringContext<'a> {
1753
1758
p : & Path ,
1754
1759
ident : Option < Ident > ,
1755
1760
param_mode : ParamMode ,
1761
+ explicit_owner : Option < NodeId > ,
1756
1762
) -> hir:: Path {
1757
1763
hir:: Path {
1758
1764
def,
@@ -1766,6 +1772,7 @@ impl<'a> LoweringContext<'a> {
1766
1772
0 ,
1767
1773
ParenthesizedGenericArgs :: Err ,
1768
1774
ImplTraitContext :: disallowed ( ) ,
1775
+ explicit_owner,
1769
1776
)
1770
1777
} )
1771
1778
. chain ( ident. map ( |ident| hir:: PathSegment :: from_ident ( ident) ) )
@@ -1776,7 +1783,7 @@ impl<'a> LoweringContext<'a> {
1776
1783
1777
1784
fn lower_path ( & mut self , id : NodeId , p : & Path , param_mode : ParamMode ) -> hir:: Path {
1778
1785
let def = self . expect_full_def ( id) ;
1779
- self . lower_path_extra ( def, p, None , param_mode)
1786
+ self . lower_path_extra ( def, p, None , param_mode, None )
1780
1787
}
1781
1788
1782
1789
fn lower_path_segment (
@@ -1787,6 +1794,7 @@ impl<'a> LoweringContext<'a> {
1787
1794
expected_lifetimes : usize ,
1788
1795
parenthesized_generic_args : ParenthesizedGenericArgs ,
1789
1796
itctx : ImplTraitContext < ' _ > ,
1797
+ explicit_owner : Option < NodeId > ,
1790
1798
) -> hir:: PathSegment {
1791
1799
let ( mut generic_args, infer_types) = if let Some ( ref generic_args) = segment. args {
1792
1800
let msg = "parenthesized parameters may only be used with a trait" ;
@@ -1858,9 +1866,15 @@ impl<'a> LoweringContext<'a> {
1858
1866
}
1859
1867
1860
1868
let def = self . expect_full_def ( segment. id ) ;
1869
+ let id = if let Some ( owner) = explicit_owner {
1870
+ self . lower_node_id_with_owner ( segment. id , owner)
1871
+ } else {
1872
+ self . lower_node_id ( segment. id )
1873
+ } ;
1874
+
1861
1875
hir:: PathSegment :: new (
1862
1876
segment. ident ,
1863
- Some ( segment . id ) ,
1877
+ Some ( id . node_id ) ,
1864
1878
Some ( def) ,
1865
1879
generic_args,
1866
1880
infer_types,
@@ -2944,19 +2958,20 @@ impl<'a> LoweringContext<'a> {
2944
2958
attrs : & hir:: HirVec < Attribute > ,
2945
2959
) -> hir:: ItemKind {
2946
2960
let path = & tree. prefix ;
2961
+ let segments = prefix
2962
+ . segments
2963
+ . iter ( )
2964
+ . chain ( path. segments . iter ( ) )
2965
+ . cloned ( )
2966
+ . collect ( ) ;
2947
2967
2948
2968
match tree. kind {
2949
2969
UseTreeKind :: Simple ( rename, id1, id2) => {
2950
2970
* name = tree. ident ( ) . name ;
2951
2971
2952
2972
// First apply the prefix to the path
2953
2973
let mut path = Path {
2954
- segments : prefix
2955
- . segments
2956
- . iter ( )
2957
- . chain ( path. segments . iter ( ) )
2958
- . cloned ( )
2959
- . collect ( ) ,
2974
+ segments,
2960
2975
span : path. span ,
2961
2976
} ;
2962
2977
@@ -2976,9 +2991,18 @@ impl<'a> LoweringContext<'a> {
2976
2991
// for later
2977
2992
let ret_def = defs. next ( ) . unwrap_or ( Def :: Err ) ;
2978
2993
2994
+ // Here, we are looping over namespaces, if they exist for the definition
2995
+ // being imported. We only handle type and value namespaces because we
2996
+ // won't be dealing with macros in the rest of the compiler.
2997
+ // Essentially a single `use` which imports two names is desugared into
2998
+ // two imports.
2979
2999
for ( def, & new_node_id) in defs. zip ( [ id1, id2] . iter ( ) ) {
2980
3000
let vis = vis. clone ( ) ;
2981
3001
let name = name. clone ( ) ;
3002
+ let mut path = path. clone ( ) ;
3003
+ for seg in & mut path. segments {
3004
+ seg. id = self . sess . next_node_id ( ) ;
3005
+ }
2982
3006
let span = path. span ;
2983
3007
self . resolver . definitions ( ) . create_def_with_parent (
2984
3008
parent_def_index,
@@ -2991,7 +3015,8 @@ impl<'a> LoweringContext<'a> {
2991
3015
2992
3016
self . with_hir_id_owner ( new_node_id, |this| {
2993
3017
let new_id = this. lower_node_id ( new_node_id) ;
2994
- let path = this. lower_path_extra ( def, & path, None , ParamMode :: Explicit ) ;
3018
+ let path =
3019
+ this. lower_path_extra ( def, & path, None , ParamMode :: Explicit , None ) ;
2995
3020
let item = hir:: ItemKind :: Use ( P ( path) , hir:: UseKind :: Single ) ;
2996
3021
let vis_kind = match vis. node {
2997
3022
hir:: VisibilityKind :: Public => hir:: VisibilityKind :: Public ,
@@ -3001,7 +3026,6 @@ impl<'a> LoweringContext<'a> {
3001
3026
let id = this. next_id ( ) ;
3002
3027
hir:: VisibilityKind :: Restricted {
3003
3028
path : path. clone ( ) ,
3004
- // We are allocating a new NodeId here
3005
3029
id : id. node_id ,
3006
3030
hir_id : id. hir_id ,
3007
3031
}
@@ -3024,50 +3048,60 @@ impl<'a> LoweringContext<'a> {
3024
3048
} ) ;
3025
3049
}
3026
3050
3027
- let path = P ( self . lower_path_extra ( ret_def, & path, None , ParamMode :: Explicit ) ) ;
3051
+ let path =
3052
+ P ( self . lower_path_extra ( ret_def, & path, None , ParamMode :: Explicit , None ) ) ;
3028
3053
hir:: ItemKind :: Use ( path, hir:: UseKind :: Single )
3029
3054
}
3030
3055
UseTreeKind :: Glob => {
3031
3056
let path = P ( self . lower_path (
3032
3057
id,
3033
3058
& Path {
3034
- segments : prefix
3035
- . segments
3036
- . iter ( )
3037
- . chain ( path. segments . iter ( ) )
3038
- . cloned ( )
3039
- . collect ( ) ,
3059
+ segments,
3040
3060
span : path. span ,
3041
3061
} ,
3042
3062
ParamMode :: Explicit ,
3043
3063
) ) ;
3044
3064
hir:: ItemKind :: Use ( path, hir:: UseKind :: Glob )
3045
3065
}
3046
3066
UseTreeKind :: Nested ( ref trees) => {
3067
+ // Nested imports are desugared into simple imports.
3068
+
3047
3069
let prefix = Path {
3048
- segments : prefix
3049
- . segments
3050
- . iter ( )
3051
- . chain ( path. segments . iter ( ) )
3052
- . cloned ( )
3053
- . collect ( ) ,
3070
+ segments,
3054
3071
span : prefix. span . to ( path. span ) ,
3055
3072
} ;
3056
3073
3057
- // Add all the nested PathListItems in the HIR
3074
+ // Add all the nested PathListItems to the HIR.
3058
3075
for & ( ref use_tree, id) in trees {
3059
3076
self . allocate_hir_id_counter ( id, & use_tree) ;
3077
+
3060
3078
let LoweredNodeId {
3061
3079
node_id : new_id,
3062
3080
hir_id : new_hir_id,
3063
3081
} = self . lower_node_id ( id) ;
3064
3082
3065
3083
let mut vis = vis. clone ( ) ;
3066
3084
let mut name = name. clone ( ) ;
3067
- let item =
3068
- self . lower_use_tree ( use_tree, & prefix, new_id, & mut vis, & mut name, & attrs) ;
3085
+ let mut prefix = prefix. clone ( ) ;
3069
3086
3087
+ // Give the segments new ids since they are being cloned.
3088
+ for seg in & mut prefix. segments {
3089
+ seg. id = self . sess . next_node_id ( ) ;
3090
+ }
3091
+
3092
+ // Each `use` import is an item and thus are owners of the
3093
+ // names in the path. Up to this point the nested import is
3094
+ // the current owner, since we want each desugared import to
3095
+ // own its own names, we have to adjust the owner before
3096
+ // lowering the rest of the import.
3070
3097
self . with_hir_id_owner ( new_id, |this| {
3098
+ let item = this. lower_use_tree ( use_tree,
3099
+ & prefix,
3100
+ new_id,
3101
+ & mut vis,
3102
+ & mut name,
3103
+ attrs) ;
3104
+
3071
3105
let vis_kind = match vis. node {
3072
3106
hir:: VisibilityKind :: Public => hir:: VisibilityKind :: Public ,
3073
3107
hir:: VisibilityKind :: Crate ( sugar) => hir:: VisibilityKind :: Crate ( sugar) ,
@@ -3076,7 +3110,6 @@ impl<'a> LoweringContext<'a> {
3076
3110
let id = this. next_id ( ) ;
3077
3111
hir:: VisibilityKind :: Restricted {
3078
3112
path : path. clone ( ) ,
3079
- // We are allocating a new NodeId here
3080
3113
id : id. node_id ,
3081
3114
hir_id : id. hir_id ,
3082
3115
}
@@ -3089,7 +3122,7 @@ impl<'a> LoweringContext<'a> {
3089
3122
hir:: Item {
3090
3123
id : new_id,
3091
3124
hir_id : new_hir_id,
3092
- name : name ,
3125
+ name,
3093
3126
attrs : attrs. clone ( ) ,
3094
3127
node : item,
3095
3128
vis,
@@ -3653,6 +3686,7 @@ impl<'a> LoweringContext<'a> {
3653
3686
0 ,
3654
3687
ParenthesizedGenericArgs :: Err ,
3655
3688
ImplTraitContext :: disallowed ( ) ,
3689
+ None ,
3656
3690
) ;
3657
3691
let args = args. iter ( ) . map ( |x| self . lower_expr ( x) ) . collect ( ) ;
3658
3692
hir:: ExprKind :: MethodCall ( hir_seg, seg. ident . span , args)
@@ -4506,8 +4540,15 @@ impl<'a> LoweringContext<'a> {
4506
4540
} else {
4507
4541
self . lower_node_id ( id)
4508
4542
} ;
4543
+ let def = self . expect_full_def ( id) ;
4509
4544
hir:: VisibilityKind :: Restricted {
4510
- path : P ( self . lower_path ( id, path, ParamMode :: Explicit ) ) ,
4545
+ path : P ( self . lower_path_extra (
4546
+ def,
4547
+ path,
4548
+ None ,
4549
+ ParamMode :: Explicit ,
4550
+ explicit_owner,
4551
+ ) ) ,
4511
4552
id : lowered_id. node_id ,
4512
4553
hir_id : lowered_id. hir_id ,
4513
4554
}
@@ -4814,8 +4855,15 @@ impl<'a> LoweringContext<'a> {
4814
4855
params : Option < P < hir:: GenericArgs > > ,
4815
4856
is_value : bool
4816
4857
) -> hir:: Path {
4817
- self . resolver
4818
- . resolve_str_path ( span, self . crate_root , components, params, is_value)
4858
+ let mut path = self . resolver
4859
+ . resolve_str_path ( span, self . crate_root , components, params, is_value) ;
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
4819
4867
}
4820
4868
4821
4869
fn ty_path ( & mut self , id : LoweredNodeId , span : Span , qpath : hir:: QPath ) -> hir:: Ty {
0 commit comments