diff --git a/crates/hir-def/src/nameres/attr_resolution.rs b/crates/hir-def/src/nameres/attr_resolution.rs index e2d83c8e3bf..eb7f4c05ae2 100644 --- a/crates/hir-def/src/nameres/attr_resolution.rs +++ b/crates/hir-def/src/nameres/attr_resolution.rs @@ -3,7 +3,7 @@ use base_db::CrateId; use hir_expand::{ attrs::{Attr, AttrId, AttrInput}, - AstId, MacroCallId, MacroCallKind, MacroDefId, + MacroCallId, MacroCallKind, MacroDefId, }; use span::SyntaxContextId; use syntax::{ast, SmolStr}; @@ -98,20 +98,7 @@ pub(crate) fn is_builtin_or_registered_attr(&self, path: &ModPath) -> bool { false } } -pub(super) fn derive_attr_macro_as_call_id( - db: &dyn DefDatabase, - item_attr: &AstId, - macro_attr: &Attr, - krate: CrateId, - def: MacroDefId, -) -> MacroCallId { - def.make_call( - db.upcast(), - krate, - MacroCallKind::DeriveAttr { ast_id: *item_attr, invoc_attr_index: macro_attr.id }, - macro_attr.ctxt, - ) -} + pub(super) fn attr_macro_as_call_id( db: &dyn DefDatabase, item_attr: &AstIdWithPath, diff --git a/crates/hir-def/src/nameres/collector.rs b/crates/hir-def/src/nameres/collector.rs index 6de967c2c64..2fae253d0a0 100644 --- a/crates/hir-def/src/nameres/collector.rs +++ b/crates/hir-def/src/nameres/collector.rs @@ -37,9 +37,7 @@ }, macro_call_as_call_id, macro_call_as_call_id_with_eager, nameres::{ - attr_resolution::{ - attr_macro_as_call_id, derive_attr_macro_as_call_id, derive_macro_as_call_id, - }, + attr_resolution::{attr_macro_as_call_id, derive_macro_as_call_id}, diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, @@ -1234,7 +1232,8 @@ enum Resolved { Some(def) if def.is_attribute() => def, _ => return Resolved::No, }; - + let call_id = + attr_macro_as_call_id(self.db, file_ast_id, attr, self.def_map.krate, def); if let MacroDefId { kind: MacroDefKind::BuiltInAttr( @@ -1266,13 +1265,7 @@ enum Resolved { let ast_id = ast_id.with_value(ast_adt_id); // the call_id for the actual derive macro. This is used so all the derive proc macros can share a token tree - let call_id = derive_attr_macro_as_call_id( - self.db, - &ast_id, - attr, - self.def_map.krate, - def, - ); + match attr.parse_path_comma_token_tree(self.db.upcast()) { Some(derive_macros) => { let mut len = 0; @@ -1320,8 +1313,7 @@ enum Resolved { return recollect_without(self); } - let call_id = - attr_macro_as_call_id(self.db, file_ast_id, attr, self.def_map.krate, def); + // Skip #[test]/#[bench] expansion, which would merely result in more memory usage // due to duplicating functions into macro expansions if matches!( diff --git a/crates/hir-expand/src/cfg_process.rs b/crates/hir-expand/src/cfg_process.rs index 7e9ff5c85b5..db3558a84e9 100644 --- a/crates/hir-expand/src/cfg_process.rs +++ b/crates/hir-expand/src/cfg_process.rs @@ -10,7 +10,7 @@ use tracing::{debug, warn}; use tt::SmolStr; -use crate::{db::ExpandDatabase, MacroCallKind, MacroCallLoc}; +use crate::{db::ExpandDatabase, proc_macro::ProcMacroKind, MacroCallLoc, MacroDefKind}; fn check_cfg_attr(attr: &Attr, loc: &MacroCallLoc, db: &dyn ExpandDatabase) -> Option { if !attr.simple_name().as_deref().map(|v| v == "cfg")? { @@ -180,7 +180,13 @@ pub(crate) fn process_cfg_attrs( db: &dyn ExpandDatabase, ) -> Option> { // FIXME: #[cfg_eval] is not implemented. But it is not stable yet - if !matches!(loc.kind, MacroCallKind::Derive { .. } | MacroCallKind::DeriveAttr { .. }) { + let is_derive = match loc.def.kind { + MacroDefKind::BuiltInDerive(..) + | MacroDefKind::ProcMacro(_, ProcMacroKind::CustomDerive, _) => true, + MacroDefKind::BuiltInAttr(expander, _) => expander.is_derive(), + _ => false, + }; + if !is_derive { return None; } let mut remove = FxHashSet::default(); diff --git a/crates/hir-expand/src/db.rs b/crates/hir-expand/src/db.rs index ae2be4b7145..6c22f79c81a 100644 --- a/crates/hir-expand/src/db.rs +++ b/crates/hir-expand/src/db.rs @@ -164,8 +164,7 @@ pub fn expand_speculative( SyntaxFixupUndoInfo::NONE, ), MacroCallKind::Derive { derive_attr_index: index, .. } - | MacroCallKind::Attr { invoc_attr_index: index, .. } - | MacroCallKind::DeriveAttr { invoc_attr_index: index, .. } => { + | MacroCallKind::Attr { invoc_attr_index: index, .. } => { let censor = if let MacroCallKind::Derive { .. } = loc.kind { censor_derive_input(index, &ast::Adt::cast(speculative_args.clone())?) } else { @@ -436,9 +435,9 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> MacroArgResult { } return (Arc::new(tt), SyntaxFixupUndoInfo::NONE, span); } + // MacroCallKind::Derive should not be here. As we are getting the argument for the derive macro - MacroCallKind::Derive { ast_id, derive_attr_index, .. } - | MacroCallKind::DeriveAttr { ast_id, invoc_attr_index: derive_attr_index } => { + MacroCallKind::Derive { ast_id, derive_attr_index, .. } => { let node = ast_id.to_ptr(db).to_node(&root); let censor_derive_input = censor_derive_input(derive_attr_index, &node); let item_node = node.into(); @@ -454,13 +453,23 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> MacroArgResult { MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => { let node = ast_id.to_ptr(db).to_node(&root); let attr_source = attr_source(invoc_attr_index, &node); + let span = map.span_for_range( attr_source .as_ref() .and_then(|it| it.path()) .map_or_else(|| node.syntax().text_range(), |it| it.syntax().text_range()), ); - (attr_source.into_iter().map(|it| it.syntax().clone().into()).collect(), node, span) + // If derive attribute we need to censor the derive input + if matches!(loc.def.kind, MacroDefKind::BuiltInAttr(expander, ..) if expander.is_derive()) + && ast::Adt::can_cast(node.syntax().kind()) + { + let adt = ast::Adt::cast(node.syntax().clone()).unwrap(); + let censor_derive_input = censor_derive_input(invoc_attr_index, &adt); + (censor_derive_input, node, span) + } else { + (attr_source.into_iter().map(|it| it.syntax().clone().into()).collect(), node, span) + } } }; diff --git a/crates/hir-expand/src/lib.rs b/crates/hir-expand/src/lib.rs index 870c3dc0aac..e7a313c68cd 100644 --- a/crates/hir-expand/src/lib.rs +++ b/crates/hir-expand/src/lib.rs @@ -228,10 +228,6 @@ 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, - invoc_attr_index: AttrId, - }, Attr { ast_id: AstId, // FIXME: This shouldn't be here, we can derive this from `invoc_attr_index` @@ -519,8 +515,7 @@ pub fn to_node(&self, db: &dyn ExpandDatabase) -> InFile { MacroCallKind::FnLike { ast_id, .. } => { ast_id.with_value(ast_id.to_node(db).syntax().clone()) } - MacroCallKind::Derive { ast_id, derive_attr_index, .. } - | MacroCallKind::DeriveAttr { ast_id, invoc_attr_index: derive_attr_index } => { + MacroCallKind::Derive { ast_id, derive_attr_index, .. } => { // FIXME: handle `cfg_attr` ast_id.with_value(ast_id.to_node(db)).map(|it| { collect_attrs(&it) @@ -554,7 +549,7 @@ pub fn to_node(&self, db: &dyn ExpandDatabase) -> InFile { fn expand_to(&self) -> ExpandTo { match self.kind { MacroCallKind::FnLike { expand_to, .. } => expand_to, - MacroCallKind::Derive { .. } | MacroCallKind::DeriveAttr { .. } => ExpandTo::Items, + MacroCallKind::Derive { .. } => ExpandTo::Items, MacroCallKind::Attr { .. } if self.def.is_attribute_derive() => ExpandTo::Items, MacroCallKind::Attr { .. } => { // FIXME(stmt_expr_attributes) @@ -588,7 +583,6 @@ fn descr(&self) -> &'static str { MacroCallKind::FnLike { .. } => "macro call", MacroCallKind::Derive { .. } => "derive macro", MacroCallKind::Attr { .. } => "attribute macro", - MacroCallKind::DeriveAttr { .. } => "derive attribute", } } @@ -597,7 +591,6 @@ pub fn file_id(&self) -> HirFileId { 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, } } @@ -605,8 +598,7 @@ pub fn file_id(&self) -> HirFileId { pub fn erased_ast_id(&self) -> ErasedFileAstId { match *self { MacroCallKind::FnLike { ast_id: InFile { value, .. }, .. } => value.erase(), - MacroCallKind::Derive { ast_id: InFile { value, .. }, .. } - | MacroCallKind::DeriveAttr { ast_id: InFile { value, .. }, .. } => value.erase(), + MacroCallKind::Derive { ast_id: InFile { value, .. }, .. } => value.erase(), MacroCallKind::Attr { ast_id: InFile { value, .. }, .. } => value.erase(), } } @@ -627,9 +619,7 @@ pub fn original_call_range_with_body(self, db: &dyn ExpandDatabase) -> FileRange let range = match kind { MacroCallKind::FnLike { 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::Derive { ast_id, .. } => ast_id.to_ptr(db).text_range(), MacroCallKind::Attr { ast_id, .. } => ast_id.to_ptr(db).text_range(), }; @@ -675,15 +665,6 @@ pub fn original_call_range(self, db: &dyn ExpandDatabase) -> FileRange { .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 } @@ -694,7 +675,7 @@ fn arg(&self, db: &dyn ExpandDatabase) -> InFile> { MacroCallKind::FnLike { ast_id, .. } => { ast_id.to_in_file_node(db).map(|it| Some(it.token_tree()?.syntax().clone())) } - MacroCallKind::Derive { ast_id, .. } | MacroCallKind::DeriveAttr { ast_id, .. } => { + MacroCallKind::Derive { ast_id, .. } => { ast_id.to_in_file_node(db).syntax().cloned().map(Some) } MacroCallKind::Attr { ast_id, .. } => { diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index acd53e19e8b..5be54feb466 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1022,26 +1022,6 @@ fn precise_macro_call_location( MacroKind::Attr, ) } - MacroCallKind::DeriveAttr { ast_id, invoc_attr_index } => { - let node = ast_id.to_node(db.upcast()); - let attr = collect_attrs(&node) - .nth(invoc_attr_index.ast_index()) - .and_then(|x| Either::left(x.1)) - .unwrap_or_else(|| { - panic!("cannot find attribute #{}", invoc_attr_index.ast_index()) - }); - - ( - ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&attr))), - Some(attr.syntax().text_range()), - attr.path() - .and_then(|path| path.segment()) - .and_then(|seg| seg.name_ref()) - .as_ref() - .map(ToString::to_string), - MacroKind::Attr, - ) - } } }