about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2021-12-28 16:50:13 +0300
committerAleksey Kladov <aleksey.kladov@gmail.com>2021-12-28 16:52:15 +0300
commit56b51852c239effd266fa3853ba5eb5da367cbf1 (patch)
treeca813b5819a9d3a7925ed205372a6bcb98a30242
parent726da9884b761d9ae1ad266ab50a9bb758ccb8a9 (diff)
downloadrust-56b51852c239effd266fa3853ba5eb5da367cbf1.tar.gz
rust-56b51852c239effd266fa3853ba5eb5da367cbf1.zip
minor: dedup
-rw-r--r--crates/hir/src/semantics.rs65
1 files changed, 32 insertions, 33 deletions
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs
index 2cf63efff82..f3d8550a87f 100644
--- a/crates/hir/src/semantics.rs
+++ b/crates/hir/src/semantics.rs
@@ -10,7 +10,7 @@ use hir_def::{
     resolver::{self, HasResolver, Resolver, TypeNs},
     AsMacroCall, FunctionId, TraitId, VariantId,
 };
-use hir_expand::{name::AsName, ExpansionInfo, MacroCallLoc};
+use hir_expand::{name::AsName, ExpansionInfo, MacroCallId, MacroCallLoc};
 use hir_ty::{associated_type_shorthand_candidates, Interner};
 use itertools::Itertools;
 use rustc_hash::{FxHashMap, FxHashSet};
@@ -448,46 +448,45 @@ impl<'db> SemanticsImpl<'db> {
     }
 
     fn resolve_derive_macro(&self, attr: &ast::Attr) -> Option<Vec<MacroDef>> {
-        let item = attr.syntax().parent().and_then(ast::Item::cast)?;
-        let file_id = self.find_file(item.syntax()).file_id;
-        let item = InFile::new(file_id, &item);
-        let src = InFile::new(file_id, attr.clone());
-        self.with_ctx(|ctx| {
-            let macro_call_ids = ctx.attr_to_derive_macro_call(item, src)?;
-
-            let res = macro_call_ids
-                .iter()
-                .map(|&call| {
-                    let loc: MacroCallLoc = self.db.lookup_intern_macro_call(call);
-                    MacroDef { id: loc.def }
-                })
-                .collect();
-            Some(res)
-        })
+        let macro_call_ids = self.derive_macro_calls(attr)?;
+        let res = macro_call_ids
+            .iter()
+            .map(|&call| {
+                let loc: MacroCallLoc = self.db.lookup_intern_macro_call(call);
+                MacroDef { id: loc.def }
+            })
+            .collect();
+        Some(res)
     }
 
     fn expand_derive_macro(&self, attr: &ast::Attr) -> Option<Vec<SyntaxNode>> {
+        let macro_call_ids = self.derive_macro_calls(attr)?;
+
+        let expansions: Vec<_> = macro_call_ids
+            .iter()
+            .map(|call| call.as_file())
+            .flat_map(|file_id| {
+                let node = self.db.parse_or_expand(file_id)?;
+                self.cache(node.clone(), file_id);
+                Some(node)
+            })
+            .collect();
+
+        if expansions.is_empty() {
+            None
+        } else {
+            Some(expansions)
+        }
+    }
+
+    fn derive_macro_calls(&self, attr: &ast::Attr) -> Option<Vec<MacroCallId>> {
         let item = attr.syntax().parent().and_then(ast::Item::cast)?;
         let file_id = self.find_file(item.syntax()).file_id;
         let item = InFile::new(file_id, &item);
         let src = InFile::new(file_id, attr.clone());
         self.with_ctx(|ctx| {
-            let macro_call_ids = ctx.attr_to_derive_macro_call(item, src)?;
-
-            let expansions: Vec<_> = macro_call_ids
-                .iter()
-                .map(|call| call.as_file())
-                .flat_map(|file_id| {
-                    let node = self.db.parse_or_expand(file_id)?;
-                    self.cache(node.clone(), file_id);
-                    Some(node)
-                })
-                .collect();
-            if expansions.is_empty() {
-                None
-            } else {
-                Some(expansions)
-            }
+            let res = ctx.attr_to_derive_macro_call(item, src)?;
+            Some(res.to_vec())
         })
     }