rust/crates/hir-ty/src/interner.rs

394 lines
13 KiB
Rust
Raw Normal View History

2020-05-22 10:14:53 -05:00
//! Implementation of the Chalk `Interner` trait, which allows customizing the
//! representation of the various objects Chalk deals with (types, goals etc.).
2024-01-06 09:58:15 -06:00
use crate::{
chalk_db, tls, AliasTy, CanonicalVarKind, CanonicalVarKinds, ClosureId, Const, ConstData,
2024-01-17 11:23:14 -06:00
ConstScalar, Constraint, Constraints, FnAbi, FnDefId, GenericArg, GenericArgData, Goal,
GoalData, Goals, InEnvironment, Lifetime, LifetimeData, OpaqueTy, OpaqueTyId, ProgramClause,
2024-01-06 09:58:15 -06:00
ProgramClauseData, ProgramClauses, ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses,
Substitution, Ty, TyData, TyKind, VariableKind, VariableKinds,
};
2020-08-13 09:25:38 -05:00
use base_db::salsa::InternId;
2024-01-06 09:58:15 -06:00
use chalk_ir::{ProgramClauseImplication, SeparatorTraitRef, Variance};
2023-02-03 05:16:25 -06:00
use hir_def::TypeAliasId;
use intern::{impl_internable, Interned};
use smallvec::SmallVec;
2023-05-02 09:12:22 -05:00
use std::fmt;
use triomphe::Arc;
2020-05-22 10:14:53 -05:00
#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)]
pub struct Interner;
2021-05-09 13:05:43 -05:00
#[derive(PartialEq, Eq, Hash)]
pub struct InternedWrapper<T>(T);
2021-05-09 13:05:43 -05:00
impl<T: fmt::Debug> fmt::Debug for InternedWrapper<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}
impl<T> std::ops::Deref for InternedWrapper<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl_internable!(
2024-01-06 09:58:15 -06:00
InternedWrapper<Vec<VariableKind>>,
2021-04-08 14:15:01 -05:00
InternedWrapper<SmallVec<[GenericArg; 2]>>,
2024-01-06 09:58:15 -06:00
InternedWrapper<TyData>,
InternedWrapper<LifetimeData>,
InternedWrapper<ConstData>,
InternedWrapper<ConstScalar>,
2024-01-06 09:58:15 -06:00
InternedWrapper<Vec<CanonicalVarKind>>,
InternedWrapper<Vec<ProgramClause>>,
InternedWrapper<Vec<QuantifiedWhereClause>>,
InternedWrapper<SmallVec<[Variance; 16]>>,
);
2021-04-08 11:13:02 -05:00
2020-05-22 10:14:53 -05:00
impl chalk_ir::interner::Interner for Interner {
2024-01-06 09:58:15 -06:00
type InternedType = Interned<InternedWrapper<TyData>>;
type InternedLifetime = Interned<InternedWrapper<LifetimeData>>;
type InternedConst = Interned<InternedWrapper<ConstData>>;
type InternedConcreteConst = ConstScalar;
2024-01-06 09:58:15 -06:00
type InternedGenericArg = GenericArgData;
// We could do the following, but that saves "only" 20mb on self while increasing inferecene
// time by ~2.5%
// type InternedGoal = Interned<InternedWrapper<GoalData>>;
type InternedGoal = Arc<GoalData>;
type InternedGoals = Vec<Goal>;
2021-04-08 14:15:01 -05:00
type InternedSubstitution = Interned<InternedWrapper<SmallVec<[GenericArg; 2]>>>;
2024-01-06 09:58:15 -06:00
type InternedProgramClauses = Interned<InternedWrapper<Vec<ProgramClause>>>;
type InternedProgramClause = ProgramClauseData;
type InternedQuantifiedWhereClauses = Interned<InternedWrapper<Vec<QuantifiedWhereClause>>>;
type InternedVariableKinds = Interned<InternedWrapper<Vec<VariableKind>>>;
type InternedCanonicalVarKinds = Interned<InternedWrapper<Vec<CanonicalVarKind>>>;
type InternedConstraints = Vec<InEnvironment<Constraint>>;
type InternedVariances = SmallVec<[Variance; 16]>;
2020-05-22 10:14:53 -05:00
type DefId = InternId;
type InternedAdtId = hir_def::AdtId;
2020-05-22 10:14:53 -05:00
type Identifier = TypeAliasId;
2024-01-17 11:23:14 -06:00
type FnAbi = FnAbi;
2020-05-22 10:14:53 -05:00
2021-04-09 07:15:26 -05:00
fn debug_adt_id(
type_kind_id: chalk_db::AdtId,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
2020-05-22 10:14:53 -05:00
tls::with_current_program(|prog| Some(prog?.debug_struct_id(type_kind_id, fmt)))
}
2021-04-09 07:15:26 -05:00
fn debug_trait_id(
type_kind_id: chalk_db::TraitId,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
2020-05-22 10:14:53 -05:00
tls::with_current_program(|prog| Some(prog?.debug_trait_id(type_kind_id, fmt)))
}
2021-04-09 07:15:26 -05:00
fn debug_assoc_type_id(
id: chalk_db::AssocTypeId,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
2020-05-22 10:14:53 -05:00
tls::with_current_program(|prog| Some(prog?.debug_assoc_type_id(id, fmt)))
}
2023-03-28 09:22:12 -05:00
fn debug_opaque_ty_id(
2024-01-06 09:58:15 -06:00
opaque_ty_id: OpaqueTyId,
2023-03-28 09:22:12 -05:00
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "OpaqueTy#{}", opaque_ty_id.0))
}
2024-01-06 09:58:15 -06:00
fn debug_fn_def_id(fn_def_id: FnDefId, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
2023-03-28 09:22:12 -05:00
tls::with_current_program(|prog| Some(prog?.debug_fn_def_id(fn_def_id, fmt)))
}
fn debug_closure_id(
2024-01-06 09:58:15 -06:00
_fn_def_id: ClosureId,
2023-03-28 09:22:12 -05:00
_fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
None
}
2024-01-06 09:58:15 -06:00
fn debug_alias(alias: &AliasTy, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
use std::fmt::Debug;
match alias {
2024-01-06 09:58:15 -06:00
AliasTy::Projection(projection_ty) => Interner::debug_projection_ty(projection_ty, fmt),
AliasTy::Opaque(opaque_ty) => Some(opaque_ty.fmt(fmt)),
}
2020-05-22 10:14:53 -05:00
}
fn debug_projection_ty(
2024-01-06 09:58:15 -06:00
proj: &ProjectionTy,
2020-05-22 10:14:53 -05:00
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
tls::with_current_program(|prog| Some(prog?.debug_projection_ty(proj, fmt)))
}
2024-01-06 09:58:15 -06:00
fn debug_opaque_ty(opaque_ty: &OpaqueTy, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
2021-05-15 15:26:55 -05:00
Some(write!(fmt, "{:?}", opaque_ty.opaque_ty_id))
2020-05-22 10:14:53 -05:00
}
2024-01-06 09:58:15 -06:00
fn debug_ty(ty: &Ty, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
2021-12-19 10:58:39 -06:00
Some(write!(fmt, "{:?}", ty.data(Interner)))
2020-05-22 10:14:53 -05:00
}
2024-01-06 09:58:15 -06:00
fn debug_lifetime(lifetime: &Lifetime, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
2021-12-19 10:58:39 -06:00
Some(write!(fmt, "{:?}", lifetime.data(Interner)))
2020-05-22 10:14:53 -05:00
}
2024-01-06 09:58:15 -06:00
fn debug_const(constant: &Const, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
2023-03-28 09:22:12 -05:00
Some(write!(fmt, "{:?}", constant.data(Interner)))
2020-05-22 10:14:53 -05:00
}
2023-03-28 09:22:12 -05:00
fn debug_generic_arg(
parameter: &GenericArg,
2020-05-22 10:14:53 -05:00
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
2023-03-28 09:22:12 -05:00
Some(write!(fmt, "{:?}", parameter.data(Interner).inner_debug()))
2020-05-22 10:14:53 -05:00
}
2020-05-22 11:05:48 -05:00
fn debug_variable_kinds(
2024-01-06 09:58:15 -06:00
variable_kinds: &VariableKinds,
2020-05-22 11:05:48 -05:00
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
2021-12-19 10:58:39 -06:00
Some(write!(fmt, "{:?}", variable_kinds.as_slice(Interner)))
2020-05-22 11:05:48 -05:00
}
2023-03-28 09:22:12 -05:00
2020-05-22 11:05:48 -05:00
fn debug_variable_kinds_with_angles(
2024-01-06 09:58:15 -06:00
variable_kinds: &VariableKinds,
2020-05-22 11:05:48 -05:00
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
2021-12-19 10:58:39 -06:00
Some(write!(fmt, "{:?}", variable_kinds.inner_debug(Interner)))
2020-05-22 11:05:48 -05:00
}
2023-03-28 09:22:12 -05:00
2020-05-22 11:05:48 -05:00
fn debug_canonical_var_kinds(
2024-01-06 09:58:15 -06:00
canonical_var_kinds: &CanonicalVarKinds,
2020-05-22 11:05:48 -05:00
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
2021-12-19 10:58:39 -06:00
Some(write!(fmt, "{:?}", canonical_var_kinds.as_slice(Interner)))
2020-05-22 11:05:48 -05:00
}
2024-01-06 09:58:15 -06:00
fn debug_goal(goal: &Goal, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
2023-03-28 09:22:12 -05:00
let goal_data = goal.data(Interner);
Some(write!(fmt, "{goal_data:?}"))
}
2024-01-06 09:58:15 -06:00
fn debug_goals(goals: &Goals, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
2023-03-28 09:22:12 -05:00
Some(write!(fmt, "{:?}", goals.debug(Interner)))
}
fn debug_program_clause_implication(
2024-01-06 09:58:15 -06:00
pci: &ProgramClauseImplication<Self>,
2023-03-28 09:22:12 -05:00
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", pci.debug(Interner)))
}
2020-05-22 11:05:48 -05:00
fn debug_program_clause(
2024-01-06 09:58:15 -06:00
clause: &ProgramClause,
2020-05-22 11:05:48 -05:00
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
2021-12-19 10:58:39 -06:00
Some(write!(fmt, "{:?}", clause.data(Interner)))
2020-05-22 11:05:48 -05:00
}
fn debug_program_clauses(
2024-01-06 09:58:15 -06:00
clauses: &ProgramClauses,
2020-05-22 11:05:48 -05:00
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
2021-12-19 10:58:39 -06:00
Some(write!(fmt, "{:?}", clauses.as_slice(Interner)))
2020-05-22 11:05:48 -05:00
}
2023-03-28 09:22:12 -05:00
fn debug_substitution(
2024-01-06 09:58:15 -06:00
substitution: &Substitution,
2023-03-28 09:22:12 -05:00
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", substitution.debug(Interner)))
}
fn debug_separator_trait_ref(
2024-01-06 09:58:15 -06:00
separator_trait_ref: &SeparatorTraitRef<'_, Interner>,
2023-03-28 09:22:12 -05:00
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", separator_trait_ref.debug(Interner)))
}
2020-05-22 11:05:48 -05:00
fn debug_quantified_where_clauses(
2024-01-06 09:58:15 -06:00
clauses: &QuantifiedWhereClauses,
2020-05-22 11:05:48 -05:00
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
2021-12-19 10:58:39 -06:00
Some(write!(fmt, "{:?}", clauses.as_slice(Interner)))
2020-05-22 11:05:48 -05:00
}
2023-03-28 09:22:12 -05:00
fn debug_constraints(
2024-01-06 09:58:15 -06:00
_clauses: &Constraints,
2023-03-28 09:22:12 -05:00
_fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
None
}
2024-01-06 09:58:15 -06:00
fn intern_ty(self, kind: TyKind) -> Self::InternedType {
let flags = kind.compute_flags(self);
2024-01-06 09:58:15 -06:00
Interned::new(InternedWrapper(TyData { kind, flags }))
2020-05-22 10:14:53 -05:00
}
2024-01-06 09:58:15 -06:00
fn ty_data(self, ty: &Self::InternedType) -> &TyData {
&ty.0
2020-05-22 10:14:53 -05:00
}
2024-01-06 09:58:15 -06:00
fn intern_lifetime(self, lifetime: LifetimeData) -> Self::InternedLifetime {
Interned::new(InternedWrapper(lifetime))
2020-05-22 10:14:53 -05:00
}
2024-01-06 09:58:15 -06:00
fn lifetime_data(self, lifetime: &Self::InternedLifetime) -> &LifetimeData {
&lifetime.0
2020-05-22 10:14:53 -05:00
}
2024-01-06 09:58:15 -06:00
fn intern_const(self, constant: ConstData) -> Self::InternedConst {
Interned::new(InternedWrapper(constant))
2020-05-22 10:14:53 -05:00
}
2024-01-06 09:58:15 -06:00
fn const_data(self, constant: &Self::InternedConst) -> &ConstData {
&constant.0
2020-05-22 10:14:53 -05:00
}
fn const_eq(
2021-12-19 10:58:39 -06:00
self,
_ty: &Self::InternedType,
c1: &Self::InternedConcreteConst,
c2: &Self::InternedConcreteConst,
) -> bool {
2023-06-05 06:27:19 -05:00
!matches!(c1, ConstScalar::Bytes(..)) || !matches!(c2, ConstScalar::Bytes(..)) || (c1 == c2)
2020-05-22 10:14:53 -05:00
}
2024-01-06 09:58:15 -06:00
fn intern_generic_arg(self, parameter: GenericArgData) -> Self::InternedGenericArg {
2020-05-22 10:14:53 -05:00
parameter
}
2024-01-06 09:58:15 -06:00
fn generic_arg_data(self, parameter: &Self::InternedGenericArg) -> &GenericArgData {
2020-05-22 10:14:53 -05:00
parameter
}
2024-01-06 09:58:15 -06:00
fn intern_goal(self, goal: GoalData) -> Self::InternedGoal {
2020-05-22 10:14:53 -05:00
Arc::new(goal)
}
2024-01-06 09:58:15 -06:00
fn goal_data(self, goal: &Self::InternedGoal) -> &GoalData {
2023-03-28 09:22:12 -05:00
goal
}
2020-05-22 10:14:53 -05:00
fn intern_goals<E>(
2021-12-19 10:58:39 -06:00
self,
2024-01-06 09:58:15 -06:00
data: impl IntoIterator<Item = Result<Goal, E>>,
2020-05-22 10:14:53 -05:00
) -> Result<Self::InternedGoals, E> {
2024-01-06 09:58:15 -06:00
// let hash =
// std::hash::BuildHasher::hash_one(&BuildHasherDefault::<FxHasher>::default(), &goal);
// Interned::new(InternedWrapper(PreHashedWrapper(goal, hash)))
2020-05-22 10:14:53 -05:00
data.into_iter().collect()
}
2024-01-06 09:58:15 -06:00
fn goals_data(self, goals: &Self::InternedGoals) -> &[Goal] {
2020-05-22 10:14:53 -05:00
goals
}
fn intern_substitution<E>(
2021-12-19 10:58:39 -06:00
self,
data: impl IntoIterator<Item = Result<GenericArg, E>>,
) -> Result<Self::InternedSubstitution, E> {
2021-04-08 14:15:01 -05:00
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
2020-05-22 10:14:53 -05:00
}
fn substitution_data(self, substitution: &Self::InternedSubstitution) -> &[GenericArg] {
&substitution.as_ref().0
2020-05-22 10:14:53 -05:00
}
2024-01-06 09:58:15 -06:00
fn intern_program_clause(self, data: ProgramClauseData) -> Self::InternedProgramClause {
data
2020-05-22 10:14:53 -05:00
}
2024-01-06 09:58:15 -06:00
fn program_clause_data(self, clause: &Self::InternedProgramClause) -> &ProgramClauseData {
2020-05-22 10:14:53 -05:00
clause
}
fn intern_program_clauses<E>(
2021-12-19 10:58:39 -06:00
self,
2024-01-06 09:58:15 -06:00
data: impl IntoIterator<Item = Result<ProgramClause, E>>,
) -> Result<Self::InternedProgramClauses, E> {
2021-04-08 13:13:21 -05:00
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
2020-05-22 10:14:53 -05:00
}
2024-01-06 09:58:15 -06:00
fn program_clauses_data(self, clauses: &Self::InternedProgramClauses) -> &[ProgramClause] {
2021-06-12 22:54:16 -05:00
clauses
2020-05-22 10:14:53 -05:00
}
fn intern_quantified_where_clauses<E>(
2021-12-19 10:58:39 -06:00
self,
2024-01-06 09:58:15 -06:00
data: impl IntoIterator<Item = Result<QuantifiedWhereClause, E>>,
2020-05-22 10:14:53 -05:00
) -> Result<Self::InternedQuantifiedWhereClauses, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
2020-05-22 10:14:53 -05:00
}
fn quantified_where_clauses_data(
2021-12-19 10:58:39 -06:00
self,
clauses: &Self::InternedQuantifiedWhereClauses,
2024-01-06 09:58:15 -06:00
) -> &[QuantifiedWhereClause] {
2020-05-22 10:14:53 -05:00
clauses
}
fn intern_generic_arg_kinds<E>(
2021-12-19 10:58:39 -06:00
self,
2024-01-06 09:58:15 -06:00
data: impl IntoIterator<Item = Result<VariableKind, E>>,
2020-05-22 10:14:53 -05:00
) -> Result<Self::InternedVariableKinds, E> {
2021-04-08 14:15:01 -05:00
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
2020-05-22 10:14:53 -05:00
}
2024-01-06 09:58:15 -06:00
fn variable_kinds_data(self, parameter_kinds: &Self::InternedVariableKinds) -> &[VariableKind] {
2021-04-08 11:13:02 -05:00
&parameter_kinds.as_ref().0
2020-05-22 10:14:53 -05:00
}
fn intern_canonical_var_kinds<E>(
2021-12-19 10:58:39 -06:00
self,
2024-01-06 09:58:15 -06:00
data: impl IntoIterator<Item = Result<CanonicalVarKind, E>>,
2020-05-22 10:14:53 -05:00
) -> Result<Self::InternedCanonicalVarKinds, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
2020-05-22 10:14:53 -05:00
}
fn canonical_var_kinds_data(
2021-12-19 10:58:39 -06:00
self,
canonical_var_kinds: &Self::InternedCanonicalVarKinds,
2024-01-06 09:58:15 -06:00
) -> &[CanonicalVarKind] {
2021-06-12 22:54:16 -05:00
canonical_var_kinds
2020-05-22 10:14:53 -05:00
}
2020-07-11 08:22:46 -05:00
fn intern_constraints<E>(
2021-12-19 10:58:39 -06:00
self,
2024-01-06 09:58:15 -06:00
data: impl IntoIterator<Item = Result<InEnvironment<Constraint>, E>>,
2020-07-11 08:22:46 -05:00
) -> Result<Self::InternedConstraints, E> {
data.into_iter().collect()
}
fn constraints_data(
2021-12-19 10:58:39 -06:00
self,
constraints: &Self::InternedConstraints,
2024-01-06 09:58:15 -06:00
) -> &[InEnvironment<Constraint>] {
2020-07-11 08:22:46 -05:00
constraints
}
fn intern_variances<E>(
2021-12-19 10:58:39 -06:00
self,
2024-01-06 09:58:15 -06:00
data: impl IntoIterator<Item = Result<Variance, E>>,
) -> Result<Self::InternedVariances, E> {
2024-01-06 09:58:15 -06:00
data.into_iter().collect::<Result<_, _>>()
}
2024-01-06 09:58:15 -06:00
fn variances_data(self, variances: &Self::InternedVariances) -> &[Variance] {
2021-06-12 22:54:16 -05:00
variances
}
2020-05-22 10:14:53 -05:00
}
impl chalk_ir::interner::HasInterner for Interner {
type Interner = Self;
}
2021-04-09 07:33:03 -05:00
#[macro_export]
macro_rules! has_interner {
($t:ty) => {
impl HasInterner for $t {
2024-01-18 06:59:49 -06:00
type Interner = $crate::Interner;
2021-04-09 07:33:03 -05:00
}
};
}