@@ -1768,80 +1768,14 @@ impl<'a> LoweringContext<'a> {
1768
1768
-> hir:: Item_ {
1769
1769
match * i {
1770
1770
ItemKind :: ExternCrate ( string) => hir:: ItemExternCrate ( string) ,
1771
- ItemKind :: Use ( ref view_path) => {
1772
- let path = match view_path. node {
1773
- ViewPathSimple ( _, ref path) => path,
1774
- ViewPathGlob ( ref path) => path,
1775
- ViewPathList ( ref path, ref path_list_idents) => {
1776
- for & Spanned { node : ref import, span } in path_list_idents {
1777
- // `use a::{self as x, b as y};` lowers to
1778
- // `use a as x; use a::b as y;`
1779
- let mut ident = import. name ;
1780
- let suffix = if ident. name == keywords:: SelfValue . name ( ) {
1781
- if let Some ( last) = path. segments . last ( ) {
1782
- ident = last. identifier ;
1783
- }
1784
- None
1785
- } else {
1786
- Some ( ident. name )
1787
- } ;
1788
-
1789
- let mut path = self . lower_path_extra ( import. id , path, suffix,
1790
- ParamMode :: Explicit , true ) ;
1791
- path. span = span;
1792
-
1793
- self . allocate_hir_id_counter ( import. id , import) ;
1794
- let LoweredNodeId {
1795
- node_id : import_node_id,
1796
- hir_id : import_hir_id,
1797
- } = self . lower_node_id ( import. id ) ;
1798
-
1799
- self . with_hir_id_owner ( import_node_id, |this| {
1800
- let vis = match * vis {
1801
- hir:: Visibility :: Public => hir:: Visibility :: Public ,
1802
- hir:: Visibility :: Crate => hir:: Visibility :: Crate ,
1803
- hir:: Visibility :: Inherited => hir:: Visibility :: Inherited ,
1804
- hir:: Visibility :: Restricted { ref path, id : _ } => {
1805
- hir:: Visibility :: Restricted {
1806
- path : path. clone ( ) ,
1807
- // We are allocating a new NodeId here
1808
- id : this. next_id ( ) . node_id ,
1809
- }
1810
- }
1811
- } ;
1812
-
1813
- this. items . insert ( import_node_id, hir:: Item {
1814
- id : import_node_id,
1815
- hir_id : import_hir_id,
1816
- name : import. rename . unwrap_or ( ident) . name ,
1817
- attrs : attrs. clone ( ) ,
1818
- node : hir:: ItemUse ( P ( path) , hir:: UseKind :: Single ) ,
1819
- vis,
1820
- span,
1821
- } ) ;
1822
- } ) ;
1823
- }
1824
- path
1825
- }
1771
+ ItemKind :: Use ( ref use_tree) => {
1772
+ // Start with an empty prefix
1773
+ let prefix = Path {
1774
+ segments : vec ! [ ] ,
1775
+ span : use_tree. span ,
1826
1776
} ;
1827
- let path = P ( self . lower_path ( id, path, ParamMode :: Explicit , true ) ) ;
1828
- let kind = match view_path. node {
1829
- ViewPathSimple ( ident, _) => {
1830
- * name = ident. name ;
1831
- hir:: UseKind :: Single
1832
- }
1833
- ViewPathGlob ( _) => {
1834
- hir:: UseKind :: Glob
1835
- }
1836
- ViewPathList ( ..) => {
1837
- // Privatize the degenerate import base, used only to check
1838
- // the stability of `use a::{};`, to avoid it showing up as
1839
- // a reexport by accident when `pub`, e.g. in documentation.
1840
- * vis = hir:: Inherited ;
1841
- hir:: UseKind :: ListStem
1842
- }
1843
- } ;
1844
- hir:: ItemUse ( path, kind)
1777
+
1778
+ self . lower_use_tree ( use_tree, & prefix, id, vis, name, attrs)
1845
1779
}
1846
1780
ItemKind :: Static ( ref t, m, ref e) => {
1847
1781
let value = self . lower_body ( None , |this| this. lower_expr ( e) ) ;
@@ -1963,6 +1897,112 @@ impl<'a> LoweringContext<'a> {
1963
1897
// not cause an assertion failure inside the `lower_defaultness` function
1964
1898
}
1965
1899
1900
+ fn lower_use_tree ( & mut self ,
1901
+ tree : & UseTree ,
1902
+ prefix : & Path ,
1903
+ id : NodeId ,
1904
+ vis : & mut hir:: Visibility ,
1905
+ name : & mut Name ,
1906
+ attrs : & hir:: HirVec < Attribute > )
1907
+ -> hir:: Item_ {
1908
+ let path = & tree. prefix ;
1909
+
1910
+ match tree. kind {
1911
+ UseTreeKind :: Simple ( ident) => {
1912
+ * name = ident. name ;
1913
+
1914
+ // First apply the prefix to the path
1915
+ let mut path = Path {
1916
+ segments : prefix. segments
1917
+ . iter ( )
1918
+ . chain ( path. segments . iter ( ) )
1919
+ . cloned ( )
1920
+ . collect ( ) ,
1921
+ span : path. span . to ( prefix. span ) ,
1922
+ } ;
1923
+
1924
+ // Correctly resolve `self` imports
1925
+ if path. segments . last ( ) . unwrap ( ) . identifier . name == keywords:: SelfValue . name ( ) {
1926
+ let _ = path. segments . pop ( ) ;
1927
+ if ident. name == keywords:: SelfValue . name ( ) {
1928
+ * name = path. segments . last ( ) . unwrap ( ) . identifier . name ;
1929
+ }
1930
+ }
1931
+
1932
+ let path = P ( self . lower_path ( id, & path, ParamMode :: Explicit , true ) ) ;
1933
+ hir:: ItemUse ( path, hir:: UseKind :: Single )
1934
+ }
1935
+ UseTreeKind :: Glob => {
1936
+ let path = P ( self . lower_path ( id, & Path {
1937
+ segments : prefix. segments
1938
+ . iter ( )
1939
+ . chain ( path. segments . iter ( ) )
1940
+ . cloned ( )
1941
+ . collect ( ) ,
1942
+ span : path. span ,
1943
+ } , ParamMode :: Explicit , true ) ) ;
1944
+ hir:: ItemUse ( path, hir:: UseKind :: Glob )
1945
+ }
1946
+ UseTreeKind :: Nested ( ref trees) => {
1947
+ let prefix = Path {
1948
+ segments : prefix. segments
1949
+ . iter ( )
1950
+ . chain ( path. segments . iter ( ) )
1951
+ . cloned ( )
1952
+ . collect ( ) ,
1953
+ span : prefix. span . to ( path. span ) ,
1954
+ } ;
1955
+
1956
+ // Add all the nested PathListItems in the HIR
1957
+ for & ( ref use_tree, id) in trees {
1958
+ self . allocate_hir_id_counter ( id, & use_tree) ;
1959
+ let LoweredNodeId {
1960
+ node_id : new_id,
1961
+ hir_id : new_hir_id,
1962
+ } = self . lower_node_id ( id) ;
1963
+
1964
+ let mut vis = vis. clone ( ) ;
1965
+ let mut name = name. clone ( ) ;
1966
+ let item = self . lower_use_tree (
1967
+ use_tree, & prefix, new_id, & mut vis, & mut name, & attrs,
1968
+ ) ;
1969
+
1970
+ self . with_hir_id_owner ( new_id, |this| {
1971
+ let vis = match vis {
1972
+ hir:: Visibility :: Public => hir:: Visibility :: Public ,
1973
+ hir:: Visibility :: Crate => hir:: Visibility :: Crate ,
1974
+ hir:: Visibility :: Inherited => hir:: Visibility :: Inherited ,
1975
+ hir:: Visibility :: Restricted { ref path, id : _ } => {
1976
+ hir:: Visibility :: Restricted {
1977
+ path : path. clone ( ) ,
1978
+ // We are allocating a new NodeId here
1979
+ id : this. next_id ( ) . node_id ,
1980
+ }
1981
+ }
1982
+ } ;
1983
+
1984
+ this. items . insert ( new_id, hir:: Item {
1985
+ id : new_id,
1986
+ hir_id : new_hir_id,
1987
+ name : name,
1988
+ attrs : attrs. clone ( ) ,
1989
+ node : item,
1990
+ vis,
1991
+ span : use_tree. span ,
1992
+ } ) ;
1993
+ } ) ;
1994
+ }
1995
+
1996
+ // Privatize the degenerate import base, used only to check
1997
+ // the stability of `use a::{};`, to avoid it showing up as
1998
+ // a reexport by accident when `pub`, e.g. in documentation.
1999
+ let path = P ( self . lower_path ( id, & prefix, ParamMode :: Explicit , true ) ) ;
2000
+ * vis = hir:: Inherited ;
2001
+ hir:: ItemUse ( path, hir:: UseKind :: ListStem )
2002
+ }
2003
+ }
2004
+ }
2005
+
1966
2006
fn lower_trait_item ( & mut self , i : & TraitItem ) -> hir:: TraitItem {
1967
2007
self . with_parent_def ( i. id , |this| {
1968
2008
let LoweredNodeId { node_id, hir_id } = this. lower_node_id ( i. id ) ;
@@ -2129,18 +2169,30 @@ impl<'a> LoweringContext<'a> {
2129
2169
2130
2170
fn lower_item_id ( & mut self , i : & Item ) -> SmallVector < hir:: ItemId > {
2131
2171
match i. node {
2132
- ItemKind :: Use ( ref view_path) => {
2133
- if let ViewPathList ( _, ref imports) = view_path. node {
2134
- return iter:: once ( i. id ) . chain ( imports. iter ( ) . map ( |import| import. node . id ) )
2135
- . map ( |id| hir:: ItemId { id : id } ) . collect ( ) ;
2136
- }
2172
+ ItemKind :: Use ( ref use_tree) => {
2173
+ let mut vec = SmallVector :: one ( hir:: ItemId { id : i. id } ) ;
2174
+ self . lower_item_id_use_tree ( use_tree, & mut vec) ;
2175
+ return vec;
2137
2176
}
2138
2177
ItemKind :: MacroDef ( ..) => return SmallVector :: new ( ) ,
2139
2178
_ => { }
2140
2179
}
2141
2180
SmallVector :: one ( hir:: ItemId { id : i. id } )
2142
2181
}
2143
2182
2183
+ fn lower_item_id_use_tree ( & self , tree : & UseTree , vec : & mut SmallVector < hir:: ItemId > ) {
2184
+ match tree. kind {
2185
+ UseTreeKind :: Nested ( ref nested_vec) => {
2186
+ for & ( ref nested, id) in nested_vec {
2187
+ vec. push ( hir:: ItemId { id, } ) ;
2188
+ self . lower_item_id_use_tree ( nested, vec) ;
2189
+ }
2190
+ }
2191
+ UseTreeKind :: Glob => { }
2192
+ UseTreeKind :: Simple ( ..) => { }
2193
+ }
2194
+ }
2195
+
2144
2196
pub fn lower_item ( & mut self , i : & Item ) -> Option < hir:: Item > {
2145
2197
let mut name = i. ident . name ;
2146
2198
let mut vis = self . lower_visibility ( & i. vis , None ) ;
0 commit comments