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

View File

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