@@ -32,8 +32,8 @@ use crate::{
32
32
derive_macro_as_call_id,
33
33
item_scope:: { ImportType , PerNsGlobImports } ,
34
34
item_tree:: {
35
- self , Fields , FileItemTreeId , ImportKind , ItemTree , ItemTreeId , ItemTreeNode , MacroCall ,
36
- MacroDef , MacroRules , Mod , ModItem , ModKind , TreeId ,
35
+ self , FieldAstId , Fields , FileItemTreeId , ImportKind , ItemTree , ItemTreeId , ItemTreeNode ,
36
+ MacroCall , MacroDef , MacroRules , Mod , ModItem , ModKind , TreeId ,
37
37
} ,
38
38
macro_call_as_call_id, macro_id_to_def_id,
39
39
nameres:: {
@@ -1511,7 +1511,10 @@ impl ModCollector<'_, '_> {
1511
1511
let attrs = self . item_tree . attrs ( self . def_collector . db , krate, item. into ( ) ) ;
1512
1512
if let Some ( cfg) = attrs. cfg ( ) {
1513
1513
if !self . is_cfg_enabled ( & cfg) {
1514
- self . emit_unconfigured_diagnostic ( item, & cfg) ;
1514
+ self . emit_unconfigured_diagnostic (
1515
+ InFile :: new ( self . file_id ( ) , item. ast_id ( & self . item_tree ) . upcast ( ) ) ,
1516
+ & cfg,
1517
+ ) ;
1515
1518
continue ;
1516
1519
}
1517
1520
}
@@ -1523,22 +1526,20 @@ impl ModCollector<'_, '_> {
1523
1526
}
1524
1527
1525
1528
let db = self . def_collector . db ;
1526
- let module = self . def_collector . def_map . module_id ( self . module_id ) ;
1527
- let def_map = & mut self . def_collector . def_map ;
1529
+ let module_id = self . module_id ;
1530
+ let module = self . def_collector . def_map . module_id ( module_id ) ;
1528
1531
let update_def =
1529
1532
|def_collector : & mut DefCollector < ' _ > , id, name : & Name , vis, has_constructor| {
1530
- def_collector. def_map . modules [ self . module_id ] . scope . declare ( id) ;
1533
+ def_collector. def_map . modules [ module_id] . scope . declare ( id) ;
1531
1534
def_collector. update (
1532
- self . module_id ,
1535
+ module_id,
1533
1536
& [ ( Some ( name. clone ( ) ) , PerNs :: from_def ( id, vis, has_constructor) ) ] ,
1534
1537
vis,
1535
1538
ImportType :: Named ,
1536
1539
)
1537
1540
} ;
1538
1541
let resolve_vis = |def_map : & DefMap , visibility| {
1539
- def_map
1540
- . resolve_visibility ( db, self . module_id , visibility)
1541
- . unwrap_or ( Visibility :: Public )
1542
+ def_map. resolve_visibility ( db, module_id, visibility) . unwrap_or ( Visibility :: Public )
1542
1543
} ;
1543
1544
1544
1545
match item {
@@ -1594,6 +1595,7 @@ impl ModCollector<'_, '_> {
1594
1595
let fn_id =
1595
1596
FunctionLoc { container, id : ItemTreeId :: new ( self . tree_id , id) } . intern ( db) ;
1596
1597
1598
+ let def_map = & self . def_collector . def_map ;
1597
1599
let vis = resolve_vis ( def_map, & self . item_tree [ it. visibility ] ) ;
1598
1600
if self . def_collector . is_proc_macro {
1599
1601
if self . module_id == def_map. root {
@@ -1614,7 +1616,10 @@ impl ModCollector<'_, '_> {
1614
1616
ModItem :: Struct ( id) => {
1615
1617
let it = & self . item_tree [ id] ;
1616
1618
1617
- let vis = resolve_vis ( def_map, & self . item_tree [ it. visibility ] ) ;
1619
+ self . process_fields ( & it. fields ) ;
1620
+
1621
+ let vis =
1622
+ resolve_vis ( & self . def_collector . def_map , & self . item_tree [ it. visibility ] ) ;
1618
1623
update_def (
1619
1624
self . def_collector ,
1620
1625
StructLoc { container : module, id : ItemTreeId :: new ( self . tree_id , id) }
@@ -1628,7 +1633,10 @@ impl ModCollector<'_, '_> {
1628
1633
ModItem :: Union ( id) => {
1629
1634
let it = & self . item_tree [ id] ;
1630
1635
1631
- let vis = resolve_vis ( def_map, & self . item_tree [ it. visibility ] ) ;
1636
+ self . process_fields ( & it. fields ) ;
1637
+
1638
+ let vis =
1639
+ resolve_vis ( & self . def_collector . def_map , & self . item_tree [ it. visibility ] ) ;
1632
1640
update_def (
1633
1641
self . def_collector ,
1634
1642
UnionLoc { container : module, id : ItemTreeId :: new ( self . tree_id , id) }
@@ -1642,7 +1650,21 @@ impl ModCollector<'_, '_> {
1642
1650
ModItem :: Enum ( id) => {
1643
1651
let it = & self . item_tree [ id] ;
1644
1652
1645
- let vis = resolve_vis ( def_map, & self . item_tree [ it. visibility ] ) ;
1653
+ for id in it. variants . clone ( ) {
1654
+ let variant = & self . item_tree [ id] ;
1655
+ let attrs = self . item_tree . attrs ( self . def_collector . db , krate, id. into ( ) ) ;
1656
+ if let Some ( cfg) = attrs. cfg ( ) {
1657
+ if !self . is_cfg_enabled ( & cfg) {
1658
+ self . emit_unconfigured_diagnostic (
1659
+ InFile :: new ( self . file_id ( ) , variant. ast_id . upcast ( ) ) ,
1660
+ & cfg,
1661
+ ) ;
1662
+ }
1663
+ }
1664
+ }
1665
+
1666
+ let vis =
1667
+ resolve_vis ( & self . def_collector . def_map , & self . item_tree [ it. visibility ] ) ;
1646
1668
update_def (
1647
1669
self . def_collector ,
1648
1670
EnumLoc { container : module, id : ItemTreeId :: new ( self . tree_id , id) }
@@ -1660,7 +1682,10 @@ impl ModCollector<'_, '_> {
1660
1682
1661
1683
match & it. name {
1662
1684
Some ( name) => {
1663
- let vis = resolve_vis ( def_map, & self . item_tree [ it. visibility ] ) ;
1685
+ let vis = resolve_vis (
1686
+ & self . def_collector . def_map ,
1687
+ & self . item_tree [ it. visibility ] ,
1688
+ ) ;
1664
1689
update_def ( self . def_collector , const_id. into ( ) , name, vis, false ) ;
1665
1690
}
1666
1691
None => {
@@ -1674,7 +1699,8 @@ impl ModCollector<'_, '_> {
1674
1699
ModItem :: Static ( id) => {
1675
1700
let it = & self . item_tree [ id] ;
1676
1701
1677
- let vis = resolve_vis ( def_map, & self . item_tree [ it. visibility ] ) ;
1702
+ let vis =
1703
+ resolve_vis ( & self . def_collector . def_map , & self . item_tree [ it. visibility ] ) ;
1678
1704
update_def (
1679
1705
self . def_collector ,
1680
1706
StaticLoc { container, id : ItemTreeId :: new ( self . tree_id , id) }
@@ -1688,7 +1714,8 @@ impl ModCollector<'_, '_> {
1688
1714
ModItem :: Trait ( id) => {
1689
1715
let it = & self . item_tree [ id] ;
1690
1716
1691
- let vis = resolve_vis ( def_map, & self . item_tree [ it. visibility ] ) ;
1717
+ let vis =
1718
+ resolve_vis ( & self . def_collector . def_map , & self . item_tree [ it. visibility ] ) ;
1692
1719
update_def (
1693
1720
self . def_collector ,
1694
1721
TraitLoc { container : module, id : ItemTreeId :: new ( self . tree_id , id) }
@@ -1702,7 +1729,8 @@ impl ModCollector<'_, '_> {
1702
1729
ModItem :: TypeAlias ( id) => {
1703
1730
let it = & self . item_tree [ id] ;
1704
1731
1705
- let vis = resolve_vis ( def_map, & self . item_tree [ it. visibility ] ) ;
1732
+ let vis =
1733
+ resolve_vis ( & self . def_collector . def_map , & self . item_tree [ it. visibility ] ) ;
1706
1734
update_def (
1707
1735
self . def_collector ,
1708
1736
TypeAliasLoc { container, id : ItemTreeId :: new ( self . tree_id , id) }
@@ -2115,17 +2143,44 @@ impl ModCollector<'_, '_> {
2115
2143
}
2116
2144
}
2117
2145
2146
+ fn process_fields ( & mut self , fields : & Fields ) {
2147
+ match fields {
2148
+ Fields :: Record ( range) | Fields :: Tuple ( range) => {
2149
+ for id in range. clone ( ) {
2150
+ let field = & self . item_tree [ id] ;
2151
+ let attrs = self . item_tree . attrs (
2152
+ self . def_collector . db ,
2153
+ self . def_collector . def_map . krate ,
2154
+ id. into ( ) ,
2155
+ ) ;
2156
+ if let Some ( cfg) = attrs. cfg ( ) {
2157
+ if !self . is_cfg_enabled ( & cfg) {
2158
+ self . emit_unconfigured_diagnostic (
2159
+ InFile :: new (
2160
+ self . file_id ( ) ,
2161
+ match field. ast_id {
2162
+ FieldAstId :: Record ( it) => it. upcast ( ) ,
2163
+ FieldAstId :: Tuple ( it) => it. upcast ( ) ,
2164
+ } ,
2165
+ ) ,
2166
+ & cfg,
2167
+ ) ;
2168
+ }
2169
+ }
2170
+ }
2171
+ }
2172
+ Fields :: Unit => { }
2173
+ }
2174
+ }
2175
+
2118
2176
fn is_cfg_enabled ( & self , cfg : & CfgExpr ) -> bool {
2119
2177
self . def_collector . cfg_options . check ( cfg) != Some ( false )
2120
2178
}
2121
2179
2122
- fn emit_unconfigured_diagnostic ( & mut self , item : ModItem , cfg : & CfgExpr ) {
2123
- let ast_id = item. ast_id ( self . item_tree ) ;
2124
-
2125
- let ast_id = InFile :: new ( self . file_id ( ) , ast_id) ;
2180
+ fn emit_unconfigured_diagnostic ( & mut self , ast : AstId < ast:: AnyHasAttrs > , cfg : & CfgExpr ) {
2126
2181
self . def_collector . def_map . diagnostics . push ( DefDiagnostic :: unconfigured_code (
2127
2182
self . module_id ,
2128
- ast_id ,
2183
+ ast ,
2129
2184
cfg. clone ( ) ,
2130
2185
self . def_collector . cfg_options . clone ( ) ,
2131
2186
) ) ;
0 commit comments