Skip to content

Commit 1747e1a

Browse files
committed
Cleanup cfg check handling in expressiont store lowering
1 parent 7d9b839 commit 1747e1a

File tree

17 files changed

+232
-188
lines changed

17 files changed

+232
-188
lines changed

crates/hir-def/src/attr.rs

+40-16
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
//! A higher level attributes based on TokenTree, with also some shortcuts.
22
3-
use std::{borrow::Cow, hash::Hash, ops};
3+
use std::{borrow::Cow, convert::identity, hash::Hash, ops};
44

55
use base_db::Crate;
66
use cfg::{CfgExpr, CfgOptions};
77
use either::Either;
88
use hir_expand::{
99
HirFileId, InFile,
1010
attrs::{Attr, AttrId, RawAttrs, collect_attrs},
11+
span_map::SpanMapRef,
1112
};
1213
use intern::{Symbol, sym};
1314
use la_arena::{ArenaMap, Idx, RawIdx};
@@ -45,8 +46,28 @@ impl Attrs {
4546
(**self).iter().find(|attr| attr.id == id)
4647
}
4748

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)
5071
}
5172
}
5273

@@ -522,46 +543,49 @@ impl AttrsWithOwner {
522543
GenericParamId::ConstParamId(it) => {
523544
let src = it.parent().child_source(db);
524545
// 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(
527548
db,
528-
src.with_value(val),
549+
val,
529550
db.span_map(src.file_id).as_ref(),
551+
def.krate(db).cfg_options(db),
530552
),
531553
None => RawAttrs::EMPTY,
532-
}
554+
});
533555
}
534556
GenericParamId::TypeParamId(it) => {
535557
let src = it.parent().child_source(db);
536558
// 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(
539561
db,
540-
src.with_value(val),
562+
val,
541563
db.span_map(src.file_id).as_ref(),
564+
def.krate(db).cfg_options(db),
542565
),
543566
None => RawAttrs::EMPTY,
544-
}
567+
});
545568
}
546569
GenericParamId::LifetimeParamId(it) => {
547570
let src = it.parent.child_source(db);
548571
// 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(
551574
db,
552-
src.with_value(val),
575+
val,
553576
db.span_map(src.file_id).as_ref(),
577+
def.krate(db).cfg_options(db),
554578
),
555579
None => RawAttrs::EMPTY,
556-
}
580+
});
557581
}
558582
},
559583
AttrDefId::ExternBlockId(it) => attrs_from_item_tree_loc(db, it),
560584
AttrDefId::ExternCrateId(it) => attrs_from_item_tree_loc(db, it),
561585
AttrDefId::UseId(it) => attrs_from_item_tree_loc(db, it),
562586
};
563587

564-
let attrs = raw_attrs.filter(db, def.krate(db));
588+
let attrs = raw_attrs.expand_cfg_attr(db, def.krate(db));
565589
Attrs(attrs)
566590
}
567591

crates/hir-def/src/expr_store/expander.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
use std::mem;
44

55
use base_db::Crate;
6+
use cfg::CfgOptions;
67
use drop_bomb::DropBomb;
78
use hir_expand::{
89
ExpandError, ExpandErrorKind, ExpandResult, HirFileId, InFile, Lookup, MacroCallId,
9-
attrs::RawAttrs, eager::EagerCallBackFn, mod_path::ModPath, span_map::SpanMap,
10+
eager::EagerCallBackFn, mod_path::ModPath, span_map::SpanMap,
1011
};
1112
use span::{AstIdMap, Edition, SyntaxContext};
1213
use syntax::ast::HasAttrs;
@@ -64,22 +65,13 @@ impl Expander {
6465
}
6566
}
6667

67-
pub(super) fn attrs(
68-
&self,
69-
db: &dyn DefDatabase,
70-
krate: Crate,
71-
has_attrs: &dyn HasAttrs,
72-
) -> Attrs {
73-
Attrs::filter(db, krate, RawAttrs::new(db, has_attrs, self.span_map.as_ref()))
74-
}
75-
7668
pub(super) fn is_cfg_enabled(
7769
&self,
7870
db: &dyn DefDatabase,
79-
krate: Crate,
8071
has_attrs: &dyn HasAttrs,
81-
) -> bool {
82-
self.attrs(db, krate, has_attrs).is_cfg_enabled(krate.cfg_options(db))
72+
cfg_options: &CfgOptions,
73+
) -> Result<(), cfg::CfgExpr> {
74+
Attrs::is_cfg_enabled_for(db, has_attrs, self.span_map.as_ref(), cfg_options)
8375
}
8476

8577
pub(super) fn call_syntax_ctx(&self) -> SyntaxContext {

0 commit comments

Comments
 (0)