From e5a2c6596ddd11b0d57042224ac7c1d7691ec33b Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 31 May 2021 13:37:11 +0200 Subject: Expand procedural attribute macros --- crates/hir/src/lib.rs | 16 +++++++++- crates/hir_def/src/lib.rs | 40 +++++++++++++++++++++++ crates/hir_def/src/nameres/collector.rs | 56 +++++++++++++++++++++++++++++---- crates/hir_expand/src/db.rs | 11 +++++-- crates/hir_expand/src/input.rs | 19 +++++++++++ crates/hir_expand/src/lib.rs | 24 ++++++++++++-- crates/hir_expand/src/proc_macro.rs | 7 ++++- 7 files changed, 159 insertions(+), 14 deletions(-) (limited to 'crates') diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index d3ef29db4..b43d61d0e 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -534,6 +534,18 @@ impl Module { Some(derive_name.clone()), ) } + MacroCallKind::Attr { ast_id, invoc_attr_index, attr_name, .. } => { + let node = ast_id.to_node(db.upcast()); + let attr = + node.attrs().nth((*invoc_attr_index) as usize).unwrap_or_else( + || panic!("cannot find attribute #{}", invoc_attr_index), + ); + ( + ast_id.file_id, + SyntaxNodePtr::from(AstPtr::new(&attr)), + Some(attr_name.clone()), + ) + } }; sink.push(UnresolvedProcMacro { file, @@ -558,7 +570,9 @@ impl Module { let node = ast_id.to_node(db.upcast()); (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) } - MacroCallKind::Derive { ast_id, .. } => { + MacroCallKind::Derive { ast_id, .. } + | MacroCallKind::Attr { ast_id, .. } => { + // FIXME: point to the attribute instead, this creates very large diagnostics let node = ast_id.to_node(db.upcast()); (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) } diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index 9aa95720a..987485acc 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs @@ -55,6 +55,7 @@ use std::{ sync::Arc, }; +use attr::Attr; use base_db::{impl_intern_key, salsa, CrateId}; use hir_expand::{ ast_id_map::FileAstId, @@ -768,3 +769,42 @@ fn derive_macro_as_call_id( .into(); Ok(res) } + +fn attr_macro_as_call_id( + item_attr: &AstIdWithPath, + macro_attr: &Attr, + db: &dyn db::DefDatabase, + krate: CrateId, + resolver: impl Fn(path::ModPath) -> Option, +) -> Result { + let def: MacroDefId = resolver(item_attr.path.clone()) + .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?; + let last_segment = item_attr + .path + .segments() + .last() + .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?; + let mut arg = match ¯o_attr.input { + Some(input) => match &**input { + attr::AttrInput::Literal(_) => tt::Subtree::default(), + attr::AttrInput::TokenTree(tt) => tt.clone(), + }, + None => tt::Subtree::default(), + }; + // The parentheses are always disposed here. + arg.delimiter = None; + + let res = def + .as_lazy_macro( + db.upcast(), + krate, + MacroCallKind::Attr { + ast_id: item_attr.ast_id, + attr_name: last_segment.to_string(), + attr_args: arg, + invoc_attr_index: macro_attr.id.ast_index, + }, + ) + .into(); + Ok(res) +} diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 6b41921ae..874a4ebb1 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -23,7 +23,7 @@ use syntax::ast; use crate::{ attr::{Attr, AttrId, AttrInput, Attrs}, - builtin_attr, + attr_macro_as_call_id, builtin_attr, db::DefDatabase, derive_macro_as_call_id, intern::Interned, @@ -223,7 +223,7 @@ struct MacroDirective { enum MacroDirectiveKind { FnLike { ast_id: AstIdWithPath, fragment: FragmentKind }, Derive { ast_id: AstIdWithPath, derive_attr: AttrId }, - Attr { ast_id: AstIdWithPath, attr: AttrId, mod_item: ModItem }, + Attr { ast_id: AstIdWithPath, attr: Attr, mod_item: ModItem }, } struct DefData<'a> { @@ -419,7 +419,7 @@ impl DefCollector<'_> { let mut unresolved_macros = std::mem::replace(&mut self.unresolved_macros, Vec::new()); let pos = unresolved_macros.iter().position(|directive| { if let MacroDirectiveKind::Attr { ast_id, mod_item, attr } = &directive.kind { - self.skip_attrs.insert(ast_id.ast_id.with_value(*mod_item), *attr); + self.skip_attrs.insert(ast_id.ast_id.with_value(*mod_item), attr.id); let file_id = ast_id.ast_id.file_id; let item_tree = self.db.file_item_tree(file_id); @@ -1050,7 +1050,7 @@ impl DefCollector<'_> { let file_id = ast_id.ast_id.file_id; let item_tree = self.db.file_item_tree(file_id); let mod_dir = self.mod_dirs[&directive.module_id].clone(); - self.skip_attrs.insert(InFile::new(file_id, *mod_item), *attr); + self.skip_attrs.insert(InFile::new(file_id, *mod_item), attr.id); ModCollector { def_collector: &mut *self, macro_depth: directive.depth, @@ -1068,7 +1068,51 @@ impl DefCollector<'_> { } // Not resolved to a derive helper, so try to resolve as a macro. - // FIXME: not yet :) + match attr_macro_as_call_id( + ast_id, + attr, + self.db, + self.def_map.krate, + &resolver, + ) { + Ok(call_id) => { + let loc: MacroCallLoc = self.db.lookup_intern_macro(call_id); + if let MacroDefKind::ProcMacro(exp, ..) = &loc.def.kind { + if exp.is_dummy() { + // Proc macros that cannot be expanded are treated as not + // resolved, in order to fall back later. + self.def_map.diagnostics.push( + DefDiagnostic::unresolved_proc_macro( + directive.module_id, + loc.kind, + ), + ); + + let file_id = ast_id.ast_id.file_id; + let item_tree = self.db.file_item_tree(file_id); + let mod_dir = self.mod_dirs[&directive.module_id].clone(); + self.skip_attrs + .insert(InFile::new(file_id, *mod_item), attr.id); + ModCollector { + def_collector: &mut *self, + macro_depth: directive.depth, + module_id: directive.module_id, + file_id, + item_tree: &item_tree, + mod_dir, + } + .collect(&[*mod_item]); + + // Remove the macro directive. + return false; + } + } + resolved.push((directive.module_id, call_id, directive.depth)); + res = ReachedFixedPoint::No; + return false; + } + Err(UnresolvedMacro { .. }) => (), + } } } @@ -1628,7 +1672,7 @@ impl ModCollector<'_, '_> { self.def_collector.unresolved_macros.push(MacroDirective { module_id: self.module_id, depth: self.macro_depth + 1, - kind: MacroDirectiveKind::Attr { ast_id, attr: attr.id, mod_item }, + kind: MacroDirectiveKind::Attr { ast_id, attr: attr.clone(), mod_item }, }); return Err(()); diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs index e8f4af309..3ebe194e4 100644 --- a/crates/hir_expand/src/db.rs +++ b/crates/hir_expand/src/db.rs @@ -13,8 +13,8 @@ use syntax::{ use crate::{ ast_id_map::AstIdMap, hygiene::HygieneFrame, input::process_macro_input, BuiltinDeriveExpander, - BuiltinFnLikeExpander, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc, MacroDefId, - MacroDefKind, MacroFile, ProcMacroExpander, + BuiltinFnLikeExpander, HirFileId, HirFileIdRepr, MacroCallId, MacroCallKind, MacroCallLoc, + MacroDefId, MacroDefKind, MacroFile, ProcMacroExpander, }; /// Total limit on the number of tokens produced by any macro invocation. @@ -377,7 +377,12 @@ fn expand_proc_macro( _ => unreachable!(), }; - expander.expand(db, loc.krate, ¯o_arg.0) + let attr_arg = match &loc.kind { + MacroCallKind::Attr { attr_args, .. } => Some(attr_args), + _ => None, + }; + + expander.expand(db, loc.krate, ¯o_arg.0, attr_arg) } fn is_self_replicating(from: &SyntaxNode, to: &SyntaxNode) -> bool { diff --git a/crates/hir_expand/src/input.rs b/crates/hir_expand/src/input.rs index fe4790e7b..40116a479 100644 --- a/crates/hir_expand/src/input.rs +++ b/crates/hir_expand/src/input.rs @@ -28,6 +28,14 @@ pub(crate) fn process_macro_input( remove_derives_up_to(item, derive_attr_index as usize).syntax().clone() } + MacroCallKind::Attr { invoc_attr_index, .. } => { + let item = match ast::Item::cast(node.clone()) { + Some(item) => item, + None => return node, + }; + + remove_attr_invoc(item, invoc_attr_index as usize).syntax().clone() + } } } @@ -46,6 +54,17 @@ fn remove_derives_up_to(item: ast::Item, attr_index: usize) -> ast::Item { item } +/// Removes the attribute invoking an attribute macro from `item`. +fn remove_attr_invoc(item: ast::Item, attr_index: usize) -> ast::Item { + let item = item.clone_for_update(); + let attr = item + .attrs() + .nth(attr_index) + .unwrap_or_else(|| panic!("cannot find attribute #{}", attr_index)); + attr.syntax().detach(); + item +} + #[cfg(test)] mod tests { use base_db::fixture::WithFixture; diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index 90d8ae240..618f26b95 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs @@ -258,14 +258,29 @@ pub enum MacroCallKind { /// out-of-line modules, which may have attributes spread across 2 files! derive_attr_index: u32, }, + Attr { + ast_id: AstId, + attr_name: String, + attr_args: tt::Subtree, + /// Syntactical index of the invoking `#[attribute]`. + /// + /// 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, + }, } +// FIXME: attribute indices do not account for `cfg_attr`, which means that we'll strip the whole +// `cfg_attr` instead of just one of the attributes it expands to + impl MacroCallKind { /// Returns the file containing the macro invocation. fn file_id(&self) -> HirFileId { match self { MacroCallKind::FnLike { ast_id, .. } => ast_id.file_id, - MacroCallKind::Derive { ast_id, .. } => ast_id.file_id, + MacroCallKind::Derive { ast_id, .. } | MacroCallKind::Attr { ast_id, .. } => { + ast_id.file_id + } } } @@ -274,7 +289,7 @@ impl MacroCallKind { MacroCallKind::FnLike { ast_id, .. } => { ast_id.with_value(ast_id.to_node(db).syntax().clone()) } - MacroCallKind::Derive { ast_id, .. } => { + MacroCallKind::Derive { ast_id, .. } | MacroCallKind::Attr { ast_id, .. } => { ast_id.with_value(ast_id.to_node(db).syntax().clone()) } } @@ -285,7 +300,9 @@ impl MacroCallKind { MacroCallKind::FnLike { ast_id, .. } => { Some(ast_id.to_node(db).token_tree()?.syntax().clone()) } - MacroCallKind::Derive { ast_id, .. } => Some(ast_id.to_node(db).syntax().clone()), + MacroCallKind::Derive { ast_id, .. } | MacroCallKind::Attr { ast_id, .. } => { + Some(ast_id.to_node(db).syntax().clone()) + } } } @@ -293,6 +310,7 @@ impl MacroCallKind { match self { MacroCallKind::FnLike { fragment, .. } => *fragment, MacroCallKind::Derive { .. } => FragmentKind::Items, + MacroCallKind::Attr { .. } => FragmentKind::Items, // is this always correct? } } } diff --git a/crates/hir_expand/src/proc_macro.rs b/crates/hir_expand/src/proc_macro.rs index d5643393a..dbe1b446e 100644 --- a/crates/hir_expand/src/proc_macro.rs +++ b/crates/hir_expand/src/proc_macro.rs @@ -28,11 +28,16 @@ impl ProcMacroExpander { Self { krate, proc_macro_id: None } } + pub fn is_dummy(&self) -> bool { + self.proc_macro_id.is_none() + } + pub fn expand( self, db: &dyn AstDatabase, calling_crate: CrateId, tt: &tt::Subtree, + attr_arg: Option<&tt::Subtree>, ) -> Result { match self.proc_macro_id { Some(id) => { @@ -46,7 +51,7 @@ impl ProcMacroExpander { // Proc macros have access to the environment variables of the invoking crate. let env = &krate_graph[calling_crate].env; - proc_macro.expander.expand(&tt, None, &env).map_err(mbe::ExpandError::from) + proc_macro.expander.expand(&tt, attr_arg, &env).map_err(mbe::ExpandError::from) } None => Err(mbe::ExpandError::UnresolvedProcMacro), } -- cgit v1.2.3