Skip to content

Commit 1a0da6d

Browse files
committed
Use TypeAliasId in Ty, pt 2
1 parent 6d2ec87 commit 1a0da6d

File tree

5 files changed

+40
-46
lines changed

5 files changed

+40
-46
lines changed

crates/ra_hir/src/ty.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ use std::{fmt, iter, mem};
1919

2020
use hir_def::{
2121
generics::GenericParams, AdtId, ContainerId, DefWithBodyId, GenericDefId, HasModule, Lookup,
22-
TypeAliasId,
22+
TraitId, TypeAliasId,
2323
};
2424
use ra_db::{impl_intern_key, salsa};
2525

2626
use crate::{
2727
db::HirDatabase, expr::ExprId, util::make_mut_slice, Adt, Crate, FloatTy, IntTy, Mutability,
28-
Name, Trait, TypeAlias, Uncertain,
28+
Name, Trait, Uncertain,
2929
};
3030
use display::{HirDisplay, HirFormatter};
3131

@@ -218,18 +218,19 @@ pub struct ApplicationTy {
218218
/// trait and all its parameters are fully known.
219219
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
220220
pub struct ProjectionTy {
221-
pub associated_ty: TypeAlias,
221+
pub associated_ty: TypeAliasId,
222222
pub parameters: Substs,
223223
}
224224

225225
impl ProjectionTy {
226226
pub fn trait_ref(&self, db: &impl HirDatabase) -> TraitRef {
227-
TraitRef {
228-
trait_: self
229-
.associated_ty
230-
.parent_trait(db)
231-
.expect("projection ty without parent trait"),
232-
substs: self.parameters.clone(),
227+
TraitRef { trait_: self.trait_(db).into(), substs: self.parameters.clone() }
228+
}
229+
230+
fn trait_(&self, db: &impl HirDatabase) -> TraitId {
231+
match self.associated_ty.lookup(db).container {
232+
ContainerId::TraitId(it) => it,
233+
_ => panic!("projection ty without parent trait"),
233234
}
234235
}
235236
}
@@ -933,18 +934,15 @@ impl HirDisplay for ProjectionTy {
933934
return write!(f, "…");
934935
}
935936

936-
let trait_name = self
937-
.associated_ty
938-
.parent_trait(f.db)
939-
.and_then(|t| t.name(f.db))
940-
.unwrap_or_else(Name::missing);
937+
let trait_name =
938+
f.db.trait_data(self.trait_(f.db)).name.clone().unwrap_or_else(Name::missing);
941939
write!(f, "<{} as {}", self.parameters[0].display(f.db), trait_name,)?;
942940
if self.parameters.len() > 1 {
943941
write!(f, "<")?;
944942
f.write_joined(&self.parameters[1..], ", ")?;
945943
write!(f, ">")?;
946944
}
947-
write!(f, ">::{}", self.associated_ty.name(f.db))?;
945+
write!(f, ">::{}", f.db.type_alias_data(self.associated_ty).name)?;
948946
Ok(())
949947
}
950948
}
@@ -1007,7 +1005,10 @@ impl HirDisplay for Ty {
10071005
write!(f, "<")?;
10081006
angle_open = true;
10091007
}
1010-
let name = projection_pred.projection_ty.associated_ty.name(f.db);
1008+
let name =
1009+
f.db.type_alias_data(projection_pred.projection_ty.associated_ty)
1010+
.name
1011+
.clone();
10111012
write!(f, "{} = ", name)?;
10121013
projection_pred.ty.hir_fmt(f)?;
10131014
}
@@ -1083,7 +1084,7 @@ impl HirDisplay for GenericPredicate {
10831084
write!(
10841085
f,
10851086
">::{} = {}",
1086-
projection_pred.projection_ty.associated_ty.name(f.db),
1087+
f.db.type_alias_data(projection_pred.projection_ty.associated_ty).name,
10871088
projection_pred.ty.display(f.db)
10881089
)?;
10891090
}

