|
| 1 | +use crate::clauses::ClauseBuilder; |
| 2 | +use crate::infer::instantiate::IntoBindersAndValue; |
| 3 | +use crate::{Interner, RustIrDatabase, TraitRef}; |
| 4 | +use chalk_ir::{TyData, ApplicationTy, TypeName, Substitution, Ty, Binders, VariableKinds}; |
| 5 | + |
| 6 | +pub fn add_fn_once_program_clauses<I: Interner>( |
| 7 | + db: &dyn RustIrDatabase<I>, |
| 8 | + builder: &mut ClauseBuilder<'_, I>, |
| 9 | + trait_ref: &TraitRef<I>, |
| 10 | + ty: &TyData<I>, |
| 11 | +) { |
| 12 | + match ty { |
| 13 | + TyData::Function(fn_val) => { |
| 14 | + let interner = db.interner(); |
| 15 | + let (binders, sub) = fn_val.into_binders_and_value(interner); |
| 16 | + |
| 17 | + // We are constructing a reference to `FnOnce<Args>`, where |
| 18 | + // `Args` is a tuple of the function's argument types |
| 19 | + let tupled = Ty::new(interner, TyData::Apply(ApplicationTy { |
| 20 | + name: TypeName::Tuple(sub.len(interner)), |
| 21 | + substitution: sub.clone() |
| 22 | + })); |
| 23 | + |
| 24 | + let self_ty = Ty::new(interner, ty); |
| 25 | + |
| 26 | + let tupled_sub = Substitution::from(interner, vec![self_ty, tupled]); |
| 27 | + // Given a function type `fn(A1, A2, ..., AN)`, construct a `TraitRef` |
| 28 | + // of the form `fn(A1, A2, ..., AN): FnOnce<(A1, A2, ..., AN)>` |
| 29 | + let new_trait_ref = TraitRef { |
| 30 | + trait_id: trait_ref.trait_id, |
| 31 | + substitution: tupled_sub |
| 32 | + }; |
| 33 | + |
| 34 | + // Functions types come with a binder, which we push so |
| 35 | + // that the `TraitRef` properly references any bound lifetimes |
| 36 | + // (e.g. `for<'a> fn(&'a u8): FnOnce<(&'b u8)>`) |
| 37 | + let bound_ref = Binders::new(VariableKinds::from(interner, binders), new_trait_ref); |
| 38 | + builder.push_binders(&bound_ref, |this, inner_trait| { |
| 39 | + this.push_fact(inner_trait); |
| 40 | + }) |
| 41 | + } |
| 42 | + _ => {} |
| 43 | + } |
| 44 | +} |
0 commit comments