Move ToChalk -> mapping

This commit is contained in:
Florian Diebold 2021-04-09 14:15:26 +02:00
parent 743faa21e7
commit 5ca481bbdc
10 changed files with 76 additions and 37 deletions

View File

@ -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<Interner>;
pub(crate) type TraitDatum = chalk_solve::rust_ir::TraitDatum<Interner>;
@ -31,19 +46,6 @@ pub(crate) type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue<Inte
pub(crate) type FnDefDatum = chalk_solve::rust_ir::FnDefDatum<Interner>;
pub(crate) type Variances = chalk_ir::Variances<Interner>;
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<T, ChalkT>(db: &dyn HirDatabase, chalk: ChalkT) -> T
where
T: ToChalk<Chalk = ChalkT>,
{
T::from_chalk(db, chalk)
}
impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
fn associated_ty_data(&self, id: AssocTypeId) -> Arc<AssociatedTyDatum> {
self.db.associated_ty_data(id)

View File

@ -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<dyn DefDatabase> {
fn associated_ty_data(&self, id: chalk_db::AssocTypeId) -> Arc<chalk_db::AssociatedTyDatum>;
#[salsa::invoke(chalk_db::trait_datum_query)]
fn trait_datum(&self, krate: CrateId, trait_id: chalk_db::TraitId) -> Arc<chalk_db::TraitDatum>;
fn trait_datum(&self, krate: CrateId, trait_id: chalk_db::TraitId)
-> Arc<chalk_db::TraitDatum>;
#[salsa::invoke(chalk_db::struct_datum_query)]
fn struct_datum(&self, krate: CrateId, struct_id: chalk_db::AdtId) -> Arc<chalk_db::StructDatum>;
fn struct_datum(
&self,
krate: CrateId,
struct_id: chalk_db::AdtId,
) -> Arc<chalk_db::StructDatum>;
#[salsa::invoke(chalk_db::impl_datum_query)]
fn impl_datum(&self, krate: CrateId, impl_id: chalk_db::ImplId) -> Arc<chalk_db::ImplDatum>;

View File

@ -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> {

View File

@ -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,

View File

@ -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<fmt::Result> {
fn debug_adt_id(
type_kind_id: chalk_db::AdtId,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
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<fmt::Result> {
fn debug_trait_id(
type_kind_id: chalk_db::TraitId,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
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<fmt::Result> {
fn debug_assoc_type_id(
id: chalk_db::AssocTypeId,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
tls::with_current_program(|prog| Some(prog?.debug_assoc_type_id(id, fmt)))
}

View File

@ -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,

View File

@ -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,
};

View File

@ -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<T, ChalkT>(db: &dyn HirDatabase, chalk: ChalkT) -> T
where
T: ToChalk<Chalk = ChalkT>,
{
T::from_chalk(db, chalk)
}
impl ToChalk for hir_def::TraitId {
type Chalk = chalk_db::TraitId;

View File

@ -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};

View File

@ -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.