about summary refs log tree commit diff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-10-26 18:18:22 +0000
committerGitHub <noreply@github.com>2021-10-26 18:18:22 +0000
commita3830dfd3b074c101e9bfb99271871eca1d44f75 (patch)
tree85b15c5d3f8bdf86043bb948aff5caa65d16462a /crates
parentc48730cb72919df7f1426085872846524b5a3cb2 (diff)
parentb42093915ab14fe61688ef1705505dbd859cb89a (diff)
downloadrust-a3830dfd3b074c101e9bfb99271871eca1d44f75.tar.gz
rust-a3830dfd3b074c101e9bfb99271871eca1d44f75.zip
Merge #10641
10641: fix: make `expand_macro` multi-token mapping aware r=spookyvision a=spookyvision



Co-authored-by: Anatol Ulrich <anatol.ulrich@ferrous-systems.com>
Co-authored-by: Anatol Ulrich <45840+spookyvision@users.noreply.github.com>
Diffstat (limited to 'crates')
-rw-r--r--crates/ide/src/expand_macro.rs40
1 files changed, 28 insertions, 12 deletions
diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs
index 5f34ba83919..c389d895f28 100644
--- a/crates/ide/src/expand_macro.rs
+++ b/crates/ide/src/expand_macro.rs
@@ -32,19 +32,32 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
         _ => 0,
     })?;
 
-    let descended = sema.descend_into_macros_single(tok.clone());
-    if let Some(attr) = descended.ancestors().find_map(ast::Attr::cast) {
-        if let Some((path, tt)) = attr.as_simple_call() {
-            if path == "derive" {
-                let mut tt = tt.syntax().children_with_tokens().skip(1).join("");
-                tt.pop();
-                let expansions = sema.expand_derive_macro(&attr)?;
-                return Some(ExpandedMacro {
-                    name: tt,
-                    expansion: expansions.into_iter().map(insert_whitespaces).join(""),
-                });
-            }
+    // due to how Rust Analyzer works internally, we need to special case derive attributes,
+    // otherwise they might not get found, e.g. here with the cursor at $0 `#[attr]` would expand:
+    // ```
+    // #[attr]
+    // #[derive($0Foo)]
+    // struct Bar;
+    // ```
+
+    let derive = sema.descend_into_macros(tok.clone()).iter().find_map(|descended| {
+        let attr = descended.ancestors().find_map(ast::Attr::cast)?;
+        let (path, tt) = attr.as_simple_call()?;
+        if path == "derive" {
+            let mut tt = tt.syntax().children_with_tokens().skip(1).join("");
+            tt.pop();
+            let expansions = sema.expand_derive_macro(&attr)?;
+            Some(ExpandedMacro {
+                name: tt,
+                expansion: expansions.into_iter().map(insert_whitespaces).join(""),
+            })
+        } else {
+            None
         }
+    });
+
+    if derive.is_some() {
+        return derive;
     }
 
     // FIXME: Intermix attribute and bang! expansions
@@ -353,9 +366,12 @@ fn main() {
     fn macro_expand_derive() {
         check(
             r#"
+//- proc_macros: identity
+
 #[rustc_builtin_macro]
 pub macro Clone {}
 
+#[proc_macros::identity]
 #[derive(C$0lone)]
 struct Foo {}
 "#,