@@ -226,13 +226,6 @@ enum MacroDirectiveKind {
226
226
Attr { ast_id : AstIdWithPath < ast:: Item > , attr : Attr , mod_item : ModItem } ,
227
227
}
228
228
229
- struct DefData < ' a > {
230
- id : ModuleDefId ,
231
- name : & ' a Name ,
232
- visibility : & ' a RawVisibility ,
233
- has_constructor : bool ,
234
- }
235
-
236
229
/// Walks the tree of module recursively
237
230
struct DefCollector < ' a > {
238
231
db : & ' a dyn DefDatabase ,
@@ -1353,19 +1346,18 @@ impl DefCollector<'_> {
1353
1346
}
1354
1347
1355
1348
for directive in & self . unresolved_imports {
1356
- if let ImportSource :: Import { id : import, use_tree } = & directive. import . source {
1357
- if let ( Some ( krate) , PathKind :: Plain | PathKind :: Abs ) =
1358
- ( directive. import . path . segments ( ) . first ( ) , & directive. import . path . kind )
1359
- {
1360
- if diagnosed_extern_crates. contains ( krate) {
1361
- continue ;
1362
- }
1349
+ if let ImportSource :: Import { id : import, use_tree } = directive. import . source {
1350
+ if matches ! (
1351
+ ( directive. import. path. segments( ) . first( ) , & directive. import. path. kind) ,
1352
+ ( Some ( krate) , PathKind :: Plain | PathKind :: Abs ) if diagnosed_extern_crates. contains( krate)
1353
+ ) {
1354
+ continue ;
1363
1355
}
1364
1356
1365
1357
self . def_map . diagnostics . push ( DefDiagnostic :: unresolved_import (
1366
1358
directive. module_id ,
1367
- * import,
1368
- * use_tree,
1359
+ import,
1360
+ use_tree,
1369
1361
) ) ;
1370
1362
}
1371
1363
}
@@ -1386,6 +1378,13 @@ struct ModCollector<'a, 'b> {
1386
1378
1387
1379
impl ModCollector < ' _ , ' _ > {
1388
1380
fn collect ( & mut self , items : & [ ModItem ] ) {
1381
+ struct DefData < ' a > {
1382
+ id : ModuleDefId ,
1383
+ name : & ' a Name ,
1384
+ visibility : & ' a RawVisibility ,
1385
+ has_constructor : bool ,
1386
+ }
1387
+
1389
1388
let krate = self . def_collector . def_map . krate ;
1390
1389
1391
1390
// Note: don't assert that inserted value is fresh: it's simply not true
@@ -1403,18 +1402,18 @@ impl ModCollector<'_, '_> {
1403
1402
// This should be processed eagerly instead of deferred to resolving.
1404
1403
// `#[macro_use] extern crate` is hoisted to imports macros before collecting
1405
1404
// any other items.
1406
- for item in items {
1407
- let attrs = self . item_tree . attrs ( self . def_collector . db , krate, ( * item) . into ( ) ) ;
1405
+ for & item in items {
1406
+ let attrs = self . item_tree . attrs ( self . def_collector . db , krate, item. into ( ) ) ;
1408
1407
if attrs. cfg ( ) . map_or ( true , |cfg| self . is_cfg_enabled ( & cfg) ) {
1409
1408
if let ModItem :: ExternCrate ( id) = item {
1410
- let import = self . item_tree [ * id] . clone ( ) ;
1409
+ let import = & self . item_tree [ id] ;
1411
1410
let attrs = self . item_tree . attrs (
1412
1411
self . def_collector . db ,
1413
1412
krate,
1414
- ModItem :: from ( * id) . into ( ) ,
1413
+ ModItem :: from ( id) . into ( ) ,
1415
1414
) ;
1416
1415
if attrs. by_key ( "macro_use" ) . exists ( ) {
1417
- self . def_collector . import_macros_from_extern_crate ( self . module_id , & import) ;
1416
+ self . def_collector . import_macros_from_extern_crate ( self . module_id , import) ;
1418
1417
}
1419
1418
}
1420
1419
}
@@ -1656,9 +1655,7 @@ impl ModCollector<'_, '_> {
1656
1655
let is_enabled = item_tree
1657
1656
. top_level_attrs ( db, self . def_collector . def_map . krate )
1658
1657
. cfg ( )
1659
- . map_or ( true , |cfg| {
1660
- self . def_collector . cfg_options . check ( & cfg) != Some ( false )
1661
- } ) ;
1658
+ . map_or ( true , |cfg| self . is_cfg_enabled ( & cfg) ) ;
1662
1659
if is_enabled {
1663
1660
let module_id = self . push_child_module (
1664
1661
module. name . clone ( ) ,
@@ -1675,12 +1672,12 @@ impl ModCollector<'_, '_> {
1675
1672
mod_dir,
1676
1673
}
1677
1674
. collect ( item_tree. top_level_items ( ) ) ;
1678
- if is_macro_use
1675
+ let is_macro_use = is_macro_use
1679
1676
|| item_tree
1680
1677
. top_level_attrs ( db, self . def_collector . def_map . krate )
1681
1678
. by_key ( "macro_use" )
1682
- . exists ( )
1683
- {
1679
+ . exists ( ) ;
1680
+ if is_macro_use {
1684
1681
self . import_all_legacy_macros ( module_id) ;
1685
1682
}
1686
1683
}
@@ -1714,14 +1711,17 @@ impl ModCollector<'_, '_> {
1714
1711
ModuleOrigin :: File { declaration, definition, is_mod_rs }
1715
1712
}
1716
1713
} ;
1714
+
1717
1715
let res = modules. alloc ( ModuleData :: new ( origin, vis) ) ;
1718
1716
modules[ res] . parent = Some ( self . module_id ) ;
1719
1717
for ( name, mac) in modules[ self . module_id ] . scope . collect_legacy_macros ( ) {
1720
1718
modules[ res] . scope . define_legacy_macro ( name, mac)
1721
1719
}
1722
1720
modules[ self . module_id ] . children . insert ( name. clone ( ) , res) ;
1721
+
1723
1722
let module = self . def_collector . def_map . module_id ( res) ;
1724
- let def: ModuleDefId = module. into ( ) ;
1723
+ let def = ModuleDefId :: from ( module) ;
1724
+
1725
1725
self . def_collector . def_map . modules [ self . module_id ] . scope . declare ( def) ;
1726
1726
self . def_collector . update (
1727
1727
self . module_id ,
@@ -1786,30 +1786,29 @@ impl ModCollector<'_, '_> {
1786
1786
}
1787
1787
1788
1788
fn is_builtin_or_registered_attr ( & self , path : & ModPath ) -> bool {
1789
- if path. kind == PathKind :: Plain {
1790
- if let Some ( tool_module) = path. segments ( ) . first ( ) {
1791
- let tool_module = tool_module. to_smol_str ( ) ;
1792
- let is_tool = builtin_attr:: TOOL_MODULES
1793
- . iter ( )
1794
- . copied ( )
1795
- . chain ( self . def_collector . registered_tools . iter ( ) . map ( SmolStr :: as_str) )
1796
- . any ( |m| tool_module == * m) ;
1797
- if is_tool {
1798
- return true ;
1799
- }
1789
+ if path. kind != PathKind :: Plain {
1790
+ return false ;
1791
+ }
1792
+
1793
+ let segments = path. segments ( ) ;
1794
+
1795
+ if let Some ( name) = segments. first ( ) {
1796
+ let name = name. to_smol_str ( ) ;
1797
+ let pred = |n : & _ | * n == name;
1798
+
1799
+ let registered = self . def_collector . registered_tools . iter ( ) . map ( SmolStr :: as_str) ;
1800
+ let is_tool = builtin_attr:: TOOL_MODULES . iter ( ) . copied ( ) . chain ( registered) . any ( pred) ;
1801
+ if is_tool {
1802
+ return true ;
1800
1803
}
1801
1804
1802
- if let Some ( name) = path. as_ident ( ) {
1803
- let name = name. to_smol_str ( ) ;
1804
- let is_inert = builtin_attr:: INERT_ATTRIBUTES
1805
- . iter ( )
1806
- . copied ( )
1807
- . chain ( self . def_collector . registered_attrs . iter ( ) . map ( SmolStr :: as_str) )
1808
- . any ( |attr| name == * attr) ;
1805
+ if segments. len ( ) == 1 {
1806
+ let registered = self . def_collector . registered_attrs . iter ( ) . map ( SmolStr :: as_str) ;
1807
+ let is_inert =
1808
+ builtin_attr:: INERT_ATTRIBUTES . iter ( ) . copied ( ) . chain ( registered) . any ( pred) ;
1809
1809
return is_inert;
1810
1810
}
1811
1811
}
1812
-
1813
1812
false
1814
1813
}
1815
1814
@@ -1831,7 +1830,7 @@ impl ModCollector<'_, '_> {
1831
1830
1832
1831
let is_export = export_attr. exists ( ) ;
1833
1832
let is_local_inner = if is_export {
1834
- export_attr. tt_values ( ) . map ( |it| & it. token_trees ) . flatten ( ) . any ( |it| match it {
1833
+ export_attr. tt_values ( ) . flat_map ( |it| & it. token_trees ) . any ( |it| match it {
1835
1834
tt:: TokenTree :: Leaf ( tt:: Leaf :: Ident ( ident) ) => {
1836
1835
ident. text . contains ( "local_inner_macros" )
1837
1836
}
@@ -1852,12 +1851,14 @@ impl ModCollector<'_, '_> {
1852
1851
& name
1853
1852
}
1854
1853
None => {
1855
- match attrs. by_key ( "rustc_builtin_macro" ) . tt_values ( ) . next ( ) . and_then ( |tt| {
1856
- match tt. token_trees . first ( ) {
1857
- Some ( tt:: TokenTree :: Leaf ( tt:: Leaf :: Ident ( name) ) ) => Some ( name) ,
1858
- _ => None ,
1859
- }
1860
- } ) {
1854
+ let explicit_name =
1855
+ attrs. by_key ( "rustc_builtin_macro" ) . tt_values ( ) . next ( ) . and_then ( |tt| {
1856
+ match tt. token_trees . first ( ) {
1857
+ Some ( tt:: TokenTree :: Leaf ( tt:: Leaf :: Ident ( name) ) ) => Some ( name) ,
1858
+ _ => None ,
1859
+ }
1860
+ } ) ;
1861
+ match explicit_name {
1861
1862
Some ( ident) => {
1862
1863
name = ident. as_name ( ) ;
1863
1864
& name
@@ -1947,7 +1948,7 @@ impl ModCollector<'_, '_> {
1947
1948
}
1948
1949
1949
1950
fn collect_macro_call ( & mut self , mac : & MacroCall ) {
1950
- let ast_id = AstIdWithPath :: new ( self . file_id ( ) , mac. ast_id , ( * mac. path ) . clone ( ) ) ;
1951
+ let ast_id = AstIdWithPath :: new ( self . file_id ( ) , mac. ast_id , ModPath :: clone ( & mac. path ) ) ;
1951
1952
1952
1953
// Case 1: try to resolve in legacy scope and expand macro_rules
1953
1954
let mut error = None ;
0 commit comments