@@ -4,6 +4,7 @@ use std::sync::Arc;
4
4
5
5
use either:: Either ;
6
6
use hir_expand:: {
7
+ hygiene:: Hygiene ,
7
8
name:: { AsName , Name } ,
8
9
InFile ,
9
10
} ;
@@ -12,9 +13,9 @@ use ra_prof::profile;
12
13
use ra_syntax:: ast:: { self , NameOwner , TypeAscriptionOwner , VisibilityOwner } ;
13
14
14
15
use crate :: {
15
- db:: DefDatabase , src:: HasChildSource , src:: HasSource , trace:: Trace , type_ref :: TypeRef ,
16
- visibility:: RawVisibility , EnumId , LocalEnumVariantId , LocalStructFieldId , Lookup , StructId ,
17
- UnionId , VariantId ,
16
+ attr :: Attrs , db:: DefDatabase , src:: HasChildSource , src:: HasSource , trace:: Trace ,
17
+ type_ref :: TypeRef , visibility:: RawVisibility , EnumId , HasModule , LocalEnumVariantId ,
18
+ LocalStructFieldId , Lookup , ModuleId , StructId , UnionId , VariantId ,
18
19
} ;
19
20
20
21
/// Note that we use `StructData` for unions as well!
@@ -56,7 +57,8 @@ impl StructData {
56
57
let src = id. lookup ( db) . source ( db) ;
57
58
58
59
let name = src. value . name ( ) . map_or_else ( Name :: missing, |n| n. as_name ( ) ) ;
59
- let variant_data = VariantData :: new ( db, src. map ( |s| s. kind ( ) ) ) ;
60
+ let variant_data =
61
+ VariantData :: new ( db, src. map ( |s| s. kind ( ) ) , id. lookup ( db) . container . module ( db) ) ;
60
62
let variant_data = Arc :: new ( variant_data) ;
61
63
Arc :: new ( StructData { name, variant_data } )
62
64
}
@@ -70,6 +72,7 @@ impl StructData {
70
72
. map ( ast:: StructKind :: Record )
71
73
. unwrap_or ( ast:: StructKind :: Unit )
72
74
} ) ,
75
+ id. lookup ( db) . container . module ( db) ,
73
76
) ;
74
77
let variant_data = Arc :: new ( variant_data) ;
75
78
Arc :: new ( StructData { name, variant_data } )
@@ -82,7 +85,7 @@ impl EnumData {
82
85
let src = e. lookup ( db) . source ( db) ;
83
86
let name = src. value . name ( ) . map_or_else ( Name :: missing, |n| n. as_name ( ) ) ;
84
87
let mut trace = Trace :: new_for_arena ( ) ;
85
- lower_enum ( db, & mut trace, & src) ;
88
+ lower_enum ( db, & mut trace, & src, e . lookup ( db ) . container . module ( db ) ) ;
86
89
Arc :: new ( EnumData { name, variants : trace. into_arena ( ) } )
87
90
}
88
91
@@ -98,7 +101,7 @@ impl HasChildSource for EnumId {
98
101
fn child_source ( & self , db : & dyn DefDatabase ) -> InFile < ArenaMap < Self :: ChildId , Self :: Value > > {
99
102
let src = self . lookup ( db) . source ( db) ;
100
103
let mut trace = Trace :: new_for_map ( ) ;
101
- lower_enum ( db, & mut trace, & src) ;
104
+ lower_enum ( db, & mut trace, & src, self . lookup ( db ) . container . module ( db ) ) ;
102
105
src. with_value ( trace. into_map ( ) )
103
106
}
104
107
}
@@ -107,22 +110,23 @@ fn lower_enum(
107
110
db : & dyn DefDatabase ,
108
111
trace : & mut Trace < EnumVariantData , ast:: EnumVariant > ,
109
112
ast : & InFile < ast:: EnumDef > ,
113
+ module_id : ModuleId ,
110
114
) {
111
115
for var in ast. value . variant_list ( ) . into_iter ( ) . flat_map ( |it| it. variants ( ) ) {
112
116
trace. alloc (
113
117
|| var. clone ( ) ,
114
118
|| EnumVariantData {
115
119
name : var. name ( ) . map_or_else ( Name :: missing, |it| it. as_name ( ) ) ,
116
- variant_data : Arc :: new ( VariantData :: new ( db, ast. with_value ( var. kind ( ) ) ) ) ,
120
+ variant_data : Arc :: new ( VariantData :: new ( db, ast. with_value ( var. kind ( ) ) , module_id ) ) ,
117
121
} ,
118
122
) ;
119
123
}
120
124
}
121
125
122
126
impl VariantData {
123
- fn new ( db : & dyn DefDatabase , flavor : InFile < ast:: StructKind > ) -> Self {
127
+ fn new ( db : & dyn DefDatabase , flavor : InFile < ast:: StructKind > , module_id : ModuleId ) -> Self {
124
128
let mut trace = Trace :: new_for_arena ( ) ;
125
- match lower_struct ( db, & mut trace, & flavor) {
129
+ match lower_struct ( db, & mut trace, & flavor, module_id ) {
126
130
StructKind :: Tuple => VariantData :: Tuple ( trace. into_arena ( ) ) ,
127
131
StructKind :: Record => VariantData :: Record ( trace. into_arena ( ) ) ,
128
132
StructKind :: Unit => VariantData :: Unit ,
@@ -155,22 +159,27 @@ impl HasChildSource for VariantId {
155
159
type Value = Either < ast:: TupleFieldDef , ast:: RecordFieldDef > ;
156
160
157
161
fn child_source ( & self , db : & dyn DefDatabase ) -> InFile < ArenaMap < Self :: ChildId , Self :: Value > > {
158
- let src = match self {
162
+ let ( src, module_id ) = match self {
159
163
VariantId :: EnumVariantId ( it) => {
160
164
// I don't really like the fact that we call into parent source
161
165
// here, this might add to more queries then necessary.
162
166
let src = it. parent . child_source ( db) ;
163
- src. map ( |map| map[ it. local_id ] . kind ( ) )
167
+ ( src. map ( |map| map[ it. local_id ] . kind ( ) ) , it . parent . lookup ( db ) . container . module ( db ) )
164
168
}
165
- VariantId :: StructId ( it) => it. lookup ( db) . source ( db) . map ( |it| it. kind ( ) ) ,
166
- VariantId :: UnionId ( it) => it. lookup ( db) . source ( db) . map ( |it| {
167
- it. record_field_def_list ( )
168
- . map ( ast:: StructKind :: Record )
169
- . unwrap_or ( ast:: StructKind :: Unit )
170
- } ) ,
169
+ VariantId :: StructId ( it) => {
170
+ ( it. lookup ( db) . source ( db) . map ( |it| it. kind ( ) ) , it. lookup ( db) . container . module ( db) )
171
+ }
172
+ VariantId :: UnionId ( it) => (
173
+ it. lookup ( db) . source ( db) . map ( |it| {
174
+ it. record_field_def_list ( )
175
+ . map ( ast:: StructKind :: Record )
176
+ . unwrap_or ( ast:: StructKind :: Unit )
177
+ } ) ,
178
+ it. lookup ( db) . container . module ( db) ,
179
+ ) ,
171
180
} ;
172
181
let mut trace = Trace :: new_for_map ( ) ;
173
- lower_struct ( db, & mut trace, & src) ;
182
+ lower_struct ( db, & mut trace, & src, module_id ) ;
174
183
src. with_value ( trace. into_map ( ) )
175
184
}
176
185
}
@@ -186,10 +195,17 @@ fn lower_struct(
186
195
db : & dyn DefDatabase ,
187
196
trace : & mut Trace < StructFieldData , Either < ast:: TupleFieldDef , ast:: RecordFieldDef > > ,
188
197
ast : & InFile < ast:: StructKind > ,
198
+ module_id : ModuleId ,
189
199
) -> StructKind {
200
+ let crate_graph = db. crate_graph ( ) ;
190
201
match & ast. value {
191
202
ast:: StructKind :: Tuple ( fl) => {
192
203
for ( i, fd) in fl. fields ( ) . enumerate ( ) {
204
+ let attrs = Attrs :: new ( & fd, & Hygiene :: new ( db. upcast ( ) , ast. file_id ) ) ;
205
+ if !attrs. is_cfg_enabled ( & crate_graph[ module_id. krate ] . cfg_options ) {
206
+ continue ;
207
+ }
208
+
193
209
trace. alloc (
194
210
|| Either :: Left ( fd. clone ( ) ) ,
195
211
|| StructFieldData {
@@ -203,6 +219,11 @@ fn lower_struct(
203
219
}
204
220
ast:: StructKind :: Record ( fl) => {
205
221
for fd in fl. fields ( ) {
222
+ let attrs = Attrs :: new ( & fd, & Hygiene :: new ( db. upcast ( ) , ast. file_id ) ) ;
223
+ if !attrs. is_cfg_enabled ( & crate_graph[ module_id. krate ] . cfg_options ) {
224
+ continue ;
225
+ }
226
+
206
227
trace. alloc (
207
228
|| Either :: Right ( fd. clone ( ) ) ,
208
229
|| StructFieldData {
0 commit comments