//! Implementation of the Chalk `Interner` trait, which allows customizing the //! representation of the various objects Chalk deals with (types, goals etc.). use super::tls; use base_db::salsa::InternId; use chalk_ir::{Goal, GoalData}; use hir_def::{ intern::{impl_internable, InternStorage, Internable, Interned}, TypeAliasId, }; use crate::GenericArg; use smallvec::SmallVec; use std::{fmt, sync::Arc}; #[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] pub struct Interner; pub(crate) type AssocTypeId = chalk_ir::AssocTypeId; pub(crate) type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum; pub(crate) type TraitId = chalk_ir::TraitId; pub(crate) type TraitDatum = chalk_solve::rust_ir::TraitDatum; pub(crate) type AdtId = chalk_ir::AdtId; pub(crate) type StructDatum = chalk_solve::rust_ir::AdtDatum; pub(crate) type ImplId = chalk_ir::ImplId; pub(crate) type ImplDatum = chalk_solve::rust_ir::ImplDatum; pub(crate) type AssociatedTyValueId = chalk_solve::rust_ir::AssociatedTyValueId; pub(crate) type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue; pub(crate) type FnDefDatum = chalk_solve::rust_ir::FnDefDatum; pub(crate) type OpaqueTyId = chalk_ir::OpaqueTyId; pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum; pub(crate) type Variances = chalk_ir::Variances; #[derive(PartialEq, Eq, Hash, Debug)] pub struct InternedVariableKindsInner(Vec>); #[derive(PartialEq, Eq, Hash, Debug)] pub struct InternedSubstitutionInner(SmallVec<[GenericArg; 2]>); #[derive(PartialEq, Eq, Hash, Debug)] pub struct InternedTypeInner(chalk_ir::TyData); #[derive(PartialEq, Eq, Hash, Debug)] pub struct InternedWrapper(T); impl std::ops::Deref for InternedWrapper { type Target = T; fn deref(&self) -> &Self::Target { &self.0 } } impl_internable!( InternedVariableKindsInner, InternedSubstitutionInner, InternedTypeInner, InternedWrapper>, InternedWrapper>, InternedWrapper>>, ); impl chalk_ir::interner::Interner for Interner { type InternedType = Interned; type InternedLifetime = Interned>>; type InternedConst = Interned>>; type InternedConcreteConst = (); type InternedGenericArg = chalk_ir::GenericArgData; type InternedGoal = Arc>; type InternedGoals = Vec>; type InternedSubstitution = Interned; type InternedProgramClause = Arc>; type InternedProgramClauses = Arc<[chalk_ir::ProgramClause]>; type InternedQuantifiedWhereClauses = Vec>; type InternedVariableKinds = Interned; type InternedCanonicalVarKinds = Interned>>>; type InternedConstraints = Vec>>; type InternedVariances = Arc<[chalk_ir::Variance]>; type DefId = InternId; type InternedAdtId = hir_def::AdtId; type Identifier = TypeAliasId; type FnAbi = (); fn debug_adt_id(type_kind_id: 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: 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: AssocTypeId, fmt: &mut fmt::Formatter<'_>) -> Option { tls::with_current_program(|prog| Some(prog?.debug_assoc_type_id(id, fmt))) } fn debug_alias( alias: &chalk_ir::AliasTy, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_alias(alias, fmt))) } fn debug_projection_ty( proj: &chalk_ir::ProjectionTy, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_projection_ty(proj, fmt))) } fn debug_opaque_ty( opaque_ty: &chalk_ir::OpaqueTy, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_opaque_ty(opaque_ty, fmt))) } fn debug_opaque_ty_id( opaque_ty_id: chalk_ir::OpaqueTyId, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_opaque_ty_id(opaque_ty_id, fmt))) } fn debug_ty(ty: &chalk_ir::Ty, fmt: &mut fmt::Formatter<'_>) -> Option { tls::with_current_program(|prog| Some(prog?.debug_ty(ty, fmt))) } fn debug_lifetime( lifetime: &chalk_ir::Lifetime, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_lifetime(lifetime, fmt))) } fn debug_generic_arg( parameter: &GenericArg, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_generic_arg(parameter, fmt))) } fn debug_goal(goal: &Goal, fmt: &mut fmt::Formatter<'_>) -> Option { tls::with_current_program(|prog| Some(prog?.debug_goal(goal, fmt))) } fn debug_goals( goals: &chalk_ir::Goals, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_goals(goals, fmt))) } fn debug_program_clause_implication( pci: &chalk_ir::ProgramClauseImplication, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_program_clause_implication(pci, fmt))) } fn debug_substitution( substitution: &chalk_ir::Substitution, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_substitution(substitution, fmt))) } fn debug_separator_trait_ref( separator_trait_ref: &chalk_ir::SeparatorTraitRef, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| { Some(prog?.debug_separator_trait_ref(separator_trait_ref, fmt)) }) } fn debug_fn_def_id( fn_def_id: chalk_ir::FnDefId, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_fn_def_id(fn_def_id, fmt))) } fn debug_const( constant: &chalk_ir::Const, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_const(constant, fmt))) } fn debug_variable_kinds( variable_kinds: &chalk_ir::VariableKinds, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_variable_kinds(variable_kinds, fmt))) } fn debug_variable_kinds_with_angles( variable_kinds: &chalk_ir::VariableKinds, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| { Some(prog?.debug_variable_kinds_with_angles(variable_kinds, fmt)) }) } fn debug_canonical_var_kinds( canonical_var_kinds: &chalk_ir::CanonicalVarKinds, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| { Some(prog?.debug_canonical_var_kinds(canonical_var_kinds, fmt)) }) } fn debug_program_clause( clause: &chalk_ir::ProgramClause, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_program_clause(clause, fmt))) } fn debug_program_clauses( clauses: &chalk_ir::ProgramClauses, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_program_clauses(clauses, fmt))) } fn debug_quantified_where_clauses( clauses: &chalk_ir::QuantifiedWhereClauses, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_quantified_where_clauses(clauses, fmt))) } fn intern_ty(&self, kind: chalk_ir::TyKind) -> Self::InternedType { let flags = kind.compute_flags(self); Interned::new(InternedTypeInner(chalk_ir::TyData { kind, flags })) } fn ty_data<'a>(&self, ty: &'a Self::InternedType) -> &'a chalk_ir::TyData { &ty.0 } fn intern_lifetime(&self, lifetime: chalk_ir::LifetimeData) -> Self::InternedLifetime { Interned::new(InternedWrapper(lifetime)) } fn lifetime_data<'a>( &self, lifetime: &'a Self::InternedLifetime, ) -> &'a chalk_ir::LifetimeData { &lifetime.0 } fn intern_const(&self, constant: chalk_ir::ConstData) -> Self::InternedConst { Interned::new(InternedWrapper(constant)) } fn const_data<'a>(&self, constant: &'a Self::InternedConst) -> &'a chalk_ir::ConstData { &constant.0 } fn const_eq( &self, _ty: &Self::InternedType, _c1: &Self::InternedConcreteConst, _c2: &Self::InternedConcreteConst, ) -> bool { true } fn intern_generic_arg( &self, parameter: chalk_ir::GenericArgData, ) -> Self::InternedGenericArg { parameter } fn generic_arg_data<'a>( &self, parameter: &'a Self::InternedGenericArg, ) -> &'a chalk_ir::GenericArgData { parameter } fn intern_goal(&self, goal: GoalData) -> Self::InternedGoal { Arc::new(goal) } fn intern_goals( &self, data: impl IntoIterator, E>>, ) -> Result { data.into_iter().collect() } fn goal_data<'a>(&self, goal: &'a Self::InternedGoal) -> &'a GoalData { goal } fn goals_data<'a>(&self, goals: &'a Self::InternedGoals) -> &'a [Goal] { goals } fn intern_substitution( &self, data: impl IntoIterator>, ) -> Result { Ok(Interned::new(InternedSubstitutionInner(data.into_iter().collect::, _>>()?))) } fn substitution_data<'a>( &self, substitution: &'a Self::InternedSubstitution, ) -> &'a [GenericArg] { &substitution.as_ref().0 } fn intern_program_clause( &self, data: chalk_ir::ProgramClauseData, ) -> Self::InternedProgramClause { Arc::new(data) } fn program_clause_data<'a>( &self, clause: &'a Self::InternedProgramClause, ) -> &'a chalk_ir::ProgramClauseData { clause } fn intern_program_clauses( &self, data: impl IntoIterator, E>>, ) -> Result { data.into_iter().collect() } fn program_clauses_data<'a>( &self, clauses: &'a Self::InternedProgramClauses, ) -> &'a [chalk_ir::ProgramClause] { &clauses } fn intern_quantified_where_clauses( &self, data: impl IntoIterator, E>>, ) -> Result { data.into_iter().collect() } fn quantified_where_clauses_data<'a>( &self, clauses: &'a Self::InternedQuantifiedWhereClauses, ) -> &'a [chalk_ir::QuantifiedWhereClause] { clauses } fn intern_generic_arg_kinds( &self, data: impl IntoIterator, E>>, ) -> Result { Ok(Interned::new(InternedVariableKindsInner( data.into_iter().collect::, E>>()?, ))) } fn variable_kinds_data<'a>( &self, parameter_kinds: &'a Self::InternedVariableKinds, ) -> &'a [chalk_ir::VariableKind] { ¶meter_kinds.as_ref().0 } fn intern_canonical_var_kinds( &self, data: impl IntoIterator, E>>, ) -> Result { Ok(Interned::new(InternedWrapper(data.into_iter().collect::>()?))) } fn canonical_var_kinds_data<'a>( &self, canonical_var_kinds: &'a Self::InternedCanonicalVarKinds, ) -> &'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 } 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 } fn intern_variances( &self, data: impl IntoIterator>, ) -> Result { data.into_iter().collect() } fn variances_data<'a>( &self, variances: &'a Self::InternedVariances, ) -> &'a [chalk_ir::Variance] { &variances } } impl chalk_ir::interner::HasInterner for Interner { type Interner = Self; }