]> git.lizzy.rs Git - rust.git/commitdiff
Implement ToDef for ast::Attr
authorLukas Wirth <lukastw97@gmail.com>
Sun, 2 Jan 2022 15:58:21 +0000 (16:58 +0100)
committerLukas Wirth <lukastw97@gmail.com>
Sun, 2 Jan 2022 22:44:26 +0000 (23:44 +0100)
crates/hir/src/semantics.rs
crates/hir/src/semantics/source_to_def.rs

index 077ab40d08151cb1d136b0f9b38887504771debb..9be1619228cdf41d8faf70c8af79273ce08b9b8e 100644 (file)
@@ -476,12 +476,12 @@ fn expand_derive_macro(&self, attr: &ast::Attr) -> Option<Vec<SyntaxNode>> {
     }
 
     fn derive_macro_calls(&self, attr: &ast::Attr) -> Option<Vec<Option<MacroCallId>>> {
-        let item = attr.syntax().parent().and_then(ast::Item::cast)?;
-        let file_id = self.find_file(item.syntax()).file_id;
-        let item = InFile::new(file_id, &item);
+        let adt = attr.syntax().parent().and_then(ast::Adt::cast)?;
+        let file_id = self.find_file(adt.syntax()).file_id;
+        let adt = InFile::new(file_id, &adt);
         let src = InFile::new(file_id, attr.clone());
         self.with_ctx(|ctx| {
-            let res = ctx.attr_to_derive_macro_call(item, src)?;
+            let res = ctx.attr_to_derive_macro_call(adt, src)?;
             Some(res.to_vec())
         })
     }
@@ -909,17 +909,8 @@ fn resolve_derive_ident(&self, ident: &ast::Ident) -> Option<PathResolution> {
             return None;
         }
 
-        // Fetch hir::Attr definition
-        // FIXME: Move this to ToDef impl?
-        let adt = attr.syntax().parent().and_then(ast::Adt::cast)?;
-        let attr_pos = adt.attrs().position(|it| it == attr)?;
-        let attrs = {
-            let file_id = self.find_file(adt.syntax()).file_id;
-            let adt = InFile::new(file_id, adt);
-            let def = self.with_ctx(|ctx| ctx.adt_to_def(adt))?;
-            self.db.attrs(def.into())
-        };
-        let attr_def = attrs.get(attr_pos)?;
+        let attr_def =
+            ast::Attr::to_def(self, self.find_file(attr.syntax()).with_value(attr.clone()))?;
 
         let mut derive_paths = attr_def.parse_path_comma_token_tree()?;
         let derives = self.resolve_derive_macro(&attr)?;
@@ -1214,6 +1205,7 @@ fn to_def(sema: &SemanticsImpl, src: InFile<Self>) -> Option<Self::Def> {
     (crate::Local, ast::SelfParam, self_param_to_def),
     (crate::Label, ast::Label, label_to_def),
     (crate::Adt, ast::Adt, adt_to_def),
+    (crate::Attr, ast::Attr, attr_to_def),
 ];
 
 fn find_root(node: &SyntaxNode) -> SyntaxNode {
index 495c84e65f43e0a73a9a5d48f6beff778084523a..fbce53eb166cdd31288ced36dd5767fe0ee47a52 100644 (file)
@@ -210,6 +210,19 @@ pub(super) fn adt_to_def(
             ast::Adt::Union(it) => self.union_to_def(InFile::new(file_id, it)).map(AdtId::UnionId),
         }
     }
+    pub(super) fn attr_to_def(
+        &mut self,
+        InFile { file_id, value }: InFile<ast::Attr>,
+    ) -> Option<crate::Attr> {
+        // FIXME: Use dynmap?
+        let adt = value.syntax().parent().and_then(ast::Adt::cast)?;
+        let attr_pos = ast::HasAttrs::attrs(&adt).position(|it| it == value)?;
+        let attrs = {
+            let def = self.adt_to_def(InFile::new(file_id, adt))?;
+            self.db.attrs(def.into())
+        };
+        attrs.get(attr_pos).cloned()
+    }
     pub(super) fn bind_pat_to_def(
         &mut self,
         src: InFile<ast::IdentPat>,
@@ -246,7 +259,7 @@ pub(super) fn item_to_macro_call(&mut self, src: InFile<ast::Item>) -> Option<Ma
 
     pub(super) fn attr_to_derive_macro_call(
         &mut self,
-        item: InFile<&ast::Item>,
+        item: InFile<&ast::Adt>,
         src: InFile<ast::Attr>,
     ) -> Option<&[Option<MacroCallId>]> {
         let map = self.dyn_map(item)?;