Make connection between Placeholder and Bound a bit more clear in the type abstraction

This commit is contained in:
Michael Goulet 2025-06-12 22:38:31 +00:00
parent 86497e6376
commit fe92efaf31
4 changed files with 42 additions and 23 deletions

View file

@ -933,7 +933,9 @@ impl Placeholder<BoundVar> {
pub type PlaceholderRegion = Placeholder<BoundRegion>;
impl rustc_type_ir::inherent::PlaceholderLike for PlaceholderRegion {
impl<'tcx> rustc_type_ir::inherent::PlaceholderLike<TyCtxt<'tcx>> for PlaceholderRegion {
type Bound = BoundRegion;
fn universe(self) -> UniverseIndex {
self.universe
}
@ -946,14 +948,20 @@ impl rustc_type_ir::inherent::PlaceholderLike for PlaceholderRegion {
Placeholder { universe: ui, ..self }
}
fn new(ui: UniverseIndex, var: BoundVar) -> Self {
fn new(ui: UniverseIndex, bound: BoundRegion) -> Self {
Placeholder { universe: ui, bound }
}
fn new_anon(ui: UniverseIndex, var: BoundVar) -> Self {
Placeholder { universe: ui, bound: BoundRegion { var, kind: BoundRegionKind::Anon } }
}
}
pub type PlaceholderType = Placeholder<BoundTy>;
impl rustc_type_ir::inherent::PlaceholderLike for PlaceholderType {
impl<'tcx> rustc_type_ir::inherent::PlaceholderLike<TyCtxt<'tcx>> for PlaceholderType {
type Bound = BoundTy;
fn universe(self) -> UniverseIndex {
self.universe
}
@ -966,7 +974,11 @@ impl rustc_type_ir::inherent::PlaceholderLike for PlaceholderType {
Placeholder { universe: ui, ..self }
}
fn new(ui: UniverseIndex, var: BoundVar) -> Self {
fn new(ui: UniverseIndex, bound: BoundTy) -> Self {
Placeholder { universe: ui, bound }
}
fn new_anon(ui: UniverseIndex, var: BoundVar) -> Self {
Placeholder { universe: ui, bound: BoundTy { var, kind: BoundTyKind::Anon } }
}
}
@ -980,7 +992,9 @@ pub struct BoundConst<'tcx> {
pub type PlaceholderConst = Placeholder<BoundVar>;
impl rustc_type_ir::inherent::PlaceholderLike for PlaceholderConst {
impl<'tcx> rustc_type_ir::inherent::PlaceholderLike<TyCtxt<'tcx>> for PlaceholderConst {
type Bound = BoundVar;
fn universe(self) -> UniverseIndex {
self.universe
}
@ -993,7 +1007,11 @@ impl rustc_type_ir::inherent::PlaceholderLike for PlaceholderConst {
Placeholder { universe: ui, ..self }
}
fn new(ui: UniverseIndex, var: BoundVar) -> Self {
fn new(ui: UniverseIndex, bound: BoundVar) -> Self {
Placeholder { universe: ui, bound }
}
fn new_anon(ui: UniverseIndex, var: BoundVar) -> Self {
Placeholder { universe: ui, bound: var }
}
}

View file

@ -435,13 +435,13 @@ impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
},
ty::Placeholder(placeholder) => match self.canonicalize_mode {
CanonicalizeMode::Input { .. } => CanonicalVarKind::PlaceholderTy(
PlaceholderLike::new(placeholder.universe(), self.variables.len().into()),
PlaceholderLike::new_anon(placeholder.universe(), self.variables.len().into()),
),
CanonicalizeMode::Response { .. } => CanonicalVarKind::PlaceholderTy(placeholder),
},
ty::Param(_) => match self.canonicalize_mode {
CanonicalizeMode::Input { .. } => CanonicalVarKind::PlaceholderTy(
PlaceholderLike::new(ty::UniverseIndex::ROOT, self.variables.len().into()),
PlaceholderLike::new_anon(ty::UniverseIndex::ROOT, self.variables.len().into()),
),
CanonicalizeMode::Response { .. } => panic!("param ty in response: {t:?}"),
},
@ -594,7 +594,7 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
},
ty::ConstKind::Placeholder(placeholder) => match self.canonicalize_mode {
CanonicalizeMode::Input { .. } => CanonicalVarKind::PlaceholderConst(
PlaceholderLike::new(placeholder.universe(), self.variables.len().into()),
PlaceholderLike::new_anon(placeholder.universe(), self.variables.len().into()),
),
CanonicalizeMode::Response { .. } => {
CanonicalVarKind::PlaceholderConst(placeholder)
@ -602,7 +602,7 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
},
ty::ConstKind::Param(_) => match self.canonicalize_mode {
CanonicalizeMode::Input { .. } => CanonicalVarKind::PlaceholderConst(
PlaceholderLike::new(ty::UniverseIndex::ROOT, self.variables.len().into()),
PlaceholderLike::new_anon(ty::UniverseIndex::ROOT, self.variables.len().into()),
),
CanonicalizeMode::Response { .. } => panic!("param ty in response: {c:?}"),
},

View file

@ -524,13 +524,14 @@ pub trait Clauses<I: Interner<Clauses = Self>>:
}
/// Common capabilities of placeholder kinds
pub trait PlaceholderLike: Copy + Debug + Hash + Eq {
pub trait PlaceholderLike<I: Interner>: Copy + Debug + Hash + Eq {
fn universe(self) -> ty::UniverseIndex;
fn var(self) -> ty::BoundVar;
type Bound: BoundVarLike<I>;
fn new(ui: ty::UniverseIndex, bound: Self::Bound) -> Self;
fn new_anon(ui: ty::UniverseIndex, var: ty::BoundVar) -> Self;
fn with_updated_universe(self, ui: ty::UniverseIndex) -> Self;
fn new(ui: ty::UniverseIndex, var: ty::BoundVar) -> Self;
}
pub trait IntoKind {
@ -539,13 +540,13 @@ pub trait IntoKind {
fn kind(self) -> Self::Kind;
}
pub trait BoundVarLike<I: Interner> {
pub trait BoundVarLike<I: Interner>: Copy + Debug + Hash + Eq {
fn var(self) -> ty::BoundVar;
fn assert_eq(self, var: I::BoundVarKind);
}
pub trait ParamLike {
pub trait ParamLike: Copy + Debug + Hash + Eq {
fn index(self) -> u32;
}

View file

@ -103,9 +103,9 @@ pub trait Interner:
type Ty: Ty<Self>;
type Tys: Tys<Self>;
type FnInputTys: Copy + Debug + Hash + Eq + SliceLike<Item = Self::Ty> + TypeVisitable<Self>;
type ParamTy: Copy + Debug + Hash + Eq + ParamLike;
type BoundTy: Copy + Debug + Hash + Eq + BoundVarLike<Self>;
type PlaceholderTy: PlaceholderLike;
type ParamTy: ParamLike;
type BoundTy: BoundVarLike<Self>;
type PlaceholderTy: PlaceholderLike<Self, Bound = Self::BoundTy>;
// Things stored inside of tys
type ErrorGuaranteed: Copy + Debug + Hash + Eq;
@ -131,19 +131,19 @@ pub trait Interner:
// Kinds of consts
type Const: Const<Self>;
type PlaceholderConst: PlaceholderLike;
type ParamConst: Copy + Debug + Hash + Eq + ParamLike;
type BoundConst: Copy + Debug + Hash + Eq + BoundVarLike<Self>;
type BoundConst: BoundVarLike<Self>;
type PlaceholderConst: PlaceholderLike<Self, Bound = Self::BoundConst>;
type ValueConst: ValueConst<Self>;
type ExprConst: ExprConst<Self>;
type ValTree: Copy + Debug + Hash + Eq;
// Kinds of regions
type Region: Region<Self>;
type EarlyParamRegion: Copy + Debug + Hash + Eq + ParamLike;
type EarlyParamRegion: ParamLike;
type LateParamRegion: Copy + Debug + Hash + Eq;
type BoundRegion: Copy + Debug + Hash + Eq + BoundVarLike<Self>;
type PlaceholderRegion: PlaceholderLike;
type BoundRegion: BoundVarLike<Self>;
type PlaceholderRegion: PlaceholderLike<Self, Bound = Self::BoundRegion>;
// Predicates
type ParamEnv: ParamEnv<Self>;