Skip to content

Commit eb9ba45

Browse files
bors[bot]Veykril
andauthored
Merge #6879
6879: Change HasChildSource::ChildId assoc item to generic param r=matklad a=Veykril Co-authored-by: Lukas Wirth <[email protected]>
2 parents bd4c352 + 2c67a4a commit eb9ba45

File tree

4 files changed

+37
-23
lines changed

4 files changed

+37
-23
lines changed

crates/hir/src/has_source.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,14 @@ impl HasSource for TypeParam {
129129
type Ast = Either<ast::Trait, ast::TypeParam>;
130130
fn source(self, db: &dyn HirDatabase) -> InFile<Self::Ast> {
131131
let child_source = self.id.parent.child_source(db.upcast());
132-
child_source.map(|it| it.type_params[self.id.local_id].clone())
132+
child_source.map(|it| it[self.id.local_id].clone())
133133
}
134134
}
135135

136136
impl HasSource for LifetimeParam {
137137
type Ast = ast::LifetimeParam;
138138
fn source(self, db: &dyn HirDatabase) -> InFile<Self::Ast> {
139139
let child_source = self.id.parent.child_source(db.upcast());
140-
child_source.map(|it| it.lifetime_params[self.id.local_id].clone())
140+
child_source.map(|it| it[self.id.local_id].clone())
141141
}
142142
}

crates/hir_def/src/adt.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,12 @@ impl EnumData {
145145
}
146146
}
147147

148-
impl HasChildSource for EnumId {
149-
type ChildId = LocalEnumVariantId;
148+
impl HasChildSource<LocalEnumVariantId> for EnumId {
150149
type Value = ast::Variant;
151-
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> {
150+
fn child_source(
151+
&self,
152+
db: &dyn DefDatabase,
153+
) -> InFile<ArenaMap<LocalEnumVariantId, Self::Value>> {
152154
let src = self.lookup(db).source(db);
153155
let mut trace = Trace::new_for_map();
154156
lower_enum(db, &mut trace, &src, self.lookup(db).container.module(db));
@@ -212,11 +214,10 @@ impl VariantData {
212214
}
213215
}
214216

215-
impl HasChildSource for VariantId {
216-
type ChildId = LocalFieldId;
217+
impl HasChildSource<LocalFieldId> for VariantId {
217218
type Value = Either<ast::TupleField, ast::RecordField>;
218219

219-
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> {
220+
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<LocalFieldId, Self::Value>> {
220221
let (src, module_id) = match self {
221222
VariantId::EnumVariantId(it) => {
222223
// I don't really like the fact that we call into parent source

crates/hir_def/src/generics.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
db::DefDatabase,
2020
dyn_map::DynMap,
2121
keys,
22-
src::HasSource,
22+
src::{HasChildSource, HasSource},
2323
type_ref::{LifetimeRef, TypeBound, TypeRef},
2424
AdtId, GenericDefId, LifetimeParamId, LocalLifetimeParamId, LocalTypeParamId, Lookup,
2525
TypeParamId,
@@ -73,9 +73,9 @@ pub enum WherePredicateTypeTarget {
7373
}
7474

7575
#[derive(Default)]
76-
pub struct SourceMaps {
77-
pub type_params: ArenaMap<LocalTypeParamId, Either<ast::Trait, ast::TypeParam>>,
78-
pub lifetime_params: ArenaMap<LocalLifetimeParamId, ast::LifetimeParam>,
76+
pub(crate) struct SourceMap {
77+
pub(crate) type_params: ArenaMap<LocalTypeParamId, Either<ast::Trait, ast::TypeParam>>,
78+
lifetime_params: ArenaMap<LocalLifetimeParamId, ast::LifetimeParam>,
7979
}
8080

8181
impl GenericParams {
@@ -133,9 +133,9 @@ impl GenericParams {
133133
Arc::new(generics)
134134
}
135135

136-
fn new(db: &dyn DefDatabase, def: GenericDefId) -> (GenericParams, InFile<SourceMaps>) {
136+
fn new(db: &dyn DefDatabase, def: GenericDefId) -> (GenericParams, InFile<SourceMap>) {
137137
let mut generics = GenericParams::default();
138-
let mut sm = SourceMaps::default();
138+
let mut sm = SourceMap::default();
139139

140140
// FIXME: add `: Sized` bound for everything except for `Self` in traits
141141
let file_id = match def {
@@ -214,7 +214,7 @@ impl GenericParams {
214214
pub(crate) fn fill(
215215
&mut self,
216216
lower_ctx: &LowerCtx,
217-
sm: &mut SourceMaps,
217+
sm: &mut SourceMap,
218218
node: &dyn GenericParamsOwner,
219219
) {
220220
if let Some(params) = node.generic_param_list() {
@@ -241,7 +241,7 @@ impl GenericParams {
241241
fn fill_params(
242242
&mut self,
243243
lower_ctx: &LowerCtx,
244-
sm: &mut SourceMaps,
244+
sm: &mut SourceMap,
245245
params: ast::GenericParamList,
246246
) {
247247
for type_param in params.type_params() {
@@ -345,10 +345,24 @@ impl GenericParams {
345345
})
346346
}
347347
}
348-
impl GenericDefId {
349-
// FIXME: Change HasChildSource's ChildId AssocItem to be a generic parameter instead
350-
pub fn child_source(&self, db: &dyn DefDatabase) -> InFile<SourceMaps> {
351-
GenericParams::new(db, *self).1
348+
349+
impl HasChildSource<LocalTypeParamId> for GenericDefId {
350+
type Value = Either<ast::Trait, ast::TypeParam>;
351+
fn child_source(
352+
&self,
353+
db: &dyn DefDatabase,
354+
) -> InFile<ArenaMap<LocalTypeParamId, Self::Value>> {
355+
GenericParams::new(db, *self).1.map(|source_maps| source_maps.type_params)
356+
}
357+
}
358+
359+
impl HasChildSource<LocalLifetimeParamId> for GenericDefId {
360+
type Value = ast::LifetimeParam;
361+
fn child_source(
362+
&self,
363+
db: &dyn DefDatabase,
364+
) -> InFile<ArenaMap<LocalLifetimeParamId, Self::Value>> {
365+
GenericParams::new(db, *self).1.map(|source_maps| source_maps.lifetime_params)
352366
}
353367
}
354368

crates/hir_def/src/src.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ impl<N: ItemTreeNode> HasSource for ItemLoc<N> {
3636
}
3737
}
3838

39-
pub trait HasChildSource {
40-
type ChildId;
39+
pub trait HasChildSource<ChildId> {
4140
type Value;
42-
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>>;
41+
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<ChildId, Self::Value>>;
4342
}

0 commit comments

Comments
 (0)