Move traits to the new loc

This commit is contained in:
Aleksey Kladov 2019-12-12 14:34:03 +01:00
parent 7b0644d81e
commit 82e9b24558
16 changed files with 70 additions and 35 deletions

View file

@ -577,7 +577,7 @@ pub struct Trait {
impl Trait {
pub fn module(self, db: &impl DefDatabase) -> Module {
Module { id: self.id.module(db) }
Module { id: self.id.lookup(db).container }
}
pub fn name(self, db: &impl DefDatabase) -> Name {

View file

@ -44,8 +44,10 @@ impl FromSource for Enum {
impl FromSource for Trait {
type Ast = ast::TraitDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(Trait { 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::TRAIT].get(&src).copied().map(Trait::from)
}
}
impl FromSource for Function {

View file

@ -93,7 +93,7 @@ impl HasSource for Static {
impl HasSource for Trait {
type Ast = ast::TraitDef;
fn source(self, db: &impl DefDatabase) -> InFile<ast::TraitDef> {
self.id.source(db)
self.id.lookup(db).source(db)
}
}
impl HasSource for TypeAlias {

View file

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

View file

@ -94,6 +94,10 @@ impl ChildBySource for ModuleId {
let src = ty.lookup(db).source(db);
res[keys::TYPE_ALIAS].insert(src, ty)
}
ModuleDefId::TraitId(trait_) => {
let src = trait_.lookup(db).source(db);
res[keys::TRAIT].insert(src, trait_)
}
_ => (),
}
}

View file

@ -12,8 +12,8 @@ use crate::{
db::DefDatabase,
src::HasSource,
type_ref::{Mutability, TypeRef},
AssocItemId, AstItemDef, ConstId, ConstLoc, ContainerId, FunctionId, FunctionLoc, ImplId,
Intern, Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc,
AssocItemId, ConstId, ConstLoc, ContainerId, FunctionId, FunctionLoc, ImplId, Intern, Lookup,
StaticId, TraitId, TypeAliasId, TypeAliasLoc,
};
#[derive(Debug, Clone, PartialEq, Eq)]
@ -94,7 +94,7 @@ pub struct TraitData {
impl TraitData {
pub(crate) fn trait_data_query(db: &impl DefDatabase, tr: TraitId) -> Arc<TraitData> {
let src = tr.source(db);
let src = tr.lookup(db).source(db);
let name = src.value.name().map_or_else(Name::missing, |n| n.as_name());
let auto = src.value.is_auto();
let ast_id_map = db.ast_id_map(src.file_id);

View file

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

View file

@ -59,7 +59,7 @@ impl Documentation {
let src = it.parent.child_source(db);
docs_from_ast(&src.value[it.local_id])
}
AttrDefId::TraitId(it) => docs_from_ast(&it.source(db).value),
AttrDefId::TraitId(it) => docs_from_ast(&it.lookup(db).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),

View file

@ -86,7 +86,7 @@ impl GenericParams {
src.file_id
}
GenericDefId::TraitId(it) => {
let src = it.source(db);
let src = it.lookup(db).source(db);
// traits get the Self type as an implicit first type parameter
let self_param_id =

View file

@ -8,7 +8,8 @@ use rustc_hash::FxHashMap;
use crate::{
dyn_map::{DynMap, Policy},
ConstId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, TypeAliasId, TypeParamId,
ConstId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, TraitId, TypeAliasId,
TypeParamId,
};
type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>;
@ -18,6 +19,7 @@ pub const CONST: Key<ast::ConstDef, ConstId> = Key::new();
pub const STATIC: Key<ast::StaticDef, StaticId> = Key::new();
pub const TYPE_ALIAS: Key<ast::TypeAliasDef, TypeAliasId> = Key::new();
pub const IMPL: Key<ast::ImplBlock, ImplId> = Key::new();
pub const TRAIT: Key<ast::TraitDef, TraitId> = Key::new();
pub const ENUM_VARIANT: Key<ast::EnumVariant, EnumVariantId> = Key::new();
pub const TUPLE_FIELD: Key<ast::TupleFieldDef, StructFieldId> = Key::new();

View file

@ -253,12 +253,24 @@ impl Lookup for StaticId {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TraitId(salsa::InternId);
impl_intern_key!(TraitId);
impl AstItemDef<ast::TraitDef> for TraitId {
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TraitDef>) -> Self {
db.intern_trait(loc)
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TraitLoc {
pub container: ModuleId,
pub ast_id: AstId<ast::TraitDef>,
}
impl Intern for TraitLoc {
type ID = TraitId;
fn intern(self, db: &impl db::DefDatabase) -> TraitId {
db.intern_trait(self)
}
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TraitDef> {
db.lookup_intern_trait(self)
}
impl Lookup for TraitId {
type Data = TraitLoc;
fn lookup(&self, db: &impl db::DefDatabase) -> TraitLoc {
db.lookup_intern_trait(*self)
}
}
@ -492,7 +504,7 @@ impl HasModule for FunctionLoc {
match self.container {
ContainerId::ModuleId(it) => it,
ContainerId::ImplId(it) => it.lookup(db).container,
ContainerId::TraitId(it) => it.module(db),
ContainerId::TraitId(it) => it.lookup(db).container,
}
}
}
@ -502,7 +514,7 @@ impl HasModule for TypeAliasLoc {
match self.container {
ContainerId::ModuleId(it) => it,
ContainerId::ImplId(it) => it.lookup(db).container,
ContainerId::TraitId(it) => it.module(db),
ContainerId::TraitId(it) => it.lookup(db).container,
}
}
}
@ -512,7 +524,7 @@ impl HasModule for ConstLoc {
match self.container {
ContainerId::ModuleId(it) => it,
ContainerId::ImplId(it) => it.lookup(db).container,
ContainerId::TraitId(it) => it.module(db),
ContainerId::TraitId(it) => it.lookup(db).container,
}
}
}
@ -542,7 +554,7 @@ impl HasModule for GenericDefId {
match self {
GenericDefId::FunctionId(it) => it.lookup(db).module(db),
GenericDefId::AdtId(it) => it.module(db),
GenericDefId::TraitId(it) => it.module(db),
GenericDefId::TraitId(it) => it.lookup(db).container,
GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
GenericDefId::ImplId(it) => it.lookup(db).container,
GenericDefId::EnumVariantId(it) => it.parent.module(db),

View file

@ -26,7 +26,7 @@ use crate::{
per_ns::PerNs,
AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplLoc,
Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructId,
TraitId, TypeAliasLoc, UnionId,
TraitLoc, TypeAliasLoc, UnionId,
};
pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap {
@ -796,7 +796,12 @@ where
PerNs::values(def.into())
}
raw::DefKind::Trait(ast_id) => PerNs::types(TraitId::from_ast_id(ctx, ast_id).into()),
raw::DefKind::Trait(ast_id) => {
let def = TraitLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db);
PerNs::types(def.into())
}
raw::DefKind::TypeAlias(ast_id) => {
let def = TypeAliasLoc {
container: ContainerId::ModuleId(module),

View file

@ -17,9 +17,9 @@ use crate::{
nameres::{BuiltinShadowMode, CrateDefMap},
path::{Path, PathKind},
per_ns::PerNs,
AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId,
GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId,
StructId, TraitId, TypeAliasId, TypeParamId, VariantId,
AdtId, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId,
HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId,
TypeAliasId, TypeParamId, VariantId,
};
#[derive(Debug, Clone, Default)]
@ -524,7 +524,7 @@ impl HasResolver for ModuleId {
impl HasResolver for TraitId {
fn resolver(self, db: &impl DefDatabase) -> Resolver {
self.module(db).resolver(db).push_generic_params_scope(db, self.into())
self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into())
}
}

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, ImplLoc, StaticLoc, TypeAliasLoc};
use crate::{db::DefDatabase, ConstLoc, FunctionLoc, ImplLoc, StaticLoc, TraitLoc, TypeAliasLoc};
pub trait HasSource {
type Value;
@ -56,6 +56,15 @@ impl HasSource for ImplLoc {
}
}
impl HasSource for TraitLoc {
type Value = ast::TraitDef;
fn source(&self, db: &impl DefDatabase) -> InFile<ast::TraitDef> {
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

@ -367,6 +367,9 @@ mod tests {
BuiltinFnLikeExpander::FormatArgs,
);
assert_eq!(expanded, r#"std::fmt::Arguments::new_v1(&[] ,&[std::fmt::ArgumentV1::new(&(arg1(a,b,c)),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(arg2),std::fmt::Display::fmt),])"#);
assert_eq!(
expanded,
r#"std::fmt::Arguments::new_v1(&[] ,&[std::fmt::ArgumentV1::new(&(arg1(a,b,c)),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(arg2),std::fmt::Display::fmt),])"#
);
}
}

View file

@ -9,9 +9,7 @@ use chalk_ir::{
};
use chalk_rust_ir::{AssociatedTyDatum, AssociatedTyValue, ImplDatum, StructDatum, TraitDatum};
use hir_def::{
AssocItemId, AstItemDef, ContainerId, GenericDefId, ImplId, Lookup, TraitId, TypeAliasId,
};
use hir_def::{AssocItemId, ContainerId, GenericDefId, ImplId, Lookup, TraitId, TypeAliasId};
use ra_db::{
salsa::{InternId, InternKey},
CrateId,
@ -593,7 +591,7 @@ pub(crate) fn trait_datum_query(
let bound_vars = Substs::bound_vars(&generic_params);
let flags = chalk_rust_ir::TraitFlags {
auto: trait_data.auto,
upstream: trait_.module(db).krate != krate,
upstream: trait_.lookup(db).container.krate != krate,
non_enumerable: true,
coinductive: false, // only relevant for Chalk testing
// FIXME set these flags correctly