-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FnOnce trait, and provide impl for Function type
Works towards #363 I've followed the approach taken by rustc, where `FnOnce` has single generic argument: the tupled function parameters (e.g. `fn(u8, bool): FnOnce<(u8, bool)>`). I also extended the grammar to allow functions to take more than one argument. I've left `Fn` and `FnMut` for a separate PR - without a representation of closures in Chalk, they are not very useful.
- Loading branch information
Showing
9 changed files
with
141 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::clauses::ClauseBuilder; | ||
use crate::infer::instantiate::IntoBindersAndValue; | ||
use crate::{Interner, RustIrDatabase, TraitRef}; | ||
use chalk_ir::{ApplicationTy, Binders, Substitution, Ty, TyData, TypeName, VariableKinds}; | ||
|
||
pub fn add_fn_once_program_clauses<I: Interner>( | ||
db: &dyn RustIrDatabase<I>, | ||
builder: &mut ClauseBuilder<'_, I>, | ||
trait_ref: &TraitRef<I>, | ||
ty: &TyData<I>, | ||
) { | ||
match ty { | ||
TyData::Function(fn_val) => { | ||
let interner = db.interner(); | ||
let (binders, sub) = fn_val.into_binders_and_value(interner); | ||
|
||
// We are constructing a reference to `FnOnce<Args>`, where | ||
// `Args` is a tuple of the function's argument types | ||
let tupled = Ty::new( | ||
interner, | ||
TyData::Apply(ApplicationTy { | ||
name: TypeName::Tuple(sub.len(interner)), | ||
substitution: sub.clone(), | ||
}), | ||
); | ||
|
||
let self_ty = Ty::new(interner, ty); | ||
|
||
let tupled_sub = Substitution::from(interner, vec![self_ty, tupled]); | ||
// Given a function type `fn(A1, A2, ..., AN)`, construct a `TraitRef` | ||
// of the form `fn(A1, A2, ..., AN): FnOnce<(A1, A2, ..., AN)>` | ||
let new_trait_ref = TraitRef { | ||
trait_id: trait_ref.trait_id, | ||
substitution: tupled_sub, | ||
}; | ||
|
||
// Functions types come with a binder, which we push so | ||
// that the `TraitRef` properly references any bound lifetimes | ||
// (e.g. `for<'a> fn(&'a u8): FnOnce<(&'b u8)>`) | ||
let bound_ref = Binders::new(VariableKinds::from(interner, binders), new_trait_ref); | ||
builder.push_binders(&bound_ref, |this, inner_trait| { | ||
this.push_fact(inner_trait); | ||
}) | ||
} | ||
_ => {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters