|
| 1 | +use crate::clauses::ClauseBuilder; |
| 2 | +use crate::infer::instantiate::IntoBindersAndValue; |
| 3 | +use crate::rust_ir::WellKnownTrait; |
| 4 | +use crate::{Interner, RustIrDatabase, TraitRef}; |
| 5 | +use chalk_ir::{ |
| 6 | + AliasTy, ApplicationTy, Binders, Floundered, Normalize, ProjectionTy, Substitution, TraitId, |
| 7 | + Ty, TyData, TypeName, VariableKinds, |
| 8 | +}; |
| 9 | + |
| 10 | +/// Handles clauses for FnOnce/FnMut/Fn. |
| 11 | +/// If `self_ty` is a function, we push a clause of the form |
| 12 | +/// `fn(A1, A2, ..., AN) -> O: FnTrait<(A1, A2, ..., AN)>`, where `FnTrait` |
| 13 | +/// is the trait corresponding to `trait_id` (FnOnce/FnMut/Fn) |
| 14 | +/// |
| 15 | +/// If `trait_id` is `FnOnce`, we also push a clause for the output type of the form: |
| 16 | +/// `Normalize(<fn(A) -> B as FnOnce<(A,)>>::Output -> B)` |
| 17 | +/// We do not add the usual `Implemented(fn(A) -> b as FnOnce<(A,)>` clause |
| 18 | +/// as a condition, since we already called `push_fact` with it |
| 19 | +pub fn add_fn_trait_program_clauses<I: Interner>( |
| 20 | + db: &dyn RustIrDatabase<I>, |
| 21 | + builder: &mut ClauseBuilder<'_, I>, |
| 22 | + trait_id: TraitId<I>, |
| 23 | + self_ty: Ty<I>, |
| 24 | +) -> Result<(), Floundered> { |
| 25 | + let interner = db.interner(); |
| 26 | + match self_ty.data(interner) { |
| 27 | + TyData::Function(fn_val) => { |
| 28 | + let (binders, orig_sub) = fn_val.into_binders_and_value(interner); |
| 29 | + let bound_ref = Binders::new(VariableKinds::from(interner, binders), orig_sub); |
| 30 | + builder.push_binders(&bound_ref, |builder, orig_sub| { |
| 31 | + // The last parameter represents the function return type |
| 32 | + let (arg_sub, fn_output_ty) = orig_sub |
| 33 | + .parameters(interner) |
| 34 | + .split_at(orig_sub.len(interner) - 1); |
| 35 | + let arg_sub = Substitution::from(interner, arg_sub); |
| 36 | + let fn_output_ty = fn_output_ty[0].assert_ty_ref(interner); |
| 37 | + |
| 38 | + // We are constructing a reference to `FnOnce<Args>`, where |
| 39 | + // `Args` is a tuple of the function's argument types |
| 40 | + let tupled = Ty::new( |
| 41 | + interner, |
| 42 | + TyData::Apply(ApplicationTy { |
| 43 | + name: TypeName::Tuple(arg_sub.len(interner)), |
| 44 | + substitution: arg_sub.clone(), |
| 45 | + }), |
| 46 | + ); |
| 47 | + |
| 48 | + let tupled_sub = Substitution::from(interner, vec![self_ty.clone(), tupled]); |
| 49 | + // Given a function type `fn(A1, A2, ..., AN)`, construct a `TraitRef` |
| 50 | + // of the form `fn(A1, A2, ..., AN): FnOnce<(A1, A2, ..., AN)>` |
| 51 | + let new_trait_ref = TraitRef { |
| 52 | + trait_id, |
| 53 | + substitution: tupled_sub.clone(), |
| 54 | + }; |
| 55 | + |
| 56 | + builder.push_fact(new_trait_ref.clone()); |
| 57 | + |
| 58 | + if let Some(WellKnownTrait::FnOnce) = db.trait_datum(trait_id).well_known { |
| 59 | + //The `Output` type is defined on the `FnOnce` |
| 60 | + let fn_once = db.trait_datum(trait_id); |
| 61 | + assert_eq!(fn_once.well_known, Some(WellKnownTrait::FnOnce)); |
| 62 | + let assoc_types = &fn_once.associated_ty_ids; |
| 63 | + assert_eq!( |
| 64 | + assoc_types.len(), |
| 65 | + 1, |
| 66 | + "FnOnce trait should have exactly one associated type, found {:?}", |
| 67 | + assoc_types |
| 68 | + ); |
| 69 | + |
| 70 | + // Construct `Normalize(<fn(A) -> B as FnOnce<(A,)>>::Output -> B)` |
| 71 | + let assoc_output_ty = assoc_types[0]; |
| 72 | + let proj_ty = ProjectionTy { |
| 73 | + associated_ty_id: assoc_output_ty, |
| 74 | + substitution: tupled_sub, |
| 75 | + }; |
| 76 | + let normalize = Normalize { |
| 77 | + alias: AliasTy::Projection(proj_ty), |
| 78 | + ty: fn_output_ty.clone(), |
| 79 | + }; |
| 80 | + |
| 81 | + builder.push_fact(normalize); |
| 82 | + } |
| 83 | + }); |
| 84 | + Ok(()) |
| 85 | + } |
| 86 | + // Function traits are non-enumerable |
| 87 | + TyData::InferenceVar(..) | TyData::Alias(..) => Err(Floundered), |
| 88 | + _ => Ok(()), |
| 89 | + } |
| 90 | +} |
0 commit comments