1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
|
//! This module contains the implementations of the `ToChalk` trait, which
//! handles conversion between our data types and their corresponding types in
//! Chalk (in both directions); plus some helper functions for more specialized
//! conversions.
use chalk_ir::{
cast::Cast, fold::shift::Shift, interner::HasInterner, PlaceholderIndex, Scalar, TypeName,
UniverseIndex,
};
use chalk_solve::rust_ir;
use hir_def::{type_ref::Mutability, AssocContainerId, GenericDefId, Lookup, TypeAliasId};
use ra_db::salsa::InternKey;
use crate::{
db::HirDatabase,
primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness, Uncertain},
traits::{builtin, AssocTyValue, Canonical, Impl, Obligation},
ApplicationTy, CallableDef, GenericPredicate, InEnvironment, ProjectionPredicate, ProjectionTy,
Substs, TraitEnvironment, TraitRef, Ty, TypeCtor,
};
use super::interner::*;
use super::*;
impl ToChalk for Ty {
type Chalk = chalk_ir::Ty<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> {
match self {
Ty::Apply(apply_ty) => match apply_ty.ctor {
TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters),
TypeCtor::FnPtr { num_args: _ } => {
let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner);
chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: 0, substitution })
.intern(&Interner)
}
_ => {
let name = apply_ty.ctor.to_chalk(db);
let substitution = apply_ty.parameters.to_chalk(db);
chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner)
}
},
Ty::Projection(proj_ty) => {
let associated_ty_id = proj_ty.associated_ty.to_chalk(db);
let substitution = proj_ty.parameters.to_chalk(db);
chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy {
associated_ty_id,
substitution,
})
.cast(&Interner)
.intern(&Interner)
}
Ty::Placeholder(id) => {
let interned_id = db.intern_type_param_id(id);
PlaceholderIndex {
ui: UniverseIndex::ROOT,
idx: interned_id.as_intern_id().as_usize(),
}
.to_ty::<Interner>(&Interner)
}
Ty::Bound(idx) => chalk_ir::TyData::BoundVar(idx).intern(&Interner),
Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"),
Ty::Dyn(predicates) => {
let where_clauses = chalk_ir::QuantifiedWhereClauses::from(
&Interner,
predicates.iter().filter(|p| !p.is_error()).cloned().map(|p| p.to_chalk(db)),
);
let bounded_ty = chalk_ir::DynTy { bounds: make_binders(where_clauses, 1) };
chalk_ir::TyData::Dyn(bounded_ty).intern(&Interner)
}
Ty::Opaque(_) | Ty::Unknown => {
let substitution = chalk_ir::Substitution::empty(&Interner);
let name = TypeName::Error;
chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner)
}
}
}
fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self {
match chalk.data(&Interner).clone() {
chalk_ir::TyData::Apply(apply_ty) => match apply_ty.name {
TypeName::Error => Ty::Unknown,
TypeName::Ref(m) => ref_from_chalk(db, m, apply_ty.substitution),
_ => {
let ctor = from_chalk(db, apply_ty.name);
let parameters = from_chalk(db, apply_ty.substitution);
Ty::Apply(ApplicationTy { ctor, parameters })
}
},
chalk_ir::TyData::Placeholder(idx) => {
assert_eq!(idx.ui, UniverseIndex::ROOT);
let interned_id = crate::db::GlobalTypeParamId::from_intern_id(
crate::salsa::InternId::from(idx.idx),
);
Ty::Placeholder(db.lookup_intern_type_param_id(interned_id))
}
chalk_ir::TyData::Alias(chalk_ir::AliasTy::Projection(proj)) => {
let associated_ty = from_chalk(db, proj.associated_ty_id);
let parameters = from_chalk(db, proj.substitution);
Ty::Projection(ProjectionTy { associated_ty, parameters })
}
chalk_ir::TyData::Alias(chalk_ir::AliasTy::Opaque(_)) => unimplemented!(),
chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: _, substitution }) => {
let parameters: Substs = from_chalk(db, substitution);
Ty::Apply(ApplicationTy {
ctor: TypeCtor::FnPtr { num_args: (parameters.len() - 1) as u16 },
parameters,
})
}
chalk_ir::TyData::BoundVar(idx) => Ty::Bound(idx),
chalk_ir::TyData::InferenceVar(_iv, _kind) => Ty::Unknown,
chalk_ir::TyData::Dyn(where_clauses) => {
assert_eq!(where_clauses.bounds.binders.len(&Interner), 1);
let predicates = where_clauses
.bounds
.skip_binders()
.iter(&Interner)
.map(|c| from_chalk(db, c.clone()))
.collect();
Ty::Dyn(predicates)
}
}
}
}
const LIFETIME_PLACEHOLDER: PlaceholderIndex =
PlaceholderIndex { ui: UniverseIndex::ROOT, idx: usize::MAX };
/// We currently don't model lifetimes, but Chalk does. So, we have to insert a
/// fake lifetime here, because Chalks built-in logic may expect it to be there.
fn ref_to_chalk(
db: &dyn HirDatabase,
mutability: Mutability,
subst: Substs,
) -> chalk_ir::Ty<Interner> {
let arg = subst[0].clone().to_chalk(db);
let lifetime = LIFETIME_PLACEHOLDER.to_lifetime(&Interner);
chalk_ir::ApplicationTy {
name: TypeName::Ref(mutability.to_chalk(db)),
substitution: chalk_ir::Substitution::from(
&Interner,
vec![lifetime.cast(&Interner), arg.cast(&Interner)],
),
}
.intern(&Interner)
}
/// Here we remove the lifetime from the type we got from Chalk.
fn ref_from_chalk(
db: &dyn HirDatabase,
mutability: chalk_ir::Mutability,
subst: chalk_ir::Substitution<Interner>,
) -> Ty {
let tys = subst
.iter(&Interner)
.filter_map(|p| Some(from_chalk(db, p.ty(&Interner)?.clone())))
.collect();
Ty::apply(TypeCtor::Ref(from_chalk(db, mutability)), Substs(tys))
}
impl ToChalk for Substs {
type Chalk = chalk_ir::Substitution<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution<Interner> {
chalk_ir::Substitution::from(&Interner, self.iter().map(|ty| ty.clone().to_chalk(db)))
}
fn from_chalk(db: &dyn HirDatabase, parameters: chalk_ir::Substitution<Interner>) -> Substs {
let tys = parameters
.iter(&Interner)
.map(|p| match p.ty(&Interner) {
Some(ty) => from_chalk(db, ty.clone()),
None => unimplemented!(),
})
.collect();
Substs(tys)
}
}
impl ToChalk for TraitRef {
type Chalk = chalk_ir::TraitRef<Interner>;
fn to_chalk(self: TraitRef, db: &dyn HirDatabase) -> chalk_ir::TraitRef<Interner> {
let trait_id = self.trait_.to_chalk(db);
let substitution = self.substs.to_chalk(db);
chalk_ir::TraitRef { trait_id, substitution }
}
fn from_chalk(db: &dyn HirDatabase, trait_ref: chalk_ir::TraitRef<Interner>) -> Self {
let trait_ = from_chalk(db, trait_ref.trait_id);
let substs = from_chalk(db, trait_ref.substitution);
TraitRef { trait_, substs }
}
}
impl ToChalk for hir_def::TraitId {
type Chalk = TraitId;
fn to_chalk(self, _db: &dyn HirDatabase) -> TraitId {
chalk_ir::TraitId(self.as_intern_id())
}
fn from_chalk(_db: &dyn HirDatabase, trait_id: TraitId) -> hir_def::TraitId {
InternKey::from_intern_id(trait_id.0)
}
}
impl ToChalk for TypeCtor {
type Chalk = TypeName<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> TypeName<Interner> {
match self {
TypeCtor::AssociatedType(type_alias) => {
let type_id = type_alias.to_chalk(db);
TypeName::AssociatedType(type_id)
}
TypeCtor::Bool => TypeName::Scalar(Scalar::Bool),
TypeCtor::Char => TypeName::Scalar(Scalar::Char),
TypeCtor::Int(Uncertain::Known(int_ty)) => TypeName::Scalar(int_ty_to_chalk(int_ty)),
TypeCtor::Float(Uncertain::Known(FloatTy { bitness: FloatBitness::X32 })) => {
TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F32))
}
TypeCtor::Float(Uncertain::Known(FloatTy { bitness: FloatBitness::X64 })) => {
TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F64))
}
TypeCtor::Tuple { cardinality } => TypeName::Tuple(cardinality.into()),
TypeCtor::RawPtr(mutability) => TypeName::Raw(mutability.to_chalk(db)),
TypeCtor::Slice => TypeName::Slice,
TypeCtor::Ref(mutability) => TypeName::Ref(mutability.to_chalk(db)),
TypeCtor::Str => TypeName::Str,
TypeCtor::FnDef(callable_def) => {
let id = callable_def.to_chalk(db);
TypeName::FnDef(id)
}
TypeCtor::Never => TypeName::Never,
TypeCtor::Int(Uncertain::Unknown)
| TypeCtor::Float(Uncertain::Unknown)
| TypeCtor::Adt(_)
| TypeCtor::Array
| TypeCtor::FnPtr { .. }
| TypeCtor::Closure { .. } => {
// other TypeCtors get interned and turned into a chalk StructId
let struct_id = db.intern_type_ctor(self).into();
TypeName::Adt(struct_id)
}
}
}
fn from_chalk(db: &dyn HirDatabase, type_name: TypeName<Interner>) -> TypeCtor {
match type_name {
TypeName::Adt(struct_id) => db.lookup_intern_type_ctor(struct_id.into()),
TypeName::AssociatedType(type_id) => TypeCtor::AssociatedType(from_chalk(db, type_id)),
TypeName::OpaqueType(_) => unreachable!(),
TypeName::Scalar(Scalar::Bool) => TypeCtor::Bool,
TypeName::Scalar(Scalar::Char) => TypeCtor::Char,
TypeName::Scalar(Scalar::Int(int_ty)) => TypeCtor::Int(Uncertain::Known(IntTy {
signedness: Signedness::Signed,
bitness: bitness_from_chalk_int(int_ty),
})),
TypeName::Scalar(Scalar::Uint(uint_ty)) => TypeCtor::Int(Uncertain::Known(IntTy {
signedness: Signedness::Unsigned,
bitness: bitness_from_chalk_uint(uint_ty),
})),
TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F32)) => {
TypeCtor::Float(Uncertain::Known(FloatTy { bitness: FloatBitness::X32 }))
}
TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F64)) => {
TypeCtor::Float(Uncertain::Known(FloatTy { bitness: FloatBitness::X64 }))
}
TypeName::Tuple(cardinality) => TypeCtor::Tuple { cardinality: cardinality as u16 },
TypeName::Raw(mutability) => TypeCtor::RawPtr(from_chalk(db, mutability)),
TypeName::Slice => TypeCtor::Slice,
TypeName::Ref(mutability) => TypeCtor::Ref(from_chalk(db, mutability)),
TypeName::Str => TypeCtor::Str,
TypeName::Never => TypeCtor::Never,
TypeName::FnDef(fn_def_id) => {
let callable_def = from_chalk(db, fn_def_id);
TypeCtor::FnDef(callable_def)
}
TypeName::Array | TypeName::Error => {
// this should not be reached, since we don't represent TypeName::Error with TypeCtor
unreachable!()
}
}
}
}
fn bitness_from_chalk_uint(uint_ty: chalk_ir::UintTy) -> IntBitness {
use chalk_ir::UintTy;
match uint_ty {
UintTy::Usize => IntBitness::Xsize,
UintTy::U8 => IntBitness::X8,
UintTy::U16 => IntBitness::X16,
UintTy::U32 => IntBitness::X32,
UintTy::U64 => IntBitness::X64,
UintTy::U128 => IntBitness::X128,
}
}
fn bitness_from_chalk_int(int_ty: chalk_ir::IntTy) -> IntBitness {
use chalk_ir::IntTy;
match int_ty {
IntTy::Isize => IntBitness::Xsize,
IntTy::I8 => IntBitness::X8,
IntTy::I16 => IntBitness::X16,
IntTy::I32 => IntBitness::X32,
IntTy::I64 => IntBitness::X64,
IntTy::I128 => IntBitness::X128,
}
}
fn int_ty_to_chalk(int_ty: IntTy) -> Scalar {
use chalk_ir::{IntTy, UintTy};
match int_ty.signedness {
Signedness::Signed => Scalar::Int(match int_ty.bitness {
IntBitness::Xsize => IntTy::Isize,
IntBitness::X8 => IntTy::I8,
IntBitness::X16 => IntTy::I16,
IntBitness::X32 => IntTy::I32,
IntBitness::X64 => IntTy::I64,
IntBitness::X128 => IntTy::I128,
}),
Signedness::Unsigned => Scalar::Uint(match int_ty.bitness {
IntBitness::Xsize => UintTy::Usize,
IntBitness::X8 => UintTy::U8,
IntBitness::X16 => UintTy::U16,
IntBitness::X32 => UintTy::U32,
IntBitness::X64 => UintTy::U64,
IntBitness::X128 => UintTy::U128,
}),
}
}
impl ToChalk for Mutability {
type Chalk = chalk_ir::Mutability;
fn to_chalk(self, _db: &dyn HirDatabase) -> Self::Chalk {
match self {
Mutability::Shared => chalk_ir::Mutability::Not,
Mutability::Mut => chalk_ir::Mutability::Mut,
}
}
fn from_chalk(_db: &dyn HirDatabase, chalk: Self::Chalk) -> Self {
match chalk {
chalk_ir::Mutability::Mut => Mutability::Mut,
chalk_ir::Mutability::Not => Mutability::Shared,
}
}
}
impl ToChalk for Impl {
type Chalk = ImplId;
fn to_chalk(self, db: &dyn HirDatabase) -> ImplId {
db.intern_chalk_impl(self).into()
}
fn from_chalk(db: &dyn HirDatabase, impl_id: ImplId) -> Impl {
db.lookup_intern_chalk_impl(impl_id.into())
}
}
impl ToChalk for CallableDef {
type Chalk = FnDefId;
fn to_chalk(self, db: &dyn HirDatabase) -> FnDefId {
db.intern_callable_def(self).into()
}
fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDef {
db.lookup_intern_callable_def(fn_def_id.into())
}
}
impl ToChalk for TypeAliasId {
type Chalk = AssocTypeId;
fn to_chalk(self, _db: &dyn HirDatabase) -> AssocTypeId {
chalk_ir::AssocTypeId(self.as_intern_id())
}
fn from_chalk(_db: &dyn HirDatabase, type_alias_id: AssocTypeId) -> TypeAliasId {
InternKey::from_intern_id(type_alias_id.0)
}
}
impl ToChalk for AssocTyValue {
type Chalk = AssociatedTyValueId;
fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValueId {
db.intern_assoc_ty_value(self).into()
}
fn from_chalk(db: &dyn HirDatabase, assoc_ty_value_id: AssociatedTyValueId) -> AssocTyValue {
db.lookup_intern_assoc_ty_value(assoc_ty_value_id.into())
}
}
impl ToChalk for GenericPredicate {
type Chalk = chalk_ir::QuantifiedWhereClause<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::QuantifiedWhereClause<Interner> {
match self {
GenericPredicate::Implemented(trait_ref) => {
let chalk_trait_ref = trait_ref.to_chalk(db);
let chalk_trait_ref = chalk_trait_ref.shifted_in(&Interner);
make_binders(chalk_ir::WhereClause::Implemented(chalk_trait_ref), 0)
}
GenericPredicate::Projection(projection_pred) => {
let ty = projection_pred.ty.to_chalk(db).shifted_in(&Interner);
let projection = projection_pred.projection_ty.to_chalk(db).shifted_in(&Interner);
let alias = chalk_ir::AliasTy::Projection(projection);
make_binders(chalk_ir::WhereClause::AliasEq(chalk_ir::AliasEq { alias, ty }), 0)
}
GenericPredicate::Error => panic!("tried passing GenericPredicate::Error to Chalk"),
}
}
fn from_chalk(
db: &dyn HirDatabase,
where_clause: chalk_ir::QuantifiedWhereClause<Interner>,
) -> GenericPredicate {
// we don't produce any where clauses with binders and can't currently deal with them
match where_clause
.skip_binders()
.shifted_out(&Interner)
.expect("unexpected bound vars in where clause")
{
chalk_ir::WhereClause::Implemented(tr) => {
GenericPredicate::Implemented(from_chalk(db, tr))
}
chalk_ir::WhereClause::AliasEq(projection_eq) => {
let projection_ty = from_chalk(
db,
match projection_eq.alias {
chalk_ir::AliasTy::Projection(p) => p,
_ => unimplemented!(),
},
);
let ty = from_chalk(db, projection_eq.ty);
GenericPredicate::Projection(ProjectionPredicate { projection_ty, ty })
}
}
}
}
impl ToChalk for ProjectionTy {
type Chalk = chalk_ir::ProjectionTy<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> {
chalk_ir::ProjectionTy {
associated_ty_id: self.associated_ty.to_chalk(db),
substitution: self.parameters.to_chalk(db),
}
}
fn from_chalk(
db: &dyn HirDatabase,
projection_ty: chalk_ir::ProjectionTy<Interner>,
) -> ProjectionTy {
ProjectionTy {
associated_ty: from_chalk(db, projection_ty.associated_ty_id),
parameters: from_chalk(db, projection_ty.substitution),
}
}
}
impl ToChalk for ProjectionPredicate {
type Chalk = chalk_ir::AliasEq<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::AliasEq<Interner> {
chalk_ir::AliasEq {
alias: chalk_ir::AliasTy::Projection(self.projection_ty.to_chalk(db)),
ty: self.ty.to_chalk(db),
}
}
fn from_chalk(_db: &dyn HirDatabase, _normalize: chalk_ir::AliasEq<Interner>) -> Self {
unimplemented!()
}
}
impl ToChalk for Obligation {
type Chalk = chalk_ir::DomainGoal<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::DomainGoal<Interner> {
match self {
Obligation::Trait(tr) => tr.to_chalk(db).cast(&Interner),
Obligation::Projection(pr) => pr.to_chalk(db).cast(&Interner),
}
}
fn from_chalk(_db: &dyn HirDatabase, _goal: chalk_ir::DomainGoal<Interner>) -> Self {
unimplemented!()
}
}
impl<T> ToChalk for Canonical<T>
where
T: ToChalk,
T::Chalk: HasInterner<Interner = Interner>,
{
type Chalk = chalk_ir::Canonical<T::Chalk>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical<T::Chalk> {
let parameter = chalk_ir::CanonicalVarKind::new(
chalk_ir::VariableKind::Ty(chalk_ir::TyKind::General),
chalk_ir::UniverseIndex::ROOT,
);
let value = self.value.to_chalk(db);
chalk_ir::Canonical {
value,
binders: chalk_ir::CanonicalVarKinds::from(&Interner, vec![parameter; self.num_vars]),
}
}
fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical<T::Chalk>) -> Canonical<T> {
Canonical {
num_vars: canonical.binders.len(&Interner),
value: from_chalk(db, canonical.value),
}
}
}
impl ToChalk for Arc<TraitEnvironment> {
type Chalk = chalk_ir::Environment<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Environment<Interner> {
let mut clauses = Vec::new();
for pred in &self.predicates {
if pred.is_error() {
// for env, we just ignore errors
continue;
}
let program_clause: chalk_ir::ProgramClause<Interner> =
pred.clone().to_chalk(db).cast(&Interner);
clauses.push(program_clause.into_from_env_clause(&Interner));
}
chalk_ir::Environment::new(&Interner).add_clauses(&Interner, clauses)
}
fn from_chalk(
_db: &dyn HirDatabase,
_env: chalk_ir::Environment<Interner>,
) -> Arc<TraitEnvironment> {
unimplemented!()
}
}
impl<T: ToChalk> ToChalk for InEnvironment<T>
where
T::Chalk: chalk_ir::interner::HasInterner<Interner = Interner>,
{
type Chalk = chalk_ir::InEnvironment<T::Chalk>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::InEnvironment<T::Chalk> {
chalk_ir::InEnvironment {
environment: self.environment.to_chalk(db),
goal: self.value.to_chalk(db),
}
}
fn from_chalk(
db: &dyn HirDatabase,
in_env: chalk_ir::InEnvironment<T::Chalk>,
) -> InEnvironment<T> {
InEnvironment {
environment: from_chalk(db, in_env.environment),
value: from_chalk(db, in_env.goal),
}
}
}
impl ToChalk for builtin::BuiltinImplData {
type Chalk = ImplDatum;
fn to_chalk(self, db: &dyn HirDatabase) -> ImplDatum {
let impl_type = rust_ir::ImplType::External;
let where_clauses = self.where_clauses.into_iter().map(|w| w.to_chalk(db)).collect();
let impl_datum_bound =
rust_ir::ImplDatumBound { trait_ref: self.trait_ref.to_chalk(db), where_clauses };
let associated_ty_value_ids =
self.assoc_ty_values.into_iter().map(|v| v.to_chalk(db)).collect();
rust_ir::ImplDatum {
binders: make_binders(impl_datum_bound, self.num_vars),
impl_type,
polarity: rust_ir::Polarity::Positive,
associated_ty_value_ids,
}
}
fn from_chalk(_db: &dyn HirDatabase, _data: ImplDatum) -> Self {
unimplemented!()
}
}
impl ToChalk for builtin::BuiltinImplAssocTyValueData {
type Chalk = AssociatedTyValue;
fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValue {
let ty = self.value.to_chalk(db);
let value_bound = rust_ir::AssociatedTyValueBound { ty };
rust_ir::AssociatedTyValue {
associated_ty_id: self.assoc_ty_id.to_chalk(db),
impl_id: self.impl_.to_chalk(db),
value: make_binders(value_bound, self.num_vars),
}
}
fn from_chalk(
_db: &dyn HirDatabase,
_data: AssociatedTyValue,
) -> builtin::BuiltinImplAssocTyValueData {
unimplemented!()
}
}
pub(super) fn make_binders<T>(value: T, num_vars: usize) -> chalk_ir::Binders<T>
where
T: HasInterner<Interner = Interner>,
{
chalk_ir::Binders::new(
chalk_ir::VariableKinds::from(
&Interner,
std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyKind::General)).take(num_vars),
),
value,
)
}
pub(super) fn convert_where_clauses(
db: &dyn HirDatabase,
def: GenericDefId,
substs: &Substs,
) -> Vec<chalk_ir::QuantifiedWhereClause<Interner>> {
let generic_predicates = db.generic_predicates(def);
let mut result = Vec::with_capacity(generic_predicates.len());
for pred in generic_predicates.iter() {
if pred.value.is_error() {
// skip errored predicates completely
continue;
}
result.push(pred.clone().subst(substs).to_chalk(db));
}
result
}
pub(super) fn generic_predicate_to_inline_bound(
db: &dyn HirDatabase,
pred: &GenericPredicate,
self_ty: &Ty,
) -> Option<rust_ir::InlineBound<Interner>> {
// An InlineBound is like a GenericPredicate, except the self type is left out.
// We don't have a special type for this, but Chalk does.
match pred {
GenericPredicate::Implemented(trait_ref) => {
if &trait_ref.substs[0] != self_ty {
// we can only convert predicates back to type bounds if they
// have the expected self type
return None;
}
let args_no_self = trait_ref.substs[1..]
.iter()
.map(|ty| ty.clone().to_chalk(db).cast(&Interner))
.collect();
let trait_bound =
rust_ir::TraitBound { trait_id: trait_ref.trait_.to_chalk(db), args_no_self };
Some(rust_ir::InlineBound::TraitBound(trait_bound))
}
GenericPredicate::Projection(proj) => {
if &proj.projection_ty.parameters[0] != self_ty {
return None;
}
let trait_ = match proj.projection_ty.associated_ty.lookup(db.upcast()).container {
AssocContainerId::TraitId(t) => t,
_ => panic!("associated type not in trait"),
};
let args_no_self = proj.projection_ty.parameters[1..]
.iter()
.map(|ty| ty.clone().to_chalk(db).cast(&Interner))
.collect();
let alias_eq_bound = rust_ir::AliasEqBound {
value: proj.ty.clone().to_chalk(db),
trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self },
associated_ty_id: proj.projection_ty.associated_ty.to_chalk(db),
parameters: Vec::new(), // FIXME we don't support generic associated types yet
};
Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound))
}
GenericPredicate::Error => None,
}
}
|