avoid speculation when completing macros

This commit is contained in:
Aleksey Kladov 2021-12-28 16:47:21 +03:00
parent 5c11b363df
commit 726da9884b
5 changed files with 41 additions and 17 deletions

View file

@ -10,7 +10,7 @@ use hir_def::{
resolver::{self, HasResolver, Resolver, TypeNs},
AsMacroCall, FunctionId, TraitId, VariantId,
};
use hir_expand::{name::AsName, ExpansionInfo};
use hir_expand::{name::AsName, ExpansionInfo, MacroCallLoc};
use hir_ty::{associated_type_shorthand_candidates, Interner};
use itertools::Itertools;
use rustc_hash::{FxHashMap, FxHashSet};
@ -160,6 +160,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
self.imp.expand_attr_macro(item)
}
pub fn resolve_derive_macro(&self, derive: &ast::Attr) -> Option<Vec<MacroDef>> {
self.imp.resolve_derive_macro(derive)
}
pub fn expand_derive_macro(&self, derive: &ast::Attr) -> Option<Vec<SyntaxNode>> {
self.imp.expand_derive_macro(derive)
}
@ -443,6 +447,25 @@ impl<'db> SemanticsImpl<'db> {
Some(node)
}
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)
})
}
fn expand_derive_macro(&self, attr: &ast::Attr) -> Option<Vec<SyntaxNode>> {
let item = attr.syntax().parent().and_then(ast::Item::cast)?;
let file_id = self.find_file(item.syntax()).file_id;