Skip to content

Commit 2d78ee4

Browse files
authored
Unrolled build for rust-lang#117851
Rollup merge of rust-lang#117851 - compiler-errors:uplift-infer-const, r=spastorino Uplift `InferConst` to `rustc_type_ir` We need this in `rustc_type_ir` because the canonicalizer must understand the difference between a const vid and an effect vid. In that way, it's not an implementation detail of the representation of an infer const, but just part of the type ir. If we find out later on that it's better to leave the representation up to the consumer of `rustc_type_ir`, we could abstract `InferConst` (and probably `InferTy` as well) with some traits, but I don't see the benefit of that indirection currently.
2 parents 8534923 + b4c3d7f commit 2d78ee4

File tree

9 files changed

+87
-94
lines changed

9 files changed

+87
-94
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

+3-5
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ use crate::traits::solve::{
2626
};
2727
use crate::ty::{
2828
self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Const, ConstData, GenericParamDefKind,
29-
ImplPolarity, InferTy, List, ParamConst, ParamTy, PolyExistentialPredicate, PolyFnSig,
30-
Predicate, PredicateKind, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind,
31-
TyVid, TypeAndMut, Visibility,
29+
ImplPolarity, List, ParamConst, ParamTy, PolyExistentialPredicate, PolyFnSig, Predicate,
30+
PredicateKind, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVid,
31+
TypeAndMut, Visibility,
3232
};
3333
use crate::ty::{GenericArg, GenericArgs, GenericArgsRef};
3434
use rustc_ast::{self as ast, attr};
@@ -95,15 +95,13 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
9595
type ParamTy = ParamTy;
9696
type BoundTy = ty::BoundTy;
9797
type PlaceholderTy = ty::PlaceholderType;
98-
type InferTy = InferTy;
9998

10099
type ErrorGuaranteed = ErrorGuaranteed;
101100
type BoundExistentialPredicates = &'tcx List<PolyExistentialPredicate<'tcx>>;
102101
type PolyFnSig = PolyFnSig<'tcx>;
103102
type AllocId = crate::mir::interpret::AllocId;
104103

105104
type Const = ty::Const<'tcx>;
106-
type InferConst = ty::InferConst;
107105
type AliasConst = ty::UnevaluatedConst<'tcx>;
108106
type PlaceholderConst = ty::PlaceholderConst;
109107
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

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

compiler/rustc_type_ir/src/debug.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
1-
use crate::{Interner, UniverseIndex};
1+
use crate::{InferConst, InferTy, Interner, UniverseIndex};
22

33
use core::fmt;
44
use std::marker::PhantomData;
55

66
pub trait InferCtxtLike {
77
type Interner: Interner;
88

9-
fn universe_of_ty(&self, ty: <Self::Interner as Interner>::InferTy) -> Option<UniverseIndex>;
9+
fn universe_of_ty(&self, ty: InferTy) -> Option<UniverseIndex>;
1010

1111
fn universe_of_lt(
1212
&self,
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>);
2120

2221
impl<I: Interner> InferCtxtLike for NoInfcx<I> {
2322
type Interner = I;
2423

25-
fn universe_of_ty(&self, _ty: <I as Interner>::InferTy) -> Option<UniverseIndex> {
24+
fn universe_of_ty(&self, _ty: InferTy) -> Option<UniverseIndex> {
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

-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ pub trait Interner: Sized {
2727
type ParamTy: Clone + Debug + Hash + Ord;
2828
type BoundTy: Clone + Debug + Hash + Ord;
2929
type PlaceholderTy: Clone + Debug + Hash + Ord;
30-
type InferTy: Clone + DebugWithInfcx<Self> + Hash + Ord;
3130

3231
// Things stored inside of tys
3332
type ErrorGuaranteed: Clone + Debug + Hash + Ord;
@@ -37,7 +36,6 @@ pub trait Interner: Sized {
3736

3837
// Kinds of consts
3938
type Const: Clone + DebugWithInfcx<Self> + Hash + Ord;
40-
type InferConst: Clone + DebugWithInfcx<Self> + Hash + Ord;
4139
type AliasConst: Clone + DebugWithInfcx<Self> + Hash + Ord;
4240
type PlaceholderConst: Clone + Debug + Hash + Ord;
4341
type ParamConst: Clone + Debug + Hash + Ord;

compiler/rustc_type_ir/src/ty_kind.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ pub enum TyKind<I: Interner> {
281281
/// correctly deal with higher ranked types. Though unlike placeholders,
282282
/// that universe is stored in the `InferCtxt` instead of directly
283283
/// inside of the type.
284-
Infer(I::InferTy),
284+
Infer(InferTy),
285285

286286
/// A placeholder for a type which could not be computed; this is
287287
/// propagated to avoid useless error messages.
@@ -491,7 +491,6 @@ where
491491
I::BoundTy: HashStable<CTX>,
492492
I::ParamTy: HashStable<CTX>,
493493
I::PlaceholderTy: HashStable<CTX>,
494-
I::InferTy: HashStable<CTX>,
495494
I::ErrorGuaranteed: HashStable<CTX>,
496495
{
497496
#[inline]
@@ -922,7 +921,7 @@ impl fmt::Debug for InferTy {
922921
}
923922
}
924923

925-
impl<I: Interner<InferTy = InferTy>> DebugWithInfcx<I> for InferTy {
924+
impl<I: Interner> DebugWithInfcx<I> for InferTy {
926925
fn fmt<Infcx: InferCtxtLike<Interner = I>>(
927926
this: WithInfcx<'_, Infcx, &Self>,
928927
f: &mut fmt::Formatter<'_>,

0 commit comments

Comments
 (0)