about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2024-06-20 10:31:20 +0200
committerLukas Wirth <lukastw97@gmail.com>2024-06-20 10:31:20 +0200
commite5d5c7b20ad3e74b85fe9c3af2852595ae8a345a (patch)
treeae227a38d0feb8676efbb24f9c57952ca4385cd1
parenta2d4e2934e86c45cf3c343650ab968c7edd05e45 (diff)
downloadrust-e5d5c7b20ad3e74b85fe9c3af2852595ae8a345a.tar.gz
rust-e5d5c7b20ad3e74b85fe9c3af2852595ae8a345a.zip
Invert matching on builtin macros in expand_allowed_builtins
-rw-r--r--src/tools/rust-analyzer/crates/hir/src/semantics.rs46
-rw-r--r--src/tools/rust-analyzer/crates/ide/src/expand_macro.rs8
2 files changed, 25 insertions, 29 deletions
diff --git a/src/tools/rust-analyzer/crates/hir/src/semantics.rs b/src/tools/rust-analyzer/crates/hir/src/semantics.rs
index 633d2aaf3e8..358f10d3b89 100644
--- a/src/tools/rust-analyzer/crates/hir/src/semantics.rs
+++ b/src/tools/rust-analyzer/crates/hir/src/semantics.rs
@@ -328,6 +328,8 @@ impl<'db> SemanticsImpl<'db> {
         Some(node)
     }
 
+    /// Expands the macro if it isn't one of the built-in ones that expand to custom syntax or dummy
+    /// expansions.
     pub fn expand_allowed_builtins(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> {
         let sa = self.analyze_no_infer(macro_call.syntax())?;
 
@@ -341,33 +343,27 @@ impl<'db> SemanticsImpl<'db> {
         };
         let macro_call = self.db.lookup_intern_macro_call(file_id.macro_call_id);
 
-        match macro_call.def.kind {
+        let skip = matches!(
+            macro_call.def.kind,
             hir_expand::MacroDefKind::BuiltIn(
                 _,
-                BuiltinFnLikeExpander::Cfg
-                | BuiltinFnLikeExpander::StdPanic
-                | BuiltinFnLikeExpander::Stringify
-                | BuiltinFnLikeExpander::CorePanic,
-            )
-            | hir_expand::MacroDefKind::BuiltInEager(
-                _,
-                EagerExpander::Env
-                | EagerExpander::Concat
-                | EagerExpander::Include
-                | EagerExpander::OptionEnv
-                | EagerExpander::IncludeStr
-                | EagerExpander::ConcatBytes
-                | EagerExpander::IncludeBytes,
-            ) => {
-                // Do nothing and allow matching macros to be expanded
-            }
-
-            hir_expand::MacroDefKind::BuiltIn(_, _)
-            | hir_expand::MacroDefKind::BuiltInAttr(_, _)
-            | hir_expand::MacroDefKind::BuiltInEager(_, _)
-            | hir_expand::MacroDefKind::BuiltInDerive(_, _) => return None,
-
-            _ => (),
+                BuiltinFnLikeExpander::Column
+                    | BuiltinFnLikeExpander::File
+                    | BuiltinFnLikeExpander::ModulePath
+                    | BuiltinFnLikeExpander::Asm
+                    | BuiltinFnLikeExpander::LlvmAsm
+                    | BuiltinFnLikeExpander::GlobalAsm
+                    | BuiltinFnLikeExpander::LogSyntax
+                    | BuiltinFnLikeExpander::TraceMacros
+                    | BuiltinFnLikeExpander::FormatArgs
+                    | BuiltinFnLikeExpander::FormatArgsNl
+                    | BuiltinFnLikeExpander::ConstFormatArgs,
+            ) | hir_expand::MacroDefKind::BuiltInEager(_, EagerExpander::CompileError)
+        );
+        if skip {
+            // these macros expand to custom builtin syntax and/or dummy things, no point in
+            // showing these to the user
+            return None;
         }
 
         let node = self.parse_or_expand(file_id.into());
diff --git a/src/tools/rust-analyzer/crates/ide/src/expand_macro.rs b/src/tools/rust-analyzer/crates/ide/src/expand_macro.rs
index e3bb159daf4..4b54c057bf3 100644
--- a/src/tools/rust-analyzer/crates/ide/src/expand_macro.rs
+++ b/src/tools/rust-analyzer/crates/ide/src/expand_macro.rs
@@ -233,8 +233,8 @@ mod tests {
     fn expand_allowed_builtin_macro() {
         check(
             r#"
-            //- minicore: concat
-            $0concat!("test", 10, 'b', true);"#,
+//- minicore: concat
+$0concat!("test", 10, 'b', true);"#,
             expect![[r#"
                 concat!
                 "test10btrue""#]],
@@ -245,8 +245,8 @@ mod tests {
     fn do_not_expand_disallowed_macro() {
         let (analysis, pos) = fixture::position(
             r#"
-        //- minicore: asm
-        $0asm!("0x300, x0");"#,
+//- minicore: asm
+$0asm!("0x300, x0");"#,
         );
         let expansion = analysis.expand_macro(pos).unwrap();
         assert!(expansion.is_none());