Use Chalk's Ty::Function for function pointer types

Function pointers can be 'higher-ranked' over lifetimes, which is why they're
not an application type in Chalk, but since we don't model lifetimes it doesn't
matter for us yet.
This commit is contained in:
Florian Diebold 2020-05-22 19:13:17 +02:00 committed by Florian Diebold
parent bfbc210bc1
commit 194dd9eb0d
2 changed files with 51 additions and 8 deletions

View file

@ -2685,6 +2685,38 @@ fn test() {
);
}
#[test]
fn builtin_fn_ptr_copy() {
assert_snapshot!(
infer_with_mismatches(r#"
#[lang = "copy"]
trait Copy {}
trait Test { fn test(&self) -> bool; }
impl<T: Copy> Test for T {}
fn test(f1: fn(), f2: fn(usize) -> u8, f3: fn(u8, u8) -> &u8) {
f1.test();
f2.test();
f3.test();
}
"#, true),
@r###"
55..59 'self': &Self
109..111 'f1': fn()
119..121 'f2': fn(usize) -> u8
140..142 'f3': fn(u8, u8) -> &u8
163..211 '{ ...t(); }': ()
169..171 'f1': fn()
169..178 'f1.test()': bool
184..186 'f2': fn(usize) -> u8
184..193 'f2.test()': bool
199..201 'f3': fn(u8, u8) -> &u8
199..208 'f3.test()': bool
"###
);
}
#[test]
fn builtin_sized() {
assert_snapshot!(

View file

@ -26,14 +26,19 @@ 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) => {
if let TypeCtor::Ref(m) = apply_ty.ctor {
return ref_to_chalk(db, m, apply_ty.parameters);
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)
}
_ => {
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);
@ -93,7 +98,13 @@ impl ToChalk for Ty {
Ty::Projection(ProjectionTy { associated_ty, parameters })
}
chalk_ir::TyData::Alias(chalk_ir::AliasTy::Opaque(_)) => unimplemented!(),
chalk_ir::TyData::Function(_) => 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) => Ty::Unknown,
chalk_ir::TyData::Dyn(where_clauses) => {