Skip to content

Commit 505329b

Browse files
9057: internal: Thread proc-macro types through the HIR r=jonas-schievink a=jonas-schievink Should help with completion of derives. cc @Veykril bors r+ Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
2 parents f6da603 + 8c639a8 commit 505329b

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

crates/base_db/src/input.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl CrateDisplayName {
147147
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
148148
pub struct ProcMacroId(pub u32);
149149

150-
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
150+
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
151151
pub enum ProcMacroKind {
152152
CustomDerive,
153153
FuncLike,

crates/hir/src/lib.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,10 +1282,16 @@ impl BuiltinType {
12821282

12831283
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12841284
pub enum MacroKind {
1285+
/// `macro_rules!` or Macros 2.0 macro.
12851286
Declarative,
1286-
ProcMacro,
1287+
/// A built-in or custom derive.
12871288
Derive,
1289+
/// A built-in function-like macro.
12881290
BuiltIn,
1291+
/// A procedural attribute macro.
1292+
Attr,
1293+
/// A function-like procedural macro.
1294+
ProcMacro,
12891295
}
12901296

12911297
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -1315,11 +1321,13 @@ impl MacroDef {
13151321
pub fn kind(&self) -> MacroKind {
13161322
match self.id.kind {
13171323
MacroDefKind::Declarative(_) => MacroKind::Declarative,
1318-
MacroDefKind::BuiltIn(_, _) => MacroKind::BuiltIn,
1324+
MacroDefKind::BuiltIn(_, _) | MacroDefKind::BuiltInEager(_, _) => MacroKind::BuiltIn,
13191325
MacroDefKind::BuiltInDerive(_, _) => MacroKind::Derive,
1320-
MacroDefKind::BuiltInEager(_, _) => MacroKind::BuiltIn,
1321-
// FIXME might be a derive
1322-
MacroDefKind::ProcMacro(_, _) => MacroKind::ProcMacro,
1326+
MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::CustomDerive, _) => {
1327+
MacroKind::Derive
1328+
}
1329+
MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::Attr, _) => MacroKind::Attr,
1330+
MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::FuncLike, _) => MacroKind::ProcMacro,
13231331
}
13241332
}
13251333
}

crates/hir_def/src/nameres/collector.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,16 +477,21 @@ impl DefCollector<'_> {
477477
/// going out of sync with what the build system sees (since we resolve using VFS state, but
478478
/// Cargo builds only on-disk files). We could and probably should add diagnostics for that.
479479
fn export_proc_macro(&mut self, def: ProcMacroDef, ast_id: AstId<ast::Fn>) {
480+
let kind = def.kind.to_basedb_kind();
480481
self.exports_proc_macros = true;
481482
let macro_def = match self.proc_macros.iter().find(|(n, _)| n == &def.name) {
482483
Some((_, expander)) => MacroDefId {
483484
krate: self.def_map.krate,
484-
kind: MacroDefKind::ProcMacro(*expander, ast_id),
485+
kind: MacroDefKind::ProcMacro(*expander, kind, ast_id),
485486
local_inner: false,
486487
},
487488
None => MacroDefId {
488489
krate: self.def_map.krate,
489-
kind: MacroDefKind::ProcMacro(ProcMacroExpander::dummy(self.def_map.krate), ast_id),
490+
kind: MacroDefKind::ProcMacro(
491+
ProcMacroExpander::dummy(self.def_map.krate),
492+
kind,
493+
ast_id,
494+
),
490495
local_inner: false,
491496
},
492497
};

crates/hir_def/src/nameres/proc_macro.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ pub(super) enum ProcMacroKind {
1818
Attr,
1919
}
2020

21+
impl ProcMacroKind {
22+
pub(super) fn to_basedb_kind(&self) -> base_db::ProcMacroKind {
23+
match self {
24+
ProcMacroKind::CustomDerive { .. } => base_db::ProcMacroKind::CustomDerive,
25+
ProcMacroKind::FnLike => base_db::ProcMacroKind::FuncLike,
26+
ProcMacroKind::Attr => base_db::ProcMacroKind::Attr,
27+
}
28+
}
29+
}
30+
2131
impl Attrs {
2232
#[rustfmt::skip]
2333
pub(super) fn parse_proc_macro_decl(&self, func_name: &Name) -> Option<ProcMacroDef> {

crates/hir_expand/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub mod quote;
1515
pub mod eager;
1616
mod input;
1717

18+
use base_db::ProcMacroKind;
1819
use either::Either;
1920

2021
pub use mbe::{ExpandError, ExpandResult};
@@ -207,7 +208,7 @@ impl MacroDefId {
207208
MacroDefKind::BuiltIn(_, id) => id,
208209
MacroDefKind::BuiltInDerive(_, id) => id,
209210
MacroDefKind::BuiltInEager(_, id) => id,
210-
MacroDefKind::ProcMacro(_, id) => return Either::Right(*id),
211+
MacroDefKind::ProcMacro(.., id) => return Either::Right(*id),
211212
};
212213
Either::Left(*id)
213214
}
@@ -224,7 +225,7 @@ pub enum MacroDefKind {
224225
// FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander
225226
BuiltInDerive(BuiltinDeriveExpander, AstId<ast::Macro>),
226227
BuiltInEager(EagerExpander, AstId<ast::Macro>),
227-
ProcMacro(ProcMacroExpander, AstId<ast::Fn>),
228+
ProcMacro(ProcMacroExpander, ProcMacroKind, AstId<ast::Fn>),
228229
}
229230

230231
#[derive(Debug, Clone, PartialEq, Eq, Hash)]

0 commit comments

Comments
 (0)