Treat Derive Macro specially.

This commit is contained in:
Wyatt Herkamp 2024-03-18 07:10:02 -04:00
parent 4bd2f948bb
commit 2c2bbe07fd
6 changed files with 87 additions and 40 deletions

View file

@ -228,6 +228,10 @@ pub enum MacroCallKind {
/// We will resolve the same token tree for all derive macros in the same derive attribute.
derive_macro_id: MacroCallId,
},
DeriveAttr {
ast_id: AstId<ast::Adt>,
invoc_attr_index: AttrId,
},
Attr {
ast_id: AstId<ast::Item>,
// FIXME: This shouldn't be here, we can derive this from `invoc_attr_index`
@ -515,7 +519,8 @@ impl MacroCallLoc {
MacroCallKind::FnLike { ast_id, .. } => {
ast_id.with_value(ast_id.to_node(db).syntax().clone())
}
MacroCallKind::Derive { ast_id, derive_attr_index, .. } => {
MacroCallKind::Derive { ast_id, derive_attr_index, .. }
| MacroCallKind::DeriveAttr { ast_id, invoc_attr_index: derive_attr_index } => {
// FIXME: handle `cfg_attr`
ast_id.with_value(ast_id.to_node(db)).map(|it| {
collect_attrs(&it)
@ -549,7 +554,7 @@ impl MacroCallLoc {
fn expand_to(&self) -> ExpandTo {
match self.kind {
MacroCallKind::FnLike { expand_to, .. } => expand_to,
MacroCallKind::Derive { .. } => ExpandTo::Items,
MacroCallKind::Derive { .. } | MacroCallKind::DeriveAttr { .. } => ExpandTo::Items,
MacroCallKind::Attr { .. } if self.def.is_attribute_derive() => ExpandTo::Items,
MacroCallKind::Attr { .. } => {
// FIXME(stmt_expr_attributes)
@ -583,6 +588,7 @@ impl MacroCallKind {
MacroCallKind::FnLike { .. } => "macro call",
MacroCallKind::Derive { .. } => "derive macro",
MacroCallKind::Attr { .. } => "attribute macro",
MacroCallKind::DeriveAttr { .. } => "derive attribute",
}
}
@ -591,6 +597,7 @@ impl MacroCallKind {
match *self {
MacroCallKind::FnLike { ast_id: InFile { file_id, .. }, .. }
| MacroCallKind::Derive { ast_id: InFile { file_id, .. }, .. }
| MacroCallKind::DeriveAttr { ast_id: InFile { file_id, .. }, .. }
| MacroCallKind::Attr { ast_id: InFile { file_id, .. }, .. } => file_id,
}
}
@ -598,7 +605,8 @@ impl MacroCallKind {
pub fn erased_ast_id(&self) -> ErasedFileAstId {
match *self {
MacroCallKind::FnLike { ast_id: InFile { value, .. }, .. } => value.erase(),
MacroCallKind::Derive { ast_id: InFile { value, .. }, .. } => value.erase(),
MacroCallKind::Derive { ast_id: InFile { value, .. }, .. }
| MacroCallKind::DeriveAttr { ast_id: InFile { value, .. }, .. } => value.erase(),
MacroCallKind::Attr { ast_id: InFile { value, .. }, .. } => value.erase(),
}
}
@ -619,7 +627,9 @@ impl MacroCallKind {
let range = match kind {
MacroCallKind::FnLike { ast_id, .. } => ast_id.to_ptr(db).text_range(),
MacroCallKind::Derive { ast_id, .. } => ast_id.to_ptr(db).text_range(),
MacroCallKind::Derive { ast_id, .. } | MacroCallKind::DeriveAttr { ast_id, .. } => {
ast_id.to_ptr(db).text_range()
}
MacroCallKind::Attr { ast_id, .. } => ast_id.to_ptr(db).text_range(),
};
@ -665,6 +675,15 @@ impl MacroCallKind {
.syntax()
.text_range()
}
MacroCallKind::DeriveAttr { ast_id, invoc_attr_index } => {
collect_attrs(&ast_id.to_node(db))
.nth(invoc_attr_index.ast_index())
.expect("missing attribute")
.1
.expect_left("attribute macro is a doc comment?")
.syntax()
.text_range()
}
};
FileRange { range, file_id }
@ -675,7 +694,7 @@ impl MacroCallKind {
MacroCallKind::FnLike { ast_id, .. } => {
ast_id.to_in_file_node(db).map(|it| Some(it.token_tree()?.syntax().clone()))
}
MacroCallKind::Derive { ast_id, .. } => {
MacroCallKind::Derive { ast_id, .. } | MacroCallKind::DeriveAttr { ast_id, .. } => {
ast_id.to_in_file_node(db).syntax().cloned().map(Some)
}
MacroCallKind::Attr { ast_id, .. } => {