aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/traits
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/traits')
-rw-r--r--crates/hir_ty/src/traits/chalk.rs141
-rw-r--r--crates/hir_ty/src/traits/chalk/interner.rs19
-rw-r--r--crates/hir_ty/src/traits/chalk/mapping.rs81
-rw-r--r--crates/hir_ty/src/traits/chalk/tls.rs14
4 files changed, 193 insertions, 62 deletions
diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs
index 17c83b6a4..27f0ed628 100644
--- a/crates/hir_ty/src/traits/chalk.rs
+++ b/crates/hir_ty/src/traits/chalk.rs
@@ -11,6 +11,7 @@ use hir_def::{
11 lang_item::{lang_attr, LangItemTarget}, 11 lang_item::{lang_attr, LangItemTarget},
12 AssocContainerId, AssocItemId, HasModule, Lookup, TypeAliasId, 12 AssocContainerId, AssocItemId, HasModule, Lookup, TypeAliasId,
13}; 13};
14use hir_expand::name::name;
14 15
15use super::ChalkContext; 16use super::ChalkContext;
16use crate::{ 17use crate::{
@@ -18,10 +19,12 @@ use crate::{
18 display::HirDisplay, 19 display::HirDisplay,
19 method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, 20 method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS},
20 utils::generics, 21 utils::generics,
21 CallableDefId, DebruijnIndex, FnSig, GenericPredicate, Substs, Ty, TypeCtor, 22 BoundVar, CallableDefId, DebruijnIndex, FnSig, GenericPredicate, ProjectionPredicate,
23 ProjectionTy, Substs, TraitRef, Ty, TypeCtor,
22}; 24};
23use mapping::{ 25use mapping::{
24 convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, 26 convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsAssocType,
27 TypeAliasAsValue,
25}; 28};
26 29
27pub use self::interner::*; 30pub use self::interner::*;
@@ -166,27 +169,88 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
166 fn opaque_ty_data(&self, id: chalk_ir::OpaqueTyId<Interner>) -> Arc<OpaqueTyDatum> { 169 fn opaque_ty_data(&self, id: chalk_ir::OpaqueTyId<Interner>) -> Arc<OpaqueTyDatum> {
167 let interned_id = crate::db::InternedOpaqueTyId::from(id); 170 let interned_id = crate::db::InternedOpaqueTyId::from(id);
168 let full_id = self.db.lookup_intern_impl_trait_id(interned_id); 171 let full_id = self.db.lookup_intern_impl_trait_id(interned_id);
169 let (func, idx) = match full_id { 172 let bound = match full_id {
170 crate::OpaqueTyId::ReturnTypeImplTrait(func, idx) => (func, idx), 173 crate::OpaqueTyId::ReturnTypeImplTrait(func, idx) => {
171 }; 174 let datas = self
172 let datas = 175 .db
173 self.db.return_type_impl_traits(func).expect("impl trait id without impl traits"); 176 .return_type_impl_traits(func)
174 let data = &datas.value.impl_traits[idx as usize]; 177 .expect("impl trait id without impl traits");
175 let bound = OpaqueTyDatumBound { 178 let data = &datas.value.impl_traits[idx as usize];
176 bounds: make_binders( 179 let bound = OpaqueTyDatumBound {
177 data.bounds 180 bounds: make_binders(
178 .value 181 data.bounds
179 .iter() 182 .value
180 .cloned() 183 .iter()
181 .filter(|b| !b.is_error()) 184 .cloned()
182 .map(|b| b.to_chalk(self.db)) 185 .filter(|b| !b.is_error())
183 .collect(), 186 .map(|b| b.to_chalk(self.db))
184 1, 187 .collect(),
185 ), 188 1,
186 where_clauses: make_binders(vec![], 0), 189 ),
190 where_clauses: make_binders(vec![], 0),
191 };
192 let num_vars = datas.num_binders;
193 make_binders(bound, num_vars)
194 }
195 crate::OpaqueTyId::AsyncBlockTypeImplTrait(..) => {
196 if let Some((future_trait, future_output)) = self
197 .db
198 .lang_item(self.krate, "future_trait".into())
199 .and_then(|item| item.as_trait())
200 .and_then(|trait_| {
201 let alias =
202 self.db.trait_data(trait_).associated_type_by_name(&name![Output])?;
203 Some((trait_, alias))
204 })
205 {
206 // Making up `AsyncBlock<T>: Future<Output = T>`
207 //
208 // |--------------------OpaqueTyDatum-------------------|
209 // |-------------OpaqueTyDatumBound--------------|
210 // for<T> <Self> [Future<Self>, Future::Output<Self> = T]
211 // ^1 ^0 ^0 ^0 ^1
212 let impl_bound = GenericPredicate::Implemented(TraitRef {
213 trait_: future_trait,
214 // Self type as the first parameter.
215 substs: Substs::single(Ty::Bound(BoundVar {
216 debruijn: DebruijnIndex::INNERMOST,
217 index: 0,
218 })),
219 });
220 let proj_bound = GenericPredicate::Projection(ProjectionPredicate {
221 // The parameter of the opaque type.
222 ty: Ty::Bound(BoundVar { debruijn: DebruijnIndex::ONE, index: 0 }),
223 projection_ty: ProjectionTy {
224 associated_ty: future_output,
225 // Self type as the first parameter.
226 parameters: Substs::single(Ty::Bound(BoundVar::new(
227 DebruijnIndex::INNERMOST,
228 0,
229 ))),
230 },
231 });
232 let bound = OpaqueTyDatumBound {
233 bounds: make_binders(
234 vec![impl_bound.to_chalk(self.db), proj_bound.to_chalk(self.db)],
235 1,
236 ),
237 where_clauses: make_binders(vec![], 0),
238 };
239 // The opaque type has 1 parameter.
240 make_binders(bound, 1)
241 } else {
242 // If failed to find `Future::Output`, return empty bounds as fallback.
243 let bound = OpaqueTyDatumBound {
244 bounds: make_binders(vec![], 0),
245 where_clauses: make_binders(vec![], 0),
246 };
247 // The opaque type has 1 parameter.
248 make_binders(bound, 1)
249 }
250 }
187 }; 251 };
188 let num_vars = datas.num_binders; 252
189 Arc::new(OpaqueTyDatum { opaque_ty_id: id, bound: make_binders(bound, num_vars) }) 253 Arc::new(OpaqueTyDatum { opaque_ty_id: id, bound })
190 } 254 }
191 255
192 fn hidden_opaque_type(&self, _id: chalk_ir::OpaqueTyId<Interner>) -> chalk_ir::Ty<Interner> { 256 fn hidden_opaque_type(&self, _id: chalk_ir::OpaqueTyId<Interner>) -> chalk_ir::Ty<Interner> {
@@ -244,13 +308,17 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
244 let id = from_chalk(self.db, trait_id); 308 let id = from_chalk(self.db, trait_id);
245 self.db.trait_data(id).name.to_string() 309 self.db.trait_data(id).name.to_string()
246 } 310 }
247 // FIXME: lookup names 311 fn adt_name(&self, adt_id: chalk_ir::AdtId<Interner>) -> String {
248 fn adt_name(&self, struct_id: chalk_ir::AdtId<Interner>) -> String { 312 let id = from_chalk(self.db, adt_id);
249 let datum = self.db.struct_datum(self.krate, struct_id); 313 match id {
250 format!("{:?}", datum.name(&Interner)) 314 hir_def::AdtId::StructId(id) => self.db.struct_data(id).name.to_string(),
315 hir_def::AdtId::EnumId(id) => self.db.enum_data(id).name.to_string(),
316 hir_def::AdtId::UnionId(id) => self.db.union_data(id).name.to_string(),
317 }
251 } 318 }
252 fn assoc_type_name(&self, assoc_ty_id: chalk_ir::AssocTypeId<Interner>) -> String { 319 fn assoc_type_name(&self, assoc_ty_id: chalk_ir::AssocTypeId<Interner>) -> String {
253 format!("Assoc_{}", assoc_ty_id.0) 320 let id = self.db.associated_ty_data(assoc_ty_id).name;
321 self.db.type_alias_data(id).name.to_string()
254 } 322 }
255 fn opaque_type_name(&self, opaque_ty_id: chalk_ir::OpaqueTyId<Interner>) -> String { 323 fn opaque_type_name(&self, opaque_ty_id: chalk_ir::OpaqueTyId<Interner>) -> String {
256 format!("Opaque_{}", opaque_ty_id.0) 324 format!("Opaque_{}", opaque_ty_id.0)
@@ -273,7 +341,7 @@ pub(crate) fn associated_ty_data_query(
273 id: AssocTypeId, 341 id: AssocTypeId,
274) -> Arc<AssociatedTyDatum> { 342) -> Arc<AssociatedTyDatum> {
275 debug!("associated_ty_data {:?}", id); 343 debug!("associated_ty_data {:?}", id);
276 let type_alias: TypeAliasId = from_chalk(db, id); 344 let type_alias: TypeAliasId = from_chalk::<TypeAliasAsAssocType, _>(db, id).0;
277 let trait_ = match type_alias.lookup(db.upcast()).container { 345 let trait_ = match type_alias.lookup(db.upcast()).container {
278 AssocContainerId::TraitId(t) => t, 346 AssocContainerId::TraitId(t) => t,
279 _ => panic!("associated type not in trait"), 347 _ => panic!("associated type not in trait"),
@@ -327,8 +395,10 @@ pub(crate) fn trait_datum_query(
327 fundamental: false, 395 fundamental: false,
328 }; 396 };
329 let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars); 397 let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars);
330 let associated_ty_ids = 398 let associated_ty_ids = trait_data
331 trait_data.associated_types().map(|type_alias| type_alias.to_chalk(db)).collect(); 399 .associated_types()
400 .map(|type_alias| TypeAliasAsAssocType(type_alias).to_chalk(db))
401 .collect();
332 let trait_datum_bound = rust_ir::TraitDatumBound { where_clauses }; 402 let trait_datum_bound = rust_ir::TraitDatumBound { where_clauses };
333 let well_known = 403 let well_known =
334 lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); 404 lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name));
@@ -366,6 +436,7 @@ fn lang_attr_from_well_known_trait(attr: WellKnownTrait) -> &'static str {
366 WellKnownTrait::FnMut => "fn_mut", 436 WellKnownTrait::FnMut => "fn_mut",
367 WellKnownTrait::Fn => "fn", 437 WellKnownTrait::Fn => "fn",
368 WellKnownTrait::Unsize => "unsize", 438 WellKnownTrait::Unsize => "unsize",
439 WellKnownTrait::Unpin => "unpin",
369 } 440 }
370} 441}
371 442
@@ -509,7 +580,7 @@ fn type_alias_associated_ty_value(
509 let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) }; 580 let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) };
510 let value = rust_ir::AssociatedTyValue { 581 let value = rust_ir::AssociatedTyValue {
511 impl_id: impl_id.to_chalk(db), 582 impl_id: impl_id.to_chalk(db),
512 associated_ty_id: assoc_ty.to_chalk(db), 583 associated_ty_id: TypeAliasAsAssocType(assoc_ty).to_chalk(db),
513 value: make_binders(value_bound, ty.num_binders), 584 value: make_binders(value_bound, ty.num_binders),
514 }; 585 };
515 Arc::new(value) 586 Arc::new(value)
@@ -544,9 +615,11 @@ pub(crate) fn fn_def_datum_query(
544 }; 615 };
545 let datum = FnDefDatum { 616 let datum = FnDefDatum {
546 id: fn_def_id, 617 id: fn_def_id,
547 abi: (), 618 sig: chalk_ir::FnSig {
548 safety: chalk_ir::Safety::Safe, 619 abi: (),
549 variadic: sig.value.is_varargs, 620 safety: chalk_ir::Safety::Safe,
621 variadic: sig.value.is_varargs,
622 },
550 binders: make_binders(bound, sig.num_binders), 623 binders: make_binders(bound, sig.num_binders),
551 }; 624 };
552 Arc::new(datum) 625 Arc::new(datum)
diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs
index fc0f9c201..f9304b7d0 100644
--- a/crates/hir_ty/src/traits/chalk/interner.rs
+++ b/crates/hir_ty/src/traits/chalk/interner.rs
@@ -12,6 +12,7 @@ pub struct Interner;
12 12
13pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>; 13pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
14pub type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum<Interner>; 14pub type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum<Interner>;
15pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>;
15pub type TraitId = chalk_ir::TraitId<Interner>; 16pub type TraitId = chalk_ir::TraitId<Interner>;
16pub type TraitDatum = chalk_solve::rust_ir::TraitDatum<Interner>; 17pub type TraitDatum = chalk_solve::rust_ir::TraitDatum<Interner>;
17pub type AdtId = chalk_ir::AdtId<Interner>; 18pub type AdtId = chalk_ir::AdtId<Interner>;
@@ -26,7 +27,7 @@ pub type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>;
26pub type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum<Interner>; 27pub type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum<Interner>;
27 28
28impl chalk_ir::interner::Interner for Interner { 29impl chalk_ir::interner::Interner for Interner {
29 type InternedType = Box<chalk_ir::TyData<Self>>; // FIXME use Arc? 30 type InternedType = Arc<chalk_ir::TyData<Self>>;
30 type InternedLifetime = chalk_ir::LifetimeData<Self>; 31 type InternedLifetime = chalk_ir::LifetimeData<Self>;
31 type InternedConst = Arc<chalk_ir::ConstData<Self>>; 32 type InternedConst = Arc<chalk_ir::ConstData<Self>>;
32 type InternedConcreteConst = (); 33 type InternedConcreteConst = ();
@@ -34,7 +35,7 @@ impl chalk_ir::interner::Interner for Interner {
34 type InternedGoal = Arc<GoalData<Self>>; 35 type InternedGoal = Arc<GoalData<Self>>;
35 type InternedGoals = Vec<Goal<Self>>; 36 type InternedGoals = Vec<Goal<Self>>;
36 type InternedSubstitution = Vec<GenericArg<Self>>; 37 type InternedSubstitution = Vec<GenericArg<Self>>;
37 type InternedProgramClause = chalk_ir::ProgramClauseData<Self>; 38 type InternedProgramClause = Arc<chalk_ir::ProgramClauseData<Self>>;
38 type InternedProgramClauses = Arc<[chalk_ir::ProgramClause<Self>]>; 39 type InternedProgramClauses = Arc<[chalk_ir::ProgramClause<Self>]>;
39 type InternedQuantifiedWhereClauses = Vec<chalk_ir::QuantifiedWhereClause<Self>>; 40 type InternedQuantifiedWhereClauses = Vec<chalk_ir::QuantifiedWhereClause<Self>>;
40 type InternedVariableKinds = Vec<chalk_ir::VariableKind<Self>>; 41 type InternedVariableKinds = Vec<chalk_ir::VariableKind<Self>>;
@@ -197,11 +198,11 @@ impl chalk_ir::interner::Interner for Interner {
197 tls::with_current_program(|prog| Some(prog?.debug_quantified_where_clauses(clauses, fmt))) 198 tls::with_current_program(|prog| Some(prog?.debug_quantified_where_clauses(clauses, fmt)))
198 } 199 }
199 200
200 fn intern_ty(&self, ty: chalk_ir::TyData<Self>) -> Box<chalk_ir::TyData<Self>> { 201 fn intern_ty(&self, ty: chalk_ir::TyData<Self>) -> Arc<chalk_ir::TyData<Self>> {
201 Box::new(ty) 202 Arc::new(ty)
202 } 203 }
203 204
204 fn ty_data<'a>(&self, ty: &'a Box<chalk_ir::TyData<Self>>) -> &'a chalk_ir::TyData<Self> { 205 fn ty_data<'a>(&self, ty: &'a Arc<chalk_ir::TyData<Self>>) -> &'a chalk_ir::TyData<Self> {
205 ty 206 ty
206 } 207 }
207 208
@@ -230,7 +231,7 @@ impl chalk_ir::interner::Interner for Interner {
230 constant 231 constant
231 } 232 }
232 233
233 fn const_eq(&self, _ty: &Box<chalk_ir::TyData<Self>>, _c1: &(), _c2: &()) -> bool { 234 fn const_eq(&self, _ty: &Arc<chalk_ir::TyData<Self>>, _c1: &(), _c2: &()) -> bool {
234 true 235 true
235 } 236 }
236 237
@@ -284,13 +285,13 @@ impl chalk_ir::interner::Interner for Interner {
284 fn intern_program_clause( 285 fn intern_program_clause(
285 &self, 286 &self,
286 data: chalk_ir::ProgramClauseData<Self>, 287 data: chalk_ir::ProgramClauseData<Self>,
287 ) -> chalk_ir::ProgramClauseData<Self> { 288 ) -> Arc<chalk_ir::ProgramClauseData<Self>> {
288 data 289 Arc::new(data)
289 } 290 }
290 291
291 fn program_clause_data<'a>( 292 fn program_clause_data<'a>(
292 &self, 293 &self,
293 clause: &'a chalk_ir::ProgramClauseData<Self>, 294 clause: &'a Arc<chalk_ir::ProgramClauseData<Self>>,
294 ) -> &'a chalk_ir::ProgramClauseData<Self> { 295 ) -> &'a chalk_ir::ProgramClauseData<Self> {
295 clause 296 clause
296 } 297 }
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs
index fe62f3fa7..d42f4bba9 100644
--- a/crates/hir_ty/src/traits/chalk/mapping.rs
+++ b/crates/hir_ty/src/traits/chalk/mapping.rs
@@ -34,9 +34,11 @@ impl ToChalk for Ty {
34 let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); 34 let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner);
35 chalk_ir::TyData::Function(chalk_ir::FnPointer { 35 chalk_ir::TyData::Function(chalk_ir::FnPointer {
36 num_binders: 0, 36 num_binders: 0,
37 abi: (), 37 sig: chalk_ir::FnSig {
38 safety: chalk_ir::Safety::Safe, 38 abi: (),
39 variadic: is_varargs, 39 safety: chalk_ir::Safety::Safe,
40 variadic: is_varargs,
41 },
40 substitution, 42 substitution,
41 }) 43 })
42 .intern(&Interner) 44 .intern(&Interner)
@@ -48,7 +50,7 @@ impl ToChalk for Ty {
48 } 50 }
49 }, 51 },
50 Ty::Projection(proj_ty) => { 52 Ty::Projection(proj_ty) => {
51 let associated_ty_id = proj_ty.associated_ty.to_chalk(db); 53 let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db);
52 let substitution = proj_ty.parameters.to_chalk(db); 54 let substitution = proj_ty.parameters.to_chalk(db);
53 chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { 55 chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy {
54 associated_ty_id, 56 associated_ty_id,
@@ -114,7 +116,8 @@ impl ToChalk for Ty {
114 Ty::Placeholder(db.lookup_intern_type_param_id(interned_id)) 116 Ty::Placeholder(db.lookup_intern_type_param_id(interned_id))
115 } 117 }
116 chalk_ir::TyData::Alias(chalk_ir::AliasTy::Projection(proj)) => { 118 chalk_ir::TyData::Alias(chalk_ir::AliasTy::Projection(proj)) => {
117 let associated_ty = from_chalk(db, proj.associated_ty_id); 119 let associated_ty =
120 from_chalk::<TypeAliasAsAssocType, _>(db, proj.associated_ty_id).0;
118 let parameters = from_chalk(db, proj.substitution); 121 let parameters = from_chalk(db, proj.substitution);
119 Ty::Projection(ProjectionTy { associated_ty, parameters }) 122 Ty::Projection(ProjectionTy { associated_ty, parameters })
120 } 123 }
@@ -125,7 +128,7 @@ impl ToChalk for Ty {
125 } 128 }
126 chalk_ir::TyData::Function(chalk_ir::FnPointer { 129 chalk_ir::TyData::Function(chalk_ir::FnPointer {
127 num_binders, 130 num_binders,
128 variadic, 131 sig: chalk_ir::FnSig { variadic, .. },
129 substitution, 132 substitution,
130 .. 133 ..
131 }) => { 134 }) => {
@@ -290,8 +293,9 @@ impl ToChalk for TypeCtor {
290 fn to_chalk(self, db: &dyn HirDatabase) -> TypeName<Interner> { 293 fn to_chalk(self, db: &dyn HirDatabase) -> TypeName<Interner> {
291 match self { 294 match self {
292 TypeCtor::AssociatedType(type_alias) => { 295 TypeCtor::AssociatedType(type_alias) => {
293 let type_id = type_alias.to_chalk(db); 296 let assoc_type = TypeAliasAsAssocType(type_alias);
294 TypeName::AssociatedType(type_id) 297 let assoc_type_id = assoc_type.to_chalk(db);
298 TypeName::AssociatedType(assoc_type_id)
295 } 299 }
296 300
297 TypeCtor::OpaqueType(impl_trait_id) => { 301 TypeCtor::OpaqueType(impl_trait_id) => {
@@ -299,6 +303,12 @@ impl ToChalk for TypeCtor {
299 TypeName::OpaqueType(id) 303 TypeName::OpaqueType(id)
300 } 304 }
301 305
306 TypeCtor::ForeignType(type_alias) => {
307 let foreign_type = TypeAliasAsForeignType(type_alias);
308 let foreign_type_id = foreign_type.to_chalk(db);
309 TypeName::Foreign(foreign_type_id)
310 }
311
302 TypeCtor::Bool => TypeName::Scalar(Scalar::Bool), 312 TypeCtor::Bool => TypeName::Scalar(Scalar::Bool),
303 TypeCtor::Char => TypeName::Scalar(Scalar::Char), 313 TypeCtor::Char => TypeName::Scalar(Scalar::Char),
304 TypeCtor::Int(int_ty) => TypeName::Scalar(int_ty_to_chalk(int_ty)), 314 TypeCtor::Int(int_ty) => TypeName::Scalar(int_ty_to_chalk(int_ty)),
@@ -339,7 +349,9 @@ impl ToChalk for TypeCtor {
339 fn from_chalk(db: &dyn HirDatabase, type_name: TypeName<Interner>) -> TypeCtor { 349 fn from_chalk(db: &dyn HirDatabase, type_name: TypeName<Interner>) -> TypeCtor {
340 match type_name { 350 match type_name {
341 TypeName::Adt(struct_id) => TypeCtor::Adt(struct_id.0), 351 TypeName::Adt(struct_id) => TypeCtor::Adt(struct_id.0),
342 TypeName::AssociatedType(type_id) => TypeCtor::AssociatedType(from_chalk(db, type_id)), 352 TypeName::AssociatedType(type_id) => {
353 TypeCtor::AssociatedType(from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0)
354 }
343 TypeName::OpaqueType(opaque_type_id) => { 355 TypeName::OpaqueType(opaque_type_id) => {
344 TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)) 356 TypeCtor::OpaqueType(from_chalk(db, opaque_type_id))
345 } 357 }
@@ -379,6 +391,10 @@ impl ToChalk for TypeCtor {
379 TypeCtor::Closure { def, expr } 391 TypeCtor::Closure { def, expr }
380 } 392 }
381 393
394 TypeName::Foreign(foreign_def_id) => {
395 TypeCtor::ForeignType(from_chalk::<TypeAliasAsForeignType, _>(db, foreign_def_id).0)
396 }
397
382 TypeName::Error => { 398 TypeName::Error => {
383 // this should not be reached, since we don't represent TypeName::Error with TypeCtor 399 // this should not be reached, since we don't represent TypeName::Error with TypeCtor
384 unreachable!() 400 unreachable!()
@@ -464,6 +480,18 @@ impl ToChalk for hir_def::ImplId {
464 } 480 }
465} 481}
466 482
483impl ToChalk for hir_def::AdtId {
484 type Chalk = AdtId;
485
486 fn to_chalk(self, _db: &dyn HirDatabase) -> Self::Chalk {
487 chalk_ir::AdtId(self.into())
488 }
489
490 fn from_chalk(_db: &dyn HirDatabase, id: AdtId) -> Self {
491 id.0
492 }
493}
494
467impl ToChalk for CallableDefId { 495impl ToChalk for CallableDefId {
468 type Chalk = FnDefId; 496 type Chalk = FnDefId;
469 497
@@ -476,15 +504,31 @@ impl ToChalk for CallableDefId {
476 } 504 }
477} 505}
478 506
479impl ToChalk for TypeAliasId { 507pub struct TypeAliasAsAssocType(pub TypeAliasId);
508
509impl ToChalk for TypeAliasAsAssocType {
480 type Chalk = AssocTypeId; 510 type Chalk = AssocTypeId;
481 511
482 fn to_chalk(self, _db: &dyn HirDatabase) -> AssocTypeId { 512 fn to_chalk(self, _db: &dyn HirDatabase) -> AssocTypeId {
483 chalk_ir::AssocTypeId(self.as_intern_id()) 513 chalk_ir::AssocTypeId(self.0.as_intern_id())
514 }
515
516 fn from_chalk(_db: &dyn HirDatabase, assoc_type_id: AssocTypeId) -> TypeAliasAsAssocType {
517 TypeAliasAsAssocType(InternKey::from_intern_id(assoc_type_id.0))
518 }
519}
520
521pub struct TypeAliasAsForeignType(pub TypeAliasId);
522
523impl ToChalk for TypeAliasAsForeignType {
524 type Chalk = ForeignDefId;
525
526 fn to_chalk(self, _db: &dyn HirDatabase) -> ForeignDefId {
527 chalk_ir::ForeignDefId(self.0.as_intern_id())
484 } 528 }
485 529
486 fn from_chalk(_db: &dyn HirDatabase, type_alias_id: AssocTypeId) -> TypeAliasId { 530 fn from_chalk(_db: &dyn HirDatabase, foreign_def_id: ForeignDefId) -> TypeAliasAsForeignType {
487 InternKey::from_intern_id(type_alias_id.0) 531 TypeAliasAsForeignType(InternKey::from_intern_id(foreign_def_id.0))
488 } 532 }
489} 533}
490 534
@@ -568,7 +612,7 @@ impl ToChalk for ProjectionTy {
568 612
569 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> { 613 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> {
570 chalk_ir::ProjectionTy { 614 chalk_ir::ProjectionTy {
571 associated_ty_id: self.associated_ty.to_chalk(db), 615 associated_ty_id: TypeAliasAsAssocType(self.associated_ty).to_chalk(db),
572 substitution: self.parameters.to_chalk(db), 616 substitution: self.parameters.to_chalk(db),
573 } 617 }
574 } 618 }
@@ -578,7 +622,11 @@ impl ToChalk for ProjectionTy {
578 projection_ty: chalk_ir::ProjectionTy<Interner>, 622 projection_ty: chalk_ir::ProjectionTy<Interner>,
579 ) -> ProjectionTy { 623 ) -> ProjectionTy {
580 ProjectionTy { 624 ProjectionTy {
581 associated_ty: from_chalk(db, projection_ty.associated_ty_id), 625 associated_ty: from_chalk::<TypeAliasAsAssocType, _>(
626 db,
627 projection_ty.associated_ty_id,
628 )
629 .0,
582 parameters: from_chalk(db, projection_ty.substitution), 630 parameters: from_chalk(db, projection_ty.substitution),
583 } 631 }
584 } 632 }
@@ -777,7 +825,8 @@ pub(super) fn generic_predicate_to_inline_bound(
777 let alias_eq_bound = rust_ir::AliasEqBound { 825 let alias_eq_bound = rust_ir::AliasEqBound {
778 value: proj.ty.clone().to_chalk(db), 826 value: proj.ty.clone().to_chalk(db),
779 trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self }, 827 trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self },
780 associated_ty_id: proj.projection_ty.associated_ty.to_chalk(db), 828 associated_ty_id: TypeAliasAsAssocType(proj.projection_ty.associated_ty)
829 .to_chalk(db),
781 parameters: Vec::new(), // FIXME we don't support generic associated types yet 830 parameters: Vec::new(), // FIXME we don't support generic associated types yet
782 }; 831 };
783 Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound)) 832 Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound))
diff --git a/crates/hir_ty/src/traits/chalk/tls.rs b/crates/hir_ty/src/traits/chalk/tls.rs
index db915625c..b4568cff6 100644
--- a/crates/hir_ty/src/traits/chalk/tls.rs
+++ b/crates/hir_ty/src/traits/chalk/tls.rs
@@ -4,7 +4,7 @@ use std::fmt;
4use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication, TypeName}; 4use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication, TypeName};
5use itertools::Itertools; 5use itertools::Itertools;
6 6
7use super::{from_chalk, Interner}; 7use super::{from_chalk, Interner, TypeAliasAsAssocType};
8use crate::{db::HirDatabase, CallableDefId, TypeCtor}; 8use crate::{db::HirDatabase, CallableDefId, TypeCtor};
9use hir_def::{AdtId, AssocContainerId, DefWithBodyId, Lookup, TypeAliasId}; 9use hir_def::{AdtId, AssocContainerId, DefWithBodyId, Lookup, TypeAliasId};
10 10
@@ -73,7 +73,14 @@ impl DebugContext<'_> {
73 crate::OpaqueTyId::ReturnTypeImplTrait(func, idx) => { 73 crate::OpaqueTyId::ReturnTypeImplTrait(func, idx) => {
74 write!(f, "{{impl trait {} of {:?}}}", idx, func)?; 74 write!(f, "{{impl trait {} of {:?}}}", idx, func)?;
75 } 75 }
76 crate::OpaqueTyId::AsyncBlockTypeImplTrait(def, idx) => {
77 write!(f, "{{impl trait of async block {} of {:?}}}", idx.into_raw(), def)?;
78 }
76 }, 79 },
80 TypeCtor::ForeignType(type_alias) => {
81 let name = self.0.type_alias_data(type_alias).name.clone();
82 write!(f, "{}", name)?;
83 }
77 TypeCtor::Closure { def, expr } => { 84 TypeCtor::Closure { def, expr } => {
78 write!(f, "{{closure {:?} in ", expr.into_raw())?; 85 write!(f, "{{closure {:?} in ", expr.into_raw())?;
79 match def { 86 match def {
@@ -116,7 +123,7 @@ impl DebugContext<'_> {
116 id: super::AssocTypeId, 123 id: super::AssocTypeId,
117 fmt: &mut fmt::Formatter<'_>, 124 fmt: &mut fmt::Formatter<'_>,
118 ) -> Result<(), fmt::Error> { 125 ) -> Result<(), fmt::Error> {
119 let type_alias: TypeAliasId = from_chalk(self.0, id); 126 let type_alias: TypeAliasId = from_chalk::<TypeAliasAsAssocType, _>(self.0, id).0;
120 let type_alias_data = self.0.type_alias_data(type_alias); 127 let type_alias_data = self.0.type_alias_data(type_alias);
121 let trait_ = match type_alias.lookup(self.0.upcast()).container { 128 let trait_ = match type_alias.lookup(self.0.upcast()).container {
122 AssocContainerId::TraitId(t) => t, 129 AssocContainerId::TraitId(t) => t,
@@ -150,7 +157,8 @@ impl DebugContext<'_> {
150 projection_ty: &chalk_ir::ProjectionTy<Interner>, 157 projection_ty: &chalk_ir::ProjectionTy<Interner>,
151 fmt: &mut fmt::Formatter<'_>, 158 fmt: &mut fmt::Formatter<'_>,
152 ) -> Result<(), fmt::Error> { 159 ) -> Result<(), fmt::Error> {
153 let type_alias: TypeAliasId = from_chalk(self.0, projection_ty.associated_ty_id); 160 let type_alias: TypeAliasId =
161 from_chalk::<TypeAliasAsAssocType, _>(self.0, projection_ty.associated_ty_id).0;
154 let type_alias_data = self.0.type_alias_data(type_alias); 162 let type_alias_data = self.0.type_alias_data(type_alias);
155 let trait_ = match type_alias.lookup(self.0.upcast()).container { 163 let trait_ = match type_alias.lookup(self.0.upcast()).container {
156 AssocContainerId::TraitId(t) => t, 164 AssocContainerId::TraitId(t) => t,