@@ -8,8 +8,9 @@ use arrayvec::ArrayVec;
8
8
use base_db:: { CrateId , Edition } ;
9
9
use chalk_ir:: { cast:: Cast , Mutability , UniverseIndex } ;
10
10
use hir_def:: {
11
- lang_item:: LangItemTarget , nameres:: DefMap , AssocItemId , BlockId , FunctionId , GenericDefId ,
12
- HasModule , ImplId , ItemContainerId , Lookup , ModuleId , TraitId ,
11
+ item_scope:: ItemScope , lang_item:: LangItemTarget , nameres:: DefMap , AssocItemId , BlockId ,
12
+ ConstId , FunctionId , GenericDefId , HasModule , ImplId , ItemContainerId , Lookup , ModuleDefId ,
13
+ ModuleId , TraitId ,
13
14
} ;
14
15
use hir_expand:: name:: Name ;
15
16
use rustc_hash:: { FxHashMap , FxHashSet } ;
@@ -177,7 +178,7 @@ impl TraitImpls {
177
178
178
179
// To better support custom derives, collect impls in all unnamed const items.
179
180
// const _: () = { ... };
180
- for konst in module_data. scope . unnamed_consts ( ) {
181
+ for konst in collect_unnamed_consts ( db , & module_data. scope ) {
181
182
let body = db. body ( konst. into ( ) ) ;
182
183
for ( _, block_def_map) in body. blocks ( db. upcast ( ) ) {
183
184
self . collect_def_map ( db, & block_def_map) ;
@@ -297,7 +298,7 @@ impl InherentImpls {
297
298
298
299
// To better support custom derives, collect impls in all unnamed const items.
299
300
// const _: () = { ... };
300
- for konst in module_data. scope . unnamed_consts ( ) {
301
+ for konst in collect_unnamed_consts ( db , & module_data. scope ) {
301
302
let body = db. body ( konst. into ( ) ) ;
302
303
for ( _, block_def_map) in body. blocks ( db. upcast ( ) ) {
303
304
self . collect_def_map ( db, & block_def_map) ;
@@ -318,6 +319,34 @@ impl InherentImpls {
318
319
}
319
320
}
320
321
322
+ fn collect_unnamed_consts < ' a > (
323
+ db : & ' a dyn HirDatabase ,
324
+ scope : & ' a ItemScope ,
325
+ ) -> impl Iterator < Item = ConstId > + ' a {
326
+ let unnamed_consts = scope. unnamed_consts ( ) ;
327
+
328
+ // FIXME: Also treat consts named `_DERIVE_*` as unnamed, since synstructure generates those.
329
+ // Should be removed once synstructure stops doing that.
330
+ let synstructure_hack_consts = scope. values ( ) . filter_map ( |( item, _) | match item {
331
+ ModuleDefId :: ConstId ( id) => {
332
+ let loc = id. lookup ( db. upcast ( ) ) ;
333
+ let item_tree = loc. id . item_tree ( db. upcast ( ) ) ;
334
+ if item_tree[ loc. id . value ]
335
+ . name
336
+ . as_ref ( )
337
+ . map_or ( false , |n| n. to_smol_str ( ) . starts_with ( "_DERIVE_" ) )
338
+ {
339
+ Some ( id)
340
+ } else {
341
+ None
342
+ }
343
+ }
344
+ _ => None ,
345
+ } ) ;
346
+
347
+ unnamed_consts. chain ( synstructure_hack_consts)
348
+ }
349
+
321
350
pub fn def_crates (
322
351
db : & dyn HirDatabase ,
323
352
ty : & Ty ,
0 commit comments