Merge hir::MacroDef::is_* into hir::MacroDef::kind

This commit is contained in:
Lukas Wirth 2021-03-22 15:56:59 +01:00
parent bad4e48672
commit 7c4eb66c1a
3 changed files with 20 additions and 16 deletions

View file

@ -1116,6 +1116,14 @@ impl BuiltinType {
} }
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MacroKind {
Declarative,
ProcMacro,
Derive,
BuiltIn,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MacroDef { pub struct MacroDef {
pub(crate) id: MacroDefId, pub(crate) id: MacroDefId,
@ -1140,20 +1148,15 @@ impl MacroDef {
} }
} }
/// Indicate it is a proc-macro pub fn kind(&self) -> MacroKind {
pub fn is_proc_macro(&self) -> bool { match self.id.kind {
matches!(self.id.kind, MacroDefKind::ProcMacro(..)) MacroDefKind::Declarative(_) => MacroKind::Declarative,
} MacroDefKind::BuiltIn(_, _) => MacroKind::BuiltIn,
MacroDefKind::BuiltInDerive(_, _) => MacroKind::Derive,
/// Indicate it is a derive macro MacroDefKind::BuiltInEager(_, _) => MacroKind::BuiltIn,
pub fn is_derive_macro(&self) -> bool { // FIXME might be a derive
// FIXME: wrong for `ProcMacro` MacroDefKind::ProcMacro(_, _) => MacroKind::ProcMacro,
matches!(self.id.kind, MacroDefKind::ProcMacro(..) | MacroDefKind::BuiltInDerive(..)) }
}
/// Indicate it is a declarative macro
pub fn is_declarative(&self) -> bool {
matches!(self.id.kind, MacroDefKind::Declarative(..))
} }
} }

View file

@ -246,7 +246,8 @@ fn get_derive_names_in_scope(ctx: &CompletionContext) -> FxHashSet<String> {
let mut result = FxHashSet::default(); let mut result = FxHashSet::default();
ctx.scope.process_all_names(&mut |name, scope_def| { ctx.scope.process_all_names(&mut |name, scope_def| {
if let hir::ScopeDef::MacroDef(mac) = scope_def { if let hir::ScopeDef::MacroDef(mac) = scope_def {
if mac.is_derive_macro() { // FIXME kind() doesn't check whether proc-macro is a derive
if mac.kind() == hir::MacroKind::Derive || mac.kind() == hir::MacroKind::ProcMacro {
result.insert(name.to_string()); result.insert(name.to_string());
} }
} }

View file

@ -257,7 +257,7 @@ impl Definition {
}; };
if let Definition::Macro(macro_def) = self { if let Definition::Macro(macro_def) = self {
if macro_def.is_declarative() { if macro_def.kind() == hir::MacroKind::Declarative {
return if macro_def.attrs(db).by_key("macro_export").exists() { return if macro_def.attrs(db).by_key("macro_export").exists() {
rev_dep_scope() rev_dep_scope()
} else { } else {