Switch to the new location for impls

This commit is contained in:
Aleksey Kladov 2019-12-12 14:09:13 +01:00
parent b0739d5a26
commit 7b0644d81e
16 changed files with 69 additions and 32 deletions

1
Cargo.lock generated
View file

@ -1123,6 +1123,7 @@ dependencies = [
"rowan 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_lexer 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)",
"smol_str 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
"test_utils 0.1.0",
"walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -809,7 +809,10 @@ impl ImplBlock {
let resolver = self.id.resolver(db);
let environment = TraitEnvironment::lower(db, &resolver);
let ty = Ty::from_hir(db, &resolver, &impl_data.target_type);
Type { krate: self.id.module(db).krate, ty: InEnvironment { value: ty, environment } }
Type {
krate: self.id.lookup(db).container.krate,
ty: InEnvironment { value: ty, environment },
}
}
pub fn items(&self, db: &impl DefDatabase) -> Vec<AssocItem> {
@ -821,7 +824,7 @@ impl ImplBlock {
}
pub fn module(&self, db: &impl DefDatabase) -> Module {
self.id.module(db).into()
self.id.lookup(db).container.into()
}
pub fn krate(&self, db: &impl DefDatabase) -> Crate {

View file

@ -107,8 +107,10 @@ impl FromSource for MacroDef {
impl FromSource for ImplBlock {
type Ast = ast::ImplBlock;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(ImplBlock { id })
// XXX: use `.parent()` to avoid finding ourselves
let parent = src.value.syntax().parent()?;
let container = Container::find(db, src.with_value(parent).as_ref())?;
container.child_by_source(db)[keys::IMPL].get(&src).copied().map(ImplBlock::from)
}
}

View file

@ -114,7 +114,7 @@ impl HasSource for MacroDef {
impl HasSource for ImplBlock {
type Ast = ast::ImplBlock;
fn source(self, db: &impl DefDatabase) -> InFile<ast::ImplBlock> {
self.id.source(db)
self.id.lookup(db).source(db)
}
}
impl HasSource for Import {

View file

@ -64,7 +64,7 @@ impl Attrs {
AttrDefId::MacroDefId(it) => {
it.ast_id.map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db))
}
AttrDefId::ImplId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db),
AttrDefId::ImplId(it) => attrs_from_loc(it.lookup(db), 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),

View file

@ -98,6 +98,11 @@ impl ChildBySource for ModuleId {
}
}
for &impl_ in crate_def_map[self.local_id].impls.iter() {
let src = impl_.lookup(db).source(db);
res[keys::IMPL].insert(src, impl_)
}
res
}
}

View file

