From 6bde542a39fe63298a31b838e59705797ed8a2cf Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 1 Jul 2020 17:15:20 +0200 Subject: Split `CrateImplDefs` in inherent and trait impls This makes the intention of inherent vs. trait impls somewhat more clear and also fixes (?) an issue where trait impls with an unresolved trait were added as inherent impls instead (hence the test changes). --- crates/ra_hir_ty/src/traits/chalk.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir_ty/src/traits') diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index 8ef4941c0..c97b81d57 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs @@ -77,8 +77,8 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { // Note: Since we're using impls_for_trait, only impls where the trait // can be resolved should ever reach Chalk. `impl_datum` relies on that // and will panic if the trait can't be resolved. - let in_deps = self.db.impls_from_deps(self.krate); - let in_self = self.db.impls_in_crate(self.krate); + let in_deps = self.db.trait_impls_in_deps(self.krate); + let in_self = self.db.trait_impls_in_crate(self.krate); let impl_maps = [in_deps, in_self]; let id_to_chalk = |id: hir_def::ImplId| Impl::ImplDef(id).to_chalk(self.db); @@ -87,14 +87,12 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { Some(fp) => impl_maps .iter() .flat_map(|crate_impl_defs| { - crate_impl_defs.lookup_impl_defs_for_trait_and_ty(trait_, fp).map(id_to_chalk) + crate_impl_defs.for_trait_and_self_ty(trait_, fp).map(id_to_chalk) }) .collect(), None => impl_maps .iter() - .flat_map(|crate_impl_defs| { - crate_impl_defs.lookup_impl_defs_for_trait(trait_).map(id_to_chalk) - }) + .flat_map(|crate_impl_defs| crate_impl_defs.for_trait(trait_).map(id_to_chalk)) .collect(), }; -- cgit v1.2.3 From d5d485ef9289589332893f2c0ad96cb366afe9d6 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 28 Jun 2020 21:17:27 +0200 Subject: Implement Chalk variable kinds This means we need to keep track of the kinds (general/int/float) of variables in `Canonical`, which requires some more ceremony. (It also exposes some places where we're not really dealing with canonicalization correctly -- another thing to be cleaned up when we switch to using Chalk's types directly.) Should fix the last remaining issue of #2534. --- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 43 +++++++++++++++++++--------- 1 file changed, 30 insertions(+), 13 deletions(-) (limited to 'crates/ra_hir_ty/src/traits') diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index ac82ea831..433d6aa03 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -17,7 +17,7 @@ use crate::{ primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness}, traits::{builtin, AssocTyValue, Canonical, Impl, Obligation}, ApplicationTy, CallableDef, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, - ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor, + ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TyKind, TypeCtor, }; use super::interner::*; @@ -555,22 +555,39 @@ where type Chalk = chalk_ir::Canonical; fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical { - let parameter = chalk_ir::CanonicalVarKind::new( - chalk_ir::VariableKind::Ty(chalk_ir::TyKind::General), - chalk_ir::UniverseIndex::ROOT, - ); + let kinds = self + .kinds + .iter() + .map(|k| match k { + TyKind::General => chalk_ir::TyKind::General, + TyKind::Integer => chalk_ir::TyKind::Integer, + TyKind::Float => chalk_ir::TyKind::Float, + }) + .map(|tk| { + chalk_ir::CanonicalVarKind::new( + chalk_ir::VariableKind::Ty(tk), + chalk_ir::UniverseIndex::ROOT, + ) + }); let value = self.value.to_chalk(db); - chalk_ir::Canonical { - value, - binders: chalk_ir::CanonicalVarKinds::from(&Interner, vec![parameter; self.num_vars]), - } + chalk_ir::Canonical { value, binders: chalk_ir::CanonicalVarKinds::from(&Interner, kinds) } } fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical) -> Canonical { - Canonical { - num_vars: canonical.binders.len(&Interner), - value: from_chalk(db, canonical.value), - } + let kinds = canonical + .binders + .iter(&Interner) + .map(|k| match k.kind { + chalk_ir::VariableKind::Ty(tk) => match tk { + chalk_ir::TyKind::General => TyKind::General, + chalk_ir::TyKind::Integer => TyKind::Integer, + chalk_ir::TyKind::Float => TyKind::Float, + }, + chalk_ir::VariableKind::Lifetime => panic!("unexpected lifetime from Chalk"), + chalk_ir::VariableKind::Const(_) => panic!("unexpected const from Chalk"), + }) + .collect(); + Canonical { kinds, value: from_chalk(db, canonical.value) } } } -- cgit v1.2.3 From 4bbc385277bcab509c321b1374f72f1ef19d7750 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 7 Jul 2020 10:14:48 +0200 Subject: Switch to fully dynamically dispatched salsa This improves compile times quite a bit --- crates/ra_hir_ty/src/traits/chalk/tls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_ty/src/traits') diff --git a/crates/ra_hir_ty/src/traits/chalk/tls.rs b/crates/ra_hir_ty/src/traits/chalk/tls.rs index 556af7098..e6a9d3211 100644 --- a/crates/ra_hir_ty/src/traits/chalk/tls.rs +++ b/crates/ra_hir_ty/src/traits/chalk/tls.rs @@ -10,7 +10,7 @@ use hir_def::{AdtId, AssocContainerId, DefWithBodyId, Lookup, TypeAliasId}; pub use unsafe_tls::{set_current_program, with_current_program}; -pub struct DebugContext<'a>(&'a (dyn HirDatabase + 'a)); +pub struct DebugContext<'a>(&'a dyn HirDatabase); impl DebugContext<'_> { pub fn debug_struct_id( -- cgit v1.2.3 From 8a72e40ca91cc51a93b8145582feaccb7254abb6 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 10 Jul 2020 19:14:33 +0200 Subject: Fix #4966 We add a level of binders when converting our function pointer to Chalk's; we need to remove it again on the way back. --- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir_ty/src/traits') diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index 433d6aa03..cb354d586 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -115,8 +115,12 @@ impl ToChalk for Ty { let parameters = from_chalk(db, opaque_ty.substitution); Ty::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters }) } - chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: _, substitution }) => { - let parameters: Substs = from_chalk(db, substitution); + chalk_ir::TyData::Function(chalk_ir::Fn { num_binders, substitution }) => { + assert_eq!(num_binders, 0); + let parameters: Substs = from_chalk( + db, + substitution.shifted_out(&Interner).expect("fn ptr should have no binders"), + ); Ty::Apply(ApplicationTy { ctor: TypeCtor::FnPtr { num_args: (parameters.len() - 1) as u16 }, parameters, -- cgit v1.2.3 From 209c492432c15b017f99dba06d5937389c1f9546 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 11 Jul 2020 15:22:46 +0200 Subject: Upgrade Chalk --- crates/ra_hir_ty/src/traits/chalk.rs | 36 ++++++++++++++++++++++++--- crates/ra_hir_ty/src/traits/chalk/interner.rs | 15 +++++++++++ crates/ra_hir_ty/src/traits/chalk/mapping.rs | 15 +++++++---- crates/ra_hir_ty/src/traits/chalk/tls.rs | 2 +- 4 files changed, 58 insertions(+), 10 deletions(-) (limited to 'crates/ra_hir_ty/src/traits') diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index c97b81d57..a9b39474a 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use log::debug; -use chalk_ir::{fold::shift::Shift, GenericArg, TypeName}; +use chalk_ir::{fold::shift::Shift, GenericArg, TypeName, CanonicalVarKinds}; use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait}; use hir_def::{ @@ -66,10 +66,13 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { &self, trait_id: TraitId, parameters: &[GenericArg], + _binders: &CanonicalVarKinds, ) -> Vec { debug!("impls_for_trait {:?}", trait_id); let trait_: hir_def::TraitId = from_chalk(self.db, trait_id); + // FIXME use binders to look for int/float impls when necessary + let ty: Ty = from_chalk(self.db, parameters[0].assert_ty_ref(&Interner).clone()); let self_ty_fp = TyFingerprint::for_impl(&ty); @@ -219,6 +222,22 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { // FIXME: implement closure support unimplemented!() } + + fn trait_name(&self, _trait_id: chalk_ir::TraitId) -> String { + unimplemented!() + } + fn adt_name(&self, _struct_id: chalk_ir::AdtId) -> String { + unimplemented!() + } + fn assoc_type_name(&self, _assoc_ty_id: chalk_ir::AssocTypeId) -> String { + unimplemented!() + } + fn opaque_type_name(&self, _opaque_ty_id: chalk_ir::OpaqueTyId) -> String { + unimplemented!() + } + fn fn_def_name(&self, _fn_def_id: chalk_ir::FnDefId) -> String { + unimplemented!() + } } pub(crate) fn program_clauses_for_chalk_env_query( @@ -354,12 +373,21 @@ pub(crate) fn struct_datum_query( fundamental: false, phantom_data: false, }; + // FIXME provide enum variants properly (for auto traits) + let variant = rust_ir::AdtVariantDatum { + fields: Vec::new(), // FIXME add fields (only relevant for auto traits), + }; let struct_datum_bound = rust_ir::AdtDatumBound { - fields: Vec::new(), // FIXME add fields (only relevant for auto traits) + variants: vec![variant], where_clauses, }; - let struct_datum = - StructDatum { id: struct_id, binders: make_binders(struct_datum_bound, num_params), flags }; + let struct_datum = StructDatum { + // FIXME set ADT kind + kind: rust_ir::AdtKind::Struct, + id: struct_id, + binders: make_binders(struct_datum_bound, num_params), + flags + }; Arc::new(struct_datum) } diff --git a/crates/ra_hir_ty/src/traits/chalk/interner.rs b/crates/ra_hir_ty/src/traits/chalk/interner.rs index 15426b022..156b691b4 100644 --- a/crates/ra_hir_ty/src/traits/chalk/interner.rs +++ b/crates/ra_hir_ty/src/traits/chalk/interner.rs @@ -39,6 +39,7 @@ impl chalk_ir::interner::Interner for Interner { type InternedQuantifiedWhereClauses = Vec>; type InternedVariableKinds = Vec>; type InternedCanonicalVarKinds = Vec>; + type InternedConstraints = Vec>>; type DefId = InternId; type InternedAdtId = crate::TypeCtorId; type Identifier = TypeAliasId; @@ -349,6 +350,20 @@ impl chalk_ir::interner::Interner for Interner { ) -> &'a [chalk_ir::CanonicalVarKind] { &canonical_var_kinds } + + fn intern_constraints( + &self, + data: impl IntoIterator>, E>>, + ) -> Result { + data.into_iter().collect() + } + + fn constraints_data<'a>( + &self, + constraints: &'a Self::InternedConstraints, + ) -> &'a [chalk_ir::InEnvironment>] { + constraints + } } impl chalk_ir::interner::HasInterner for Interner { diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index 433d6aa03..bc0c6de17 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -61,7 +61,7 @@ impl ToChalk for Ty { Ty::Bound(idx) => chalk_ir::TyData::BoundVar(idx).intern(&Interner), Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"), Ty::Dyn(predicates) => { - let where_clauses = chalk_ir::QuantifiedWhereClauses::from( + let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter( &Interner, predicates.iter().filter(|p| !p.is_error()).cloned().map(|p| p.to_chalk(db)), ); @@ -152,7 +152,7 @@ fn ref_to_chalk( let lifetime = LIFETIME_PLACEHOLDER.to_lifetime(&Interner); chalk_ir::ApplicationTy { name: TypeName::Ref(mutability.to_chalk(db)), - substitution: chalk_ir::Substitution::from( + substitution: chalk_ir::Substitution::from_iter( &Interner, vec![lifetime.cast(&Interner), arg.cast(&Interner)], ), @@ -177,7 +177,7 @@ impl ToChalk for Substs { type Chalk = chalk_ir::Substitution; fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution { - chalk_ir::Substitution::from(&Interner, self.iter().map(|ty| ty.clone().to_chalk(db))) + chalk_ir::Substitution::from_iter(&Interner, self.iter().map(|ty| ty.clone().to_chalk(db))) } fn from_chalk(db: &dyn HirDatabase, parameters: chalk_ir::Substitution) -> Substs { @@ -492,6 +492,11 @@ impl ToChalk for GenericPredicate { // we shouldn't get these from Chalk panic!("encountered LifetimeOutlives from Chalk") } + + chalk_ir::WhereClause::TypeOutlives(_) => { + // we shouldn't get these from Chalk + panic!("encountered TypeOutlives from Chalk") + } } } } @@ -570,7 +575,7 @@ where ) }); let value = self.value.to_chalk(db); - chalk_ir::Canonical { value, binders: chalk_ir::CanonicalVarKinds::from(&Interner, kinds) } + chalk_ir::Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) } } fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical) -> Canonical { @@ -691,7 +696,7 @@ where T: HasInterner, { chalk_ir::Binders::new( - chalk_ir::VariableKinds::from( + chalk_ir::VariableKinds::from_iter( &Interner, std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyKind::General)).take(num_vars), ), diff --git a/crates/ra_hir_ty/src/traits/chalk/tls.rs b/crates/ra_hir_ty/src/traits/chalk/tls.rs index e6a9d3211..1e226baea 100644 --- a/crates/ra_hir_ty/src/traits/chalk/tls.rs +++ b/crates/ra_hir_ty/src/traits/chalk/tls.rs @@ -157,7 +157,7 @@ impl DebugContext<'_> { _ => panic!("associated type not in trait"), }; let trait_data = self.0.trait_data(trait_); - let params = projection_ty.substitution.parameters(&Interner); + let params = projection_ty.substitution.as_slice(&Interner); write!(fmt, "<{:?} as {}", ¶ms[0], trait_data.name,)?; if params.len() > 1 { write!( -- cgit v1.2.3 From c82f5379de49344eb418cc6aaf5bf8c35bc4aaef Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 10 Jul 2020 18:30:32 +0200 Subject: Enable Chalk tracing in hir_ty tests --- crates/ra_hir_ty/src/traits/chalk.rs | 9 +++------ crates/ra_hir_ty/src/traits/chalk/mapping.rs | 5 ++++- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir_ty/src/traits') diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index a9b39474a..e944c1976 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use log::debug; -use chalk_ir::{fold::shift::Shift, GenericArg, TypeName, CanonicalVarKinds}; +use chalk_ir::{fold::shift::Shift, CanonicalVarKinds, GenericArg, TypeName}; use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait}; use hir_def::{ @@ -377,16 +377,13 @@ pub(crate) fn struct_datum_query( let variant = rust_ir::AdtVariantDatum { fields: Vec::new(), // FIXME add fields (only relevant for auto traits), }; - let struct_datum_bound = rust_ir::AdtDatumBound { - variants: vec![variant], - where_clauses, - }; + let struct_datum_bound = rust_ir::AdtDatumBound { variants: vec![variant], where_clauses }; let struct_datum = StructDatum { // FIXME set ADT kind kind: rust_ir::AdtKind::Struct, id: struct_id, binders: make_binders(struct_datum_bound, num_params), - flags + flags, }; Arc::new(struct_datum) } diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index bc0c6de17..848cb6e7d 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -575,7 +575,10 @@ where ) }); let value = self.value.to_chalk(db); - chalk_ir::Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) } + chalk_ir::Canonical { + value, + binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds), + } } fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical) -> Canonical { -- cgit v1.2.3 From 2a4166501d8990d3a489e89af3d92002540c288c Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 11 Jul 2020 16:29:09 +0200 Subject: Remove built-in Unsize impls They exist in Chalk now. --- crates/ra_hir_ty/src/traits/builtin.rs | 201 +-------------------------- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 1 + 2 files changed, 5 insertions(+), 197 deletions(-) (limited to 'crates/ra_hir_ty/src/traits') diff --git a/crates/ra_hir_ty/src/traits/builtin.rs b/crates/ra_hir_ty/src/traits/builtin.rs index 6d5f2d46a..86e22e459 100644 --- a/crates/ra_hir_ty/src/traits/builtin.rs +++ b/crates/ra_hir_ty/src/traits/builtin.rs @@ -1,15 +1,12 @@ //! This module provides the built-in trait implementations, e.g. to make //! closures implement `Fn`. -use hir_def::{expr::Expr, lang_item::LangItemTarget, TraitId, TypeAliasId}; +use hir_def::{expr::Expr, TraitId, TypeAliasId}; use hir_expand::name::name; use ra_db::CrateId; -use super::{AssocTyValue, Impl, UnsizeToSuperTraitObjectData}; +use super::{AssocTyValue, Impl}; use crate::{ - db::HirDatabase, - utils::{all_super_traits, generics}, - ApplicationTy, Binders, BoundVar, DebruijnIndex, GenericPredicate, Substs, TraitRef, Ty, - TypeCtor, TypeWalk, + db::HirDatabase, ApplicationTy, BoundVar, DebruijnIndex, Substs, TraitRef, Ty, TypeCtor, }; pub(super) struct BuiltinImplData { @@ -31,7 +28,7 @@ pub(super) fn get_builtin_impls( krate: CrateId, ty: &Ty, // The first argument for the trait, if present - arg: &Option, + _arg: &Option, trait_: TraitId, mut callback: impl FnMut(Impl), ) { @@ -50,60 +47,12 @@ pub(super) fn get_builtin_impls( } } } - - let unsize_trait = get_unsize_trait(db, krate); - if let Some(actual_trait) = unsize_trait { - if trait_ == actual_trait { - get_builtin_unsize_impls(db, krate, ty, arg, callback); - } - } -} - -fn get_builtin_unsize_impls( - db: &dyn HirDatabase, - krate: CrateId, - ty: &Ty, - // The first argument for the trait, if present - arg: &Option, - mut callback: impl FnMut(Impl), -) { - if !check_unsize_impl_prerequisites(db, krate) { - return; - } - - if let Ty::Apply(ApplicationTy { ctor: TypeCtor::Array, .. }) = ty { - callback(Impl::UnsizeArray); - return; // array is unsized, the rest of the impls shouldn't apply - } - - if let Some(target_trait) = arg.as_ref().and_then(|t| t.dyn_trait_ref()) { - // FIXME what about more complicated dyn tys with marker traits? - if let Some(trait_ref) = ty.dyn_trait_ref() { - if trait_ref.trait_ != target_trait.trait_ { - let super_traits = all_super_traits(db.upcast(), trait_ref.trait_); - if super_traits.contains(&target_trait.trait_) { - callback(Impl::UnsizeToSuperTraitObject(UnsizeToSuperTraitObjectData { - trait_: trait_ref.trait_, - super_trait: target_trait.trait_, - })); - } - } - } else { - // FIXME only for sized types - callback(Impl::UnsizeToTraitObject(target_trait.trait_)); - } - } } pub(super) fn impl_datum(db: &dyn HirDatabase, krate: CrateId, impl_: Impl) -> BuiltinImplData { match impl_ { Impl::ImplDef(_) => unreachable!(), Impl::ClosureFnTraitImpl(data) => closure_fn_trait_impl_datum(db, krate, data), - Impl::UnsizeArray => array_unsize_impl_datum(db, krate), - Impl::UnsizeToTraitObject(trait_) => trait_object_unsize_impl_datum(db, krate, trait_), - Impl::UnsizeToSuperTraitObject(data) => { - super_trait_object_unsize_impl_datum(db, krate, data) - } } } @@ -227,145 +176,3 @@ fn closure_fn_trait_output_assoc_ty_value( value: output_ty, } } - -// Array unsizing - -fn check_unsize_impl_prerequisites(db: &dyn HirDatabase, krate: CrateId) -> bool { - // the Unsize trait needs to exist and have two type parameters (Self and T) - let unsize_trait = match get_unsize_trait(db, krate) { - Some(t) => t, - None => return false, - }; - let generic_params = generics(db.upcast(), unsize_trait.into()); - generic_params.len() == 2 -} - -fn array_unsize_impl_datum(db: &dyn HirDatabase, krate: CrateId) -> BuiltinImplData { - // impl Unsize<[T]> for [T; _] - // (this can be a single impl because we don't distinguish array sizes currently) - - let trait_ = get_unsize_trait(db, krate) // get unsize trait - // the existence of the Unsize trait has been checked before - .expect("Unsize trait missing"); - - let var = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); - let substs = Substs::builder(2) - .push(Ty::apply_one(TypeCtor::Array, var.clone())) - .push(Ty::apply_one(TypeCtor::Slice, var)) - .build(); - - let trait_ref = TraitRef { trait_, substs }; - - BuiltinImplData { - num_vars: 1, - trait_ref, - where_clauses: Vec::new(), - assoc_ty_values: Vec::new(), - } -} - -// Trait object unsizing - -fn trait_object_unsize_impl_datum( - db: &dyn HirDatabase, - krate: CrateId, - trait_: TraitId, -) -> BuiltinImplData { - // impl Unsize> for T where T: Trait - - let unsize_trait = get_unsize_trait(db, krate) // get unsize trait - // the existence of the Unsize trait has been checked before - .expect("Unsize trait missing"); - - let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); - - let target_substs = Substs::build_for_def(db, trait_) - .push(Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0))) - .fill_with_bound_vars(DebruijnIndex::ONE, 1) - .build(); - let num_vars = target_substs.len(); - let target_trait_ref = TraitRef { trait_, substs: target_substs }; - let target_bounds = vec![GenericPredicate::Implemented(target_trait_ref)]; - - let self_substs = - Substs::build_for_def(db, trait_).fill_with_bound_vars(DebruijnIndex::INNERMOST, 0).build(); - let self_trait_ref = TraitRef { trait_, substs: self_substs }; - let where_clauses = vec![GenericPredicate::Implemented(self_trait_ref)]; - - let impl_substs = Substs::builder(2).push(self_ty).push(Ty::Dyn(target_bounds.into())).build(); - - let trait_ref = TraitRef { trait_: unsize_trait, substs: impl_substs }; - - BuiltinImplData { num_vars, trait_ref, where_clauses, assoc_ty_values: Vec::new() } -} - -fn super_trait_object_unsize_impl_datum( - db: &dyn HirDatabase, - krate: CrateId, - data: UnsizeToSuperTraitObjectData, -) -> BuiltinImplData { - // impl Unsize for dyn Trait - - let unsize_trait = get_unsize_trait(db, krate) // get unsize trait - // the existence of the Unsize trait has been checked before - .expect("Unsize trait missing"); - - let self_substs = Substs::build_for_def(db, data.trait_) - .fill_with_bound_vars(DebruijnIndex::INNERMOST, 0) - .build(); - let self_trait_ref = TraitRef { trait_: data.trait_, substs: self_substs.clone() }; - - let num_vars = self_substs.len() - 1; - - // we need to go from our trait to the super trait, substituting type parameters - let path = crate::utils::find_super_trait_path(db.upcast(), data.trait_, data.super_trait); - - let mut current_trait_ref = self_trait_ref.clone(); - for t in path.into_iter().skip(1) { - let bounds = db.generic_predicates(current_trait_ref.trait_.into()); - let super_trait_ref = bounds - .iter() - .find_map(|b| match &b.value { - GenericPredicate::Implemented(tr) - if tr.trait_ == t - && tr.substs[0] - == Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)) => - { - Some(Binders { value: tr, num_binders: b.num_binders }) - } - _ => None, - }) - .expect("trait bound for known super trait not found"); - current_trait_ref = super_trait_ref.cloned().subst(¤t_trait_ref.substs); - } - - // We need to renumber the variables a bit now: from ^0.0, ^0.1, ^0.2, ... - // to ^0.0, ^1.0, ^1.1. The reason for this is that the first variable comes - // from the dyn Trait binder, while the other variables come from the impl. - let new_substs = Substs::builder(num_vars + 1) - .push(Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0))) - .fill_with_bound_vars(DebruijnIndex::ONE, 0) - .build(); - - let self_bounds = - vec![GenericPredicate::Implemented(self_trait_ref.subst_bound_vars(&new_substs))]; - let super_bounds = - vec![GenericPredicate::Implemented(current_trait_ref.subst_bound_vars(&new_substs))]; - - let substs = Substs::builder(2) - .push(Ty::Dyn(self_bounds.into())) - .push(Ty::Dyn(super_bounds.into())) - .build(); - - let trait_ref = TraitRef { trait_: unsize_trait, substs }; - - BuiltinImplData { num_vars, trait_ref, where_clauses: Vec::new(), assoc_ty_values: Vec::new() } -} - -fn get_unsize_trait(db: &dyn HirDatabase, krate: CrateId) -> Option { - let target = db.lang_item(krate, "unsize".into())?; - match target { - LangItemTarget::TraitId(t) => Some(t), - _ => None, - } -} diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index 848cb6e7d..7bdb6264e 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -271,6 +271,7 @@ impl ToChalk for TypeCtor { } TypeCtor::Never => TypeName::Never, + // FIXME convert these TypeCtor::Adt(_) | TypeCtor::Array | TypeCtor::FnPtr { .. } -- cgit v1.2.3 From 94f5f69ff4b3185e559339c86f9528b9443ca67d Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 11 Jul 2020 18:33:50 +0200 Subject: Use Chalk built-in representation for array types --- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 43 +++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir_ty/src/traits') diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index 7bdb6264e..7dc9ee759 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -29,6 +29,7 @@ impl ToChalk for Ty { match self { Ty::Apply(apply_ty) => match apply_ty.ctor { TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), + TypeCtor::Array => array_to_chalk(db, 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 }) @@ -67,7 +68,7 @@ impl ToChalk for Ty { ); let bounded_ty = chalk_ir::DynTy { bounds: make_binders(where_clauses, 1), - lifetime: LIFETIME_PLACEHOLDER.to_lifetime(&Interner), + lifetime: FAKE_PLACEHOLDER.to_lifetime(&Interner), }; chalk_ir::TyData::Dyn(bounded_ty).intern(&Interner) } @@ -92,6 +93,7 @@ impl ToChalk for Ty { chalk_ir::TyData::Apply(apply_ty) => match apply_ty.name { TypeName::Error => Ty::Unknown, TypeName::Ref(m) => ref_from_chalk(db, m, apply_ty.substitution), + TypeName::Array => array_from_chalk(db, apply_ty.substitution), _ => { let ctor = from_chalk(db, apply_ty.name); let parameters = from_chalk(db, apply_ty.substitution); @@ -138,7 +140,7 @@ impl ToChalk for Ty { } } -const LIFETIME_PLACEHOLDER: PlaceholderIndex = +const FAKE_PLACEHOLDER: PlaceholderIndex = PlaceholderIndex { ui: UniverseIndex::ROOT, idx: usize::MAX }; /// We currently don't model lifetimes, but Chalk does. So, we have to insert a @@ -149,7 +151,7 @@ fn ref_to_chalk( subst: Substs, ) -> chalk_ir::Ty { let arg = subst[0].clone().to_chalk(db); - let lifetime = LIFETIME_PLACEHOLDER.to_lifetime(&Interner); + let lifetime = FAKE_PLACEHOLDER.to_lifetime(&Interner); chalk_ir::ApplicationTy { name: TypeName::Ref(mutability.to_chalk(db)), substitution: chalk_ir::Substitution::from_iter( @@ -173,6 +175,35 @@ fn ref_from_chalk( Ty::apply(TypeCtor::Ref(from_chalk(db, mutability)), Substs(tys)) } +/// We currently don't model constants, but Chalk does. So, we have to insert a +/// fake constant here, because Chalks built-in logic may expect it to be there. +fn array_to_chalk(db: &dyn HirDatabase, subst: Substs) -> chalk_ir::Ty { + let arg = subst[0].clone().to_chalk(db); + let usize_ty = chalk_ir::ApplicationTy { + name: TypeName::Scalar(Scalar::Uint(chalk_ir::UintTy::Usize)), + substitution: chalk_ir::Substitution::empty(&Interner), + } + .intern(&Interner); + let const_ = FAKE_PLACEHOLDER.to_const(&Interner, usize_ty); + chalk_ir::ApplicationTy { + name: TypeName::Array, + substitution: chalk_ir::Substitution::from_iter( + &Interner, + vec![arg.cast(&Interner), const_.cast(&Interner)], + ), + } + .intern(&Interner) +} + +/// Here we remove the const from the type we got from Chalk. +fn array_from_chalk(db: &dyn HirDatabase, subst: chalk_ir::Substitution) -> Ty { + let tys = subst + .iter(&Interner) + .filter_map(|p| Some(from_chalk(db, p.ty(&Interner)?.clone()))) + .collect(); + Ty::apply(TypeCtor::Array, Substs(tys)) +} + impl ToChalk for Substs { type Chalk = chalk_ir::Substitution; @@ -263,6 +294,7 @@ impl ToChalk for TypeCtor { TypeCtor::Tuple { cardinality } => TypeName::Tuple(cardinality.into()), TypeCtor::RawPtr(mutability) => TypeName::Raw(mutability.to_chalk(db)), TypeCtor::Slice => TypeName::Slice, + TypeCtor::Array => TypeName::Array, TypeCtor::Ref(mutability) => TypeName::Ref(mutability.to_chalk(db)), TypeCtor::Str => TypeName::Str, TypeCtor::FnDef(callable_def) => { @@ -272,10 +304,7 @@ impl ToChalk for TypeCtor { TypeCtor::Never => TypeName::Never, // FIXME convert these - TypeCtor::Adt(_) - | TypeCtor::Array - | TypeCtor::FnPtr { .. } - | TypeCtor::Closure { .. } => { + TypeCtor::Adt(_) | TypeCtor::FnPtr { .. } | TypeCtor::Closure { .. } => { // other TypeCtors get interned and turned into a chalk StructId let struct_id = db.intern_type_ctor(self).into(); TypeName::Adt(struct_id) -- cgit v1.2.3 From 7e9c4d58f189d4ac3c390a6ea345f2578dd5f661 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 11 Jul 2020 19:12:10 +0200 Subject: Search more efficiently for int/float impls --- crates/ra_hir_ty/src/traits/chalk.rs | 46 ++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 12 deletions(-) (limited to 'crates/ra_hir_ty/src/traits') diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index e944c1976..c448aea65 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs @@ -14,7 +14,10 @@ use ra_db::{salsa::InternKey, CrateId}; use super::{builtin, AssocTyValue, ChalkContext, Impl}; use crate::{ - db::HirDatabase, display::HirDisplay, method_resolution::TyFingerprint, utils::generics, + db::HirDatabase, + display::HirDisplay, + method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, + utils::generics, CallableDef, DebruijnIndex, GenericPredicate, Substs, Ty, TypeCtor, }; use mapping::{convert_where_clauses, generic_predicate_to_inline_bound, make_binders}; @@ -66,16 +69,31 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { &self, trait_id: TraitId, parameters: &[GenericArg], - _binders: &CanonicalVarKinds, + binders: &CanonicalVarKinds, ) -> Vec { debug!("impls_for_trait {:?}", trait_id); let trait_: hir_def::TraitId = from_chalk(self.db, trait_id); - // FIXME use binders to look for int/float impls when necessary - let ty: Ty = from_chalk(self.db, parameters[0].assert_ty_ref(&Interner).clone()); + fn binder_kind(ty: &Ty, binders: &CanonicalVarKinds) -> Option { + if let Ty::Bound(bv) = ty { + let binders = binders.as_slice(&Interner); + if bv.debruijn == DebruijnIndex::INNERMOST { + if let chalk_ir::VariableKind::Ty(tk) = binders[bv.index].kind { + return Some(tk); + } + } + } + None + } + let self_ty_fp = TyFingerprint::for_impl(&ty); + let fps: &[TyFingerprint] = match binder_kind(&ty, binders) { + Some(chalk_ir::TyKind::Integer) => &ALL_INT_FPS, + Some(chalk_ir::TyKind::Float) => &ALL_FLOAT_FPS, + _ => self_ty_fp.as_ref().map(std::slice::from_ref).unwrap_or(&[]), + }; // Note: Since we're using impls_for_trait, only impls where the trait // can be resolved should ever reach Chalk. `impl_datum` relies on that @@ -86,17 +104,21 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { let id_to_chalk = |id: hir_def::ImplId| Impl::ImplDef(id).to_chalk(self.db); - let mut result: Vec<_> = match self_ty_fp { - Some(fp) => impl_maps + let mut result: Vec<_> = if fps.is_empty() { + debug!("Unrestricted search for {:?} impls...", trait_); + impl_maps + .iter() + .flat_map(|crate_impl_defs| crate_impl_defs.for_trait(trait_).map(id_to_chalk)) + .collect() + } else { + impl_maps .iter() .flat_map(|crate_impl_defs| { - crate_impl_defs.for_trait_and_self_ty(trait_, fp).map(id_to_chalk) + fps.iter().flat_map(move |fp| { + crate_impl_defs.for_trait_and_self_ty(trait_, *fp).map(id_to_chalk) + }) }) - .collect(), - None => impl_maps - .iter() - .flat_map(|crate_impl_defs| crate_impl_defs.for_trait(trait_).map(id_to_chalk)) - .collect(), + .collect() }; let arg: Option = -- cgit v1.2.3 From a09d48380204fa948a3af397dc2188b93bf5793f Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 14 Jul 2020 18:23:45 +0200 Subject: Thread varargs through r-a --- crates/ra_hir_ty/src/traits/builtin.rs | 2 +- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir_ty/src/traits') diff --git a/crates/ra_hir_ty/src/traits/builtin.rs b/crates/ra_hir_ty/src/traits/builtin.rs index 86e22e459..60cc9a9f5 100644 --- a/crates/ra_hir_ty/src/traits/builtin.rs +++ b/crates/ra_hir_ty/src/traits/builtin.rs @@ -121,7 +121,7 @@ fn closure_fn_trait_impl_datum( .build(), ); let sig_ty = Ty::apply( - TypeCtor::FnPtr { num_args }, + TypeCtor::FnPtr { num_args, is_varargs: false }, Substs::builder(num_args as usize + 1) .fill_with_bound_vars(DebruijnIndex::INNERMOST, 0) .build(), diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index 06453ef82..5ba2ff51b 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -30,7 +30,7 @@ impl ToChalk for Ty { Ty::Apply(apply_ty) => match apply_ty.ctor { TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), TypeCtor::Array => array_to_chalk(db, apply_ty.parameters), - TypeCtor::FnPtr { num_args: _ } => { + TypeCtor::FnPtr { num_args: _, is_varargs: _ } => { let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: 0, substitution }) .intern(&Interner) @@ -124,7 +124,10 @@ impl ToChalk for Ty { substitution.shifted_out(&Interner).expect("fn ptr should have no binders"), ); Ty::Apply(ApplicationTy { - ctor: TypeCtor::FnPtr { num_args: (parameters.len() - 1) as u16 }, + ctor: TypeCtor::FnPtr { + num_args: (parameters.len() - 1) as u16, + is_varargs: false, + }, parameters, }) } -- cgit v1.2.3 From fdce4d9f5140085c6c362ecbcf837f1b6a7d50ca Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 14 Jul 2020 18:59:07 +0200 Subject: Add FIXME --- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'crates/ra_hir_ty/src/traits') diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index 5ba2ff51b..3ebb55f77 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -31,6 +31,7 @@ impl ToChalk for Ty { TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), TypeCtor::Array => array_to_chalk(db, apply_ty.parameters), TypeCtor::FnPtr { num_args: _, is_varargs: _ } => { + // FIXME: handle is_varargs let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: 0, substitution }) .intern(&Interner) -- cgit v1.2.3 From a48843a16a2306399f2f6a78c69d9192a6480c88 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 12 Jul 2020 15:26:02 +0200 Subject: Use Chalk closure support --- crates/ra_hir_ty/src/traits/builtin.rs | 178 --------------------------- crates/ra_hir_ty/src/traits/chalk.rs | 87 ++++++------- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 97 +++++---------- 3 files changed, 68 insertions(+), 294 deletions(-) delete mode 100644 crates/ra_hir_ty/src/traits/builtin.rs (limited to 'crates/ra_hir_ty/src/traits') diff --git a/crates/ra_hir_ty/src/traits/builtin.rs b/crates/ra_hir_ty/src/traits/builtin.rs deleted file mode 100644 index 60cc9a9f5..000000000 --- a/crates/ra_hir_ty/src/traits/builtin.rs +++ /dev/null @@ -1,178 +0,0 @@ -//! This module provides the built-in trait implementations, e.g. to make -//! closures implement `Fn`. -use hir_def::{expr::Expr, TraitId, TypeAliasId}; -use hir_expand::name::name; -use ra_db::CrateId; - -use super::{AssocTyValue, Impl}; -use crate::{ - db::HirDatabase, ApplicationTy, BoundVar, DebruijnIndex, Substs, TraitRef, Ty, TypeCtor, -}; - -pub(super) struct BuiltinImplData { - pub num_vars: usize, - pub trait_ref: TraitRef, - pub where_clauses: Vec, - pub assoc_ty_values: Vec, -} - -pub(super) struct BuiltinImplAssocTyValueData { - pub impl_: Impl, - pub assoc_ty_id: TypeAliasId, - pub num_vars: usize, - pub value: Ty, -} - -pub(super) fn get_builtin_impls( - db: &dyn HirDatabase, - krate: CrateId, - ty: &Ty, - // The first argument for the trait, if present - _arg: &Option, - trait_: TraitId, - mut callback: impl FnMut(Impl), -) { - // Note: since impl_datum needs to be infallible, we need to make sure here - // that we have all prerequisites to build the respective impls. - if let Ty::Apply(ApplicationTy { ctor: TypeCtor::Closure { def, expr }, .. }) = ty { - for &fn_trait in [super::FnTrait::FnOnce, super::FnTrait::FnMut, super::FnTrait::Fn].iter() - { - if let Some(actual_trait) = fn_trait.get_id(db, krate) { - if trait_ == actual_trait { - let impl_ = super::ClosureFnTraitImplData { def: *def, expr: *expr, fn_trait }; - if check_closure_fn_trait_impl_prerequisites(db, krate, impl_) { - callback(Impl::ClosureFnTraitImpl(impl_)); - } - } - } - } - } -} - -pub(super) fn impl_datum(db: &dyn HirDatabase, krate: CrateId, impl_: Impl) -> BuiltinImplData { - match impl_ { - Impl::ImplDef(_) => unreachable!(), - Impl::ClosureFnTraitImpl(data) => closure_fn_trait_impl_datum(db, krate, data), - } -} - -pub(super) fn associated_ty_value( - db: &dyn HirDatabase, - krate: CrateId, - data: AssocTyValue, -) -> BuiltinImplAssocTyValueData { - match data { - AssocTyValue::TypeAlias(_) => unreachable!(), - AssocTyValue::ClosureFnTraitImplOutput(data) => { - closure_fn_trait_output_assoc_ty_value(db, krate, data) - } - } -} - -// Closure Fn trait impls - -fn check_closure_fn_trait_impl_prerequisites( - db: &dyn HirDatabase, - krate: CrateId, - data: super::ClosureFnTraitImplData, -) -> bool { - // the respective Fn/FnOnce/FnMut trait needs to exist - if data.fn_trait.get_id(db, krate).is_none() { - return false; - } - - // FIXME: there are more assumptions that we should probably check here: - // the traits having no type params, FnOnce being a supertrait - - // the FnOnce trait needs to exist and have an assoc type named Output - let fn_once_trait = match (super::FnTrait::FnOnce).get_id(db, krate) { - Some(t) => t, - None => return false, - }; - db.trait_data(fn_once_trait).associated_type_by_name(&name![Output]).is_some() -} - -fn closure_fn_trait_impl_datum( - db: &dyn HirDatabase, - krate: CrateId, - data: super::ClosureFnTraitImplData, -) -> BuiltinImplData { - // for some closure |X, Y| -> Z: - // impl Fn<(T, U)> for closure V> { Output = V } - - let trait_ = data - .fn_trait - .get_id(db, krate) // get corresponding fn trait - // the existence of the Fn trait has been checked before - .expect("fn trait for closure impl missing"); - - let num_args: u16 = match &db.body(data.def)[data.expr] { - Expr::Lambda { args, .. } => args.len() as u16, - _ => { - log::warn!("closure for closure type {:?} not found", data); - 0 - } - }; - - let arg_ty = Ty::apply( - TypeCtor::Tuple { cardinality: num_args }, - Substs::builder(num_args as usize) - .fill_with_bound_vars(DebruijnIndex::INNERMOST, 0) - .build(), - ); - let sig_ty = Ty::apply( - TypeCtor::FnPtr { num_args, is_varargs: false }, - Substs::builder(num_args as usize + 1) - .fill_with_bound_vars(DebruijnIndex::INNERMOST, 0) - .build(), - ); - - let self_ty = Ty::apply_one(TypeCtor::Closure { def: data.def, expr: data.expr }, sig_ty); - - let trait_ref = TraitRef { - trait_, - substs: Substs::build_for_def(db, trait_).push(self_ty).push(arg_ty).build(), - }; - - let output_ty_id = AssocTyValue::ClosureFnTraitImplOutput(data); - - BuiltinImplData { - num_vars: num_args as usize + 1, - trait_ref, - where_clauses: Vec::new(), - assoc_ty_values: vec![output_ty_id], - } -} - -fn closure_fn_trait_output_assoc_ty_value( - db: &dyn HirDatabase, - krate: CrateId, - data: super::ClosureFnTraitImplData, -) -> BuiltinImplAssocTyValueData { - let impl_ = Impl::ClosureFnTraitImpl(data); - - let num_args: u16 = match &db.body(data.def)[data.expr] { - Expr::Lambda { args, .. } => args.len() as u16, - _ => { - log::warn!("closure for closure type {:?} not found", data); - 0 - } - }; - - let output_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, num_args.into())); - - let fn_once_trait = - (super::FnTrait::FnOnce).get_id(db, krate).expect("assoc ty value should not exist"); - - let output_ty_id = db - .trait_data(fn_once_trait) - .associated_type_by_name(&name![Output]) - .expect("assoc ty value should not exist"); - - BuiltinImplAssocTyValueData { - impl_, - assoc_ty_id: output_ty_id, - num_vars: num_args as usize + 1, - value: output_ty, - } -} diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index c448aea65..7f8ba2f12 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs @@ -12,15 +12,17 @@ use hir_def::{ }; use ra_db::{salsa::InternKey, CrateId}; -use super::{builtin, AssocTyValue, ChalkContext, Impl}; +use super::ChalkContext; use crate::{ db::HirDatabase, display::HirDisplay, method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, utils::generics, - CallableDef, DebruijnIndex, GenericPredicate, Substs, Ty, TypeCtor, + CallableDef, DebruijnIndex, FnSig, GenericPredicate, Substs, Ty, TypeCtor, +}; +use mapping::{ + convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, }; -use mapping::{convert_where_clauses, generic_predicate_to_inline_bound, make_binders}; pub use self::interner::*; @@ -102,9 +104,9 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { let in_self = self.db.trait_impls_in_crate(self.krate); let impl_maps = [in_deps, in_self]; - let id_to_chalk = |id: hir_def::ImplId| Impl::ImplDef(id).to_chalk(self.db); + let id_to_chalk = |id: hir_def::ImplId| id.to_chalk(self.db); - let mut result: Vec<_> = if fps.is_empty() { + let result: Vec<_> = if fps.is_empty() { debug!("Unrestricted search for {:?} impls...", trait_); impl_maps .iter() @@ -121,13 +123,6 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { .collect() }; - let arg: Option = - parameters.get(1).map(|p| from_chalk(self.db, p.assert_ty_ref(&Interner).clone())); - - builtin::get_builtin_impls(self.db, self.krate, &ty, &arg, trait_, |i| { - result.push(i.to_chalk(self.db)) - }); - debug!("impls_for_trait returned {} impls", result.len()); result } @@ -217,32 +212,40 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { _closure_id: chalk_ir::ClosureId, _substs: &chalk_ir::Substitution, ) -> rust_ir::ClosureKind { - // FIXME: implement closure support - unimplemented!() + // Fn is the closure kind that implements all three traits + rust_ir::ClosureKind::Fn } fn closure_inputs_and_output( &self, _closure_id: chalk_ir::ClosureId, - _substs: &chalk_ir::Substitution, + substs: &chalk_ir::Substitution, ) -> chalk_ir::Binders> { - // FIXME: implement closure support - unimplemented!() + let sig_ty: Ty = + from_chalk(self.db, substs.at(&Interner, 0).assert_ty_ref(&Interner).clone()); + let sig = FnSig::from_fn_ptr_substs( + &sig_ty.substs().expect("first closure param should be fn ptr"), + false, + ); + let io = rust_ir::FnDefInputsAndOutputDatum { + argument_types: sig.params().iter().map(|ty| ty.clone().to_chalk(self.db)).collect(), + return_type: sig.ret().clone().to_chalk(self.db), + }; + make_binders(io.shifted_in(&Interner), 0) } fn closure_upvars( &self, _closure_id: chalk_ir::ClosureId, _substs: &chalk_ir::Substitution, ) -> chalk_ir::Binders> { - // FIXME: implement closure support - unimplemented!() + let ty = Ty::unit().to_chalk(self.db); + make_binders(ty, 0) } fn closure_fn_substitution( &self, _closure_id: chalk_ir::ClosureId, _substs: &chalk_ir::Substitution, ) -> chalk_ir::Substitution { - // FIXME: implement closure support - unimplemented!() + Substs::empty().to_chalk(self.db) } fn trait_name(&self, _trait_id: chalk_ir::TraitId) -> String { @@ -417,11 +420,8 @@ pub(crate) fn impl_datum_query( ) -> Arc { let _p = ra_prof::profile("impl_datum"); debug!("impl_datum {:?}", impl_id); - let impl_: Impl = from_chalk(db, impl_id); - match impl_ { - Impl::ImplDef(impl_def) => impl_def_datum(db, krate, impl_id, impl_def), - _ => Arc::new(builtin::impl_datum(db, krate, impl_).to_chalk(db)), - } + let impl_: hir_def::ImplId = from_chalk(db, impl_id); + impl_def_datum(db, krate, impl_id, impl_) } fn impl_def_datum( @@ -472,7 +472,7 @@ fn impl_def_datum( let name = &db.type_alias_data(type_alias).name; trait_data.associated_type_by_name(name).is_some() }) - .map(|type_alias| AssocTyValue::TypeAlias(type_alias).to_chalk(db)) + .map(|type_alias| TypeAliasAsValue(type_alias).to_chalk(db)) .collect(); debug!("impl_datum: {:?}", impl_datum_bound); let impl_datum = ImplDatum { @@ -489,13 +489,8 @@ pub(crate) fn associated_ty_value_query( krate: CrateId, id: AssociatedTyValueId, ) -> Arc { - let data: AssocTyValue = from_chalk(db, id); - match data { - AssocTyValue::TypeAlias(type_alias) => { - type_alias_associated_ty_value(db, krate, type_alias) - } - _ => Arc::new(builtin::associated_ty_value(db, krate, data).to_chalk(db)), - } + let type_alias: TypeAliasAsValue = from_chalk(db, id); + type_alias_associated_ty_value(db, krate, type_alias.0) } fn type_alias_associated_ty_value( @@ -518,7 +513,7 @@ fn type_alias_associated_ty_value( let ty = db.ty(type_alias.into()); let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) }; let value = rust_ir::AssociatedTyValue { - impl_id: Impl::ImplDef(impl_id).to_chalk(db), + impl_id: impl_id.to_chalk(db), associated_ty_id: assoc_ty.to_chalk(db), value: make_binders(value_bound, ty.num_binders), }; @@ -581,18 +576,6 @@ impl From for FnDefId { } } -impl From for crate::traits::GlobalImplId { - fn from(impl_id: ImplId) -> Self { - InternKey::from_intern_id(impl_id.0) - } -} - -impl From for ImplId { - fn from(impl_id: crate::traits::GlobalImplId) -> Self { - chalk_ir::ImplId(impl_id.as_intern_id()) - } -} - impl From for crate::db::InternedOpaqueTyId { fn from(id: OpaqueTyId) -> Self { InternKey::from_intern_id(id.0) @@ -605,14 +588,14 @@ impl From for OpaqueTyId { } } -impl From> for crate::traits::AssocTyValueId { - fn from(id: rust_ir::AssociatedTyValueId) -> Self { +impl From> for crate::db::ClosureId { + fn from(id: chalk_ir::ClosureId) -> Self { Self::from_intern_id(id.0) } } -impl From for rust_ir::AssociatedTyValueId { - fn from(assoc_ty_value_id: crate::traits::AssocTyValueId) -> Self { - rust_ir::AssociatedTyValueId(assoc_ty_value_id.as_intern_id()) +impl From for chalk_ir::ClosureId { + fn from(id: crate::db::ClosureId) -> Self { + chalk_ir::ClosureId(id.as_intern_id()) } } diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index 3ebb55f77..796947e69 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -15,7 +15,7 @@ use ra_db::salsa::InternKey; use crate::{ db::HirDatabase, primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness}, - traits::{builtin, AssocTyValue, Canonical, Impl, Obligation}, + traits::{Canonical, Obligation}, ApplicationTy, CallableDef, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TyKind, TypeCtor, }; @@ -311,8 +311,15 @@ impl ToChalk for TypeCtor { } TypeCtor::Never => TypeName::Never, - // FIXME convert these - TypeCtor::Adt(_) | TypeCtor::FnPtr { .. } | TypeCtor::Closure { .. } => { + TypeCtor::Closure { def, expr } => { + let closure_id = db.intern_closure((def, expr)); + TypeName::Closure(closure_id.into()) + } + + TypeCtor::FnPtr { .. } => panic!("Trying to convert FnPtr to TypeName"), + + TypeCtor::Adt(_) => { + // FIXME no interning needed anymore // other TypeCtors get interned and turned into a chalk StructId let struct_id = db.intern_type_ctor(self).into(); TypeName::Adt(struct_id) @@ -355,13 +362,16 @@ impl ToChalk for TypeCtor { let callable_def = from_chalk(db, fn_def_id); TypeCtor::FnDef(callable_def) } + TypeName::Array => TypeCtor::Array, - TypeName::Array | TypeName::Error => { - // this should not be reached, since we don't represent TypeName::Error with TypeCtor - unreachable!() + TypeName::Closure(id) => { + let id: crate::db::ClosureId = id.into(); + let (def, expr) = db.lookup_intern_closure(id); + TypeCtor::Closure { def, expr } } - TypeName::Closure(_) => { - // FIXME: implement closure support + + TypeName::Error => { + // this should not be reached, since we don't represent TypeName::Error with TypeCtor unreachable!() } } @@ -433,15 +443,15 @@ impl ToChalk for Mutability { } } -impl ToChalk for Impl { +impl ToChalk for hir_def::ImplId { type Chalk = ImplId; - fn to_chalk(self, db: &dyn HirDatabase) -> ImplId { - db.intern_chalk_impl(self).into() + fn to_chalk(self, _db: &dyn HirDatabase) -> ImplId { + chalk_ir::ImplId(self.as_intern_id()) } - fn from_chalk(db: &dyn HirDatabase, impl_id: ImplId) -> Impl { - db.lookup_intern_chalk_impl(impl_id.into()) + fn from_chalk(_db: &dyn HirDatabase, impl_id: ImplId) -> hir_def::ImplId { + InternKey::from_intern_id(impl_id.0) } } @@ -469,15 +479,20 @@ impl ToChalk for TypeAliasId { } } -impl ToChalk for AssocTyValue { +pub struct TypeAliasAsValue(pub TypeAliasId); + +impl ToChalk for TypeAliasAsValue { type Chalk = AssociatedTyValueId; - fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValueId { - db.intern_assoc_ty_value(self).into() + fn to_chalk(self, _db: &dyn HirDatabase) -> AssociatedTyValueId { + rust_ir::AssociatedTyValueId(self.0.as_intern_id()) } - fn from_chalk(db: &dyn HirDatabase, assoc_ty_value_id: AssociatedTyValueId) -> AssocTyValue { - db.lookup_intern_assoc_ty_value(assoc_ty_value_id.into()) + fn from_chalk( + _db: &dyn HirDatabase, + assoc_ty_value_id: AssociatedTyValueId, + ) -> TypeAliasAsValue { + TypeAliasAsValue(TypeAliasId::from_intern_id(assoc_ty_value_id.0)) } } @@ -686,52 +701,6 @@ where } } -impl ToChalk for builtin::BuiltinImplData { - type Chalk = ImplDatum; - - fn to_chalk(self, db: &dyn HirDatabase) -> ImplDatum { - let impl_type = rust_ir::ImplType::External; - let where_clauses = self.where_clauses.into_iter().map(|w| w.to_chalk(db)).collect(); - - let impl_datum_bound = - rust_ir::ImplDatumBound { trait_ref: self.trait_ref.to_chalk(db), where_clauses }; - let associated_ty_value_ids = - self.assoc_ty_values.into_iter().map(|v| v.to_chalk(db)).collect(); - rust_ir::ImplDatum { - binders: make_binders(impl_datum_bound, self.num_vars), - impl_type, - polarity: rust_ir::Polarity::Positive, - associated_ty_value_ids, - } - } - - fn from_chalk(_db: &dyn HirDatabase, _data: ImplDatum) -> Self { - unimplemented!() - } -} - -impl ToChalk for builtin::BuiltinImplAssocTyValueData { - type Chalk = AssociatedTyValue; - - fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValue { - let ty = self.value.to_chalk(db); - let value_bound = rust_ir::AssociatedTyValueBound { ty }; - - rust_ir::AssociatedTyValue { - associated_ty_id: self.assoc_ty_id.to_chalk(db), - impl_id: self.impl_.to_chalk(db), - value: make_binders(value_bound, self.num_vars), - } - } - - fn from_chalk( - _db: &dyn HirDatabase, - _data: AssociatedTyValue, - ) -> builtin::BuiltinImplAssocTyValueData { - unimplemented!() - } -} - pub(super) fn make_binders(value: T, num_vars: usize) -> chalk_ir::Binders where T: HasInterner, -- cgit v1.2.3 From 20770044631fd0c21caa12f9bc87489ea6c848ee Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 15 Jul 2020 21:47:45 +0200 Subject: Remove TypeCtor interning Our TypeCtor and Chalk's TypeName match now! --- crates/ra_hir_ty/src/traits/chalk.rs | 12 ------------ crates/ra_hir_ty/src/traits/chalk/interner.rs | 14 +++++++++++++- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 13 ++++++------- 3 files changed, 19 insertions(+), 20 deletions(-) (limited to 'crates/ra_hir_ty/src/traits') diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index 7f8ba2f12..1ef5baa05 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs @@ -552,18 +552,6 @@ pub(crate) fn fn_def_datum_query( Arc::new(datum) } -impl From for crate::TypeCtorId { - fn from(struct_id: AdtId) -> Self { - struct_id.0 - } -} - -impl From for AdtId { - fn from(type_ctor_id: crate::TypeCtorId) -> Self { - chalk_ir::AdtId(type_ctor_id) - } -} - impl From for crate::CallableDefId { fn from(fn_def_id: FnDefId) -> Self { InternKey::from_intern_id(fn_def_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 156b691b4..8d4c51a8f 100644 --- a/crates/ra_hir_ty/src/traits/chalk/interner.rs +++ b/crates/ra_hir_ty/src/traits/chalk/interner.rs @@ -41,7 +41,7 @@ impl chalk_ir::interner::Interner for Interner { type InternedCanonicalVarKinds = Vec>; type InternedConstraints = Vec>>; type DefId = InternId; - type InternedAdtId = crate::TypeCtorId; + type InternedAdtId = hir_def::AdtId; type Identifier = TypeAliasId; type FnAbi = (); @@ -364,6 +364,18 @@ impl chalk_ir::interner::Interner for Interner { ) -> &'a [chalk_ir::InEnvironment>] { constraints } + fn debug_closure_id( + _fn_def_id: chalk_ir::ClosureId, + _fmt: &mut fmt::Formatter<'_>, + ) -> Option { + None + } + fn debug_constraints( + _clauses: &chalk_ir::Constraints, + _fmt: &mut fmt::Formatter<'_>, + ) -> Option { + None + } } impl chalk_ir::interner::HasInterner for Interner { diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index 796947e69..a852ce2ac 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -316,20 +316,19 @@ impl ToChalk for TypeCtor { TypeName::Closure(closure_id.into()) } - TypeCtor::FnPtr { .. } => panic!("Trying to convert FnPtr to TypeName"), + TypeCtor::Adt(adt_id) => TypeName::Adt(chalk_ir::AdtId(adt_id)), - TypeCtor::Adt(_) => { - // FIXME no interning needed anymore - // other TypeCtors get interned and turned into a chalk StructId - let struct_id = db.intern_type_ctor(self).into(); - TypeName::Adt(struct_id) + TypeCtor::FnPtr { .. } => { + // This should not be reached, since Chalk doesn't represent + // function pointers with TypeName + unreachable!() } } } fn from_chalk(db: &dyn HirDatabase, type_name: TypeName) -> TypeCtor { match type_name { - TypeName::Adt(struct_id) => db.lookup_intern_type_ctor(struct_id.into()), + TypeName::Adt(struct_id) => TypeCtor::Adt(struct_id.0), TypeName::AssociatedType(type_id) => TypeCtor::AssociatedType(from_chalk(db, type_id)), TypeName::OpaqueType(opaque_type_id) => { TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)) -- cgit v1.2.3 From b598ab8be46b50d5f407c3858c7b2c788328edc7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jul 2020 13:13:17 +0200 Subject: Rename CallableDefId -> InternedCallabelDefid --- crates/ra_hir_ty/src/traits/chalk.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir_ty/src/traits') diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index 1ef5baa05..e0a6cfe88 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs @@ -552,14 +552,14 @@ pub(crate) fn fn_def_datum_query( Arc::new(datum) } -impl From for crate::CallableDefId { +impl From for crate::db::InternedCallableDefId { fn from(fn_def_id: FnDefId) -> Self { InternKey::from_intern_id(fn_def_id.0) } } -impl From for FnDefId { - fn from(callable_def_id: crate::CallableDefId) -> Self { +impl From for FnDefId { + fn from(callable_def_id: crate::db::InternedCallableDefId) -> Self { chalk_ir::FnDefId(callable_def_id.as_intern_id()) } } -- cgit v1.2.3 From b5ce84b17023d27f4e96ec7911aca712db0e000b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jul 2020 13:15:00 +0200 Subject: Align CallableDefId naming with other ids --- crates/ra_hir_ty/src/traits/chalk.rs | 4 ++-- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 6 +++--- crates/ra_hir_ty/src/traits/chalk/tls.rs | 24 ++++++++++++------------ 3 files changed, 17 insertions(+), 17 deletions(-) (limited to 'crates/ra_hir_ty/src/traits') diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index e0a6cfe88..78d0bc43b 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs @@ -18,7 +18,7 @@ use crate::{ display::HirDisplay, method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, utils::generics, - CallableDef, DebruijnIndex, FnSig, GenericPredicate, Substs, Ty, TypeCtor, + CallableDefId, DebruijnIndex, FnSig, GenericPredicate, Substs, Ty, TypeCtor, }; use mapping::{ convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, @@ -525,7 +525,7 @@ pub(crate) fn fn_def_datum_query( _krate: CrateId, fn_def_id: FnDefId, ) -> Arc { - let callable_def: CallableDef = from_chalk(db, fn_def_id); + let callable_def: CallableDefId = from_chalk(db, fn_def_id); let generic_params = generics(db.upcast(), callable_def.into()); let sig = db.callable_item_signature(callable_def); let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index a852ce2ac..09d8347ca 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -16,7 +16,7 @@ use crate::{ db::HirDatabase, primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness}, traits::{Canonical, Obligation}, - ApplicationTy, CallableDef, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, + ApplicationTy, CallableDefId, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TyKind, TypeCtor, }; @@ -454,14 +454,14 @@ impl ToChalk for hir_def::ImplId { } } -impl ToChalk for CallableDef { +impl ToChalk for CallableDefId { type Chalk = FnDefId; fn to_chalk(self, db: &dyn HirDatabase) -> FnDefId { db.intern_callable_def(self).into() } - fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDef { + fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDefId { db.lookup_intern_callable_def(fn_def_id.into()) } } diff --git a/crates/ra_hir_ty/src/traits/chalk/tls.rs b/crates/ra_hir_ty/src/traits/chalk/tls.rs index 1e226baea..db915625c 100644 --- a/crates/ra_hir_ty/src/traits/chalk/tls.rs +++ b/crates/ra_hir_ty/src/traits/chalk/tls.rs @@ -5,7 +5,7 @@ use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplicat use itertools::Itertools; use super::{from_chalk, Interner}; -use crate::{db::HirDatabase, CallableDef, TypeCtor}; +use crate::{db::HirDatabase, CallableDefId, TypeCtor}; use hir_def::{AdtId, AssocContainerId, DefWithBodyId, Lookup, TypeAliasId}; pub use unsafe_tls::{set_current_program, with_current_program}; @@ -38,16 +38,16 @@ impl DebugContext<'_> { } TypeCtor::FnDef(def) => { let name = match def { - CallableDef::FunctionId(ff) => self.0.function_data(ff).name.clone(), - CallableDef::StructId(s) => self.0.struct_data(s).name.clone(), - CallableDef::EnumVariantId(e) => { + CallableDefId::FunctionId(ff) => self.0.function_data(ff).name.clone(), + CallableDefId::StructId(s) => self.0.struct_data(s).name.clone(), + CallableDefId::EnumVariantId(e) => { let enum_data = self.0.enum_data(e.parent); enum_data.variants[e.local_id].name.clone() } }; match def { - CallableDef::FunctionId(_) => write!(f, "{{fn {}}}", name)?, - CallableDef::StructId(_) | CallableDef::EnumVariantId(_) => { + CallableDefId::FunctionId(_) => write!(f, "{{fn {}}}", name)?, + CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => { write!(f, "{{ctor {}}}", name)? } } @@ -255,18 +255,18 @@ impl DebugContext<'_> { fn_def_id: chalk_ir::FnDefId, fmt: &mut fmt::Formatter<'_>, ) -> Result<(), fmt::Error> { - let def: CallableDef = from_chalk(self.0, fn_def_id); + let def: CallableDefId = from_chalk(self.0, fn_def_id); let name = match def { - CallableDef::FunctionId(ff) => self.0.function_data(ff).name.clone(), - CallableDef::StructId(s) => self.0.struct_data(s).name.clone(), - CallableDef::EnumVariantId(e) => { + CallableDefId::FunctionId(ff) => self.0.function_data(ff).name.clone(), + CallableDefId::StructId(s) => self.0.struct_data(s).name.clone(), + CallableDefId::EnumVariantId(e) => { let enum_data = self.0.enum_data(e.parent); enum_data.variants[e.local_id].name.clone() } }; match def { - CallableDef::FunctionId(_) => write!(fmt, "{{fn {}}}", name), - CallableDef::StructId(_) | CallableDef::EnumVariantId(_) => { + CallableDefId::FunctionId(_) => write!(fmt, "{{fn {}}}", name), + CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => { write!(fmt, "{{ctor {}}}", name) } } -- cgit v1.2.3 From e11006986ab1bfc770acb4047814f99f7ca6114f Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Sun, 19 Jul 2020 15:26:51 +0200 Subject: Specify default adt representation for chalk integration --- crates/ra_hir_ty/src/traits/chalk.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_ty/src/traits') diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index 78d0bc43b..5298dbecf 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs @@ -54,7 +54,7 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { self.db.struct_datum(self.krate, struct_id) } fn adt_repr(&self, _struct_id: AdtId) -> rust_ir::AdtRepr { - unreachable!() + rust_ir::AdtRepr { repr_c: false, repr_packed: false } } fn impl_datum(&self, impl_id: ImplId) -> Arc { self.db.impl_datum(self.krate, impl_id) -- cgit v1.2.3 From 797cdb00d91a221d62438b23dfd625a78163a58d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Thu, 30 Jul 2020 20:37:28 +0300 Subject: Bump chalk --- crates/ra_hir_ty/src/traits/chalk.rs | 19 ++++++++----------- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 22 ++++++++++++++++------ 2 files changed, 24 insertions(+), 17 deletions(-) (limited to 'crates/ra_hir_ty/src/traits') diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index 5298dbecf..1c7065364 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs @@ -183,6 +183,7 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { .collect(), 1, ), + where_clauses: make_binders(vec![], 0), }; let num_vars = datas.num_binders; Arc::new(OpaqueTyDatum { opaque_ty_id: id, bound: make_binders(bound, num_vars) }) @@ -193,15 +194,6 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { Ty::Unknown.to_chalk(self.db) } - fn force_impl_for( - &self, - _well_known: rust_ir::WellKnownTrait, - _ty: &chalk_ir::TyData, - ) -> Option { - // this method is mostly for rustc - None - } - fn is_object_safe(&self, _trait_id: chalk_ir::TraitId) -> bool { // FIXME: implement actual object safety true @@ -547,8 +539,13 @@ pub(crate) fn fn_def_datum_query( ), where_clauses, }; - let datum = - FnDefDatum { id: fn_def_id, binders: make_binders(bound, sig.num_binders), abi: () }; + let datum = FnDefDatum { + id: fn_def_id, + abi: (), + safety: chalk_ir::Safety::Safe, + variadic: sig.value.is_varargs, + binders: make_binders(bound, sig.num_binders), + }; Arc::new(datum) } diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index 09d8347ca..b3e92993d 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -30,11 +30,16 @@ impl ToChalk for Ty { Ty::Apply(apply_ty) => match apply_ty.ctor { TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), TypeCtor::Array => array_to_chalk(db, apply_ty.parameters), - TypeCtor::FnPtr { num_args: _, is_varargs: _ } => { - // FIXME: handle is_varargs + TypeCtor::FnPtr { num_args: _, is_varargs } => { let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); - chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: 0, substitution }) - .intern(&Interner) + chalk_ir::TyData::Function(chalk_ir::FnPointer { + num_binders: 0, + abi: (), + safety: chalk_ir::Safety::Safe, + variadic: is_varargs, + substitution, + }) + .intern(&Interner) } _ => { let name = apply_ty.ctor.to_chalk(db); @@ -118,7 +123,12 @@ impl ToChalk for Ty { let parameters = from_chalk(db, opaque_ty.substitution); Ty::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters }) } - chalk_ir::TyData::Function(chalk_ir::Fn { num_binders, substitution }) => { + chalk_ir::TyData::Function(chalk_ir::FnPointer { + num_binders, + variadic, + substitution, + .. + }) => { assert_eq!(num_binders, 0); let parameters: Substs = from_chalk( db, @@ -127,7 +137,7 @@ impl ToChalk for Ty { Ty::Apply(ApplicationTy { ctor: TypeCtor::FnPtr { num_args: (parameters.len() - 1) as u16, - is_varargs: false, + is_varargs: variadic, }, parameters, }) -- cgit v1.2.3