Skip to content

Store an AstId for procedural macros #8088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions crates/hir/src/has_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use hir_def::{
src::{HasChildSource, HasSource as _},
Lookup, VariantId,
};
use hir_expand::InFile;
use hir_expand::{InFile, MacroDefKind};
use syntax::ast;

use crate::{
Expand Down Expand Up @@ -111,10 +111,17 @@ impl HasSource for TypeAlias {
}
}
impl HasSource for MacroDef {
type Ast = ast::Macro;
type Ast = Either<ast::Macro, ast::Fn>;
fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
let ast_id = self.id.ast_id()?;
Some(InFile { file_id: ast_id.file_id, value: ast_id.to_node(db.upcast()) })
Some(match &self.id.kind {
MacroDefKind::Declarative(id)
| MacroDefKind::BuiltIn(_, id)
| MacroDefKind::BuiltInDerive(_, id)
| MacroDefKind::BuiltInEager(_, id) => {
id.with_value(Either::Left(id.to_node(db.upcast())))
}
MacroDefKind::ProcMacro(_, id) => id.map(|_| Either::Right(id.to_node(db.upcast()))),
})
}
}
impl HasSource for Impl {
Expand Down
7 changes: 5 additions & 2 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,12 +1144,15 @@ impl MacroDef {

/// XXX: this parses the file
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
self.source(db)?.value.name().map(|it| it.as_name())
match self.source(db)?.value {
Either::Left(it) => it.name().map(|it| it.as_name()),
Either::Right(it) => it.name().map(|it| it.as_name()),
}
}

/// Indicate it is a proc-macro
pub fn is_proc_macro(&self) -> bool {
matches!(self.id.kind, MacroDefKind::ProcMacro(_))
matches!(self.id.kind, MacroDefKind::ProcMacro(..))
}

/// Indicate it is a derive macro
Expand Down
2 changes: 1 addition & 1 deletion crates/hir_def/src/item_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl ItemScope {
.for_each(|vis| *vis = Visibility::Module(this_module));

for (mac, vis) in self.macros.values_mut() {
if let MacroDefKind::ProcMacro(_) = mac.kind {
if let MacroDefKind::ProcMacro(..) = mac.kind {
// FIXME: Technically this is insufficient since reexports of proc macros are also
// forbidden. Practically nobody does that.
continue;
Expand Down
13 changes: 7 additions & 6 deletions crates/hir_def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,17 +353,17 @@ impl DefCollector<'_> {
/// use a dummy expander that always errors. This comes with the drawback of macros potentially
/// going out of sync with what the build system sees (since we resolve using VFS state, but
/// Cargo builds only on-disk files). We could and probably should add diagnostics for that.
fn resolve_proc_macro(&mut self, name: &Name) {
fn resolve_proc_macro(&mut self, name: &Name, ast_id: AstId<ast::Fn>) {
self.exports_proc_macros = true;
let macro_def = match self.proc_macros.iter().find(|(n, _)| n == name) {
Some((_, expander)) => MacroDefId {
krate: self.def_map.krate,
kind: MacroDefKind::ProcMacro(*expander),
kind: MacroDefKind::ProcMacro(*expander, ast_id),
local_inner: false,
},
None => MacroDefId {
krate: self.def_map.krate,
kind: MacroDefKind::ProcMacro(ProcMacroExpander::dummy(self.def_map.krate)),
kind: MacroDefKind::ProcMacro(ProcMacroExpander::dummy(self.def_map.krate), ast_id),
local_inner: false,
},
};
Expand Down Expand Up @@ -1116,7 +1116,8 @@ impl ModCollector<'_, '_> {
ModItem::Function(id) => {
let func = &self.item_tree[id];

self.collect_proc_macro_def(&func.name, &attrs);
let ast_id = InFile::new(self.file_id, func.ast_id);
self.collect_proc_macro_def(&func.name, ast_id, &attrs);

def = Some(DefData {
id: FunctionLoc {
Expand Down Expand Up @@ -1383,7 +1384,7 @@ impl ModCollector<'_, '_> {
}

/// If `attrs` registers a procedural macro, collects its definition.
fn collect_proc_macro_def(&mut self, func_name: &Name, attrs: &Attrs) {
fn collect_proc_macro_def(&mut self, func_name: &Name, ast_id: AstId<ast::Fn>, attrs: &Attrs) {
// FIXME: this should only be done in the root module of `proc-macro` crates, not everywhere
// FIXME: distinguish the type of macro
let macro_name = if attrs.by_key("proc_macro").exists()
Expand All @@ -1404,7 +1405,7 @@ impl ModCollector<'_, '_> {
}
};

self.def_collector.resolve_proc_macro(&macro_name);
self.def_collector.resolve_proc_macro(&macro_name, ast_id);
}

fn collect_macro_rules(&mut self, id: FileItemTreeId<MacroRules>) {
Expand Down
4 changes: 2 additions & 2 deletions crates/hir_expand/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander,
Some(Arc::new((TokenExpander::BuiltinDerive(expander), mbe::TokenMap::default())))
}
MacroDefKind::BuiltInEager(..) => None,
MacroDefKind::ProcMacro(expander) => {
MacroDefKind::ProcMacro(expander, ..) => {
Some(Arc::new((TokenExpander::ProcMacro(expander), mbe::TokenMap::default())))
}
}
Expand Down Expand Up @@ -269,7 +269,7 @@ fn expand_proc_macro(
};

let expander = match loc.def.kind {
MacroDefKind::ProcMacro(expander) => expander,
MacroDefKind::ProcMacro(expander, ..) => expander,
_ => unreachable!(),
};

Expand Down
2 changes: 1 addition & 1 deletion crates/hir_expand/src/eager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ fn eager_macro_recur(
MacroDefKind::Declarative(_)
| MacroDefKind::BuiltIn(..)
| MacroDefKind::BuiltInDerive(..)
| MacroDefKind::ProcMacro(_) => {
| MacroDefKind::ProcMacro(..) => {
let res = lazy_expand(db, &def, curr.with_value(child.clone()), krate);
let val = diagnostic_sink.expand_result_option(res)?;

Expand Down
2 changes: 1 addition & 1 deletion crates/hir_expand/src/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl HygieneFrame {
MacroDefKind::BuiltIn(..) => (info, Some(loc.def.krate), false),
MacroDefKind::BuiltInDerive(..) => (info, None, false),
MacroDefKind::BuiltInEager(..) => (info, None, false),
MacroDefKind::ProcMacro(_) => (info, None, false),
MacroDefKind::ProcMacro(..) => (info, None, false),
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions crates/hir_expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl MacroDefId {
MacroDefKind::BuiltIn(_, id) => id,
MacroDefKind::BuiltInDerive(_, id) => id,
MacroDefKind::BuiltInEager(_, id) => id,
MacroDefKind::ProcMacro(_) => return None,
MacroDefKind::ProcMacro(..) => return None,
};
Some(*id)
}
Expand All @@ -258,7 +258,7 @@ pub enum MacroDefKind {
// FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander
BuiltInDerive(BuiltinDeriveExpander, AstId<ast::Macro>),
BuiltInEager(EagerExpander, AstId<ast::Macro>),
ProcMacro(ProcMacroExpander),
ProcMacro(ProcMacroExpander, AstId<ast::Fn>),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down
8 changes: 6 additions & 2 deletions crates/ide/src/display/navigation_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,14 @@ impl TryToNav for hir::Field {
impl TryToNav for hir::MacroDef {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
let src = self.source(db)?;
log::debug!("nav target {:#?}", src.value.syntax());
let name_owner: &dyn ast::NameOwner = match &src.value {
Either::Left(it) => it,
Either::Right(it) => it,
};
log::debug!("nav target {:#?}", name_owner.syntax());
let mut res = NavigationTarget::from_named(
db,
src.as_ref().map(|it| it as &dyn ast::NameOwner),
src.as_ref().with_value(name_owner),
SymbolKind::Macro,
);
res.docs = self.docs(db);
Expand Down
14 changes: 10 additions & 4 deletions crates/ide/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,16 @@ fn hover_for_definition(
) -> Option<Markup> {
let mod_path = definition_mod_path(db, &def);
return match def {
Definition::Macro(it) => {
let label = macro_label(&it.source(db)?.value);
from_def_source_labeled(db, it, Some(label), mod_path)
}
Definition::Macro(it) => match &it.source(db)?.value {
Either::Left(mac) => {
let label = macro_label(&mac);
from_def_source_labeled(db, it, Some(label), mod_path)
}
Either::Right(_) => {
// FIXME
None
}
},
Definition::Field(def) => from_hir_fmt(db, def, mod_path),
Definition::ModuleDef(it) => match it {
ModuleDef::Module(it) => from_hir_fmt(db, it, mod_path),
Expand Down
2 changes: 1 addition & 1 deletion crates/ide_completion/src/render/macro_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl<'a> MacroRender<'a> {
}

fn detail(&self) -> Option<String> {
let ast_node = self.macro_.source(self.ctx.db())?.value;
let ast_node = self.macro_.source(self.ctx.db())?.value.left()?;
Some(macro_label(&ast_node))
}
}
Expand Down