Skip to content

Commit aeb5d64

Browse files
committed
Implement ToDef for ast::Attr
1 parent 6b7b09d commit aeb5d64

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

crates/hir/src/semantics.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,12 @@ impl<'db> SemanticsImpl<'db> {
476476
}
477477

478478
fn derive_macro_calls(&self, attr: &ast::Attr) -> Option<Vec<Option<MacroCallId>>> {
479-
let item = attr.syntax().parent().and_then(ast::Item::cast)?;
480-
let file_id = self.find_file(item.syntax()).file_id;
481-
let item = InFile::new(file_id, &item);
479+
let adt = attr.syntax().parent().and_then(ast::Adt::cast)?;
480+
let file_id = self.find_file(adt.syntax()).file_id;
481+
let adt = InFile::new(file_id, &adt);
482482
let src = InFile::new(file_id, attr.clone());
483483
self.with_ctx(|ctx| {
484-
let res = ctx.attr_to_derive_macro_call(item, src)?;
484+
let res = ctx.attr_to_derive_macro_call(adt, src)?;
485485
Some(res.to_vec())
486486
})
487487
}
@@ -909,17 +909,8 @@ impl<'db> SemanticsImpl<'db> {
909909
return None;
910910
}
911911

912-
// Fetch hir::Attr definition
913-
// FIXME: Move this to ToDef impl?
914-
let adt = attr.syntax().parent().and_then(ast::Adt::cast)?;
915-
let attr_pos = adt.attrs().position(|it| it == attr)?;
916-
let attrs = {
917-
let file_id = self.find_file(adt.syntax()).file_id;
918-
let adt = InFile::new(file_id, adt);
919-
let def = self.with_ctx(|ctx| ctx.adt_to_def(adt))?;
920-
self.db.attrs(def.into())
921-
};
922-
let attr_def = attrs.get(attr_pos)?;
912+
let attr_def =
913+
ast::Attr::to_def(self, self.find_file(attr.syntax()).with_value(attr.clone()))?;
923914

924915
let mut derive_paths = attr_def.parse_path_comma_token_tree()?;
925916
let derives = self.resolve_derive_macro(&attr)?;
@@ -1214,6 +1205,7 @@ to_def_impls![
12141205
(crate::Local, ast::SelfParam, self_param_to_def),
12151206
(crate::Label, ast::Label, label_to_def),
12161207
(crate::Adt, ast::Adt, adt_to_def),
1208+
(crate::Attr, ast::Attr, attr_to_def),
12171209
];
12181210

12191211
fn find_root(node: &SyntaxNode) -> SyntaxNode {

crates/hir/src/semantics/source_to_def.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,19 @@ impl SourceToDefCtx<'_, '_> {
210210
ast::Adt::Union(it) => self.union_to_def(InFile::new(file_id, it)).map(AdtId::UnionId),
211211
}
212212
}
213+
pub(super) fn attr_to_def(
214+
&mut self,
215+
InFile { file_id, value }: InFile<ast::Attr>,
216+
) -> Option<crate::Attr> {
217+
// FIXME: Use dynmap?
218+
let adt = value.syntax().parent().and_then(ast::Adt::cast)?;
219+
let attr_pos = ast::HasAttrs::attrs(&adt).position(|it| it == value)?;
220+
let attrs = {
221+
let def = self.adt_to_def(InFile::new(file_id, adt))?;
222+
self.db.attrs(def.into())
223+
};
224+
attrs.get(attr_pos).cloned()
225+
}
213226
pub(super) fn bind_pat_to_def(
214227
&mut self,
215228
src: InFile<ast::IdentPat>,
@@ -246,7 +259,7 @@ impl SourceToDefCtx<'_, '_> {
246259

247260
pub(super) fn attr_to_derive_macro_call(
248261
&mut self,
249-
item: InFile<&ast::Item>,
262+
item: InFile<&ast::Adt>,
250263
src: InFile<ast::Attr>,
251264
) -> Option<&[Option<MacroCallId>]> {
252265
let map = self.dyn_map(item)?;

0 commit comments

Comments
 (0)