Switch to StaticLoc for statics

This commit is contained in:
Aleksey Kladov 2019-11-24 15:13:56 +03:00
parent 982a32aca3
commit e0b06cb672
13 changed files with 65 additions and 28 deletions

View File

@ -734,7 +734,7 @@ pub struct Static {
impl Static {
pub fn module(self, db: &impl DefDatabase) -> Module {
Module { id: self.id.module(db) }
Module { id: self.id.lookup(db).module(db) }
}
pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {

View File

@ -88,7 +88,7 @@ impl HasSource for Const {
impl HasSource for Static {
type Ast = ast::StaticDef;
fn source(self, db: &impl DefDatabase) -> Source<ast::StaticDef> {
self.id.source(db)
self.id.lookup(db).source(db)
}
}
impl HasSource for Trait {

View File

@ -104,10 +104,21 @@ impl FromSource for Const {
impl FromSource for Static {
type Ast = ast::StaticDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(Static { id })
let module = match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
Container::Module(it) => it,
Container::Trait(_) | Container::ImplBlock(_) => return None,
};
module
.declarations(db)
.into_iter()
.filter_map(|it| match it {
ModuleDef::Static(it) => Some(it),
_ => None,
})
.find(|it| same_source(&it.source(db), &src))
}
}
impl FromSource for TypeAlias {
type Ast = ast::TypeAliasDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {

View File

@ -11,7 +11,7 @@ use hir_def::{
expr::{ExprId, PatId},
path::known,
resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs},
DefWithBodyId, LocationCtx,
DefWithBodyId,
};
use hir_expand::{
name::AsName, AstId, HirFileId, MacroCallId, MacroCallLoc, MacroFileKind, Source,
@ -28,8 +28,8 @@ use crate::{
expr::{BodySourceMap, ExprScopes, ScopeId},
ty::method_resolution::{self, implements_trait},
Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function,
GenericParam, HasBody, Local, MacroDef, Module, Name, Path, ScopeDef, Static, Struct, Trait,
Ty, TypeAlias,
GenericParam, HasBody, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Ty,
TypeAlias,
};
fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option<Resolver> {
@ -68,16 +68,12 @@ fn def_with_body_from_child_node(
db: &impl HirDatabase,
child: Source<&SyntaxNode>,
) -> Option<DefWithBody> {
let module_source = crate::ModuleSource::from_child_node(db, child);
let module = Module::from_definition(db, Source::new(child.file_id, module_source))?;
let ctx = LocationCtx::new(db, module.id, child.file_id);
child.value.ancestors().find_map(|node| {
match_ast! {
match node {
ast::FnDef(def) => { return Function::from_source(db, child.with_value(def)).map(DefWithBody::from); },
ast::ConstDef(def) => { return Const::from_source(db, child.with_value(def)).map(DefWithBody::from); },
ast::StaticDef(def) => { Some(Static { id: ctx.to_def(&def) }.into()) },
ast::StaticDef(def) => { return Static::from_source(db, child.with_value(def)).map(DefWithBody::from); },
_ => { None },
}
}

View File

@ -2550,8 +2550,6 @@ fn test() {
[233; 246) 'GLOBAL_STATIC': u32
[256; 257) 'w': u32
[260; 277) 'GLOBAL...IC_MUT': u32
[118; 120) '99': u32
[161; 163) '99': u32
"###
);
}

View File

@ -63,11 +63,11 @@ impl Attrs {
AdtId::EnumId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db),
AdtId::UnionId(it) => attrs_from_ast(it.0.lookup_intern(db).ast_id, db),
},
AttrDefId::StaticId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db),
AttrDefId::TraitId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db),
AttrDefId::MacroDefId(it) => attrs_from_ast(it.ast_id, db),
AttrDefId::ImplId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db),
AttrDefId::ConstId(it) => attrs_from_loc(it.lookup(db), db),
AttrDefId::StaticId(it) => attrs_from_loc(it.lookup(db), db),
AttrDefId::FunctionId(it) => attrs_from_loc(it.lookup(db), db),
AttrDefId::TypeAliasId(it) => attrs_from_loc(it.lookup(db), db),
}

View File

@ -17,7 +17,7 @@ use crate::{
expr::{Expr, ExprId, Pat, PatId},
nameres::CrateDefMap,
path::Path,
AstItemDef, DefWithBodyId, HasModule, HasSource, Lookup, ModuleId,
DefWithBodyId, HasModule, HasSource, Lookup, ModuleId,
};
pub struct Expander {
@ -160,6 +160,7 @@ impl Body {
(src.file_id, c.module(db), src.value.body())
}
DefWithBodyId::StaticId(s) => {
let s = s.lookup(db);
let src = s.source(db);
(src.file_id, s.module(db), src.value.body())
}

View File

@ -204,7 +204,7 @@ impl ConstData {
}
pub(crate) fn static_data_query(db: &impl DefDatabase, konst: StaticId) -> Arc<ConstData> {
let node = konst.source(db).value;
let node = konst.lookup(db).source(db).value;
const_data_for(&node)
}
}

View File

@ -18,7 +18,8 @@ use crate::{
CrateDefMap,
},
AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, FunctionId, FunctionLoc, GenericDefId,
ImplId, ItemLoc, ModuleId, StaticId, StructOrUnionId, TraitId, TypeAliasId, TypeAliasLoc,
ImplId, ItemLoc, ModuleId, StaticId, StaticLoc, StructOrUnionId, TraitId, TypeAliasId,
TypeAliasLoc,
};
#[salsa::query_group(InternDatabaseStorage)]
@ -32,7 +33,7 @@ pub trait InternDatabase: SourceDatabase {
#[salsa::interned]
fn intern_const(&self, loc: ConstLoc) -> ConstId;
#[salsa::interned]
fn intern_static(&self, loc: ItemLoc<ast::StaticDef>) -> StaticId;
fn intern_static(&self, loc: StaticLoc) -> StaticId;
#[salsa::interned]
fn intern_trait(&self, loc: ItemLoc<ast::TraitDef>) -> TraitId;
#[salsa::interned]

View File

@ -52,10 +52,10 @@ impl Documentation {
let src = it.parent.child_source(db);
docs_from_ast(&src.value[it.local_id])
}
AttrDefId::StaticId(it) => docs_from_ast(&it.source(db).value),
AttrDefId::TraitId(it) => docs_from_ast(&it.source(db).value),
AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id.to_node(db)),
AttrDefId::ConstId(it) => docs_from_ast(&it.lookup(db).source(db).value),
AttrDefId::StaticId(it) => docs_from_ast(&it.lookup(db).source(db).value),
AttrDefId::FunctionId(it) => docs_from_ast(&it.lookup(db).source(db).value),
AttrDefId::TypeAliasId(it) => docs_from_ast(&it.lookup(db).source(db).value),
AttrDefId::ImplId(_) => None,

View File

@ -245,12 +245,24 @@ impl Lookup for ConstId {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StaticId(salsa::InternId);
impl_intern_key!(StaticId);
impl AstItemDef<ast::StaticDef> for StaticId {
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StaticDef>) -> Self {
db.intern_static(loc)
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StaticLoc {
pub container: ModuleId,
pub ast_id: AstId<ast::StaticDef>,
}
impl Intern for StaticLoc {
type ID = StaticId;
fn intern(self, db: &impl db::DefDatabase) -> StaticId {
db.intern_static(self)
}
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StaticDef> {
db.lookup_intern_static(self)
}
impl Lookup for StaticId {
type Data = StaticLoc;
fn lookup(&self, db: &impl db::DefDatabase) -> StaticLoc {
db.lookup_intern_static(*self)
}
}
@ -481,6 +493,12 @@ impl HasModule for ConstLoc {
}
}
impl HasModule for StaticLoc {
fn module(&self, _db: &impl db::DefDatabase) -> ModuleId {
self.container
}
}
pub trait HasSource {
type Value;
fn source(&self, db: &impl db::DefDatabase) -> Source<Self::Value>;
@ -513,6 +531,15 @@ impl HasSource for ConstLoc {
}
}
impl HasSource for StaticLoc {
type Value = ast::StaticDef;
fn source(&self, db: &impl db::DefDatabase) -> Source<ast::StaticDef> {
let node = self.ast_id.to_node(db);
Source::new(self.ast_id.file_id(), node)
}
}
pub trait HasChildSource {
type ChildId;
type Value;

View File

@ -21,7 +21,7 @@ use crate::{
path::{Path, PathKind},
per_ns::PerNs,
AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplId,
Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticId, StructId,
Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructId,
StructOrUnionId, TraitId, TypeAliasLoc, UnionId,
};
@ -715,7 +715,10 @@ where
PerNs::values(def.into())
}
raw::DefKind::Static(ast_id) => {
PerNs::values(StaticId::from_ast_id(ctx, ast_id).into())
let def = StaticLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db);
PerNs::values(def.into())
}
raw::DefKind::Trait(ast_id) => PerNs::types(TraitId::from_ast_id(ctx, ast_id).into()),
raw::DefKind::TypeAlias(ast_id) => {

View File

@ -540,7 +540,7 @@ impl HasResolver for ConstId {
impl HasResolver for StaticId {
fn resolver(self, db: &impl DefDatabase) -> Resolver {
self.module(db).resolver(db)
self.lookup(db).container.resolver(db)
}
}