Skip to content

Commit af6e10f

Browse files
Uplift InferConst to rustc_type_ir
1 parent 1d9159b commit af6e10f

File tree

8 files changed

+77
-84
lines changed

8 files changed

+77
-84
lines changed

compiler/rustc_middle/src/ty/consts/kind.rs

-26
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::mir;
33
use crate::ty::abstract_const::CastKind;
44
use crate::ty::GenericArgsRef;
55
use crate::ty::{self, visit::TypeVisitableExt as _, List, Ty, TyCtxt};
6-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
76
use rustc_hir::def_id::DefId;
87
use rustc_macros::HashStable;
98

@@ -77,28 +76,3 @@ static_assert_size!(Expr<'_>, 24);
7776

7877
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
7978
static_assert_size!(super::ConstKind<'_>, 32);
80-
81-
/// An inference variable for a const, for use in const generics.
82-
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Hash)]
83-
pub enum InferConst {
84-
/// Infer the value of the const.
85-
Var(ty::ConstVid),
86-
/// Infer the value of the effect.
87-
///
88-
/// For why this is separate from the `Var` variant above, see the
89-
/// documentation on `EffectVid`.
90-
EffectVar(ty::EffectVid),
91-
/// A fresh const variable. See `infer::freshen` for more details.
92-
Fresh(u32),
93-
}
94-
95-
impl<CTX> HashStable<CTX> for InferConst {
96-
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
97-
match self {
98-
InferConst::Var(_) | InferConst::EffectVar(_) => {
99-
panic!("const variables should not be hashed: {self:?}")
100-
}
101-
InferConst::Fresh(i) => i.hash_stable(hcx, hasher),
102-
}
103-
}
104-
}

compiler/rustc_middle/src/ty/context.rs

-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
102102
type AllocId = crate::mir::interpret::AllocId;
103103

104104
type Const = ty::Const<'tcx>;
105-
type InferConst = ty::InferConst;
106105
type AliasConst = ty::UnevaluatedConst<'tcx>;
107106
type PlaceholderConst = ty::PlaceholderConst;
108107
type ParamConst = ty::ParamConst;

compiler/rustc_middle/src/ty/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ pub use self::closure::{
8484
CapturedPlace, ClosureKind, ClosureTypeInfo, MinCaptureInformationMap, MinCaptureList,
8585
RootVariableMinCaptureList, UpvarCapture, UpvarId, UpvarPath, CAPTURE_STRUCT_LOCAL,
8686
};
87-
pub use self::consts::{
88-
Const, ConstData, ConstInt, Expr, InferConst, ScalarInt, UnevaluatedConst, ValTree,
89-
};
87+
pub use self::consts::{Const, ConstData, ConstInt, Expr, ScalarInt, UnevaluatedConst, ValTree};
9088
pub use self::context::{
9189
tls, CtxtInterners, DeducedParamAttrs, FreeRegionInfo, GlobalCtxt, Lift, TyCtxt, TyCtxtFeed,
9290
};
@@ -98,7 +96,7 @@ pub use self::sty::BoundRegionKind::*;
9896
pub use self::sty::{
9997
AliasTy, Article, Binder, BoundRegion, BoundRegionKind, BoundTy, BoundTyKind, BoundVar,
10098
BoundVariableKind, CanonicalPolyFnSig, ClauseKind, ClosureArgs, ClosureArgsParts, ConstKind,
101-
ConstVid, CoroutineArgs, CoroutineArgsParts, EarlyParamRegion, EffectVid, ExistentialPredicate,
99+
CoroutineArgs, CoroutineArgsParts, EarlyParamRegion, ExistentialPredicate,
102100
ExistentialProjection, ExistentialTraitRef, FnSig, GenSig, InlineConstArgs,
103101
InlineConstArgsParts, LateParamRegion, ParamConst, ParamTy, PolyExistentialPredicate,
104102
PolyExistentialProjection, PolyExistentialTraitRef, PolyFnSig, PolyGenSig, PolyTraitRef,

compiler/rustc_middle/src/ty/structural_impls.rs

-28
Original file line numberDiff line numberDiff line change
@@ -202,34 +202,6 @@ impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for AliasTy<'tcx> {
202202
}
203203
}
204204

