@@ -147,8 +147,16 @@ impl PartialResolvedImport {
147
147
// FIXME: `item_tree_id` can be derived from `id`, look into deduplicating this
148
148
#[ derive( Clone , Debug , Eq , PartialEq ) ]
149
149
enum ImportSource {
150
- Use { item_tree_id : ItemTreeId < item_tree:: Use > , use_tree : Idx < ast:: UseTree > , id : UseId } ,
151
- ExternCrate { item_tree_id : ItemTreeId < item_tree:: ExternCrate > , id : ExternCrateId } ,
150
+ Use {
151
+ item_tree_id : ItemTreeId < item_tree:: Use > ,
152
+ use_tree : Idx < ast:: UseTree > ,
153
+ id : UseId ,
154
+ is_prelude : bool ,
155
+ } ,
156
+ ExternCrate {
157
+ item_tree_id : ItemTreeId < item_tree:: ExternCrate > ,
158
+ id : ExternCrateId ,
159
+ } ,
152
160
}
153
161
154
162
#[ derive( Debug , Eq , PartialEq ) ]
@@ -158,53 +166,41 @@ struct Import {
158
166
visibility : RawVisibility ,
159
167
kind : ImportKind ,
160
168
source : ImportSource ,
161
- is_prelude : bool ,
162
- is_macro_use : bool ,
163
169
}
164
170
165
171
impl Import {
166
172
fn from_use (
167
- db : & dyn DefDatabase ,
168
- krate : CrateId ,
169
173
tree : & ItemTree ,
170
174
item_tree_id : ItemTreeId < item_tree:: Use > ,
171
175
id : UseId ,
176
+ is_prelude : bool ,
172
177
mut cb : impl FnMut ( Self ) ,
173
178
) {
174
179
let it = & tree[ item_tree_id. value ] ;
175
- let attrs = & tree. attrs ( db, krate, ModItem :: from ( item_tree_id. value ) . into ( ) ) ;
176
180
let visibility = & tree[ it. visibility ] ;
177
- let is_prelude = attrs. by_key ( "prelude_import" ) . exists ( ) ;
178
181
it. use_tree . expand ( |idx, path, kind, alias| {
179
182
cb ( Self {
180
183
path,
181
184
alias,
182
185
visibility : visibility. clone ( ) ,
183
186
kind,
184
- is_prelude,
185
- is_macro_use : false ,
186
- source : ImportSource :: Use { item_tree_id, use_tree : idx, id } ,
187
+ source : ImportSource :: Use { item_tree_id, use_tree : idx, id, is_prelude } ,
187
188
} ) ;
188
189
} ) ;
189
190
}
190
191
191
192
fn from_extern_crate (
192
- db : & dyn DefDatabase ,
193
- krate : CrateId ,
194
193
tree : & ItemTree ,
195
194
item_tree_id : ItemTreeId < item_tree:: ExternCrate > ,
196
195
id : ExternCrateId ,
197
196
) -> Self {
198
197
let it = & tree[ item_tree_id. value ] ;
199
- let attrs = & tree. attrs ( db, krate, ModItem :: from ( item_tree_id. value ) . into ( ) ) ;
200
198
let visibility = & tree[ it. visibility ] ;
201
199
Self {
202
200
path : ModPath :: from_segments ( PathKind :: Plain , iter:: once ( it. name . clone ( ) ) ) ,
203
201
alias : it. alias . clone ( ) ,
204
202
visibility : visibility. clone ( ) ,
205
203
kind : ImportKind :: Plain ,
206
- is_prelude : false ,
207
- is_macro_use : attrs. by_key ( "macro_use" ) . exists ( ) ,
208
204
source : ImportSource :: ExternCrate { item_tree_id, id } ,
209
205
}
210
206
}
@@ -893,17 +889,11 @@ impl DefCollector<'_> {
893
889
tracing:: debug!( "glob import: {:?}" , import) ;
894
890
match def. take_types ( ) {
895
891
Some ( ModuleDefId :: ModuleId ( m) ) => {
896
- if import. is_prelude {
892
+ if let ImportSource :: Use { id , is_prelude : true , .. } = import. source {
897
893
// Note: This dodgily overrides the injected prelude. The rustc
898
894
// implementation seems to work the same though.
899
895
cov_mark:: hit!( std_prelude) ;
900
- self . def_map . prelude = Some ( (
901
- m,
902
- match import. source {
903
- ImportSource :: Use { id, .. } => Some ( id) ,
904
- ImportSource :: ExternCrate { .. } => None ,
905
- } ,
906
- ) ) ;
896
+ self . def_map . prelude = Some ( ( m, Some ( id) ) ) ;
907
897
} else if m. krate != self . def_map . krate {
908
898
cov_mark:: hit!( glob_across_crates) ;
909
899
// glob import from other crate => we can just import everything once
@@ -1493,7 +1483,9 @@ impl DefCollector<'_> {
1493
1483
}
1494
1484
1495
1485
for directive in & self . unresolved_imports {
1496
- if let ImportSource :: Use { item_tree_id, use_tree, id : _ } = directive. import . source {
1486
+ if let ImportSource :: Use { item_tree_id, use_tree, id : _, is_prelude : _ } =
1487
+ directive. import . source
1488
+ {
1497
1489
if matches ! (
1498
1490
( directive. import. path. segments( ) . first( ) , & directive. import. path. kind) ,
1499
1491
( Some ( krate) , PathKind :: Plain | PathKind :: Abs ) if diagnosed_extern_crates. contains( krate)
@@ -1592,12 +1584,12 @@ impl ModCollector<'_, '_> {
1592
1584
id : ItemTreeId :: new ( self . tree_id , item_tree_id) ,
1593
1585
}
1594
1586
. intern ( db) ;
1587
+ let is_prelude = attrs. by_key ( "prelude_import" ) . exists ( ) ;
1595
1588
Import :: from_use (
1596
- db,
1597
- krate,
1598
1589
self . item_tree ,
1599
1590
ItemTreeId :: new ( self . tree_id , item_tree_id) ,
1600
1591
id,
1592
+ is_prelude,
1601
1593
|import| {
1602
1594
self . def_collector . unresolved_imports . push ( ImportDirective {
1603
1595
module_id : self . module_id ,
@@ -1614,7 +1606,11 @@ impl ModCollector<'_, '_> {
1614
1606
}
1615
1607
. intern ( db) ;
1616
1608
if is_crate_root {
1617
- self . process_macro_use_extern_crate ( item_tree_id, id) ;
1609
+ self . process_macro_use_extern_crate (
1610
+ item_tree_id,
1611
+ id,
1612
+ attrs. by_key ( "macro_use" ) . attrs ( ) ,
1613
+ ) ;
1618
1614
}
1619
1615
1620
1616
self . def_collector . def_map . modules [ self . module_id ]
@@ -1623,8 +1619,6 @@ impl ModCollector<'_, '_> {
1623
1619
self . def_collector . unresolved_imports . push ( ImportDirective {
1624
1620
module_id : self . module_id ,
1625
1621
import : Import :: from_extern_crate (
1626
- db,
1627
- krate,
1628
1622
self . item_tree ,
1629
1623
ItemTreeId :: new ( self . tree_id , item_tree_id) ,
1630
1624
id,
@@ -1807,22 +1801,13 @@ impl ModCollector<'_, '_> {
1807
1801
}
1808
1802
}
1809
1803
1810
- fn process_macro_use_extern_crate (
1804
+ fn process_macro_use_extern_crate < ' a > (
1811
1805
& mut self ,
1812
1806
extern_crate : FileItemTreeId < ExternCrate > ,
1813
1807
extern_crate_id : ExternCrateId ,
1808
+ macro_use_attrs : impl Iterator < Item = & ' a Attr > ,
1814
1809
) {
1815
1810
let db = self . def_collector . db ;
1816
- let attrs = self . item_tree . attrs (
1817
- db,
1818
- self . def_collector . def_map . krate ,
1819
- ModItem :: from ( extern_crate) . into ( ) ,
1820
- ) ;
1821
- if let Some ( cfg) = attrs. cfg ( ) {
1822
- if !self . is_cfg_enabled ( & cfg) {
1823
- return ;
1824
- }
1825
- }
1826
1811
1827
1812
let target_crate =
1828
1813
match self . def_collector . resolve_extern_crate ( & self . item_tree [ extern_crate] . name ) {
@@ -1838,7 +1823,7 @@ impl ModCollector<'_, '_> {
1838
1823
1839
1824
let mut single_imports = Vec :: new ( ) ;
1840
1825
let hygiene = Hygiene :: new_unhygienic ( ) ;
1841
- for attr in attrs . by_key ( "macro_use" ) . attrs ( ) {
1826
+ for attr in macro_use_attrs {
1842
1827
let Some ( paths) = attr. parse_path_comma_token_tree ( db. upcast ( ) , & hygiene) else {
1843
1828
// `#[macro_use]` (without any paths) found, forget collected names and just import
1844
1829
// all visible macros.
0 commit comments