mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 04:19:13 +00:00
Thread proc-macro types through the HIR
This commit is contained in:
parent
f6da603c7f
commit
8c639a87bd
5 changed files with 34 additions and 10 deletions
|
@ -147,7 +147,7 @@ impl CrateDisplayName {
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct ProcMacroId(pub u32);
|
pub struct ProcMacroId(pub u32);
|
||||||
|
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
|
||||||
pub enum ProcMacroKind {
|
pub enum ProcMacroKind {
|
||||||
CustomDerive,
|
CustomDerive,
|
||||||
FuncLike,
|
FuncLike,
|
||||||
|
|
|
@ -1282,10 +1282,16 @@ impl BuiltinType {
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub enum MacroKind {
|
pub enum MacroKind {
|
||||||
|
/// `macro_rules!` or Macros 2.0 macro.
|
||||||
Declarative,
|
Declarative,
|
||||||
ProcMacro,
|
/// A built-in or custom derive.
|
||||||
Derive,
|
Derive,
|
||||||
|
/// A built-in function-like macro.
|
||||||
BuiltIn,
|
BuiltIn,
|
||||||
|
/// A procedural attribute macro.
|
||||||
|
Attr,
|
||||||
|
/// A function-like procedural macro.
|
||||||
|
ProcMacro,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
@ -1315,11 +1321,13 @@ impl MacroDef {
|
||||||
pub fn kind(&self) -> MacroKind {
|
pub fn kind(&self) -> MacroKind {
|
||||||
match self.id.kind {
|
match self.id.kind {
|
||||||
MacroDefKind::Declarative(_) => MacroKind::Declarative,
|
MacroDefKind::Declarative(_) => MacroKind::Declarative,
|
||||||
MacroDefKind::BuiltIn(_, _) => MacroKind::BuiltIn,
|
MacroDefKind::BuiltIn(_, _) | MacroDefKind::BuiltInEager(_, _) => MacroKind::BuiltIn,
|
||||||
MacroDefKind::BuiltInDerive(_, _) => MacroKind::Derive,
|
MacroDefKind::BuiltInDerive(_, _) => MacroKind::Derive,
|
||||||
MacroDefKind::BuiltInEager(_, _) => MacroKind::BuiltIn,
|
MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::CustomDerive, _) => {
|
||||||
// FIXME might be a derive
|
MacroKind::Derive
|
||||||
MacroDefKind::ProcMacro(_, _) => MacroKind::ProcMacro,
|
}
|
||||||
|
MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::Attr, _) => MacroKind::Attr,
|
||||||
|
MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::FuncLike, _) => MacroKind::ProcMacro,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -477,16 +477,21 @@ impl DefCollector<'_> {
|
||||||
/// going out of sync with what the build system sees (since we resolve using VFS state, but
|
/// 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.
|
/// Cargo builds only on-disk files). We could and probably should add diagnostics for that.
|
||||||
fn export_proc_macro(&mut self, def: ProcMacroDef, ast_id: AstId<ast::Fn>) {
|
fn export_proc_macro(&mut self, def: ProcMacroDef, ast_id: AstId<ast::Fn>) {
|
||||||
|
let kind = def.kind.to_basedb_kind();
|
||||||
self.exports_proc_macros = true;
|
self.exports_proc_macros = true;
|
||||||
let macro_def = match self.proc_macros.iter().find(|(n, _)| n == &def.name) {
|
let macro_def = match self.proc_macros.iter().find(|(n, _)| n == &def.name) {
|
||||||
Some((_, expander)) => MacroDefId {
|
Some((_, expander)) => MacroDefId {
|
||||||
krate: self.def_map.krate,
|
krate: self.def_map.krate,
|
||||||
kind: MacroDefKind::ProcMacro(*expander, ast_id),
|
kind: MacroDefKind::ProcMacro(*expander, kind, ast_id),
|
||||||
local_inner: false,
|
local_inner: false,
|
||||||
},
|
},
|
||||||
None => MacroDefId {
|
None => MacroDefId {
|
||||||
krate: self.def_map.krate,
|
krate: self.def_map.krate,
|
||||||
kind: MacroDefKind::ProcMacro(ProcMacroExpander::dummy(self.def_map.krate), ast_id),
|
kind: MacroDefKind::ProcMacro(
|
||||||
|
ProcMacroExpander::dummy(self.def_map.krate),
|
||||||
|
kind,
|
||||||
|
ast_id,
|
||||||
|
),
|
||||||
local_inner: false,
|
local_inner: false,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -18,6 +18,16 @@ pub(super) enum ProcMacroKind {
|
||||||
Attr,
|
Attr,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ProcMacroKind {
|
||||||
|
pub(super) fn to_basedb_kind(&self) -> base_db::ProcMacroKind {
|
||||||
|
match self {
|
||||||
|
ProcMacroKind::CustomDerive { .. } => base_db::ProcMacroKind::CustomDerive,
|
||||||
|
ProcMacroKind::FnLike => base_db::ProcMacroKind::FuncLike,
|
||||||
|
ProcMacroKind::Attr => base_db::ProcMacroKind::Attr,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Attrs {
|
impl Attrs {
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
pub(super) fn parse_proc_macro_decl(&self, func_name: &Name) -> Option<ProcMacroDef> {
|
pub(super) fn parse_proc_macro_decl(&self, func_name: &Name) -> Option<ProcMacroDef> {
|
||||||
|
|
|
@ -15,6 +15,7 @@ pub mod quote;
|
||||||
pub mod eager;
|
pub mod eager;
|
||||||
mod input;
|
mod input;
|
||||||
|
|
||||||
|
use base_db::ProcMacroKind;
|
||||||
use either::Either;
|
use either::Either;
|
||||||
|
|
||||||
pub use mbe::{ExpandError, ExpandResult};
|
pub use mbe::{ExpandError, ExpandResult};
|
||||||
|
@ -207,7 +208,7 @@ impl MacroDefId {
|
||||||
MacroDefKind::BuiltIn(_, id) => id,
|
MacroDefKind::BuiltIn(_, id) => id,
|
||||||
MacroDefKind::BuiltInDerive(_, id) => id,
|
MacroDefKind::BuiltInDerive(_, id) => id,
|
||||||
MacroDefKind::BuiltInEager(_, id) => id,
|
MacroDefKind::BuiltInEager(_, id) => id,
|
||||||
MacroDefKind::ProcMacro(_, id) => return Either::Right(*id),
|
MacroDefKind::ProcMacro(.., id) => return Either::Right(*id),
|
||||||
};
|
};
|
||||||
Either::Left(*id)
|
Either::Left(*id)
|
||||||
}
|
}
|
||||||
|
@ -224,7 +225,7 @@ pub enum MacroDefKind {
|
||||||
// FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander
|
// FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander
|
||||||
BuiltInDerive(BuiltinDeriveExpander, AstId<ast::Macro>),
|
BuiltInDerive(BuiltinDeriveExpander, AstId<ast::Macro>),
|
||||||
BuiltInEager(EagerExpander, AstId<ast::Macro>),
|
BuiltInEager(EagerExpander, AstId<ast::Macro>),
|
||||||
ProcMacro(ProcMacroExpander, AstId<ast::Fn>),
|
ProcMacro(ProcMacroExpander, ProcMacroKind, AstId<ast::Fn>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue