diff options
author | Florian Diebold <[email protected]> | 2020-05-22 18:13:17 +0100 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2020-05-22 20:05:28 +0100 |
commit | 194dd9eb0d44284f7e952a1e84296fcda4d90f5e (patch) | |
tree | 0a8f9cd65553cd6cae0a4cf3598adfcf61ac9688 /crates/ra_hir_ty | |
parent | bfbc210bc1216b79e355eb70449caf08dc67d5ad (diff) |
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.
Diffstat (limited to 'crates/ra_hir_ty')
-rw-r--r-- | crates/ra_hir_ty/src/tests/traits.rs | 32 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk/mapping.rs | 27 |
2 files changed, 51 insertions, 8 deletions
diff --git a/crates/ra_hir_ty/src/tests/traits.rs b/crates/ra_hir_ty/src/tests/traits.rs index 17c646076..0419bc751 100644 --- a/crates/ra_hir_ty/src/tests/traits.rs +++ b/crates/ra_hir_ty/src/tests/traits.rs | |||
@@ -2686,6 +2686,38 @@ fn test() { | |||
2686 | } | 2686 | } |
2687 | 2687 | ||
2688 | #[test] | 2688 | #[test] |
2689 | fn builtin_fn_ptr_copy() { | ||
2690 | assert_snapshot!( | ||
2691 | infer_with_mismatches(r#" | ||
2692 | #[lang = "copy"] | ||
2693 | trait Copy {} | ||
2694 | |||
2695 | trait Test { fn test(&self) -> bool; } | ||
2696 | impl<T: Copy> Test for T {} | ||
2697 | |||
2698 | fn test(f1: fn(), f2: fn(usize) -> u8, f3: fn(u8, u8) -> &u8) { | ||
2699 | f1.test(); | ||
2700 | f2.test(); | ||
2701 | f3.test(); | ||
2702 | } | ||
2703 | "#, true), | ||
2704 | @r###" | ||
2705 | 55..59 'self': &Self | ||
2706 | 109..111 'f1': fn() | ||
2707 | 119..121 'f2': fn(usize) -> u8 | ||
2708 | 140..142 'f3': fn(u8, u8) -> &u8 | ||
2709 | 163..211 '{ ...t(); }': () | ||
2710 | 169..171 'f1': fn() | ||
2711 | 169..178 'f1.test()': bool | ||
2712 | 184..186 'f2': fn(usize) -> u8 | ||
2713 | 184..193 'f2.test()': bool | ||
2714 | 199..201 'f3': fn(u8, u8) -> &u8 | ||
2715 | 199..208 'f3.test()': bool | ||
2716 | "### | ||
2717 | ); | ||
2718 | } | ||
2719 | |||
2720 | #[test] | ||
2689 | fn builtin_sized() { | 2721 | fn builtin_sized() { |
2690 | assert_snapshot!( | 2722 | assert_snapshot!( |
2691 | infer_with_mismatches(r#" | 2723 | infer_with_mismatches(r#" |
diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index 7841a0e21..7082cb095 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs | |||
@@ -26,14 +26,19 @@ impl ToChalk for Ty { | |||
26 | type Chalk = chalk_ir::Ty<Interner>; | 26 | type Chalk = chalk_ir::Ty<Interner>; |
27 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> { | 27 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> { |
28 | match self { | 28 | match self { |
29 | Ty::Apply(apply_ty) => { | 29 | Ty::Apply(apply_ty) => match apply_ty.ctor { |
30 | if let TypeCtor::Ref(m) = apply_ty.ctor { | 30 | TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), |
31 | return ref_to_chalk(db, m, apply_ty.parameters); | 31 | TypeCtor::FnPtr { num_args: _ } => { |
32 | let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); | ||
33 | chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: 0, substitution }) | ||
34 | .intern(&Interner) | ||
32 | } | 35 | } |
33 | let name = apply_ty.ctor.to_chalk(db); | 36 | _ => { |
34 | let substitution = apply_ty.parameters.to_chalk(db); | 37 | let name = apply_ty.ctor.to_chalk(db); |
35 | chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner) | 38 | let substitution = apply_ty.parameters.to_chalk(db); |
36 | } | 39 | chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner) |
40 | } | ||
41 | }, | ||
37 | Ty::Projection(proj_ty) => { | 42 | Ty::Projection(proj_ty) => { |
38 | let associated_ty_id = proj_ty.associated_ty.to_chalk(db); | 43 | let associated_ty_id = proj_ty.associated_ty.to_chalk(db); |
39 | let substitution = proj_ty.parameters.to_chalk(db); | 44 | let substitution = proj_ty.parameters.to_chalk(db); |
@@ -93,7 +98,13 @@ impl ToChalk for Ty { | |||
93 | Ty::Projection(ProjectionTy { associated_ty, parameters }) | 98 | Ty::Projection(ProjectionTy { associated_ty, parameters }) |
94 | } | 99 | } |
95 | chalk_ir::TyData::Alias(chalk_ir::AliasTy::Opaque(_)) => unimplemented!(), | 100 | chalk_ir::TyData::Alias(chalk_ir::AliasTy::Opaque(_)) => unimplemented!(), |
96 | chalk_ir::TyData::Function(_) => unimplemented!(), | 101 | chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: _, substitution }) => { |
102 | let parameters: Substs = from_chalk(db, substitution); | ||
103 | Ty::Apply(ApplicationTy { | ||
104 | ctor: TypeCtor::FnPtr { num_args: (parameters.len() - 1) as u16 }, | ||
105 | parameters, | ||
106 | }) | ||
107 | } | ||
97 | chalk_ir::TyData::BoundVar(idx) => Ty::Bound(idx), | 108 | chalk_ir::TyData::BoundVar(idx) => Ty::Bound(idx), |
98 | chalk_ir::TyData::InferenceVar(_iv) => Ty::Unknown, | 109 | chalk_ir::TyData::InferenceVar(_iv) => Ty::Unknown, |
99 | chalk_ir::TyData::Dyn(where_clauses) => { | 110 | chalk_ir::TyData::Dyn(where_clauses) => { |