diff options
author | Florian Diebold <[email protected]> | 2020-05-22 17:15:53 +0100 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2020-05-22 20:05:13 +0100 |
commit | bfbc210bc1216b79e355eb70449caf08dc67d5ad (patch) | |
tree | 405645695f7eac402b000f99bef634e49f9e0e6a /crates/ra_hir_ty/src/traits | |
parent | d4daca9f02ec46be1beef79e9ed647a3a24e2434 (diff) |
Use Chalk's built-in representation of function item types
Diffstat (limited to 'crates/ra_hir_ty/src/traits')
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk.rs | 39 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk/interner.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk/mapping.rs | 26 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk/tls.rs | 18 |
4 files changed, 75 insertions, 10 deletions
diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index e2f2a9ccb..5b0f12a3c 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs | |||
@@ -14,7 +14,7 @@ use ra_db::{salsa::InternKey, CrateId}; | |||
14 | use super::{builtin, AssocTyValue, ChalkContext, Impl}; | 14 | use super::{builtin, AssocTyValue, ChalkContext, Impl}; |
15 | use crate::{ | 15 | use crate::{ |
16 | db::HirDatabase, display::HirDisplay, method_resolution::TyFingerprint, utils::generics, | 16 | db::HirDatabase, display::HirDisplay, method_resolution::TyFingerprint, utils::generics, |
17 | DebruijnIndex, GenericPredicate, Substs, Ty, TypeCtor, | 17 | CallableDef, DebruijnIndex, GenericPredicate, Substs, Ty, TypeCtor, |
18 | }; | 18 | }; |
19 | use chalk_rust_ir::WellKnownTrait; | 19 | use chalk_rust_ir::WellKnownTrait; |
20 | use mapping::{convert_where_clauses, generic_predicate_to_inline_bound, make_binders}; | 20 | use mapping::{convert_where_clauses, generic_predicate_to_inline_bound, make_binders}; |
@@ -54,10 +54,9 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
54 | 54 | ||
55 | fn fn_def_datum( | 55 | fn fn_def_datum( |
56 | &self, | 56 | &self, |
57 | _fn_def_id: chalk_ir::FnDefId<Interner>, | 57 | fn_def_id: chalk_ir::FnDefId<Interner>, |
58 | ) -> Arc<chalk_rust_ir::FnDefDatum<Interner>> { | 58 | ) -> Arc<chalk_rust_ir::FnDefDatum<Interner>> { |
59 | // We don't yet provide any FnDefs to Chalk | 59 | self.db.fn_def_datum(self.krate, fn_def_id) |
60 | unimplemented!() | ||
61 | } | 60 | } |
62 | 61 | ||
63 | fn impls_for_trait( | 62 | fn impls_for_trait( |
@@ -405,6 +404,26 @@ fn type_alias_associated_ty_value( | |||
405 | Arc::new(value) | 404 | Arc::new(value) |
406 | } | 405 | } |
407 | 406 | ||
407 | pub(crate) fn fn_def_datum_query( | ||
408 | db: &dyn HirDatabase, | ||
409 | _krate: CrateId, | ||
410 | fn_def_id: FnDefId, | ||
411 | ) -> Arc<FnDefDatum> { | ||
412 | let callable_def: CallableDef = from_chalk(db, fn_def_id); | ||
413 | let generic_params = generics(db.upcast(), callable_def.into()); | ||
414 | let sig = db.callable_item_signature(callable_def); | ||
415 | let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | ||
416 | let where_clauses = convert_where_clauses(db, callable_def.into(), &bound_vars); | ||
417 | let bound = chalk_rust_ir::FnDefDatumBound { | ||
418 | // Note: Chalk doesn't actually use this information yet as far as I am aware, but we provide it anyway | ||
419 | argument_types: sig.value.params().iter().map(|ty| ty.clone().to_chalk(db)).collect(), | ||
420 | return_type: sig.value.ret().clone().to_chalk(db), | ||
421 | where_clauses, | ||
422 | }; | ||
423 | let datum = FnDefDatum { id: fn_def_id, binders: make_binders(bound, sig.num_binders) }; | ||
424 | Arc::new(datum) | ||
425 | } | ||
426 | |||
408 | impl From<AdtId> for crate::TypeCtorId { | 427 | impl From<AdtId> for crate::TypeCtorId { |
409 | fn from(struct_id: AdtId) -> Self { | 428 | fn from(struct_id: AdtId) -> Self { |
410 | struct_id.0 | 429 | struct_id.0 |
@@ -417,6 +436,18 @@ impl From<crate::TypeCtorId> for AdtId { | |||
417 | } | 436 | } |
418 | } | 437 | } |
419 | 438 | ||
439 | impl From<FnDefId> for crate::CallableDefId { | ||
440 | fn from(fn_def_id: FnDefId) -> Self { | ||
441 | InternKey::from_intern_id(fn_def_id.0) | ||
442 | } | ||
443 | } | ||
444 | |||
445 | impl From<crate::CallableDefId> for FnDefId { | ||
446 | fn from(callable_def_id: crate::CallableDefId) -> Self { | ||
447 | chalk_ir::FnDefId(callable_def_id.as_intern_id()) | ||
448 | } | ||
449 | } | ||
450 | |||
420 | impl From<ImplId> for crate::traits::GlobalImplId { | 451 | impl From<ImplId> for crate::traits::GlobalImplId { |
421 | fn from(impl_id: ImplId) -> Self { | 452 | fn from(impl_id: ImplId) -> Self { |
422 | InternKey::from_intern_id(impl_id.0) | 453 | InternKey::from_intern_id(impl_id.0) |
diff --git a/crates/ra_hir_ty/src/traits/chalk/interner.rs b/crates/ra_hir_ty/src/traits/chalk/interner.rs index 060372819..2a27f8ed8 100644 --- a/crates/ra_hir_ty/src/traits/chalk/interner.rs +++ b/crates/ra_hir_ty/src/traits/chalk/interner.rs | |||
@@ -20,6 +20,8 @@ pub type ImplId = chalk_ir::ImplId<Interner>; | |||
20 | pub type ImplDatum = chalk_rust_ir::ImplDatum<Interner>; | 20 | pub type ImplDatum = chalk_rust_ir::ImplDatum<Interner>; |
21 | pub type AssociatedTyValueId = chalk_rust_ir::AssociatedTyValueId<Interner>; | 21 | pub type AssociatedTyValueId = chalk_rust_ir::AssociatedTyValueId<Interner>; |
22 | pub type AssociatedTyValue = chalk_rust_ir::AssociatedTyValue<Interner>; | 22 | pub type AssociatedTyValue = chalk_rust_ir::AssociatedTyValue<Interner>; |
23 | pub type FnDefId = chalk_ir::FnDefId<Interner>; | ||
24 | pub type FnDefDatum = chalk_rust_ir::FnDefDatum<Interner>; | ||
23 | 25 | ||
24 | impl chalk_ir::interner::Interner for Interner { | 26 | impl chalk_ir::interner::Interner for Interner { |
25 | type InternedType = Box<chalk_ir::TyData<Self>>; // FIXME use Arc? | 27 | type InternedType = Box<chalk_ir::TyData<Self>>; // FIXME use Arc? |
diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index a83d82fd8..7841a0e21 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs | |||
@@ -15,8 +15,8 @@ use crate::{ | |||
15 | db::HirDatabase, | 15 | db::HirDatabase, |
16 | primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness, Uncertain}, | 16 | primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness, Uncertain}, |
17 | traits::{builtin, AssocTyValue, Canonical, Impl, Obligation}, | 17 | traits::{builtin, AssocTyValue, Canonical, Impl, Obligation}, |
18 | ApplicationTy, GenericPredicate, InEnvironment, ProjectionPredicate, ProjectionTy, Substs, | 18 | ApplicationTy, CallableDef, GenericPredicate, InEnvironment, ProjectionPredicate, ProjectionTy, |
19 | TraitEnvironment, TraitRef, Ty, TypeCtor, | 19 | Substs, TraitEnvironment, TraitRef, Ty, TypeCtor, |
20 | }; | 20 | }; |
21 | 21 | ||
22 | use super::interner::*; | 22 | use super::interner::*; |
@@ -217,11 +217,14 @@ impl ToChalk for TypeCtor { | |||
217 | TypeCtor::Slice => TypeName::Slice, | 217 | TypeCtor::Slice => TypeName::Slice, |
218 | TypeCtor::Ref(mutability) => TypeName::Ref(mutability.to_chalk(db)), | 218 | TypeCtor::Ref(mutability) => TypeName::Ref(mutability.to_chalk(db)), |
219 | TypeCtor::Str => TypeName::Str, | 219 | TypeCtor::Str => TypeName::Str, |
220 | TypeCtor::FnDef(callable_def) => { | ||
221 | let id = callable_def.to_chalk(db); | ||
222 | TypeName::FnDef(id) | ||
223 | } | ||
220 | TypeCtor::Int(Uncertain::Unknown) | 224 | TypeCtor::Int(Uncertain::Unknown) |
221 | | TypeCtor::Float(Uncertain::Unknown) | 225 | | TypeCtor::Float(Uncertain::Unknown) |
222 | | TypeCtor::Adt(_) | 226 | | TypeCtor::Adt(_) |
223 | | TypeCtor::Array | 227 | | TypeCtor::Array |
224 | | TypeCtor::FnDef(_) | ||
225 | | TypeCtor::FnPtr { .. } | 228 | | TypeCtor::FnPtr { .. } |
226 | | TypeCtor::Never | 229 | | TypeCtor::Never |
227 | | TypeCtor::Closure { .. } => { | 230 | | TypeCtor::Closure { .. } => { |
@@ -260,7 +263,10 @@ impl ToChalk for TypeCtor { | |||
260 | TypeName::Ref(mutability) => TypeCtor::Ref(from_chalk(db, mutability)), | 263 | TypeName::Ref(mutability) => TypeCtor::Ref(from_chalk(db, mutability)), |
261 | TypeName::Str => TypeCtor::Str, | 264 | TypeName::Str => TypeCtor::Str, |
262 | 265 | ||
263 | TypeName::FnDef(_) => unreachable!(), | 266 | TypeName::FnDef(fn_def_id) => { |
267 | let callable_def = from_chalk(db, fn_def_id); | ||
268 | TypeCtor::FnDef(callable_def) | ||
269 | } | ||
264 | 270 | ||
265 | TypeName::Error => { | 271 | TypeName::Error => { |
266 | // this should not be reached, since we don't represent TypeName::Error with TypeCtor | 272 | // this should not be reached, since we don't represent TypeName::Error with TypeCtor |
@@ -347,6 +353,18 @@ impl ToChalk for Impl { | |||
347 | } | 353 | } |
348 | } | 354 | } |
349 | 355 | ||
356 | impl ToChalk for CallableDef { | ||
357 | type Chalk = FnDefId; | ||
358 | |||
359 | fn to_chalk(self, db: &dyn HirDatabase) -> FnDefId { | ||
360 | db.intern_callable_def(self).into() | ||
361 | } | ||
362 | |||
363 | fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDef { | ||
364 | db.lookup_intern_callable_def(fn_def_id.into()) | ||
365 | } | ||
366 | } | ||
367 | |||
350 | impl ToChalk for TypeAliasId { | 368 | impl ToChalk for TypeAliasId { |
351 | type Chalk = AssocTypeId; | 369 | type Chalk = AssocTypeId; |
352 | 370 | ||
diff --git a/crates/ra_hir_ty/src/traits/chalk/tls.rs b/crates/ra_hir_ty/src/traits/chalk/tls.rs index ebf402a07..d88828c7c 100644 --- a/crates/ra_hir_ty/src/traits/chalk/tls.rs +++ b/crates/ra_hir_ty/src/traits/chalk/tls.rs | |||
@@ -247,10 +247,24 @@ impl DebugContext<'_> { | |||
247 | 247 | ||
248 | pub fn debug_fn_def_id( | 248 | pub fn debug_fn_def_id( |
249 | &self, | 249 | &self, |
250 | _fn_def_id: chalk_ir::FnDefId<Interner>, | 250 | fn_def_id: chalk_ir::FnDefId<Interner>, |
251 | fmt: &mut fmt::Formatter<'_>, | 251 | fmt: &mut fmt::Formatter<'_>, |
252 | ) -> Result<(), fmt::Error> { | 252 | ) -> Result<(), fmt::Error> { |
253 | write!(fmt, "fn") | 253 | let def: CallableDef = from_chalk(self.0, fn_def_id); |
254 | let name = match def { | ||
255 | CallableDef::FunctionId(ff) => self.0.function_data(ff).name.clone(), | ||
256 | CallableDef::StructId(s) => self.0.struct_data(s).name.clone(), | ||
257 | CallableDef::EnumVariantId(e) => { | ||
258 | let enum_data = self.0.enum_data(e.parent); | ||
259 | enum_data.variants[e.local_id].name.clone() | ||
260 | } | ||
261 | }; | ||
262 | match def { | ||
263 | CallableDef::FunctionId(_) => write!(fmt, "{{fn {}}}", name), | ||
264 | CallableDef::StructId(_) | CallableDef::EnumVariantId(_) => { | ||
265 | write!(fmt, "{{ctor {}}}", name) | ||
266 | } | ||
267 | } | ||
254 | } | 268 | } |
255 | 269 | ||
256 | pub fn debug_const( | 270 | pub fn debug_const( |