@ -167,7 +167,7 @@ pub struct ImplData {
impl ImplData {
pub(crate) fn impl_data_query(db: &impl DefDatabase, id: ImplId) -> Arc<ImplData> {
let src = id.source(db);
let src = id.lookup(db).source(db);
let items = db.ast_id_map(src.file_id);
let target_trait = src.value.target_trait().map(TypeRef::from_ast);

View file

@ -18,8 +18,8 @@ use crate::{
CrateDefMap,
},
AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, FunctionId, FunctionLoc, GenericDefId,
ImplId, ItemLoc, ModuleId, StaticId, StaticLoc, StructId, TraitId, TypeAliasId, TypeAliasLoc,
UnionId,
ImplId, ImplLoc, ItemLoc, ModuleId, StaticId, StaticLoc, StructId, TraitId, TypeAliasId,
TypeAliasLoc, UnionId,
};
#[salsa::query_group(InternDatabaseStorage)]
@ -41,7 +41,7 @@ pub trait InternDatabase: SourceDatabase {
#[salsa::interned]
fn intern_type_alias(&self, loc: TypeAliasLoc) -> TypeAliasId;
#[salsa::interned]
fn intern_impl(&self, loc: ItemLoc<ast::ImplBlock>) -> ImplId;
fn intern_impl(&self, loc: ImplLoc) -> ImplId;
}
#[salsa::query_group(DefDatabaseStorage)]

View file

@ -109,7 +109,7 @@ impl GenericParams {
// type-parameter, but rather is a type-alias for impl's target
// type, so this is handled by the resolver.
GenericDefId::ImplId(it) => {
let src = it.source(db);
let src = it.lookup(db).source(db);
generics.fill(&mut sm, &src.value);
src.file_id
}

View file

@ -8,7 +8,7 @@ use rustc_hash::FxHashMap;
use crate::{
dyn_map::{DynMap, Policy},
ConstId, EnumVariantId, FunctionId, StaticId, StructFieldId, TypeAliasId, TypeParamId,
ConstId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, TypeAliasId, TypeParamId,
};
type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>;
@ -16,8 +16,10 @@ type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>;
pub const FUNCTION: Key<ast::FnDef, FunctionId> = Key::new();
pub const CONST: Key<ast::ConstDef, ConstId> = Key::new();
pub const STATIC: Key<ast::StaticDef, StaticId> = Key::new();
pub const ENUM_VARIANT: Key<ast::EnumVariant, EnumVariantId> = Key::new();
pub const TYPE_ALIAS: Key<ast::TypeAliasDef, TypeAliasId> = Key::new();
pub const IMPL: Key<ast::ImplBlock, ImplId> = Key::new();
pub const ENUM_VARIANT: Key<ast::EnumVariant, EnumVariantId> = Key::new();
pub const TUPLE_FIELD: Key<ast::TupleFieldDef, StructFieldId> = Key::new();
pub const RECORD_FIELD: Key<ast::RecordFieldDef, StructFieldId> = Key::new();
pub const TYPE_PARAM: Key<ast::TypeParam, TypeParamId> = Key::new();

View file

@ -289,12 +289,24 @@ impl Lookup for TypeAliasId {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ImplId(salsa::InternId);
impl_intern_key!(ImplId);
impl AstItemDef<ast::ImplBlock> for ImplId {
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::ImplBlock>) -> Self {
db.intern_impl(loc)
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ImplLoc {
pub container: ModuleId,
pub ast_id: AstId<ast::ImplBlock>,
}
impl Intern for ImplLoc {
type ID = ImplId;
fn intern(self, db: &impl db::DefDatabase) -> ImplId {
db.intern_impl(self)
}
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::ImplBlock> {
db.lookup_intern_impl(self)
}
impl Lookup for ImplId {
type Data = ImplLoc;
fn lookup(&self, db: &impl db::DefDatabase) -> ImplLoc {
db.lookup_intern_impl(*self)
}
}
@ -479,7 +491,7 @@ impl HasModule for FunctionLoc {
fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
match self.container {
ContainerId::ModuleId(it) => it,
ContainerId::ImplId(it) => it.module(db),
ContainerId::ImplId(it) => it.lookup(db).container,
ContainerId::TraitId(it) => it.module(db),
}
}
@ -489,7 +501,7 @@ impl HasModule for TypeAliasLoc {
fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
match self.container {
ContainerId::ModuleId(it) => it,
ContainerId::ImplId(it) => it.module(db),
ContainerId::ImplId(it) => it.lookup(db).container,
ContainerId::TraitId(it) => it.module(db),
}
}
@ -499,7 +511,7 @@ impl HasModule for ConstLoc {
fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
match self.container {
ContainerId::ModuleId(it) => it,
ContainerId::ImplId(it) => it.module(db),
ContainerId::ImplId(it) => it.lookup(db).container,
ContainerId::TraitId(it) => it.module(db),
}
}
@ -532,7 +544,7 @@ impl HasModule for GenericDefId {
GenericDefId::AdtId(it) => it.module(db),
GenericDefId::TraitId(it) => it.module(db),
GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
GenericDefId::ImplId(it) => it.module(db),
GenericDefId::ImplId(it) => it.lookup(db).container,
GenericDefId::EnumVariantId(it) => it.parent.module(db),
GenericDefId::ConstId(it) => it.lookup(db).module(db),
}

View file

@ -24,7 +24,7 @@ use crate::{
},
path::{Path, PathKind},
per_ns::PerNs,
AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplId,
AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplLoc,
Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructId,
TraitId, TypeAliasLoc, UnionId,
};
@ -661,9 +661,11 @@ where
krate: self.def_collector.def_map.krate,
local_id: self.module_id,
};
let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id);
let imp_id = ImplId::from_ast_id(ctx, self.raw_items[imp].ast_id);
self.def_collector.def_map.modules[self.module_id].impls.push(imp_id)
let ast_id = self.raw_items[imp].ast_id;
let impl_id =
ImplLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db);
self.def_collector.def_map.modules[self.module_id].impls.push(impl_id)
}
}
}

View file

@ -564,7 +564,8 @@ impl HasResolver for TypeAliasId {
impl HasResolver for ImplId {
fn resolver(self, db: &impl DefDatabase) -> Resolver {
self.module(db)
self.lookup(db)
.container
.resolver(db)
.push_generic_params_scope(db, self.into())
.push_impl_block_scope(self)

View file

@ -4,7 +4,7 @@ use hir_expand::InFile;
use ra_arena::map::ArenaMap;
use ra_syntax::ast;
use crate::{db::DefDatabase, ConstLoc, FunctionLoc, StaticLoc, TypeAliasLoc};
use crate::{db::DefDatabase, ConstLoc, FunctionLoc, ImplLoc, StaticLoc, TypeAliasLoc};
pub trait HasSource {
type Value;
@ -47,6 +47,15 @@ impl HasSource for StaticLoc {
}
}
impl HasSource for ImplLoc {
type Value = ast::ImplBlock;
fn source(&self, db: &impl DefDatabase) -> InFile<ast::ImplBlock> {
let node = self.ast_id.to_node(db);
InFile::new(self.ast_id.file_id, node)
}
}
pub trait HasChildSource {
type ChildId;
type Value;

View file

@ -6,8 +6,8 @@ use std::sync::Arc;
use arrayvec::ArrayVec;
use hir_def::{
lang_item::LangItemTarget, resolver::Resolver, type_ref::Mutability, AssocItemId, AstItemDef,
FunctionId, HasModule, ImplId, Lookup, TraitId,
lang_item::LangItemTarget, resolver::Resolver, type_ref::Mutability, AssocItemId, FunctionId,
HasModule, ImplId, Lookup, TraitId,
};
use hir_expand::name::Name;
use ra_db::CrateId;
@ -134,7 +134,7 @@ impl Ty {
LangItemTarget::ImplBlockId(it) => Some(it),
_ => None,
})
.map(|it| it.module(db).krate)
.map(|it| it.lookup(db).container.krate)
.collect();
Some(res)
}

View file

@ -673,7 +673,7 @@ fn impl_block_datum(
let bound_vars = Substs::bound_vars(&generic_params);
let trait_ref = trait_ref.subst(&bound_vars);
let trait_ = trait_ref.trait_;
let impl_type = if impl_id.module(db).krate == krate {
let impl_type = if impl_id.lookup(db).container.krate == krate {
chalk_rust_ir::ImplType::Local
} else {
chalk_rust_ir::ImplType::External