6879: Change HasChildSource::ChildId assoc item to generic param r=matklad a=Veykril



Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2020-12-15 17:22:03 +00:00 committed by GitHub
commit eb9ba457b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 23 deletions

View File

@ -129,7 +129,7 @@ impl HasSource for TypeParam {
type Ast = Either<ast::Trait, ast::TypeParam>;
fn source(self, db: &dyn HirDatabase) -> InFile<Self::Ast> {
let child_source = self.id.parent.child_source(db.upcast());
child_source.map(|it| it.type_params[self.id.local_id].clone())
child_source.map(|it| it[self.id.local_id].clone())
}
}
@ -137,6 +137,6 @@ impl HasSource for LifetimeParam {
type Ast = ast::LifetimeParam;
fn source(self, db: &dyn HirDatabase) -> InFile<Self::Ast> {
let child_source = self.id.parent.child_source(db.upcast());
child_source.map(|it| it.lifetime_params[self.id.local_id].clone())
child_source.map(|it| it[self.id.local_id].clone())
}
}

View File

@ -145,10 +145,12 @@ impl EnumData {
}
}
impl HasChildSource for EnumId {
type ChildId = LocalEnumVariantId;
impl HasChildSource<LocalEnumVariantId> for EnumId {
type Value = ast::Variant;
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> {
fn child_source(
&self,
db: &dyn DefDatabase,
) -> InFile<ArenaMap<LocalEnumVariantId, Self::Value>> {
let src = self.lookup(db).source(db);
let mut trace = Trace::new_for_map();
lower_enum(db, &mut trace, &src, self.lookup(db).container.module(db));
@ -212,11 +214,10 @@ impl VariantData {
}
}
impl HasChildSource for VariantId {
type ChildId = LocalFieldId;
impl HasChildSource<LocalFieldId> for VariantId {
type Value = Either<ast::TupleField, ast::RecordField>;
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> {
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<LocalFieldId, Self::Value>> {
let (src, module_id) = match self {
VariantId::EnumVariantId(it) => {
// I don't really like the fact that we call into parent source

View File

@ -19,7 +19,7 @@ use crate::{
db::DefDatabase,
dyn_map::DynMap,
keys,
src::HasSource,
src::{HasChildSource, HasSource},
type_ref::{LifetimeRef, TypeBound, TypeRef},
AdtId, GenericDefId, LifetimeParamId, LocalLifetimeParamId, LocalTypeParamId, Lookup,
TypeParamId,
@ -73,9 +73,9 @@ pub enum WherePredicateTypeTarget {
}
#[derive(Default)]
pub struct SourceMaps {
pub type_params: ArenaMap<LocalTypeParamId, Either<ast::Trait, ast::TypeParam>>,
pub lifetime_params: ArenaMap<LocalLifetimeParamId, ast::LifetimeParam>,
pub(crate) struct SourceMap {
pub(crate) type_params: ArenaMap<LocalTypeParamId, Either<ast::Trait, ast::TypeParam>>,
lifetime_params: ArenaMap<LocalLifetimeParamId, ast::LifetimeParam>,
}
impl GenericParams {
@ -133,9 +133,9 @@ impl GenericParams {
Arc::new(generics)
}
fn new(db: &dyn DefDatabase, def: GenericDefId) -> (GenericParams, InFile<SourceMaps>) {
fn new(db: &dyn DefDatabase, def: GenericDefId) -> (GenericParams, InFile<SourceMap>) {
let mut generics = GenericParams::default();
let mut sm = SourceMaps::default();
let mut sm = SourceMap::default();
// FIXME: add `: Sized` bound for everything except for `Self` in traits
let file_id = match def {
@ -214,7 +214,7 @@ impl GenericParams {
pub(crate) fn fill(
&mut self,
lower_ctx: &LowerCtx,
sm: &mut SourceMaps,
sm: &mut SourceMap,
node: &dyn GenericParamsOwner,
) {
if let Some(params) = node.generic_param_list() {
@ -241,7 +241,7 @@ impl GenericParams {
fn fill_params(
&mut self,
lower_ctx: &LowerCtx,
sm: &mut SourceMaps,
sm: &mut SourceMap,
params: ast::GenericParamList,
) {
for type_param in params.type_params() {
@ -345,10 +345,24 @@ impl GenericParams {
})
}
}
impl GenericDefId {
// FIXME: Change HasChildSource's ChildId AssocItem to be a generic parameter instead
pub fn child_source(&self, db: &dyn DefDatabase) -> InFile<SourceMaps> {
GenericParams::new(db, *self).1
impl HasChildSource<LocalTypeParamId> for GenericDefId {
type Value = Either<ast::Trait, ast::TypeParam>;
fn child_source(
&self,
db: &dyn DefDatabase,
) -> InFile<ArenaMap<LocalTypeParamId, Self::Value>> {
GenericParams::new(db, *self).1.map(|source_maps| source_maps.type_params)
}
}
impl HasChildSource<LocalLifetimeParamId> for GenericDefId {
type Value = ast::LifetimeParam;
fn child_source(
&self,
db: &dyn DefDatabase,
) -> InFile<ArenaMap<LocalLifetimeParamId, Self::Value>> {
GenericParams::new(db, *self).1.map(|source_maps| source_maps.lifetime_params)
}
}

View File

@ -36,8 +36,7 @@ impl<N: ItemTreeNode> HasSource for ItemLoc<N> {
}
}
pub trait HasChildSource {
type ChildId;
pub trait HasChildSource<ChildId> {
type Value;
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>>;
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<ChildId, Self::Value>>;
}