More aliases

This commit is contained in:
Lukas Wirth 2024-01-06 16:58:15 +01:00
parent 963568b46f
commit c9c4053eed
2 changed files with 130 additions and 115 deletions

View File

@ -1,9 +1,15 @@
//! Implementation of the Chalk `Interner` trait, which allows customizing the
//! representation of the various objects Chalk deals with (types, goals etc.).
use crate::{chalk_db, tls, ConstScalar, GenericArg};
use crate::{
chalk_db, tls, AliasTy, CanonicalVarKind, CanonicalVarKinds, ClosureId, Const, ConstData,
ConstScalar, Constraint, Constraints, FnDefId, GenericArg, GenericArgData, Goal, GoalData,
Goals, InEnvironment, Lifetime, LifetimeData, OpaqueTy, OpaqueTyId, ProgramClause,
ProgramClauseData, ProgramClauses, ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses,
Substitution, Ty, TyData, TyKind, VariableKind, VariableKinds,
};
use base_db::salsa::InternId;
use chalk_ir::{Goal, GoalData};
use chalk_ir::{ProgramClauseImplication, SeparatorTraitRef, Variance};
use hir_def::TypeAliasId;
use intern::{impl_internable, Interned};
use smallvec::SmallVec;
@ -30,37 +36,70 @@ fn deref(&self) -> &Self::Target {
}
}
#[derive(Eq)]
pub struct PreHashedWrapper<T>(T, u64);
impl<T: fmt::Debug> fmt::Debug for PreHashedWrapper<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}
impl<T: PartialEq> PartialEq for PreHashedWrapper<T> {
fn eq(&self, other: &Self) -> bool {
self.1 == other.1 && self.0 == other.0
}
}
impl<T> std::ops::Deref for PreHashedWrapper<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: std::hash::Hash> std::hash::Hash for PreHashedWrapper<T> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
state.write_u64(self.1);
}
}
impl_internable!(
InternedWrapper<Vec<chalk_ir::VariableKind<Interner>>>,
InternedWrapper<Vec<VariableKind>>,
InternedWrapper<SmallVec<[GenericArg; 2]>>,
InternedWrapper<chalk_ir::TyData<Interner>>,
InternedWrapper<chalk_ir::LifetimeData<Interner>>,
InternedWrapper<chalk_ir::ConstData<Interner>>,
InternedWrapper<TyData>,
InternedWrapper<LifetimeData>,
InternedWrapper<ConstData>,
InternedWrapper<ConstScalar>,
InternedWrapper<Vec<chalk_ir::CanonicalVarKind<Interner>>>,
InternedWrapper<Vec<chalk_ir::ProgramClause<Interner>>>,
InternedWrapper<Vec<chalk_ir::QuantifiedWhereClause<Interner>>>,
InternedWrapper<Vec<chalk_ir::Variance>>,
InternedWrapper<Vec<CanonicalVarKind>>,
InternedWrapper<Vec<ProgramClause>>,
InternedWrapper<Vec<QuantifiedWhereClause>>,
InternedWrapper<SmallVec<[Variance; 16]>>,
// InternedWrapper<PreHashedWrapper<Goals>>,
);
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 InternedType = Interned<InternedWrapper<TyData>>;
type InternedLifetime = Interned<InternedWrapper<LifetimeData>>;
type InternedConst = Interned<InternedWrapper<ConstData>>;
type InternedConcreteConst = ConstScalar;
type InternedGenericArg = chalk_ir::GenericArgData<Self>;
type InternedGoal = Arc<GoalData<Self>>;
type InternedGoals = Vec<Goal<Self>>;
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 = Interned<InternedWrapper<PreHashedWrapper<GoalData>>>;
type InternedGoal = Arc<GoalData>;
type InternedGoals = Vec<Goal>;
type InternedSubstitution = Interned<InternedWrapper<SmallVec<[GenericArg; 2]>>>;
type InternedProgramClauses = Interned<InternedWrapper<Vec<chalk_ir::ProgramClause<Self>>>>;
type InternedProgramClause = chalk_ir::ProgramClauseData<Self>;
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>>>>;
type InternedConstraints = Vec<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>>;
type InternedVariances = Interned<InternedWrapper<Vec<chalk_ir::Variance>>>;
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 = SmallVec<[InEnvironment<Constraint>; 1]>;
type InternedConstraints = Vec<InEnvironment<Constraint>>;
type InternedVariances = SmallVec<[Variance; 16]>;
type DefId = InternId;
type InternedAdtId = hir_def::AdtId;
type Identifier = TypeAliasId;
@ -88,68 +127,51 @@ fn debug_assoc_type_id(
}
fn debug_opaque_ty_id(
opaque_ty_id: chalk_ir::OpaqueTyId<Self>,
opaque_ty_id: OpaqueTyId,
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> {
fn debug_fn_def_id(fn_def_id: FnDefId, 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>,
_fn_def_id: ClosureId,
_fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
None
}
fn debug_alias(
alias: &chalk_ir::AliasTy<Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
fn debug_alias(alias: &AliasTy, 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)),
AliasTy::Projection(projection_ty) => Interner::debug_projection_ty(projection_ty, fmt),
AliasTy::Opaque(opaque_ty) => Some(opaque_ty.fmt(fmt)),
}
}
fn debug_projection_ty(
proj: &chalk_ir::ProjectionTy<Interner>,
proj: &ProjectionTy,
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> {
fn debug_opaque_ty(opaque_ty: &OpaqueTy, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", opaque_ty.opaque_ty_id))
}
fn debug_ty(ty: &chalk_ir::Ty<Interner>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
fn debug_ty(ty: &Ty, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", ty.data(Interner)))
}
fn debug_lifetime(
lifetime: &chalk_ir::Lifetime<Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
fn debug_lifetime(lifetime: &Lifetime, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", lifetime.data(Interner)))
}
fn debug_const(
constant: &chalk_ir::Const<Self>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
fn debug_const(constant: &Const, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", constant.data(Interner)))
}
@ -161,102 +183,99 @@ fn debug_generic_arg(
}
fn debug_variable_kinds(
variable_kinds: &chalk_ir::VariableKinds<Self>,
variable_kinds: &VariableKinds,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", variable_kinds.as_slice(Interner)))
}
fn debug_variable_kinds_with_angles(
variable_kinds: &chalk_ir::VariableKinds<Self>,
variable_kinds: &VariableKinds,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", variable_kinds.inner_debug(Interner)))
}
fn debug_canonical_var_kinds(
canonical_var_kinds: &chalk_ir::CanonicalVarKinds<Self>,
canonical_var_kinds: &CanonicalVarKinds,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", canonical_var_kinds.as_slice(Interner)))
}
fn debug_goal(goal: &Goal<Interner>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
fn debug_goal(goal: &Goal, 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> {
fn debug_goals(goals: &Goals, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", goals.debug(Interner)))
}
fn debug_program_clause_implication(
pci: &chalk_ir::ProgramClauseImplication<Interner>,
pci: &ProgramClauseImplication<Self>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", pci.debug(Interner)))
}
fn debug_program_clause(
clause: &chalk_ir::ProgramClause<Self>,
clause: &ProgramClause,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", clause.data(Interner)))
}
fn debug_program_clauses(
clauses: &chalk_ir::ProgramClauses<Self>,
clauses: &ProgramClauses,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", clauses.as_slice(Interner)))
}
fn debug_substitution(
substitution: &chalk_ir::Substitution<Interner>,
substitution: &Substitution,
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>,
separator_trait_ref: &SeparatorTraitRef<'_, Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", separator_trait_ref.debug(Interner)))
}
fn debug_quantified_where_clauses(
clauses: &chalk_ir::QuantifiedWhereClauses<Self>,
clauses: &QuantifiedWhereClauses,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", clauses.as_slice(Interner)))
}
fn debug_constraints(
_clauses: &chalk_ir::Constraints<Self>,
_clauses: &Constraints,
_fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
None
}
fn intern_ty(self, kind: chalk_ir::TyKind<Self>) -> Self::InternedType {
fn intern_ty(self, kind: TyKind) -> Self::InternedType {
let flags = kind.compute_flags(self);
Interned::new(InternedWrapper(chalk_ir::TyData { kind, flags }))
Interned::new(InternedWrapper(TyData { kind, flags }))
}
fn ty_data(self, ty: &Self::InternedType) -> &chalk_ir::TyData<Self> {
fn ty_data(self, ty: &Self::InternedType) -> &TyData {
&ty.0
}
fn intern_lifetime(self, lifetime: chalk_ir::LifetimeData<Self>) -> Self::InternedLifetime {
fn intern_lifetime(self, lifetime: LifetimeData) -> Self::InternedLifetime {
Interned::new(InternedWrapper(lifetime))
}
fn lifetime_data(self, lifetime: &Self::InternedLifetime) -> &chalk_ir::LifetimeData<Self> {
fn lifetime_data(self, lifetime: &Self::InternedLifetime) -> &LifetimeData {
&lifetime.0
}
fn intern_const(self, constant: chalk_ir::ConstData<Self>) -> Self::InternedConst {
fn intern_const(self, constant: ConstData) -> Self::InternedConst {
Interned::new(InternedWrapper(constant))
}
fn const_data(self, constant: &Self::InternedConst) -> &chalk_ir::ConstData<Self> {
fn const_data(self, constant: &Self::InternedConst) -> &ConstData {
&constant.0
}
@ -269,36 +288,33 @@ fn const_eq(
!matches!(c1, ConstScalar::Bytes(..)) || !matches!(c2, ConstScalar::Bytes(..)) || (c1 == c2)
}
fn intern_generic_arg(
self,
parameter: chalk_ir::GenericArgData<Self>,
) -> Self::InternedGenericArg {
fn intern_generic_arg(self, parameter: GenericArgData) -> Self::InternedGenericArg {
parameter
}
fn generic_arg_data(
self,
parameter: &Self::InternedGenericArg,
) -> &chalk_ir::GenericArgData<Self> {
fn generic_arg_data(self, parameter: &Self::InternedGenericArg) -> &GenericArgData {
parameter
}
fn intern_goal(self, goal: GoalData<Self>) -> Self::InternedGoal {
fn intern_goal(self, goal: GoalData) -> Self::InternedGoal {
Arc::new(goal)
}
fn goal_data(self, goal: &Self::InternedGoal) -> &GoalData<Self> {
fn goal_data(self, goal: &Self::InternedGoal) -> &GoalData {
goal
}
fn intern_goals<E>(
self,
data: impl IntoIterator<Item = Result<Goal<Self>, E>>,
data: impl IntoIterator<Item = Result<Goal, E>>,
) -> Result<Self::InternedGoals, E> {
// let hash =
// std::hash::BuildHasher::hash_one(&BuildHasherDefault::<FxHasher>::default(), &goal);
// Interned::new(InternedWrapper(PreHashedWrapper(goal, hash)))
data.into_iter().collect()
}
fn goals_data(self, goals: &Self::InternedGoals) -> &[Goal<Interner>] {
fn goals_data(self, goals: &Self::InternedGoals) -> &[Goal] {
goals
}
@ -313,37 +329,28 @@ fn substitution_data(self, substitution: &Self::InternedSubstitution) -> &[Gener
&substitution.as_ref().0
}
fn intern_program_clause(
self,
data: chalk_ir::ProgramClauseData<Self>,
) -> Self::InternedProgramClause {
fn intern_program_clause(self, data: ProgramClauseData) -> Self::InternedProgramClause {
data
}
fn program_clause_data(
self,
clause: &Self::InternedProgramClause,
) -> &chalk_ir::ProgramClauseData<Self> {
fn program_clause_data(self, clause: &Self::InternedProgramClause) -> &ProgramClauseData {
clause
}
fn intern_program_clauses<E>(
self,
data: impl IntoIterator<Item = Result<chalk_ir::ProgramClause<Self>, E>>,
data: impl IntoIterator<Item = Result<ProgramClause, E>>,
) -> Result<Self::InternedProgramClauses, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
}
fn program_clauses_data(
self,
clauses: &Self::InternedProgramClauses,
) -> &[chalk_ir::ProgramClause<Self>] {
fn program_clauses_data(self, clauses: &Self::InternedProgramClauses) -> &[ProgramClause] {
clauses
}
fn intern_quantified_where_clauses<E>(
self,
data: impl IntoIterator<Item = Result<chalk_ir::QuantifiedWhereClause<Self>, E>>,
data: impl IntoIterator<Item = Result<QuantifiedWhereClause, E>>,
) -> Result<Self::InternedQuantifiedWhereClauses, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
}
@ -351,27 +358,24 @@ fn intern_quantified_where_clauses<E>(
fn quantified_where_clauses_data(
self,
clauses: &Self::InternedQuantifiedWhereClauses,
) -> &[chalk_ir::QuantifiedWhereClause<Self>] {
) -> &[QuantifiedWhereClause] {
clauses
}
fn intern_generic_arg_kinds<E>(
self,
data: impl IntoIterator<Item = Result<chalk_ir::VariableKind<Self>, E>>,
data: impl IntoIterator<Item = Result<VariableKind, E>>,
) -> Result<Self::InternedVariableKinds, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
}
fn variable_kinds_data(
self,
parameter_kinds: &Self::InternedVariableKinds,
) -> &[chalk_ir::VariableKind<Self>] {
fn variable_kinds_data(self, parameter_kinds: &Self::InternedVariableKinds) -> &[VariableKind] {
&parameter_kinds.as_ref().0
}
fn intern_canonical_var_kinds<E>(
self,
data: impl IntoIterator<Item = Result<chalk_ir::CanonicalVarKind<Self>, E>>,
data: impl IntoIterator<Item = Result<CanonicalVarKind, E>>,
) -> Result<Self::InternedCanonicalVarKinds, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
}
@ -379,30 +383,30 @@ fn intern_canonical_var_kinds<E>(
fn canonical_var_kinds_data(
self,
canonical_var_kinds: &Self::InternedCanonicalVarKinds,
) -> &[chalk_ir::CanonicalVarKind<Self>] {
) -> &[CanonicalVarKind] {
canonical_var_kinds
}
fn intern_constraints<E>(
self,
data: impl IntoIterator<Item = Result<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>, E>>,
data: impl IntoIterator<Item = Result<InEnvironment<Constraint>, E>>,
) -> Result<Self::InternedConstraints, E> {
data.into_iter().collect()
}
fn constraints_data(
self,
constraints: &Self::InternedConstraints,
) -> &[chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>] {
) -> &[InEnvironment<Constraint>] {
constraints
}
fn intern_variances<E>(
self,
data: impl IntoIterator<Item = Result<chalk_ir::Variance, E>>,
data: impl IntoIterator<Item = Result<Variance, E>>,
) -> Result<Self::InternedVariances, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
data.into_iter().collect::<Result<_, _>>()
}
fn variances_data(self, variances: &Self::InternedVariances) -> &[chalk_ir::Variance] {
fn variances_data(self, variances: &Self::InternedVariances) -> &[Variance] {
variances
}
}

View File

@ -45,7 +45,7 @@ macro_rules! eprintln {
fold::{Shift, TypeFoldable},
interner::HasInterner,
visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor},
NoSolution, TyData,
NoSolution,
};
use either::Either;
use hir_def::{hir::ExprId, type_ref::Rawness, GeneralConstId, TypeOrConstParamId};
@ -152,10 +152,21 @@ macro_rules! eprintln {
pub type Goal = chalk_ir::Goal<Interner>;
pub type AliasEq = chalk_ir::AliasEq<Interner>;
pub type Solution = chalk_solve::Solution<Interner>;
pub type Constraint = chalk_ir::Constraint<Interner>;
pub type Constraints = chalk_ir::Constraints<Interner>;
pub type ConstrainedSubst = chalk_ir::ConstrainedSubst<Interner>;
pub type Guidance = chalk_solve::Guidance<Interner>;
pub type WhereClause = chalk_ir::WhereClause<Interner>;
pub type CanonicalVarKind = chalk_ir::CanonicalVarKind<Interner>;
pub type GoalData = chalk_ir::GoalData<Interner>;
pub type Goals = chalk_ir::Goals<Interner>;
pub type ProgramClauseData = chalk_ir::ProgramClauseData<Interner>;
pub type ProgramClause = chalk_ir::ProgramClause<Interner>;
pub type ProgramClauses = chalk_ir::ProgramClauses<Interner>;
pub type TyData = chalk_ir::TyData<Interner>;
pub type Variances = chalk_ir::Variances<Interner>;
/// A constant can have reference to other things. Memory map job is holding
/// the necessary bits of memory of the const eval session to keep the constant
/// meaningful.