@@ -147,8 +147,16 @@ impl PartialResolvedImport {
147147// FIXME: `item_tree_id` can be derived from `id`, look into deduplicating this
148148#[ derive( Clone , Debug , Eq , PartialEq ) ]
149149enum 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+ } ,
152160}
153161
154162#[ derive( Debug , Eq , PartialEq ) ]
@@ -158,53 +166,41 @@ struct Import {
158166 visibility : RawVisibility ,
159167 kind : ImportKind ,
160168 source : ImportSource ,
161- is_prelude : bool ,
162- is_macro_use : bool ,
163169}
164170
165171impl Import {
166172 fn from_use (
167- db : & dyn DefDatabase ,
168- krate : CrateId ,
169173 tree : & ItemTree ,
170174 item_tree_id : ItemTreeId < item_tree:: Use > ,
171175 id : UseId ,
176+ is_prelude : bool ,
172177 mut cb : impl FnMut ( Self ) ,
173178 ) {
174179 let it = & tree[ item_tree_id. value ] ;
175- let attrs = & tree. attrs ( db, krate, ModItem :: from ( item_tree_id. value ) . into ( ) ) ;
176180 let visibility = & tree[ it. visibility ] ;
177- let is_prelude = attrs. by_key ( "prelude_import" ) . exists ( ) ;
178181 it. use_tree . expand ( |idx, path, kind, alias| {
179182 cb ( Self {
180183 path,
181184 alias,
182185 visibility : visibility. clone ( ) ,
183186 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 } ,
187188 } ) ;
188189 } ) ;
189190 }
190191
191192 fn from_extern_crate (
192- db : & dyn DefDatabase ,
193- krate : CrateId ,
194193 tree : & ItemTree ,
195194 item_tree_id : ItemTreeId < item_tree:: ExternCrate > ,
196195 id : ExternCrateId ,
197196 ) -> Self {
198197 let it = & tree[ item_tree_id. value ] ;
199- let attrs = & tree. attrs ( db, krate, ModItem :: from ( item_tree_id. value ) . into ( ) ) ;
200198 let visibility = & tree[ it. visibility ] ;
201199 Self {
202200 path : ModPath :: from_segments ( PathKind :: Plain , iter:: once ( it. name . clone ( ) ) ) ,
203201 alias : it. alias . clone ( ) ,
204202 visibility : visibility. clone ( ) ,
205203 kind : ImportKind :: Plain ,
206- is_prelude : false ,
207- is_macro_use : attrs. by_key ( "macro_use" ) . exists ( ) ,
208204 source : ImportSource :: ExternCrate { item_tree_id, id } ,
209205 }
210206 }
@@ -893,17 +889,11 @@ impl DefCollector<'_> {
893889 tracing:: debug!( "glob import: {:?}" , import) ;
894890 match def. take_types ( ) {
895891 Some ( ModuleDefId :: ModuleId ( m) ) => {
896- if import. is_prelude {
892+ if let ImportSource :: Use { id , is_prelude : true , .. } = import. source {
897893 // Note: This dodgily overrides the injected prelude. The rustc
898894 // implementation seems to work the same though.
899895 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) ) ) ;
907897 } else if m. krate != self . def_map . krate {
908898 cov_mark:: hit!( glob_across_crates) ;
909899 // glob import from other crate => we can just import everything once
@@ -1493,7 +1483,9 @@ impl DefCollector<'_> {
14931483 }
14941484
14951485 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+ {
14971489 if matches ! (
14981490 ( directive. import. path. segments( ) . first( ) , & directive. import. path. kind) ,
14991491 ( Some ( krate) , PathKind :: Plain | PathKind :: Abs ) if diagnosed_extern_crates. contains( krate)
@@ -1592,12 +1584,12 @@ impl ModCollector<'_, '_> {
15921584 id : ItemTreeId :: new ( self . tree_id , item_tree_id) ,
15931585 }
15941586 . intern ( db) ;
1587+ let is_prelude = attrs. by_key ( "prelude_import" ) . exists ( ) ;
15951588 Import :: from_use (
1596- db,
1597- krate,
15981589 self . item_tree ,
15991590 ItemTreeId :: new ( self . tree_id , item_tree_id) ,
16001591 id,
1592+ is_prelude,
16011593 |import| {
16021594 self . def_collector . unresolved_imports . push ( ImportDirective {
16031595 module_id : self . module_id ,
@@ -1614,7 +1606,11 @@ impl ModCollector<'_, '_> {
16141606 }
16151607 . intern ( db) ;
16161608 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+ ) ;
16181614 }
16191615
16201616 self . def_collector . def_map . modules [ self . module_id ]
@@ -1623,8 +1619,6 @@ impl ModCollector<'_, '_> {
16231619 self . def_collector . unresolved_imports . push ( ImportDirective {
16241620 module_id : self . module_id ,
16251621 import : Import :: from_extern_crate (
1626- db,
1627- krate,
16281622 self . item_tree ,
16291623 ItemTreeId :: new ( self . tree_id , item_tree_id) ,
16301624 id,
@@ -1807,22 +1801,13 @@ impl ModCollector<'_, '_> {
18071801 }
18081802 }
18091803
1810- fn process_macro_use_extern_crate (
1804+ fn process_macro_use_extern_crate < ' a > (
18111805 & mut self ,
18121806 extern_crate : FileItemTreeId < ExternCrate > ,
18131807 extern_crate_id : ExternCrateId ,
1808+ macro_use_attrs : impl Iterator < Item = & ' a Attr > ,
18141809 ) {
18151810 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- }
18261811
18271812 let target_crate =
18281813 match self . def_collector . resolve_extern_crate ( & self . item_tree [ extern_crate] . name ) {
@@ -1838,7 +1823,7 @@ impl ModCollector<'_, '_> {
18381823
18391824 let mut single_imports = Vec :: new ( ) ;
18401825 let hygiene = Hygiene :: new_unhygienic ( ) ;
1841- for attr in attrs . by_key ( "macro_use" ) . attrs ( ) {
1826+ for attr in macro_use_attrs {
18421827 let Some ( paths) = attr. parse_path_comma_token_tree ( db. upcast ( ) , & hygiene) else {
18431828 // `#[macro_use]` (without any paths) found, forget collected names and just import
18441829 // all visible macros.
0 commit comments