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

422 lines
14 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.).
2023-02-03 05:16:25 -06:00
use crate::{chalk_db, tls, ConstScalar, GenericArg};
2020-08-13 09:25:38 -05:00
use base_db::salsa::InternId;
use chalk_ir::{Goal, GoalData};
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!(
2021-04-08 14:15:01 -05:00
InternedWrapper<Vec<chalk_ir::VariableKind<Interner>>>,
InternedWrapper<SmallVec<[GenericArg; 2]>>,
InternedWrapper<chalk_ir::TyData<Interner>>,
InternedWrapper<chalk_ir::LifetimeData<Interner>>,
InternedWrapper<chalk_ir::ConstData<Interner>>,
InternedWrapper<ConstScalar>,
InternedWrapper<Vec<chalk_ir::CanonicalVarKind<Interner>>>,
2021-04-08 13:13:21 -05:00
InternedWrapper<Vec<chalk_ir::ProgramClause<Interner>>>,
InternedWrapper<Vec<chalk_ir::QuantifiedWhereClause<Interner>>>,
InternedWrapper<Vec<chalk_ir::Variance>>,
);
2021-04-08 11:13:02 -05:00
2020-05-22 10:14:53 -05:00
impl chalk_ir::interner::Interner for Interner {
type InternedType = Interned<InternedWrapper<chalk_ir::TyData<Self>>>;
type InternedLifetime = Interned<InternedWrapper<chalk_ir::LifetimeData<Self>>>;
type InternedConst = Interned<InternedWrapper<chalk_ir::ConstData<Self>>>;
type InternedConcreteConst = ConstScalar;
2020-05-22 10:14:53 -05:00
type InternedGenericArg = chalk_ir::GenericArgData<Self>;
type InternedGoal = Arc<GoalData<Self>>;
type InternedGoals = Vec<Goal<Self>>;
2021-04-08 14:15:01 -05:00
type InternedSubstitution = Interned<InternedWrapper<SmallVec<[GenericArg; 2]>>>;
2021-04-08 13:13:21 -05:00
type InternedProgramClauses = Interned<InternedWrapper<Vec<chalk_ir::ProgramClause<Self>>>>;
2023-03-28 09:22:12 -05:00
type InternedProgramClause = chalk_ir::ProgramClauseData<Self>;
2021-04-08 14:15:01 -05:00
type InternedQuantifiedWhereClauses =
Interned<InternedWrapper<Vec<chalk_ir::QuantifiedWhereClause<Self>>>>;
type InternedVariableKinds = Interned<InternedWrapper<Vec<chalk_ir::VariableKind<Interner>>>>;
type InternedCanonicalVarKinds =
Interned<InternedWrapper<Vec<chalk_ir::CanonicalVarKind<Self>>>>;
2020-07-11 08:22:46 -05:00
type InternedConstraints = Vec<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>>;
type InternedVariances = Interned<InternedWrapper<Vec<chalk_ir::Variance>>>;
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;
2020-06-22 06:18:10 -05:00
type 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(
opaque_ty_id: chalk_ir::OpaqueTyId<Self>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "OpaqueTy#{}", opaque_ty_id.0))
}
fn debug_fn_def_id(
fn_def_id: chalk_ir::FnDefId<Self>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
tls::with_current_program(|prog| Some(prog?.debug_fn_def_id(fn_def_id, fmt)))
}
fn debug_closure_id(
_fn_def_id: chalk_ir::ClosureId<Self>,
_fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
None
}
2020-05-22 10:14:53 -05:00
fn debug_alias(
alias: &chalk_ir::AliasTy<Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
use std::fmt::Debug;
match alias {
chalk_ir::AliasTy::Projection(projection_ty) => {
Interner::debug_projection_ty(projection_ty, fmt)
}
chalk_ir::AliasTy::Opaque(opaque_ty) => Some(opaque_ty.fmt(fmt)),
}
2020-05-22 10:14:53 -05:00
}
fn debug_projection_ty(
proj: &chalk_ir::ProjectionTy<Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
tls::with_current_program(|prog| Some(prog?.debug_projection_ty(proj, fmt)))
}
fn debug_opaque_ty(
opaque_ty: &chalk_ir::OpaqueTy<Interner>,
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
}
fn debug_ty(ty: &chalk_ir::Ty<Interner>, 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
}
fn debug_lifetime(
lifetime: &chalk_ir::Lifetime<Interner>,
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
}
2023-03-28 09:22:12 -05:00
fn debug_const(
constant: &chalk_ir::Const<Self>,
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, "{:?}", 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(
variable_kinds: &chalk_ir::VariableKinds<Self>,
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(
variable_kinds: &chalk_ir::VariableKinds<Self>,
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(
canonical_var_kinds: &chalk_ir::CanonicalVarKinds<Self>,
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
}
2023-03-28 09:22:12 -05:00
fn debug_goal(goal: &Goal<Interner>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
let goal_data = goal.data(Interner);
Some(write!(fmt, "{goal_data:?}"))
}
fn debug_goals(
goals: &chalk_ir::Goals<Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", goals.debug(Interner)))
}
fn debug_program_clause_implication(
pci: &chalk_ir::ProgramClauseImplication<Interner>,
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(
clause: &chalk_ir::ProgramClause<Self>,
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(
clauses: &chalk_ir::ProgramClauses<Self>,
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(
substitution: &chalk_ir::Substitution<Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", substitution.debug(Interner)))
}
fn debug_separator_trait_ref(
separator_trait_ref: &chalk_ir::SeparatorTraitRef<'_, Interner>,
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(
clauses: &chalk_ir::QuantifiedWhereClauses<Self>,
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(
_clauses: &chalk_ir::Constraints<Self>,
_fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
None
}
2021-12-19 10:58:39 -06:00
fn intern_ty(self, kind: chalk_ir::TyKind<Self>) -> Self::InternedType {
let flags = kind.compute_flags(self);
2021-04-08 14:15:01 -05:00
Interned::new(InternedWrapper(chalk_ir::TyData { kind, flags }))
2020-05-22 10:14:53 -05:00
}
fn ty_data(self, ty: &Self::InternedType) -> &chalk_ir::TyData<Self> {
&ty.0
2020-05-22 10:14:53 -05:00
}
2021-12-19 10:58:39 -06:00
fn intern_lifetime(self, lifetime: chalk_ir::LifetimeData<Self>) -> Self::InternedLifetime {
Interned::new(InternedWrapper(lifetime))
2020-05-22 10:14:53 -05:00
}
fn lifetime_data(self, lifetime: &Self::InternedLifetime) -> &chalk_ir::LifetimeData<Self> {
&lifetime.0
2020-05-22 10:14:53 -05:00
}
2021-12-19 10:58:39 -06:00
fn intern_const(self, constant: chalk_ir::ConstData<Self>) -> Self::InternedConst {
Interned::new(InternedWrapper(constant))
2020-05-22 10:14:53 -05:00
}
fn const_data(self, constant: &Self::InternedConst) -> &chalk_ir::ConstData<Self> {
&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 {
2022-07-17 10:22:11 -05:00
(c1 == &ConstScalar::Unknown) || (c2 == &ConstScalar::Unknown) || (c1 == c2)
2020-05-22 10:14:53 -05:00
}
fn intern_generic_arg(
2021-12-19 10:58:39 -06:00
self,
2020-05-22 10:14:53 -05:00
parameter: chalk_ir::GenericArgData<Self>,
) -> Self::InternedGenericArg {
2020-05-22 10:14:53 -05:00
parameter
}
fn generic_arg_data(
2021-12-19 10:58:39 -06:00
self,
parameter: &Self::InternedGenericArg,
) -> &chalk_ir::GenericArgData<Self> {
2020-05-22 10:14:53 -05:00
parameter
}
2021-12-19 10:58:39 -06:00
fn intern_goal(self, goal: GoalData<Self>) -> Self::InternedGoal {
2020-05-22 10:14:53 -05:00
Arc::new(goal)
}
2023-03-28 09:22:12 -05:00
fn goal_data(self, goal: &Self::InternedGoal) -> &GoalData<Self> {
goal
}
2020-05-22 10:14:53 -05:00
fn intern_goals<E>(
2021-12-19 10:58:39 -06:00
self,
2020-05-22 10:14:53 -05:00
data: impl IntoIterator<Item = Result<Goal<Self>, E>>,
) -> Result<Self::InternedGoals, E> {
data.into_iter().collect()
}
fn goals_data(self, goals: &Self::InternedGoals) -> &[Goal<Interner>] {
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
}
fn intern_program_clause(
2021-12-19 10:58:39 -06:00
self,
2020-05-22 10:14:53 -05:00
data: chalk_ir::ProgramClauseData<Self>,
) -> Self::InternedProgramClause {
data
2020-05-22 10:14:53 -05:00
}
fn program_clause_data(
2021-12-19 10:58:39 -06:00
self,
clause: &Self::InternedProgramClause,
) -> &chalk_ir::ProgramClauseData<Self> {
2020-05-22 10:14:53 -05:00
clause
}
fn intern_program_clauses<E>(
2021-12-19 10:58:39 -06:00
self,
2020-05-22 10:14:53 -05:00
data: impl IntoIterator<Item = Result<chalk_ir::ProgramClause<Self>, 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
}
fn program_clauses_data(
2021-12-19 10:58:39 -06:00
self,
clauses: &Self::InternedProgramClauses,
) -> &[chalk_ir::ProgramClause<Self>] {
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,
2020-05-22 10:14:53 -05:00
data: impl IntoIterator<Item = Result<chalk_ir::QuantifiedWhereClause<Self>, E>>,
) -> 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,
) -> &[chalk_ir::QuantifiedWhereClause<Self>] {
2020-05-22 10:14:53 -05:00
clauses
}
fn intern_generic_arg_kinds<E>(
2021-12-19 10:58:39 -06:00
self,
2020-05-22 10:14:53 -05:00
data: impl IntoIterator<Item = Result<chalk_ir::VariableKind<Self>, E>>,
) -> 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
}
fn variable_kinds_data(
2021-12-19 10:58:39 -06:00
self,
parameter_kinds: &Self::InternedVariableKinds,
) -> &[chalk_ir::VariableKind<Self>] {
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,
2020-05-22 10:14:53 -05:00
data: impl IntoIterator<Item = Result<chalk_ir::CanonicalVarKind<Self>, E>>,
) -> 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,
) -> &[chalk_ir::CanonicalVarKind<Self>] {
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,
2020-07-11 08:22:46 -05:00
data: impl IntoIterator<Item = Result<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>, E>>,
) -> Result<Self::InternedConstraints, E> {
data.into_iter().collect()
}
fn constraints_data(
2021-12-19 10:58:39 -06:00
self,
constraints: &Self::InternedConstraints,
) -> &[chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>] {
2020-07-11 08:22:46 -05:00
constraints
}
fn intern_variances<E>(
2021-12-19 10:58:39 -06:00
self,
data: impl IntoIterator<Item = Result<chalk_ir::Variance, E>>,
) -> Result<Self::InternedVariances, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
}
fn variances_data(self, variances: &Self::InternedVariances) -> &[chalk_ir::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 {
type Interner = crate::Interner;
}
};
}