@@ -19,8 +19,8 @@ use tt::Subtree;
19
19
use crate :: {
20
20
db:: DefDatabase ,
21
21
intern:: Interned ,
22
- item_tree:: { Fields , ItemTreeId , ItemTreeNode } ,
23
- nameres:: ModuleSource ,
22
+ item_tree:: { AttrOwner , Fields , ItemTreeId , ItemTreeNode } ,
23
+ nameres:: { ModuleOrigin , ModuleSource } ,
24
24
path:: { ModPath , PathKind } ,
25
25
src:: { HasChildSource , HasSource } ,
26
26
AdtId , AttrDefId , EnumId , GenericParamId , LocalEnumVariantId , LocalFieldId , Lookup , MacroId ,
@@ -201,6 +201,7 @@ impl Attrs {
201
201
db : & dyn DefDatabase ,
202
202
e : EnumId ,
203
203
) -> Arc < ArenaMap < LocalEnumVariantId , Attrs > > {
204
+ // FIXME: There should be some proper form of mapping between item tree enum variant ids and hir enum variant ids
204
205
let mut res = ArenaMap :: default ( ) ;
205
206
206
207
let loc = e. lookup ( db) ;
@@ -226,6 +227,7 @@ impl Attrs {
226
227
db : & dyn DefDatabase ,
227
228
v : VariantId ,
228
229
) -> Arc < ArenaMap < LocalFieldId , Attrs > > {
230
+ // FIXME: There should be some proper form of mapping between item tree field ids and hir field ids
229
231
let mut res = ArenaMap :: default ( ) ;
230
232
231
233
let crate_graph = db. crate_graph ( ) ;
@@ -295,11 +297,14 @@ impl Attrs {
295
297
296
298
impl Attrs {
297
299
pub fn cfg ( & self ) -> Option < CfgExpr > {
298
- let mut cfgs = self . by_key ( "cfg" ) . tt_values ( ) . map ( CfgExpr :: parse) . collect :: < Vec < _ > > ( ) ;
299
- match cfgs. len ( ) {
300
- 0 => None ,
301
- 1 => Some ( cfgs. pop ( ) . unwrap ( ) ) ,
302
- _ => Some ( CfgExpr :: All ( cfgs) ) ,
300
+ let mut cfgs = self . by_key ( "cfg" ) . tt_values ( ) . map ( CfgExpr :: parse) ;
301
+ let first = cfgs. next ( ) ?;
302
+ match cfgs. next ( ) {
303
+ Some ( second) => {
304
+ let cfgs = [ first, second] . into_iter ( ) . chain ( cfgs) ;
305
+ Some ( CfgExpr :: All ( cfgs. collect ( ) ) )
306
+ }
307
+ None => Some ( first) ,
303
308
}
304
309
}
305
310
pub ( crate ) fn is_cfg_enabled ( & self , cfg_options : & CfgOptions ) -> bool {
@@ -367,25 +372,37 @@ impl AttrsWithOwner {
367
372
AttrDefId :: ModuleId ( module) => {
368
373
let def_map = module. def_map ( db) ;
369
374
let mod_data = & def_map[ module. local_id ] ;
370
- match mod_data. declaration_source ( db) {
371
- Some ( it) => {
375
+
376
+ match mod_data. origin {
377
+ // FIXME: We should be able to leverage the item tree instead of parsing declaration here
378
+ // but we don't save the module's item tree id anywhere
379
+ ModuleOrigin :: File { definition, declaration, .. } => {
380
+ let value = declaration. to_node ( db. upcast ( ) ) ;
381
+ let it = InFile { file_id : declaration. file_id , value } ;
372
382
let raw_attrs = RawAttrs :: from_attrs_owner (
373
383
db,
374
384
it. as_ref ( ) . map ( |it| it as & dyn ast:: HasAttrs ) ,
375
385
) ;
376
- match mod_data. definition_source ( db) {
377
- InFile { file_id, value : ModuleSource :: SourceFile ( file) } => raw_attrs
378
- . merge ( RawAttrs :: from_attrs_owner ( db, InFile :: new ( file_id, & file) ) ) ,
379
- _ => raw_attrs,
380
- }
386
+ let tree = db. file_item_tree ( definition. into ( ) ) ;
387
+ raw_attrs. merge ( tree. raw_attrs ( AttrOwner :: TopLevel ) . clone ( ) )
388
+ }
389
+ ModuleOrigin :: CrateRoot { definition } => {
390
+ let tree = db. file_item_tree ( definition. into ( ) ) ;
391
+ tree. raw_attrs ( AttrOwner :: TopLevel ) . clone ( )
381
392
}
382
- None => RawAttrs :: from_attrs_owner (
393
+ // FIXME: We should be able to leverage the item tree instead of parsing here
394
+ // but we don't save the module's item tree id anywhere
395
+ ModuleOrigin :: Inline { definition } => RawAttrs :: from_attrs_owner (
383
396
db,
384
- mod_data. definition_source ( db) . as_ref ( ) . map ( |src| match src {
385
- ModuleSource :: SourceFile ( file) => file as & dyn ast:: HasAttrs ,
386
- ModuleSource :: Module ( module) => module as & dyn ast:: HasAttrs ,
387
- ModuleSource :: BlockExpr ( block) => block as & dyn ast:: HasAttrs ,
388
- } ) ,
397
+ InFile :: new ( definition. file_id , definition. to_node ( db. upcast ( ) ) )
398
+ . as_ref ( )
399
+ . map ( |it| it as & dyn ast:: HasAttrs ) ,
400
+ ) ,
401
+ ModuleOrigin :: BlockExpr { block } => RawAttrs :: from_attrs_owner (
402
+ db,
403
+ InFile :: new ( block. file_id , block. to_node ( db. upcast ( ) ) )
404
+ . as_ref ( )
405
+ . map ( |it| it as & dyn ast:: HasAttrs ) ,
389
406
) ,
390
407
}
391
408
}
0 commit comments