]> git.lizzy.rs Git - rust.git/commitdiff
Fix `expand_macro` not working for derive attributes
authorLukas Wirth <lukastw97@gmail.com>
Mon, 21 Feb 2022 10:51:53 +0000 (11:51 +0100)
committerLukas Wirth <lukastw97@gmail.com>
Tue, 22 Feb 2022 09:20:44 +0000 (10:20 +0100)
crates/hir_expand/src/lib.rs
crates/ide/src/expand_macro.rs

index 2ccf4c56eebb887667a4a8d77cc15ad39c82f205..2c6378cd3eee669d74787a9877aa0d75fcb88e3a 100644 (file)
@@ -219,9 +219,18 @@ pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option<ExpansionInfo> {
 
                 let arg_tt = loc.kind.arg(db)?;
 
+                let macro_def = db.macro_def(loc.def).ok()?;
+                let (parse, exp_map) = db.parse_macro_expansion(macro_file).value?;
+                let macro_arg = db.macro_arg(macro_file.macro_call_id)?;
+
                 let def = loc.def.ast_id().left().and_then(|id| {
                     let def_tt = match id.to_node(db) {
                         ast::Macro::MacroRules(mac) => mac.token_tree()?,
+                        ast::Macro::MacroDef(_)
+                            if matches!(*macro_def, TokenExpander::BuiltinAttr(_)) =>
+                        {
+                            return None
+                        }
                         ast::Macro::MacroDef(mac) => mac.body()?,
                     };
                     Some(InFile::new(id.file_id, def_tt))
@@ -239,10 +248,6 @@ pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option<ExpansionInfo> {
                     _ => None,
                 });
 
-                let macro_def = db.macro_def(loc.def).ok()?;
-                let (parse, exp_map) = db.parse_macro_expansion(macro_file).value?;
-                let macro_arg = db.macro_arg(macro_file.macro_call_id)?;
-
                 Some(ExpansionInfo {
                     expanded: InFile::new(self, parse.syntax_node()),
                     arg: InFile::new(loc.kind.file_id(), arg_tt),
@@ -292,7 +297,7 @@ pub fn is_include_macro(&self, db: &dyn db::AstDatabase) -> bool {
         }
     }
 
-    /// Return whether this file is an include macro
+    /// Return whether this file is an attr macro
     pub fn is_attr_macro(&self, db: &dyn db::AstDatabase) -> bool {
         match self.0 {
             HirFileIdRepr::MacroFile(macro_file) => {
@@ -303,6 +308,17 @@ pub fn is_attr_macro(&self, db: &dyn db::AstDatabase) -> bool {
         }
     }
 
+    /// Return whether this file is the pseudo expansion of the derive attribute.
+    pub fn is_derive_attr_macro(&self, db: &dyn db::AstDatabase) -> bool {
+        match self.0 {
+            HirFileIdRepr::MacroFile(macro_file) => {
+                let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
+                matches!(loc.kind, MacroCallKind::Attr { is_derive: true, .. })
+            }
+            _ => false,
+        }
+    }
+
     pub fn is_macro(self) -> bool {
         matches!(self.0, HirFileIdRepr::MacroFile(_))
     }
@@ -567,6 +583,9 @@ pub fn map_token_up(
 
         // Attributes are a bit special for us, they have two inputs, the input tokentree and the annotated item.
         let (token_map, tt) = match &loc.kind {
+            MacroCallKind::Attr { attr_args, is_derive: true, .. } => {
+                (&attr_args.1, self.attr_input_or_mac_def.clone()?.syntax().cloned())
+            }
             MacroCallKind::Attr { attr_args, .. } => {
                 // try unshifting the the token id, if unshifting fails, the token resides in the non-item attribute input
                 // note that the `TokenExpander::map_id_up` earlier only unshifts for declarative macros, so we don't double unshift with this
@@ -722,6 +741,13 @@ pub fn original_file_range_opt(self, db: &dyn db::AstDatabase) -> Option<FileRan
     }
 }
 
+impl InFile<SyntaxToken> {
+    pub fn upmap(self, db: &dyn db::AstDatabase) -> Option<InFile<SyntaxToken>> {
+        let expansion = self.file_id.expansion_info(db)?;
+        expansion.map_token_up(db, self.as_ref()).map(|(it, _)| it)
+    }
+}
+
 fn ascend_node_border_tokens(
     db: &dyn db::AstDatabase,
     InFile { file_id, value: node }: InFile<&SyntaxNode>,
index 32dbd9070b9eb7f1d98e3379b90ce018c5d1027f..c234eb3db6bd289d8044de2865de5a4f0dc1239f 100644 (file)
@@ -41,20 +41,18 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
     // 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_ws_into).join(""),
-            })
-        } else {
-            None
+    let derive = sema.descend_into_macros(tok.clone()).into_iter().find_map(|descended| {
+        let hir_file = sema.hir_file_for(&descended.parent()?);
+        if !hir_file.is_derive_attr_macro(db) {
+            return None;
         }
+
+        let name = descended.ancestors().filter_map(ast::Path::cast).last()?.to_string();
+        // up map out of the #[derive] expansion
+        let token = hir::InFile::new(hir_file, descended).upmap(db)?.value;
+        let attr = token.ancestors().find_map(ast::Attr::cast)?;
+        let expansions = sema.expand_derive_macro(&attr)?;
+        Some(ExpandedMacro { name, expansion: expansions.into_iter().map(insert_ws_into).join("") })
     });
 
     if derive.is_some() {