205-
impl fmt::Debug for ty::InferConst {
206-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
207-
match self {
208-
InferConst::Var(var) => write!(f, "{var:?}"),
209-
InferConst::EffectVar(var) => write!(f, "{var:?}"),
210-
InferConst::Fresh(var) => write!(f, "Fresh({var:?})"),
211-
}
212-
}
213-
}
214-
impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for ty::InferConst {
215-
fn fmt<Infcx: InferCtxtLike<Interner = TyCtxt<'tcx>>>(
216-
this: WithInfcx<'_, Infcx, &Self>,
217-
f: &mut core::fmt::Formatter<'_>,
218-
) -> core::fmt::Result {
219-
use ty::InferConst::*;
220-
match this.infcx.universe_of_ct(*this.data) {
221-
None => write!(f, "{:?}", this.data),
222-
Some(universe) => match *this.data {
223-
Var(vid) => write!(f, "?{}_{}c", vid.index(), universe.index()),
224-
EffectVar(vid) => write!(f, "?{}_{}e", vid.index(), universe.index()),
225-
Fresh(_) => {
226-
unreachable!()
227-
}
228-
},
229-
}
230-
}
231-
}
232-
233205
impl<'tcx> fmt::Debug for ty::consts::Expr<'tcx> {
234206
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
235207
WithInfcx::with_no_infcx(self).fmt(f)

compiler/rustc_middle/src/ty/sty.rs

-18
Original file line numberDiff line numberDiff line change
@@ -1608,24 +1608,6 @@ impl fmt::Debug for EarlyParamRegion {
16081608
}
16091609
}
16101610

1611-
rustc_index::newtype_index! {
1612-
/// A **`const`** **v**ariable **ID**.
1613-
#[debug_format = "?{}c"]
1614-
pub struct ConstVid {}
1615-
}
1616-
1617-
rustc_index::newtype_index! {
1618-
/// An **effect** **v**ariable **ID**.
1619-
///
1620-
/// Handling effect infer variables happens separately from const infer variables
1621-
/// because we do not want to reuse any of the const infer machinery. If we try to
1622-
/// relate an effect variable with a normal one, we would ICE, which can catch bugs
1623-
/// where we are not correctly using the effect var for an effect param. Fallback
1624-
/// is also implemented on top of having separate effect and normal const variables.
1625-
#[debug_format = "?{}e"]
1626-
pub struct EffectVid {}
1627-
}
1628-
16291611
rustc_index::newtype_index! {
16301612
/// A **region** (lifetime) **v**ariable **ID**.
16311613
#[derive(HashStable)]

compiler/rustc_type_ir/src/const_kind.rs

+72-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub enum ConstKind<I: Interner> {
2222
Param(I::ParamConst),
2323

2424
/// Infer the value of the const.
25-
Infer(I::InferConst),
25+
Infer(InferConst),
2626

2727
/// Bound const variable, used only when preparing a trait query.
2828
Bound(DebruijnIndex, I::BoundConst),
@@ -63,7 +63,6 @@ const fn const_kind_discriminant<I: Interner>(value: &ConstKind<I>) -> usize {
6363
impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for ConstKind<I>
6464
where
6565
I::ParamConst: HashStable<CTX>,
66-
I::InferConst: HashStable<CTX>,
6766
I::BoundConst: HashStable<CTX>,
6867
I::PlaceholderConst: HashStable<CTX>,
6968
I::AliasConst: HashStable<CTX>,
@@ -134,3 +133,74 @@ impl<I: Interner> DebugWithInfcx<I> for ConstKind<I> {
134133
}
135134
}
136135
}
136+
137+
rustc_index::newtype_index! {
138+
/// A **`const`** **v**ariable **ID**.
139+
#[debug_format = "?{}c"]
140+
pub struct ConstVid {}
141+
}
142+
143+
rustc_index::newtype_index! {
144+
/// An **effect** **v**ariable **ID**.
145+
///
146+
/// Handling effect infer variables happens separately from const infer variables
147+
/// because we do not want to reuse any of the const infer machinery. If we try to
148+
/// relate an effect variable with a normal one, we would ICE, which can catch bugs
149+
/// where we are not correctly using the effect var for an effect param. Fallback
150+
/// is also implemented on top of having separate effect and normal const variables.
151+
#[debug_format = "?{}e"]
152+
pub struct EffectVid {}
153+
}
154+
155+
/// An inference variable for a const, for use in const generics.
156+
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
157+
#[derive(TyEncodable, TyDecodable)]
158+
pub enum InferConst {
159+
/// Infer the value of the const.
160+
Var(ConstVid),
161+
/// Infer the value of the effect.
162+
///
163+
/// For why this is separate from the `Var` variant above, see the
164+
/// documentation on `EffectVid`.
165+
EffectVar(EffectVid),
166+
/// A fresh const variable. See `infer::freshen` for more details.
167+
Fresh(u32),
168+
}
169+
170+
impl fmt::Debug for InferConst {
171+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172+
match self {
173+
InferConst::Var(var) => write!(f, "{var:?}"),
174+
InferConst::EffectVar(var) => write!(f, "{var:?}"),
175+
InferConst::Fresh(var) => write!(f, "Fresh({var:?})"),
176+
}
177+
}
178+
}
179+
impl<I: Interner> DebugWithInfcx<I> for InferConst {
180+
fn fmt<Infcx: InferCtxtLike<Interner = I>>(
181+
this: WithInfcx<'_, Infcx, &Self>,
182+
f: &mut core::fmt::Formatter<'_>,
183+
) -> core::fmt::Result {
184+
match this.infcx.universe_of_ct(*this.data) {
185+
None => write!(f, "{:?}", this.data),
186+
Some(universe) => match *this.data {
187+
InferConst::Var(vid) => write!(f, "?{}_{}c", vid.index(), universe.index()),
188+
InferConst::EffectVar(vid) => write!(f, "?{}_{}e", vid.index(), universe.index()),
189+
InferConst::Fresh(_) => {
190+
unreachable!()
191+
}
192+
},
193+
}
194+
}
195+
}
196+
197+
impl<CTX> HashStable<CTX> for InferConst {
198+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
199+
match self {
200+
InferConst::Var(_) | InferConst::EffectVar(_) => {
201+
panic!("const variables should not be hashed: {self:?}")
202+
}
203+
InferConst::Fresh(i) => i.hash_stable(hcx, hasher),
204+
}
205+
}
206+
}

