Skip to content

Commit 99d40e7

Browse files
Don't stringify cfg_attr-gated attributes
This preserves the assigned `TokenId`s
1 parent 1d6eef1 commit 99d40e7

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

crates/hir_def/src/attr.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ impl RawAttrs {
106106
) -> Self {
107107
let entries = collect_attrs(owner)
108108
.flat_map(|(id, attr)| match attr {
109-
Either::Left(attr) => Attr::from_src(db, attr, hygiene, id),
109+
Either::Left(attr) => {
110+
attr.meta().and_then(|meta| Attr::from_src(db, meta, hygiene, id))
111+
}
110112
Either::Right(comment) => comment.doc_comment().map(|doc| Attr {
111113
id,
112114
input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))),
@@ -172,10 +174,9 @@ impl RawAttrs {
172174
let index = attr.id;
173175
let attrs = parts.filter(|a| !a.is_empty()).filter_map(|attr| {
174176
let tree = Subtree { delimiter: None, token_trees: attr.to_vec() };
175-
let attr = ast::Attr::parse(&format!("#[{}]", tree)).ok()?;
176177
// FIXME hygiene
177178
let hygiene = Hygiene::new_unhygienic();
178-
Attr::from_src(db, attr, &hygiene, index)
179+
Attr::from_tt(db, &tree, &hygiene, index)
179180
});
180181

181182
let cfg_options = &crate_graph[krate].cfg_options;
@@ -664,7 +665,7 @@ impl fmt::Display for AttrInput {
664665
impl Attr {
665666
fn from_src(
666667
db: &dyn DefDatabase,
667-
ast: ast::Attr,
668+
ast: ast::Meta,
668669
hygiene: &Hygiene,
669670
id: AttrId,
670671
) -> Option<Attr> {
@@ -683,6 +684,19 @@ impl Attr {
683684
Some(Attr { id, path, input })
684685
}
685686

687+
fn from_tt(
688+
db: &dyn DefDatabase,
689+
tt: &tt::Subtree,
690+
hygiene: &Hygiene,
691+
id: AttrId,
692+
) -> Option<Attr> {
693+
let (parse, _) =
694+
mbe::token_tree_to_syntax_node(tt, hir_expand::FragmentKind::MetaItem).ok()?;
695+
let ast = ast::Meta::cast(parse.syntax_node())?;
696+
697+
Self::from_src(db, ast, hygiene, id)
698+
}
699+
686700
/// Parses this attribute as a `#[derive]`, returns an iterator that yields all contained paths
687701
/// to derive macros.
688702
///

0 commit comments

Comments
 (0)