X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=crates%2Fhir_expand%2Fsrc%2Flib.rs;h=ba0f10151246519037a551231995e38bd905fb42;hb=0b53744f2d7e0694cd7207cca632fd6de1dc5bff;hp=2c6378cd3eee669d74787a9877aa0d75fcb88e3a;hpb=be3168dabe0b5ee3d7fed4a5e7133e5f2d4f3a4b;p=rust.git diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index 2c6378cd3ee..ba0f1015124 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs @@ -166,6 +166,7 @@ pub enum MacroCallKind { /// Outer attributes are counted first, then inner attributes. This does not support /// out-of-line modules, which may have attributes spread across 2 files! invoc_attr_index: u32, + /// Whether this attribute is the `#[derive]` attribute. is_derive: bool, }, } @@ -262,16 +263,16 @@ pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option { } /// Indicate it is macro file generated for builtin derive - pub fn is_builtin_derive(&self, db: &dyn db::AstDatabase) -> Option> { + pub fn is_builtin_derive(&self, db: &dyn db::AstDatabase) -> Option> { match self.0 { HirFileIdRepr::FileId(_) => None, HirFileIdRepr::MacroFile(macro_file) => { let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id); - let item = match loc.def.kind { + let attr = match loc.def.kind { MacroDefKind::BuiltInDerive(..) => loc.kind.to_node(db), _ => return None, }; - Some(item.with_value(ast::Item::cast(item.value.clone())?)) + Some(attr.with_value(ast::Attr::cast(attr.value.clone())?)) } } } @@ -309,7 +310,8 @@ pub fn is_attr_macro(&self, db: &dyn db::AstDatabase) -> bool { } /// Return whether this file is the pseudo expansion of the derive attribute. - pub fn is_derive_attr_macro(&self, db: &dyn db::AstDatabase) -> bool { + /// See [`crate::builtin_attr_macro::derive_attr_expand`]. + pub fn is_derive_attr_pseudo_expansion(&self, db: &dyn db::AstDatabase) -> bool { match self.0 { HirFileIdRepr::MacroFile(macro_file) => { let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id); @@ -383,8 +385,29 @@ pub fn to_node(&self, db: &dyn db::AstDatabase) -> InFile { MacroCallKind::FnLike { ast_id, .. } => { ast_id.with_value(ast_id.to_node(db).syntax().clone()) } - MacroCallKind::Derive { ast_id, .. } => { - ast_id.with_value(ast_id.to_node(db).syntax().clone()) + MacroCallKind::Derive { ast_id, derive_attr_index, .. } => { + // FIXME: handle `cfg_attr` + ast_id.with_value(ast_id.to_node(db)).map(|it| { + it.doc_comments_and_attrs() + .nth(*derive_attr_index as usize) + .and_then(|it| match it { + Either::Left(attr) => Some(attr.syntax().clone()), + Either::Right(_) => None, + }) + .unwrap_or_else(|| it.syntax().clone()) + }) + } + MacroCallKind::Attr { ast_id, is_derive: true, invoc_attr_index, .. } => { + // FIXME: handle `cfg_attr` + ast_id.with_value(ast_id.to_node(db)).map(|it| { + it.doc_comments_and_attrs() + .nth(*invoc_attr_index as usize) + .and_then(|it| match it { + Either::Left(attr) => Some(attr.syntax().clone()), + Either::Right(_) => None, + }) + .unwrap_or_else(|| it.syntax().clone()) + }) } MacroCallKind::Attr { ast_id, .. } => { ast_id.with_value(ast_id.to_node(db).syntax().clone())