compiler/rustc_type_ir/src/debug.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{InferTy, Interner, UniverseIndex};
1+
use crate::{InferConst, InferTy, Interner, UniverseIndex};
22

33
use core::fmt;
44
use std::marker::PhantomData;
@@ -13,8 +13,7 @@ pub trait InferCtxtLike {
1313
lt: <Self::Interner as Interner>::InferRegion,
1414
) -> Option<UniverseIndex>;
1515

16-
fn universe_of_ct(&self, ct: <Self::Interner as Interner>::InferConst)
17-
-> Option<UniverseIndex>;
16+
fn universe_of_ct(&self, ct: InferConst) -> Option<UniverseIndex>;
1817
}
1918

2019
pub struct NoInfcx<I>(PhantomData<I>);
@@ -26,7 +25,7 @@ impl<I: Interner> InferCtxtLike for NoInfcx<I> {
2625
None
2726
}
2827

29-
fn universe_of_ct(&self, _ct: <I as Interner>::InferConst) -> Option<UniverseIndex> {
28+
fn universe_of_ct(&self, _ct: InferConst) -> Option<UniverseIndex> {
3029
None
3130
}
3231

compiler/rustc_type_ir/src/interner.rs

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ pub trait Interner: Sized {
3636

3737
// Kinds of consts
3838
type Const: Clone + DebugWithInfcx<Self> + Hash + Ord;
39-
type InferConst: Clone + DebugWithInfcx<Self> + Hash + Ord;
4039
type AliasConst: Clone + DebugWithInfcx<Self> + Hash + Ord;
4140
type PlaceholderConst: Clone + Debug + Hash + Ord;
4241
type ParamConst: Clone + Debug + Hash + Ord;

0 commit comments

Comments
 (0)