From 5ca481bbdc75bd3a9dbce4c94b014bd0eb8bd9e8 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 9 Apr 2021 14:15:26 +0200 Subject: Move ToChalk -> mapping --- crates/hir_ty/src/chalk_db.rs | 32 +++++++++++++++++--------------- crates/hir_ty/src/db.rs | 16 +++++++++++++--- crates/hir_ty/src/display.rs | 10 +++++----- crates/hir_ty/src/infer/expr.rs | 2 +- crates/hir_ty/src/interner.rs | 17 +++++++++++++---- crates/hir_ty/src/lib.rs | 2 +- crates/hir_ty/src/lower.rs | 8 ++++---- crates/hir_ty/src/mapping.rs | 20 ++++++++++++++++++-- crates/hir_ty/src/tls.rs | 4 +++- crates/hir_ty/src/traits.rs | 2 +- 10 files changed, 76 insertions(+), 37 deletions(-) diff --git a/crates/hir_ty/src/chalk_db.rs b/crates/hir_ty/src/chalk_db.rs index 566e4f406..f5b2c5ff0 100644 --- a/crates/hir_ty/src/chalk_db.rs +++ b/crates/hir_ty/src/chalk_db.rs @@ -1,4 +1,5 @@ -//! Conversion code from/to Chalk. +//! The implementation of `RustIrDatabase` for Chalk, which provides information +//! about the code that Chalk needs. use std::sync::Arc; use log::debug; @@ -13,7 +14,21 @@ use hir_def::{ }; use hir_expand::name::name; -use crate::{AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, Interner, ProjectionTy, Substitution, TraitRef, TraitRefExt, Ty, TyBuilder, TyExt, TyKind, WhereClause, traits::ChalkContext, db::HirDatabase, display::HirDisplay, from_assoc_type_id, make_only_type_binders, mapping::{convert_where_clauses, generic_predicate_to_inline_bound, TypeAliasAsValue}, method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, to_assoc_type_id, to_chalk_trait_id, utils::generics}; +use crate::{ + db::HirDatabase, + display::HirDisplay, + from_assoc_type_id, make_only_type_binders, + mapping::{ + convert_where_clauses, from_chalk, generic_predicate_to_inline_bound, ToChalk, + TypeAliasAsValue, + }, + method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, + to_assoc_type_id, to_chalk_trait_id, + traits::ChalkContext, + utils::generics, + AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, Interner, ProjectionTy, + Substitution, TraitRef, TraitRefExt, Ty, TyBuilder, TyExt, TyKind, WhereClause, +}; pub(crate) type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum; pub(crate) type TraitDatum = chalk_solve::rust_ir::TraitDatum; @@ -31,19 +46,6 @@ pub(crate) type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue; pub(crate) type Variances = chalk_ir::Variances; -pub(crate) trait ToChalk { - type Chalk; - fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk; - fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self; -} - -pub(crate) fn from_chalk(db: &dyn HirDatabase, chalk: ChalkT) -> T -where - T: ToChalk, -{ - T::from_chalk(db, chalk) -} - impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { fn associated_ty_data(&self, id: AssocTypeId) -> Arc { self.db.associated_ty_data(id) diff --git a/crates/hir_ty/src/db.rs b/crates/hir_ty/src/db.rs index ea0580df1..e11fc579e 100644 --- a/crates/hir_ty/src/db.rs +++ b/crates/hir_ty/src/db.rs @@ -9,7 +9,12 @@ use hir_def::{ }; use la_arena::ArenaMap; -use crate::{Binders, CallableDefId, FnDefId, ImplTraitId, InferenceResult, Interner, PolyFnSig, QuantifiedWhereClause, ReturnTypeImplTraits, TraitRef, Ty, TyDefId, ValueTyDefId, chalk_db, method_resolution::{InherentImpls, TraitImpls}}; +use crate::{ + chalk_db, + method_resolution::{InherentImpls, TraitImpls}, + Binders, CallableDefId, FnDefId, ImplTraitId, InferenceResult, Interner, PolyFnSig, + QuantifiedWhereClause, ReturnTypeImplTraits, TraitRef, Ty, TyDefId, ValueTyDefId, +}; use hir_expand::name::Name; #[salsa::query_group(HirDatabaseStorage)] @@ -93,10 +98,15 @@ pub trait HirDatabase: DefDatabase + Upcast { fn associated_ty_data(&self, id: chalk_db::AssocTypeId) -> Arc; #[salsa::invoke(chalk_db::trait_datum_query)] - fn trait_datum(&self, krate: CrateId, trait_id: chalk_db::TraitId) -> Arc; + fn trait_datum(&self, krate: CrateId, trait_id: chalk_db::TraitId) + -> Arc; #[salsa::invoke(chalk_db::struct_datum_query)] - fn struct_datum(&self, krate: CrateId, struct_id: chalk_db::AdtId) -> Arc; + fn struct_datum( + &self, + krate: CrateId, + struct_id: chalk_db::AdtId, + ) -> Arc; #[salsa::invoke(chalk_db::impl_datum_query)] fn impl_datum(&self, krate: CrateId, impl_id: chalk_db::ImplId) -> Arc; diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index a0746cd8f..ae3a82780 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -20,11 +20,11 @@ use hir_expand::name::Name; use crate::{ const_from_placeholder_idx, db::HirDatabase, from_assoc_type_id, from_foreign_def_id, - from_placeholder_idx, lt_from_placeholder_idx, primitive, subst_prefix, to_assoc_type_id, - chalk_db::from_chalk, utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, - CallableSig, Const, ConstValue, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, - LifetimeData, LifetimeOutlives, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt, - QuantifiedWhereClause, Scalar, TraitRef, TraitRefExt, Ty, TyExt, TyKind, WhereClause, + from_placeholder_idx, lt_from_placeholder_idx, mapping::from_chalk, primitive, subst_prefix, + to_assoc_type_id, utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, CallableSig, Const, + ConstValue, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, LifetimeData, + LifetimeOutlives, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt, QuantifiedWhereClause, + Scalar, TraitRef, TraitRefExt, Ty, TyExt, TyKind, WhereClause, }; pub struct HirFormatter<'a> { diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index d42383012..50497eecb 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -17,11 +17,11 @@ use syntax::ast::RangeOp; use crate::{ autoderef, dummy_usize_const, lower::lower_to_chalk_mutability, + mapping::from_chalk, method_resolution, op, primitive::{self, UintTy}, static_lifetime, to_chalk_trait_id, traits::FnTrait, - chalk_db::from_chalk, utils::{generics, Generics}, AdtId, Binders, CallableDefId, FnPointer, FnSig, FnSubst, InEnvironment, Interner, ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyExt, TyKind, diff --git a/crates/hir_ty/src/interner.rs b/crates/hir_ty/src/interner.rs index 0187767ce..02e26c04e 100644 --- a/crates/hir_ty/src/interner.rs +++ b/crates/hir_ty/src/interner.rs @@ -1,7 +1,7 @@ //! Implementation of the Chalk `Interner` trait, which allows customizing the //! representation of the various objects Chalk deals with (types, goals etc.). -use crate::{GenericArg, tls, chalk_db}; +use crate::{chalk_db, tls, GenericArg}; use base_db::salsa::InternId; use chalk_ir::{Goal, GoalData}; use hir_def::{ @@ -60,15 +60,24 @@ impl chalk_ir::interner::Interner for Interner { type Identifier = TypeAliasId; type FnAbi = (); - fn debug_adt_id(type_kind_id: chalk_db::AdtId, fmt: &mut fmt::Formatter<'_>) -> Option { + fn debug_adt_id( + type_kind_id: chalk_db::AdtId, + fmt: &mut fmt::Formatter<'_>, + ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_struct_id(type_kind_id, fmt))) } - fn debug_trait_id(type_kind_id: chalk_db::TraitId, fmt: &mut fmt::Formatter<'_>) -> Option { + fn debug_trait_id( + type_kind_id: chalk_db::TraitId, + fmt: &mut fmt::Formatter<'_>, + ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_trait_id(type_kind_id, fmt))) } - fn debug_assoc_type_id(id: chalk_db::AssocTypeId, fmt: &mut fmt::Formatter<'_>) -> Option { + fn debug_assoc_type_id( + id: chalk_db::AssocTypeId, + fmt: &mut fmt::Formatter<'_>, + ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_assoc_type_id(id, fmt))) } diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 0743d3d30..434e2e533 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -51,13 +51,13 @@ pub use autoderef::autoderef; pub use builder::TyBuilder; pub use chalk_ext::*; pub use infer::{could_unify, InferenceResult}; +pub use interner::Interner; pub use lower::{ associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, TyDefId, TyLoweringContext, ValueTyDefId, }; pub use traits::TraitEnvironment; pub use walk::TypeWalk; -pub use interner::Interner; pub use chalk_ir::{ cast::Cast, AdtId, BoundVar, DebruijnIndex, Mutability, Safety, Scalar, TyVariableKind, diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 0cdded3e1..a035686bc 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -27,14 +27,14 @@ use stdx::impl_from; use crate::{ db::HirDatabase, - dummy_usize_const, static_lifetime, to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx, - chalk_db::ToChalk, - Interner, + dummy_usize_const, + mapping::ToChalk, + static_lifetime, to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx, utils::{ all_super_trait_refs, associated_type_by_name_including_super_traits, generics, Generics, }, AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig, - FnSubst, ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, + FnSubst, ImplTraitId, Interner, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause, }; diff --git a/crates/hir_ty/src/mapping.rs b/crates/hir_ty/src/mapping.rs index 4d3c41b2c..f57115de6 100644 --- a/crates/hir_ty/src/mapping.rs +++ b/crates/hir_ty/src/mapping.rs @@ -3,13 +3,29 @@ //! Chalk (in both directions); plus some helper functions for more specialized //! conversions. -use chalk_ir::{DebruijnIndex, cast::Cast, fold::Shift}; +use chalk_ir::{cast::Cast, fold::Shift, DebruijnIndex}; use chalk_solve::rust_ir; use base_db::salsa::InternKey; use hir_def::{GenericDefId, TypeAliasId}; -use crate::{AliasEq, AliasTy, CallableDefId, FnDefId, Interner, ProjectionTyExt, QuantifiedWhereClause, Substitution, Ty, WhereClause, chalk_db::{self, ToChalk}, db::HirDatabase}; +use crate::{ + chalk_db, db::HirDatabase, AliasEq, AliasTy, CallableDefId, FnDefId, Interner, ProjectionTyExt, + QuantifiedWhereClause, Substitution, Ty, WhereClause, +}; + +pub(crate) trait ToChalk { + type Chalk; + fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk; + fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self; +} + +pub(crate) fn from_chalk(db: &dyn HirDatabase, chalk: ChalkT) -> T +where + T: ToChalk, +{ + T::from_chalk(db, chalk) +} impl ToChalk for hir_def::TraitId { type Chalk = chalk_db::TraitId; diff --git a/crates/hir_ty/src/tls.rs b/crates/hir_ty/src/tls.rs index c319bcf70..92989f11f 100644 --- a/crates/hir_ty/src/tls.rs +++ b/crates/hir_ty/src/tls.rs @@ -4,7 +4,9 @@ use std::fmt; use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication}; use itertools::Itertools; -use crate::{db::HirDatabase, from_assoc_type_id, CallableDefId, chalk_db::{from_chalk, self}, Interner}; +use crate::{ + chalk_db, db::HirDatabase, from_assoc_type_id, mapping::from_chalk, CallableDefId, Interner, +}; use hir_def::{AdtId, AssocContainerId, Lookup, TypeAliasId}; pub(crate) use unsafe_tls::{set_current_program, with_current_program}; diff --git a/crates/hir_ty/src/traits.rs b/crates/hir_ty/src/traits.rs index 5f8666d90..7f77e421d 100644 --- a/crates/hir_ty/src/traits.rs +++ b/crates/hir_ty/src/traits.rs @@ -9,7 +9,7 @@ use stdx::panic_context; use crate::{ db::HirDatabase, AliasEq, AliasTy, Canonical, DomainGoal, Guidance, HirDisplay, InEnvironment, - Solution, TraitRefExt, Ty, TyKind, WhereClause, Interner, + Interner, Solution, TraitRefExt, Ty, TyKind, WhereClause, }; /// This controls how much 'time' we give the Chalk solver before giving up. -- cgit v1.2.3