aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-06-11 17:20:15 +0100
committerJonas Schievink <[email protected]>2021-06-11 17:34:30 +0100
commit99d40e7a3a22604690753322f9274bc91fa03de4 (patch)
tree9538be2b7dc921afff9c36ed96ee5ccb6b8e47e7
parent1d6eef1350ee0793fcd2a0eb191cdb127b76a49d (diff)
Don't stringify `cfg_attr`-gated attributes
This preserves the assigned `TokenId`s
-rw-r--r--crates/hir_def/src/attr.rs22
1 files changed, 18 insertions, 4 deletions
diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs
index 3886b6c04..d9f9fadc1 100644
--- a/crates/hir_def/src/attr.rs
+++ b/crates/hir_def/src/attr.rs
@@ -106,7 +106,9 @@ impl RawAttrs {
106 ) -> Self { 106 ) -> Self {
107 let entries = collect_attrs(owner) 107 let entries = collect_attrs(owner)
108 .flat_map(|(id, attr)| match attr { 108 .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 }
110 Either::Right(comment) => comment.doc_comment().map(|doc| Attr { 112 Either::Right(comment) => comment.doc_comment().map(|doc| Attr {
111 id, 113 id,
112 input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))), 114 input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))),
@@ -172,10 +174,9 @@ impl RawAttrs {
172 let index = attr.id; 174 let index = attr.id;
173 let attrs = parts.filter(|a| !a.is_empty()).filter_map(|attr| { 175 let attrs = parts.filter(|a| !a.is_empty()).filter_map(|attr| {
174 let tree = Subtree { delimiter: None, token_trees: attr.to_vec() }; 176 let tree = Subtree { delimiter: None, token_trees: attr.to_vec() };
175 let attr = ast::Attr::parse(&format!("#[{}]", tree)).ok()?;
176 // FIXME hygiene 177 // FIXME hygiene
177 let hygiene = Hygiene::new_unhygienic(); 178 let hygiene = Hygiene::new_unhygienic();
178 Attr::from_src(db, attr, &hygiene, index) 179 Attr::from_tt(db, &tree, &hygiene, index)
179 }); 180 });
180 181
181 let cfg_options = &crate_graph[krate].cfg_options; 182 let cfg_options = &crate_graph[krate].cfg_options;
@@ -664,7 +665,7 @@ impl fmt::Display for AttrInput {
664impl Attr { 665impl Attr {
665 fn from_src( 666 fn from_src(
666 db: &dyn DefDatabase, 667 db: &dyn DefDatabase,
667 ast: ast::Attr, 668 ast: ast::Meta,
668 hygiene: &Hygiene, 669 hygiene: &Hygiene,
669 id: AttrId, 670 id: AttrId,
670 ) -> Option<Attr> { 671 ) -> Option<Attr> {
@@ -683,6 +684,19 @@ impl Attr {
683 Some(Attr { id, path, input }) 684 Some(Attr { id, path, input })
684 } 685 }
685 686
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
686 /// Parses this attribute as a `#[derive]`, returns an iterator that yields all contained paths 700 /// Parses this attribute as a `#[derive]`, returns an iterator that yields all contained paths
687 /// to derive macros. 701 /// to derive macros.
688 /// 702 ///