crates/ra_hir/src/ty/autoderef.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn deref_by_trait(
6969

7070
let projection = super::traits::ProjectionPredicate {
7171
ty: Ty::Bound(0),
72-
projection_ty: super::ProjectionTy { associated_ty: target, parameters },
72+
projection_ty: super::ProjectionTy { associated_ty: target.id, parameters },
7373
};
7474

7575
let obligation = super::Obligation::Projection(projection);

crates/ra_hir/src/ty/infer/expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
101101
let projection = ProjectionPredicate {
102102
ty: pat_ty.clone(),
103103
projection_ty: ProjectionTy {
104-
associated_ty: into_iter_item_alias,
104+
associated_ty: into_iter_item_alias.id,
105105
parameters: Substs::single(iterable_ty),
106106
},
107107
};
@@ -283,7 +283,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
283283
let projection = ProjectionPredicate {
284284
ty: ty.clone(),
285285
projection_ty: ProjectionTy {
286-
associated_ty: future_future_output_alias,
286+
associated_ty: future_future_output_alias.id,
287287
parameters: Substs::single(inner_ty),
288288
},
289289
};
@@ -302,7 +302,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
302302
let projection = ProjectionPredicate {
303303
ty: ty.clone(),
304304
projection_ty: ProjectionTy {
305-
associated_ty: ops_try_ok_alias,
305+
associated_ty: ops_try_ok_alias.id,
306306
parameters: Substs::single(inner_ty),
307307
},
308308
};

crates/ra_hir/src/ty/lower.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl Ty {
176176
Some(associated_ty) => {
177177
// FIXME handle type parameters on the segment
178178
Ty::Projection(ProjectionTy {
179-
associated_ty,
179+
associated_ty: associated_ty.id,
180180
parameters: trait_ref.substs,
181181
})
182182
}
@@ -268,7 +268,10 @@ impl Ty {
268268
.fill_with_unknown()
269269
.build();
270270
// FIXME handle type parameters on the segment
271-
return Ty::Projection(ProjectionTy { associated_ty, parameters: substs });
271+
return Ty::Projection(ProjectionTy {
272+
associated_ty: associated_ty.id,
273+
parameters: substs,
274+
});
272275
}
273276
}
274277
Ty::Unknown
@@ -508,7 +511,7 @@ fn assoc_type_bindings_from_type_bound<'a>(
508511
let associated_ty =
509512
match trait_ref.trait_.associated_type_by_name_including_super_traits(db, &name) {
510513
None => return GenericPredicate::Error,
511-
Some(t) => t,
514+
Some(t) => t.id,
512515
};
513516
let projection_ty =
514517
ProjectionTy { associated_ty, parameters: trait_ref.substs.clone() };

crates/ra_hir/src/ty/traits/chalk.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use chalk_ir::{
99
};
1010
use chalk_rust_ir::{AssociatedTyDatum, AssociatedTyValue, ImplDatum, StructDatum, TraitDatum};
1111

12-
use hir_def::{lang_item::LangItemTarget, GenericDefId, TypeAliasId};
12+
use hir_def::{lang_item::LangItemTarget, ContainerId, GenericDefId, Lookup, TypeAliasId};
1313
use hir_expand::name;
1414

1515
use ra_db::salsa::{InternId, InternKey};
@@ -203,18 +203,6 @@ impl ToChalk for Impl {
203203
}
204204
}
205205

206-
impl ToChalk for TypeAlias {
207-
type Chalk = chalk_ir::TypeId;
208-
209-
fn to_chalk(self, _db: &impl HirDatabase) -> chalk_ir::TypeId {
210-
chalk_ir::TypeId(id_to_chalk(self.id))
211-
}
212-
213-
fn from_chalk(_db: &impl HirDatabase, type_alias_id: chalk_ir::TypeId) -> TypeAlias {
214-
TypeAlias { id: id_from_chalk(type_alias_id.0) }
215-
}
216-
}
217-
218206
impl ToChalk for TypeAliasId {
219207
type Chalk = chalk_ir::TypeId;
220208

@@ -516,21 +504,21 @@ pub(crate) fn associated_ty_data_query(
516504
id: TypeId,
517505
) -> Arc<AssociatedTyDatum<ChalkIr>> {
518506
debug!("associated_ty_data {:?}", id);
519-
let type_alias: TypeAlias = from_chalk(db, id);
520-
let trait_ = match type_alias.container(db) {
521-
Some(crate::Container::Trait(t)) => t,
507+
let type_alias: TypeAliasId = from_chalk(db, id);
508+
let trait_ = match type_alias.lookup(db).container {
509+
ContainerId::TraitId(t) => t,
522510
_ => panic!("associated type not in trait"),
523511
};
524-
let generic_params = db.generic_params(type_alias.id.into());
512+
let generic_params = db.generic_params(type_alias.into());
525513
let bound_data = chalk_rust_ir::AssociatedTyDatumBound {
526514
// FIXME add bounds and where clauses
527515
bounds: vec![],
528516
where_clauses: vec![],
529517
};
530518
let datum = AssociatedTyDatum {
531-
trait_id: trait_.to_chalk(db),
519+
trait_id: Trait::from(trait_).to_chalk(db),
532520
id,
533-
name: lalrpop_intern::intern(&type_alias.name(db).to_string()),
521+
name: lalrpop_intern::intern(&db.type_alias_data(type_alias).name.to_string()),
534522
binders: make_binders(bound_data, generic_params.count_params_including_parent()),
535523
};
536524
Arc::new(datum)
@@ -578,7 +566,7 @@ pub(crate) fn trait_datum_query(
578566
.items(db)
579567
.into_iter()
580568
.filter_map(|trait_item| match trait_item {
581-
crate::AssocItem::TypeAlias(type_alias) => Some(type_alias),
569+
crate::AssocItem::TypeAlias(type_alias) => Some(type_alias.id),
582570
_ => None,
583571
})
584572
.map(|type_alias| type_alias.to_chalk(db))
@@ -797,7 +785,8 @@ fn type_alias_associated_ty_value(
797785
.trait_;
798786
let assoc_ty = trait_
799787
.associated_type_by_name(db, &type_alias.name(db))
800-
.expect("assoc ty value should not exist"); // validated when building the impl data as well
788+
.expect("assoc ty value should not exist") // validated when building the impl data as well
789+
.id;
801790
let generic_params = db.generic_params(impl_block.id.into());
802791
let bound_vars = Substs::bound_vars(&generic_params);
803792
let ty = db.type_for_def(type_alias.into(), crate::ty::Namespace::Types).subst(&bound_vars);
@@ -832,7 +821,8 @@ fn closure_fn_trait_output_assoc_ty_value(
832821

833822
let output_ty_id = fn_once_trait
834823
.associated_type_by_name(db, &name::OUTPUT_TYPE)
835-
.expect("assoc ty value should not exist");
824+
.expect("assoc ty value should not exist")
825+
.id;
836826

837827
let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty: output_ty.to_chalk(db) };
838828

0 commit comments

Comments
 (0)