diff options
Diffstat (limited to 'crates/hir_ty/src/traits')
-rw-r--r-- | crates/hir_ty/src/traits/chalk/mapping.rs | 140 |
1 files changed, 60 insertions, 80 deletions
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 8700d664e..0e9fc3265 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs | |||
@@ -4,7 +4,7 @@ | |||
4 | //! conversions. | 4 | //! conversions. |
5 | 5 | ||
6 | use chalk_ir::{ | 6 | use chalk_ir::{ |
7 | cast::Cast, fold::shift::Shift, interner::HasInterner, LifetimeData, PlaceholderIndex, Scalar, | 7 | cast::Cast, fold::shift::Shift, interner::HasInterner, LifetimeData, PlaceholderIndex, |
8 | UniverseIndex, | 8 | UniverseIndex, |
9 | }; | 9 | }; |
10 | use chalk_solve::rust_ir; | 10 | use chalk_solve::rust_ir; |
@@ -14,10 +14,11 @@ use hir_def::{type_ref::Mutability, AssocContainerId, GenericDefId, Lookup, Type | |||
14 | 14 | ||
15 | use crate::{ | 15 | use crate::{ |
16 | db::HirDatabase, | 16 | db::HirDatabase, |
17 | primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness}, | 17 | primitive::{FloatTy, IntTy, UintTy}, |
18 | traits::{Canonical, Obligation}, | 18 | traits::{Canonical, Obligation}, |
19 | ApplicationTy, CallableDefId, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, | 19 | ApplicationTy, CallableDefId, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, |
20 | ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TyKind, TypeCtor, | 20 | ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, TraitRef, Ty, TyKind, |
21 | TypeCtor, | ||
21 | }; | 22 | }; |
22 | 23 | ||
23 | use super::interner::*; | 24 | use super::interner::*; |
@@ -63,19 +64,31 @@ impl ToChalk for Ty { | |||
63 | chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner) | 64 | chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner) |
64 | } | 65 | } |
65 | 66 | ||
66 | TypeCtor::Bool => chalk_ir::TyKind::Scalar(Scalar::Bool).intern(&Interner), | 67 | TypeCtor::Scalar(scalar) => chalk_ir::TyKind::Scalar(match scalar { |
67 | TypeCtor::Char => chalk_ir::TyKind::Scalar(Scalar::Char).intern(&Interner), | 68 | Scalar::Bool => chalk_ir::Scalar::Bool, |
68 | TypeCtor::Int(int_ty) => { | 69 | Scalar::Char => chalk_ir::Scalar::Char, |
69 | chalk_ir::TyKind::Scalar(int_ty_to_chalk(int_ty)).intern(&Interner) | 70 | Scalar::Int(it) => chalk_ir::Scalar::Int(match it { |
70 | } | 71 | IntTy::Isize => chalk_ir::IntTy::Isize, |
71 | TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 }) => { | 72 | IntTy::I8 => chalk_ir::IntTy::I8, |
72 | chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F32)) | 73 | IntTy::I16 => chalk_ir::IntTy::I16, |
73 | .intern(&Interner) | 74 | IntTy::I32 => chalk_ir::IntTy::I32, |
74 | } | 75 | IntTy::I64 => chalk_ir::IntTy::I64, |
75 | TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 }) => { | 76 | IntTy::I128 => chalk_ir::IntTy::I128, |
76 | chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F64)) | 77 | }), |
77 | .intern(&Interner) | 78 | Scalar::Uint(it) => chalk_ir::Scalar::Uint(match it { |
78 | } | 79 | UintTy::Usize => chalk_ir::UintTy::Usize, |
80 | UintTy::U8 => chalk_ir::UintTy::U8, | ||
81 | UintTy::U16 => chalk_ir::UintTy::U16, | ||
82 | UintTy::U32 => chalk_ir::UintTy::U32, | ||
83 | UintTy::U64 => chalk_ir::UintTy::U64, | ||
84 | UintTy::U128 => chalk_ir::UintTy::U128, | ||
85 | }), | ||
86 | Scalar::Float(it) => chalk_ir::Scalar::Float(match it { | ||
87 | FloatTy::F32 => chalk_ir::FloatTy::F32, | ||
88 | FloatTy::F64 => chalk_ir::FloatTy::F64, | ||
89 | }), | ||
90 | }) | ||
91 | .intern(&Interner), | ||
79 | 92 | ||
80 | TypeCtor::Tuple { cardinality } => { | 93 | TypeCtor::Tuple { cardinality } => { |
81 | let substitution = apply_ty.parameters.to_chalk(db); | 94 | let substitution = apply_ty.parameters.to_chalk(db); |
@@ -219,21 +232,37 @@ impl ToChalk for Ty { | |||
219 | apply_ty_from_chalk(db, TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)), subst) | 232 | apply_ty_from_chalk(db, TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)), subst) |
220 | } | 233 | } |
221 | 234 | ||
222 | chalk_ir::TyKind::Scalar(Scalar::Bool) => Ty::simple(TypeCtor::Bool), | 235 | chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Bool) => { |
223 | chalk_ir::TyKind::Scalar(Scalar::Char) => Ty::simple(TypeCtor::Char), | 236 | Ty::simple(TypeCtor::Scalar(Scalar::Bool)) |
224 | chalk_ir::TyKind::Scalar(Scalar::Int(int_ty)) => Ty::simple(TypeCtor::Int(IntTy { | 237 | } |
225 | signedness: Signedness::Signed, | 238 | chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Char) => { |
226 | bitness: bitness_from_chalk_int(int_ty), | 239 | Ty::simple(TypeCtor::Scalar(Scalar::Char)) |
227 | })), | 240 | } |
228 | chalk_ir::TyKind::Scalar(Scalar::Uint(uint_ty)) => Ty::simple(TypeCtor::Int(IntTy { | 241 | chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Int(int_ty)) => { |
229 | signedness: Signedness::Unsigned, | 242 | Ty::simple(TypeCtor::Scalar(Scalar::Int(match int_ty { |
230 | bitness: bitness_from_chalk_uint(uint_ty), | 243 | chalk_ir::IntTy::Isize => IntTy::Isize, |
231 | })), | 244 | chalk_ir::IntTy::I8 => IntTy::I8, |
232 | chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F32)) => { | 245 | chalk_ir::IntTy::I16 => IntTy::I16, |
233 | Ty::simple(TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 })) | 246 | chalk_ir::IntTy::I32 => IntTy::I32, |
247 | chalk_ir::IntTy::I64 => IntTy::I64, | ||
248 | chalk_ir::IntTy::I128 => IntTy::I128, | ||
249 | }))) | ||
234 | } | 250 | } |
235 | chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F64)) => { | 251 | chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Uint(int_ty)) => { |
236 | Ty::simple(TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 })) | 252 | Ty::simple(TypeCtor::Scalar(Scalar::Uint(match int_ty { |
253 | chalk_ir::UintTy::Usize => UintTy::Usize, | ||
254 | chalk_ir::UintTy::U8 => UintTy::U8, | ||
255 | chalk_ir::UintTy::U16 => UintTy::U16, | ||
256 | chalk_ir::UintTy::U32 => UintTy::U32, | ||
257 | chalk_ir::UintTy::U64 => UintTy::U64, | ||
258 | chalk_ir::UintTy::U128 => UintTy::U128, | ||
259 | }))) | ||
260 | } | ||
261 | chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Float(float_ty)) => { | ||
262 | Ty::simple(TypeCtor::Scalar(Scalar::Float(match float_ty { | ||
263 | chalk_ir::FloatTy::F32 => FloatTy::F32, | ||
264 | chalk_ir::FloatTy::F64 => FloatTy::F64, | ||
265 | }))) | ||
237 | } | 266 | } |
238 | chalk_ir::TyKind::Tuple(cardinality, subst) => { | 267 | chalk_ir::TyKind::Tuple(cardinality, subst) => { |
239 | apply_ty_from_chalk(db, TypeCtor::Tuple { cardinality: cardinality as u16 }, subst) | 268 | apply_ty_from_chalk(db, TypeCtor::Tuple { cardinality: cardinality as u16 }, subst) |
@@ -293,7 +322,7 @@ fn ref_to_chalk( | |||
293 | fn array_to_chalk(db: &dyn HirDatabase, subst: Substs) -> chalk_ir::Ty<Interner> { | 322 | fn array_to_chalk(db: &dyn HirDatabase, subst: Substs) -> chalk_ir::Ty<Interner> { |
294 | let arg = subst[0].clone().to_chalk(db); | 323 | let arg = subst[0].clone().to_chalk(db); |
295 | let usize_ty = | 324 | let usize_ty = |
296 | chalk_ir::TyKind::Scalar(Scalar::Uint(chalk_ir::UintTy::Usize)).intern(&Interner); | 325 | chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Uint(chalk_ir::UintTy::Usize)).intern(&Interner); |
297 | let const_ = chalk_ir::ConstData { | 326 | let const_ = chalk_ir::ConstData { |
298 | ty: usize_ty, | 327 | ty: usize_ty, |
299 | value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: () }), | 328 | value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: () }), |
@@ -364,55 +393,6 @@ impl ToChalk for OpaqueTyId { | |||
364 | } | 393 | } |
365 | } | 394 | } |
366 | 395 | ||
367 | fn bitness_from_chalk_uint(uint_ty: chalk_ir::UintTy) -> IntBitness { | ||
368 | use chalk_ir::UintTy; | ||
369 | |||
370 | match uint_ty { | ||
371 | UintTy::Usize => IntBitness::Xsize, | ||
372 | UintTy::U8 => IntBitness::X8, | ||
373 | UintTy::U16 => IntBitness::X16, | ||
374 | UintTy::U32 => IntBitness::X32, | ||
375 | UintTy::U64 => IntBitness::X64, | ||
376 | UintTy::U128 => IntBitness::X128, | ||
377 | } | ||
378 | } | ||
379 | |||
380 | fn bitness_from_chalk_int(int_ty: chalk_ir::IntTy) -> IntBitness { | ||
381 | use chalk_ir::IntTy; | ||
382 | |||
383 | match int_ty { | ||
384 | IntTy::Isize => IntBitness::Xsize, | ||
385 | IntTy::I8 => IntBitness::X8, | ||
386 | IntTy::I16 => IntBitness::X16, | ||
387 | IntTy::I32 => IntBitness::X32, | ||
388 | IntTy::I64 => IntBitness::X64, | ||
389 | IntTy::I128 => IntBitness::X128, | ||
390 | } | ||
391 | } | ||
392 | |||
393 | fn int_ty_to_chalk(int_ty: IntTy) -> Scalar { | ||
394 | use chalk_ir::{IntTy, UintTy}; | ||
395 | |||
396 | match int_ty.signedness { | ||
397 | Signedness::Signed => Scalar::Int(match int_ty.bitness { | ||
398 | IntBitness::Xsize => IntTy::Isize, | ||
399 | IntBitness::X8 => IntTy::I8, | ||
400 | IntBitness::X16 => IntTy::I16, | ||
401 | IntBitness::X32 => IntTy::I32, | ||
402 | IntBitness::X64 => IntTy::I64, | ||
403 | IntBitness::X128 => IntTy::I128, | ||
404 | }), | ||
405 | Signedness::Unsigned => Scalar::Uint(match int_ty.bitness { | ||
406 | IntBitness::Xsize => UintTy::Usize, | ||
407 | IntBitness::X8 => UintTy::U8, | ||
408 | IntBitness::X16 => UintTy::U16, | ||
409 | IntBitness::X32 => UintTy::U32, | ||
410 | IntBitness::X64 => UintTy::U64, | ||
411 | IntBitness::X128 => UintTy::U128, | ||
412 | }), | ||
413 | } | ||
414 | } | ||
415 | |||
416 | impl ToChalk for Mutability { | 396 | impl ToChalk for Mutability { |
417 | type Chalk = chalk_ir::Mutability; | 397 | type Chalk = chalk_ir::Mutability; |
418 | fn to_chalk(self, _db: &dyn HirDatabase) -> Self::Chalk { | 398 | fn to_chalk(self, _db: &dyn HirDatabase) -> Self::Chalk { |