From 2c67a4abe47ead6a00ce1b33ccc3d3bfcaaf0983 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 14 Dec 2020 17:11:27 +0100 Subject: [PATCH] Change HasChildSource::ChildId assoc item to generic param --- crates/hir/src/has_source.rs | 4 ++-- crates/hir_def/src/adt.rs | 13 ++++++------ crates/hir_def/src/generics.rs | 38 +++++++++++++++++++++++----------- crates/hir_def/src/src.rs | 5 ++--- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/crates/hir/src/has_source.rs b/crates/hir/src/has_source.rs index 1e64a1614b3..552c8e433e5 100644 --- a/crates/hir/src/has_source.rs +++ b/crates/hir/src/has_source.rs @@ -129,7 +129,7 @@ impl HasSource for TypeParam { type Ast = Either; fn source(self, db: &dyn HirDatabase) -> InFile { 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 { 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()) } } diff --git a/crates/hir_def/src/adt.rs b/crates/hir_def/src/adt.rs index 6539959c3c3..eafa3abb6ab 100644 --- a/crates/hir_def/src/adt.rs +++ b/crates/hir_def/src/adt.rs @@ -145,10 +145,12 @@ impl EnumData { } } -impl HasChildSource for EnumId { - type ChildId = LocalEnumVariantId; +impl HasChildSource for EnumId { type Value = ast::Variant; - fn child_source(&self, db: &dyn DefDatabase) -> InFile> { + fn child_source( + &self, + db: &dyn DefDatabase, + ) -> InFile> { 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 for VariantId { type Value = Either; - fn child_source(&self, db: &dyn DefDatabase) -> InFile> { + fn child_source(&self, db: &dyn DefDatabase) -> InFile> { let (src, module_id) = match self { VariantId::EnumVariantId(it) => { // I don't really like the fact that we call into parent source diff --git a/crates/hir_def/src/generics.rs b/crates/hir_def/src/generics.rs index 81912a4546f..924046435b8 100644 --- a/crates/hir_def/src/generics.rs +++ b/crates/hir_def/src/generics.rs @@ -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>, - pub lifetime_params: ArenaMap, +pub(crate) struct SourceMap { + pub(crate) type_params: ArenaMap>, + lifetime_params: ArenaMap, } impl GenericParams { @@ -133,9 +133,9 @@ impl GenericParams { Arc::new(generics) } - fn new(db: &dyn DefDatabase, def: GenericDefId) -> (GenericParams, InFile) { + fn new(db: &dyn DefDatabase, def: GenericDefId) -> (GenericParams, InFile) { 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 { - GenericParams::new(db, *self).1 + +impl HasChildSource for GenericDefId { + type Value = Either; + fn child_source( + &self, + db: &dyn DefDatabase, + ) -> InFile> { + GenericParams::new(db, *self).1.map(|source_maps| source_maps.type_params) + } +} + +impl HasChildSource for GenericDefId { + type Value = ast::LifetimeParam; + fn child_source( + &self, + db: &dyn DefDatabase, + ) -> InFile> { + GenericParams::new(db, *self).1.map(|source_maps| source_maps.lifetime_params) } } diff --git a/crates/hir_def/src/src.rs b/crates/hir_def/src/src.rs index 7a79b031499..f67244b4647 100644 --- a/crates/hir_def/src/src.rs +++ b/crates/hir_def/src/src.rs @@ -36,8 +36,7 @@ impl HasSource for ItemLoc { } } -pub trait HasChildSource { - type ChildId; +pub trait HasChildSource { type Value; - fn child_source(&self, db: &dyn DefDatabase) -> InFile>; + fn child_source(&self, db: &dyn DefDatabase) -> InFile>; }