Use FileAstId<ast::Adt> in nameres where appropriate instead

This commit is contained in:
Lukas Wirth 2022-01-07 14:19:11 +01:00
parent 08adce61a1
commit ca4baa6e55
23 changed files with 98 additions and 65 deletions

View file

@ -649,8 +649,12 @@ impl Module {
let node = ast_id.to_node(db.upcast());
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node)))
}
MacroCallKind::Derive { ast_id, .. }
| MacroCallKind::Attr { ast_id, .. } => {
MacroCallKind::Derive { ast_id, .. } => {
// FIXME: point to the attribute instead, this creates very large diagnostics
let node = ast_id.to_node(db.upcast());
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node)))
}
MacroCallKind::Attr { ast_id, .. } => {
// FIXME: point to the attribute instead, this creates very large diagnostics
let node = ast_id.to_node(db.upcast());
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node)))

View file

@ -18,7 +18,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
use smallvec::{smallvec, SmallVec};
use syntax::{
algo::skip_trivia_token,
ast::{self, HasAttrs, HasGenericParams, HasLoopBody},
ast::{self, HasAttrs as _, HasGenericParams, HasLoopBody},
match_ast, AstNode, AstToken, Direction, SyntaxElement, SyntaxNode, SyntaxNodePtr, SyntaxToken,
TextSize, T,
};
@ -27,9 +27,9 @@ use crate::{
db::HirDatabase,
semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx},
source_analyzer::{resolve_hir_path, SourceAnalyzer},
Access, AssocItem, BuiltinAttr, Callable, ConstParam, Crate, Field, Function, HasSource,
HirFileId, Impl, InFile, Label, LifetimeParam, Local, MacroDef, Module, ModuleDef, Name, Path,
ScopeDef, ToolModule, Trait, Type, TypeAlias, TypeParam, VariantDef,
Access, AssocItem, BuiltinAttr, Callable, ConstParam, Crate, Field, Function, HasAttrs as _,
HasSource, HirFileId, Impl, InFile, Label, LifetimeParam, Local, MacroDef, Module, ModuleDef,
Name, Path, ScopeDef, ToolModule, Trait, Type, TypeAlias, TypeParam, VariantDef,
};
#[derive(Debug, Clone, PartialEq, Eq)]
@ -486,7 +486,7 @@ impl<'db> SemanticsImpl<'db> {
let adt = InFile::new(file_id, &adt);
let src = InFile::new(file_id, attr.clone());
self.with_ctx(|ctx| {
let res = ctx.attr_to_derive_macro_call(adt, src)?;
let (_, res) = ctx.attr_to_derive_macro_call(adt, src)?;
Some(res.to_vec())
})
}
@ -917,15 +917,14 @@ impl<'db> SemanticsImpl<'db> {
let tt = derive.token_tree()?;
let file = self.find_file(derive.syntax());
let adt = derive.syntax().parent().and_then(ast::Adt::cast)?;
let adt_def = ToDef::to_def(self, file.with_value(adt.clone()))?;
let res = self.with_ctx(|ctx| {
let attr_def = ctx.attr_to_def(file.with_value(derive.clone()))?;
let derives = ctx.attr_to_derive_macro_call(
let (attr_id, derives) = ctx.attr_to_derive_macro_call(
file.with_value(&adt),
file.with_value(derive.clone()),
)?;
let mut derive_paths = attr_def.parse_path_comma_token_tree()?;
let attrs = adt_def.attrs(self.db);
let mut derive_paths = attrs[attr_id].parse_path_comma_token_tree()?;
let derive_idx = tt
.syntax()
@ -1225,7 +1224,6 @@ to_def_impls![
(crate::Local, ast::SelfParam, self_param_to_def),
(crate::Label, ast::Label, label_to_def),
(crate::Adt, ast::Adt, adt_to_def),
(crate::Attr, ast::Attr, attr_to_def),
];
fn find_root(node: &SyntaxNode) -> SyntaxNode {

View file

@ -87,6 +87,7 @@
use base_db::FileId;
use hir_def::{
attr::AttrId,
child_by_source::ChildBySource,
dyn_map::DynMap,
expr::{LabelId, PatId},
@ -210,19 +211,6 @@ impl SourceToDefCtx<'_, '_> {
ast::Adt::Union(it) => self.union_to_def(InFile::new(file_id, it)).map(AdtId::UnionId),
}
}
pub(super) fn attr_to_def(
&mut self,
InFile { file_id, value }: InFile<ast::Attr>,
) -> Option<crate::Attr> {
// FIXME: Use dynmap?
let adt = value.syntax().parent().and_then(ast::Adt::cast)?;
let attr_pos = ast::HasAttrs::attrs(&adt).position(|it| it == value)?;
let attrs = {
let def = self.adt_to_def(InFile::new(file_id, adt))?;
self.db.attrs(def.into())
};
attrs.get(attr_pos).cloned()
}
pub(super) fn bind_pat_to_def(
&mut self,
src: InFile<ast::IdentPat>,
@ -254,16 +242,16 @@ impl SourceToDefCtx<'_, '_> {
pub(super) fn item_to_macro_call(&mut self, src: InFile<ast::Item>) -> Option<MacroCallId> {
let map = self.dyn_map(src.as_ref())?;
map[keys::ATTR_MACRO].get(&src).copied()
map[keys::ATTR_MACRO_CALL].get(&src).copied()
}
pub(super) fn attr_to_derive_macro_call(
&mut self,
item: InFile<&ast::Adt>,
src: InFile<ast::Attr>,
) -> Option<&[Option<MacroCallId>]> {
) -> Option<(AttrId, &[Option<MacroCallId>])> {
let map = self.dyn_map(item)?;
map[keys::DERIVE_MACRO].get(&src).map(AsRef::as_ref)
map[keys::DERIVE_MACRO_CALL].get(&src).map(|(id, ids)| (*id, &**ids))
}
fn to_def<Ast: AstNode + 'static, ID: Copy + 'static>(
@ -328,7 +316,8 @@ impl SourceToDefCtx<'_, '_> {
}
pub(super) fn macro_to_def(&mut self, src: InFile<ast::Macro>) -> Option<MacroDefId> {
let makro = self.dyn_map(src.as_ref()).and_then(|it| it[keys::MACRO].get(&src).copied());
let makro =
self.dyn_map(src.as_ref()).and_then(|it| it[keys::MACRO_CALL].get(&src).copied());
if let res @ Some(_) = makro {
return res;
}