@@ -150,51 +150,31 @@ pub struct TraitData {
150
150
151
151
impl TraitData {
152
152
pub ( crate ) fn trait_data_query ( db : & dyn DefDatabase , tr : TraitId ) -> Arc < TraitData > {
153
- let src = tr. lookup ( db) . source ( db) ;
153
+ let tr_loc = tr. lookup ( db) ;
154
+ let src = tr_loc. source ( db) ;
154
155
let name = src. value . name ( ) . map_or_else ( Name :: missing, |n| n. as_name ( ) ) ;
155
156
let auto = src. value . auto_token ( ) . is_some ( ) ;
156
- let ast_id_map = db . ast_id_map ( src . file_id ) ;
157
+ let module_id = tr_loc . container . module ( db ) ;
157
158
158
159
let container = AssocContainerId :: TraitId ( tr) ;
159
- let items = if let Some ( item_list) = src. value . item_list ( ) {
160
- item_list
161
- . impl_items ( )
162
- . map ( |item_node| match item_node {
163
- ast:: ImplItem :: FnDef ( it) => {
164
- let name = it. name ( ) . map_or_else ( Name :: missing, |it| it. as_name ( ) ) ;
165
- let def = FunctionLoc {
166
- container,
167
- ast_id : AstId :: new ( src. file_id , ast_id_map. ast_id ( & it) ) ,
168
- }
169
- . intern ( db)
170
- . into ( ) ;
171
- ( name, def)
172
- }
173
- ast:: ImplItem :: ConstDef ( it) => {
174
- let name = it. name ( ) . map_or_else ( Name :: missing, |it| it. as_name ( ) ) ;
175
- let def = ConstLoc {
176
- container,
177
- ast_id : AstId :: new ( src. file_id , ast_id_map. ast_id ( & it) ) ,
178
- }
179
- . intern ( db)
180
- . into ( ) ;
181
- ( name, def)
182
- }
183
- ast:: ImplItem :: TypeAliasDef ( it) => {
184
- let name = it. name ( ) . map_or_else ( Name :: missing, |it| it. as_name ( ) ) ;
185
- let def = TypeAliasLoc {
186
- container,
187
- ast_id : AstId :: new ( src. file_id , ast_id_map. ast_id ( & it) ) ,
188
- }
189
- . intern ( db)
190
- . into ( ) ;
191
- ( name, def)
192
- }
193
- } )
194
- . collect ( )
195
- } else {
196
- Vec :: new ( )
197
- } ;
160
+ let mut items = Vec :: new ( ) ;
161
+
162
+ if let Some ( item_list) = src. value . item_list ( ) {
163
+ let mut expander = Expander :: new ( db, tr_loc. ast_id . file_id , module_id) ;
164
+ items. extend ( collect_impl_items (
165
+ db,
166
+ & mut expander,
167
+ item_list. impl_items ( ) ,
168
+ src. file_id ,
169
+ container,
170
+ ) ) ;
171
+ items. extend ( collect_impl_items_in_macros (
172
+ db,
173
+ & mut expander,
174
+ & src. with_value ( item_list) ,
175
+ container,
176
+ ) ) ;
177
+ }
198
178
Arc :: new ( TraitData { name, items, auto } )
199
179
}
200
180
@@ -232,24 +212,33 @@ impl ImplData {
232
212
let target_type = TypeRef :: from_ast_opt ( & lower_ctx, src. value . target_type ( ) ) ;
233
213
let is_negative = src. value . excl_token ( ) . is_some ( ) ;
234
214
let module_id = impl_loc. container . module ( db) ;
215
+ let container = AssocContainerId :: ImplId ( id) ;
235
216
236
- let mut items = Vec :: new ( ) ;
217
+ let mut items: Vec < AssocItemId > = Vec :: new ( ) ;
237
218
238
219
if let Some ( item_list) = src. value . item_list ( ) {
239
220
let mut expander = Expander :: new ( db, impl_loc. ast_id . file_id , module_id) ;
240
- items. extend ( collect_impl_items (
241
- db,
242
- & mut expander,
243
- item_list. impl_items ( ) ,
244
- src. file_id ,
245
- id,
246
- ) ) ;
247
- items. extend ( collect_impl_items_in_macros (
248
- db,
249
- & mut expander,
250
- & src. with_value ( item_list) ,
251
- id,
252
- ) ) ;
221
+ items. extend (
222
+ collect_impl_items (
223
+ db,
224
+ & mut expander,
225
+ item_list. impl_items ( ) ,
226
+ src. file_id ,
227
+ container,
228
+ )
229
+ . into_iter ( )
230
+ . map ( |( _, item) | item) ,
231
+ ) ;
232
+ items. extend (
233
+ collect_impl_items_in_macros (
234
+ db,
235
+ & mut expander,
236
+ & src. with_value ( item_list) ,
237
+ container,
238
+ )
239
+ . into_iter ( )
240
+ . map ( |( _, item) | item) ,
241
+ ) ;
253
242
}
254
243
255
244
let res = ImplData { target_trait, target_type, items, is_negative } ;
@@ -296,15 +285,15 @@ fn collect_impl_items_in_macros(
296
285
db : & dyn DefDatabase ,
297
286
expander : & mut Expander ,
298
287
impl_def : & InFile < ast:: ItemList > ,
299
- id : ImplId ,
300
- ) -> Vec < AssocItemId > {
288
+ container : AssocContainerId ,
289
+ ) -> Vec < ( Name , AssocItemId ) > {
301
290
let mut res = Vec :: new ( ) ;
302
291
303
292
// We set a limit to protect against infinite recursion
304
293
let limit = 100 ;
305
294
306
295
for m in impl_def. value . syntax ( ) . children ( ) . filter_map ( ast:: MacroCall :: cast) {
307
- res. extend ( collect_impl_items_in_macro ( db, expander, m, id , limit) )
296
+ res. extend ( collect_impl_items_in_macro ( db, expander, m, container , limit) )
308
297
}
309
298
310
299
res
@@ -314,9 +303,9 @@ fn collect_impl_items_in_macro(
314
303
db : & dyn DefDatabase ,
315
304
expander : & mut Expander ,
316
305
m : ast:: MacroCall ,
317
- id : ImplId ,
306
+ container : AssocContainerId ,
318
307
limit : usize ,
319
- ) -> Vec < AssocItemId > {
308
+ ) -> Vec < ( Name , AssocItemId ) > {
320
309
if limit == 0 {
321
310
return Vec :: new ( ) ;
322
311
}
@@ -328,13 +317,14 @@ fn collect_impl_items_in_macro(
328
317
expander,
329
318
items. value . items ( ) . filter_map ( |it| ImplItem :: cast ( it. syntax ( ) . clone ( ) ) ) ,
330
319
items. file_id ,
331
- id ,
320
+ container ,
332
321
) ;
322
+
333
323
// Recursive collect macros
334
324
// Note that ast::ModuleItem do not include ast::MacroCall
335
325
// We cannot use ModuleItemOwner::items here
336
326
for it in items. value . syntax ( ) . children ( ) . filter_map ( ast:: MacroCall :: cast) {
337
- res. extend ( collect_impl_items_in_macro ( db, expander, it, id , limit - 1 ) )
327
+ res. extend ( collect_impl_items_in_macro ( db, expander, it, container , limit - 1 ) )
338
328
}
339
329
expander. exit ( db, mark) ;
340
330
res
@@ -348,39 +338,34 @@ fn collect_impl_items(
348
338
expander : & mut Expander ,
349
339
impl_items : impl Iterator < Item = ImplItem > ,
350
340
file_id : crate :: HirFileId ,
351
- id : ImplId ,
352
- ) -> Vec < AssocItemId > {
341
+ container : AssocContainerId ,
342
+ ) -> Vec < ( Name , AssocItemId ) > {
353
343
let items = db. ast_id_map ( file_id) ;
354
344
355
345
impl_items
356
346
. filter_map ( |item_node| match item_node {
357
347
ast:: ImplItem :: FnDef ( it) => {
348
+ let name = it. name ( ) . map_or_else ( Name :: missing, |it| it. as_name ( ) ) ;
358
349
let attrs = expander. parse_attrs ( & it) ;
359
350
if !expander. is_cfg_enabled ( & attrs) {
360
351
return None ;
361
352
}
362
- let def = FunctionLoc {
363
- container : AssocContainerId :: ImplId ( id) ,
364
- ast_id : AstId :: new ( file_id, items. ast_id ( & it) ) ,
365
- }
366
- . intern ( db) ;
367
- Some ( def. into ( ) )
353
+ let def = FunctionLoc { container, ast_id : AstId :: new ( file_id, items. ast_id ( & it) ) }
354
+ . intern ( db) ;
355
+ Some ( ( name, def. into ( ) ) )
368
356
}
369
357
ast:: ImplItem :: ConstDef ( it) => {
370
- let def = ConstLoc {
371
- container : AssocContainerId :: ImplId ( id) ,
372
- ast_id : AstId :: new ( file_id, items. ast_id ( & it) ) ,
373
- }
374
- . intern ( db) ;
375
- Some ( def. into ( ) )
358
+ let name = it. name ( ) . map_or_else ( Name :: missing, |it| it. as_name ( ) ) ;
359
+ let def = ConstLoc { container, ast_id : AstId :: new ( file_id, items. ast_id ( & it) ) }
360
+ . intern ( db) ;
361
+ Some ( ( name, def. into ( ) ) )
376
362
}
377
363
ast:: ImplItem :: TypeAliasDef ( it) => {
378
- let def = TypeAliasLoc {
379
- container : AssocContainerId :: ImplId ( id) ,
380
- ast_id : AstId :: new ( file_id, items. ast_id ( & it) ) ,
381
- }
382
- . intern ( db) ;
383
- Some ( def. into ( ) )
364
+ let name = it. name ( ) . map_or_else ( Name :: missing, |it| it. as_name ( ) ) ;
365
+ let def =
366
+ TypeAliasLoc { container, ast_id : AstId :: new ( file_id, items. ast_id ( & it) ) }
367
+ . intern ( db) ;
368
+ Some ( ( name, def. into ( ) ) )
384
369
}
385
370
} )
386
371
. collect ( )
0 commit comments