]> git.lizzy.rs Git - rust.git/commitdiff
Merge hir::MacroDef::is_* into hir::MacroDef::kind
authorLukas Wirth <lukastw97@gmail.com>
Mon, 22 Mar 2021 14:56:59 +0000 (15:56 +0100)
committerLukas Wirth <lukastw97@gmail.com>
Tue, 23 Mar 2021 10:32:14 +0000 (11:32 +0100)
crates/hir/src/lib.rs
crates/ide_completion/src/completions/attribute.rs
crates/ide_db/src/search.rs

index ea0a609612309b63de3a097123ae6fa144ecd18b..5da6a034045879c942de2185572245c44f752a9c 100644 (file)
@@ -1116,6 +1116,14 @@ pub fn name(self) -> Name {
     }
 }
 
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+pub enum MacroKind {
+    Declarative,
+    ProcMacro,
+    Derive,
+    BuiltIn,
+}
+
 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
 pub struct MacroDef {
     pub(crate) id: MacroDefId,
@@ -1140,20 +1148,15 @@ pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
         }
     }
 
-    /// Indicate it is a proc-macro
-    pub fn is_proc_macro(&self) -> bool {
-        matches!(self.id.kind, MacroDefKind::ProcMacro(..))
-    }
-
-    /// Indicate it is a derive macro
-    pub fn is_derive_macro(&self) -> bool {
-        // FIXME: wrong for `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(..))
+    pub fn kind(&self) -> MacroKind {
+        match self.id.kind {
+            MacroDefKind::Declarative(_) => MacroKind::Declarative,
+            MacroDefKind::BuiltIn(_, _) => MacroKind::BuiltIn,
+            MacroDefKind::BuiltInDerive(_, _) => MacroKind::Derive,
+            MacroDefKind::BuiltInEager(_, _) => MacroKind::BuiltIn,
+            // FIXME might be a derive
+            MacroDefKind::ProcMacro(_, _) => MacroKind::ProcMacro,
+        }
     }
 }
 
index e846678b4b9736c87b26ea7109dddf0d881d2bd8..b1505c74b4ee968a971bc6d3ae1e340e3184d50d 100644 (file)
@@ -246,7 +246,8 @@ fn get_derive_names_in_scope(ctx: &CompletionContext) -> FxHashSet<String> {
     let mut result = FxHashSet::default();
     ctx.scope.process_all_names(&mut |name, 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());
             }
         }
index 5fdcb13cfb9a07ae0caa4dbd20167720246c06db..ff958c757c5df45871f70854f473124272cb2b71 100644 (file)
@@ -257,7 +257,7 @@ fn search_scope(&self, db: &RootDatabase) -> SearchScope {
         };
 
         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() {
                     rev_dep_scope()
                 } else {