|
1 | 1 | //! A higher level attributes based on TokenTree, with also some shortcuts. |
2 | 2 |
|
3 | | -use std::{borrow::Cow, hash::Hash, ops}; |
| 3 | +use std::{borrow::Cow, convert::identity, hash::Hash, ops}; |
4 | 4 |
|
5 | 5 | use base_db::Crate; |
6 | 6 | use cfg::{CfgExpr, CfgOptions}; |
7 | 7 | use either::Either; |
8 | 8 | use hir_expand::{ |
9 | 9 | HirFileId, InFile, |
10 | 10 | attrs::{Attr, AttrId, RawAttrs, collect_attrs}, |
| 11 | + span_map::SpanMapRef, |
11 | 12 | }; |
12 | 13 | use intern::{Symbol, sym}; |
13 | 14 | use la_arena::{ArenaMap, Idx, RawIdx}; |
@@ -45,8 +46,28 @@ impl Attrs { |
45 | 46 | (**self).iter().find(|attr| attr.id == id) |
46 | 47 | } |
47 | 48 |
|
48 | | - pub(crate) fn filter(db: &dyn DefDatabase, krate: Crate, raw_attrs: RawAttrs) -> Attrs { |
49 | | - Attrs(raw_attrs.filter(db, krate)) |
| 49 | + pub(crate) fn expand_cfg_attr( |
| 50 | + db: &dyn DefDatabase, |
| 51 | + krate: Crate, |
| 52 | + raw_attrs: RawAttrs, |
| 53 | + ) -> Attrs { |
| 54 | + Attrs(raw_attrs.expand_cfg_attr(db, krate)) |
| 55 | + } |
| 56 | + |
| 57 | + pub(crate) fn is_cfg_enabled_for( |
| 58 | + db: &dyn DefDatabase, |
| 59 | + owner: &dyn ast::HasAttrs, |
| 60 | + span_map: SpanMapRef<'_>, |
| 61 | + cfg_options: &CfgOptions, |
| 62 | + ) -> Result<(), CfgExpr> { |
| 63 | + // FIXME: THis unnecessarily desugars doc comments |
| 64 | + RawAttrs::attrs_iter_expanded(db, owner, span_map, cfg_options) |
| 65 | + .filter_map(|attr| attr.cfg()) |
| 66 | + .find_map(|cfg| match cfg_options.check(&cfg).is_none_or(identity) { |
| 67 | + true => None, |
| 68 | + false => Some(cfg), |
| 69 | + }) |
| 70 | + .map_or(Ok(()), Err) |
50 | 71 | } |
51 | 72 | } |
52 | 73 |
|
@@ -522,46 +543,49 @@ impl AttrsWithOwner { |
522 | 543 | GenericParamId::ConstParamId(it) => { |
523 | 544 | let src = it.parent().child_source(db); |
524 | 545 | // FIXME: We should be never getting `None` here. |
525 | | - match src.value.get(it.local_id()) { |
526 | | - Some(val) => RawAttrs::from_attrs_owner( |
| 546 | + return Attrs(match src.value.get(it.local_id()) { |
| 547 | + Some(val) => RawAttrs::new_expanded( |
527 | 548 | db, |
528 | | - src.with_value(val), |
| 549 | + val, |
529 | 550 | db.span_map(src.file_id).as_ref(), |
| 551 | + def.krate(db).cfg_options(db), |
530 | 552 | ), |
531 | 553 | None => RawAttrs::EMPTY, |
532 | | - } |
| 554 | + }); |
533 | 555 | } |
534 | 556 | GenericParamId::TypeParamId(it) => { |
535 | 557 | let src = it.parent().child_source(db); |
536 | 558 | // FIXME: We should be never getting `None` here. |
537 | | - match src.value.get(it.local_id()) { |
538 | | - Some(val) => RawAttrs::from_attrs_owner( |
| 559 | + return Attrs(match src.value.get(it.local_id()) { |
| 560 | + Some(val) => RawAttrs::new_expanded( |
539 | 561 | db, |
540 | | - src.with_value(val), |
| 562 | + val, |
541 | 563 | db.span_map(src.file_id).as_ref(), |
| 564 | + def.krate(db).cfg_options(db), |
542 | 565 | ), |
543 | 566 | None => RawAttrs::EMPTY, |
544 | | - } |
| 567 | + }); |
545 | 568 | } |
546 | 569 | GenericParamId::LifetimeParamId(it) => { |
547 | 570 | let src = it.parent.child_source(db); |
548 | 571 | // FIXME: We should be never getting `None` here. |
549 | | - match src.value.get(it.local_id) { |
550 | | - Some(val) => RawAttrs::from_attrs_owner( |
| 572 | + return Attrs(match src.value.get(it.local_id) { |
| 573 | + Some(val) => RawAttrs::new_expanded( |
551 | 574 | db, |
552 | | - src.with_value(val), |
| 575 | + val, |
553 | 576 | db.span_map(src.file_id).as_ref(), |
| 577 | + def.krate(db).cfg_options(db), |
554 | 578 | ), |
555 | 579 | None => RawAttrs::EMPTY, |
556 | | - } |
| 580 | + }); |
557 | 581 | } |
558 | 582 | }, |
559 | 583 | AttrDefId::ExternBlockId(it) => attrs_from_item_tree_loc(db, it), |
560 | 584 | AttrDefId::ExternCrateId(it) => attrs_from_item_tree_loc(db, it), |
561 | 585 | AttrDefId::UseId(it) => attrs_from_item_tree_loc(db, it), |
562 | 586 | }; |
563 | 587 |
|
564 | | - let attrs = raw_attrs.filter(db, def.krate(db)); |
| 588 | + let attrs = raw_attrs.expand_cfg_attr(db, def.krate(db)); |
565 | 589 | Attrs(attrs) |
566 | 590 | } |
567 | 591 |
|
|
0 commit comments