Skip to content

Commit 69c4aa2

Browse files
committed
Auto merge of rust-lang#88710 - Mark-Simulacrum:tyvid-idx, r=jackh726
Use index newtyping for TyVid This is useful for using TyVid in types like VecGraph, and just otherwise seems like a small win.
2 parents 73641cd + 2eac09d commit 69c4aa2

File tree

5 files changed

+21
-26
lines changed

5 files changed

+21
-26
lines changed

compiler/rustc_infer/src/infer/fudge.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for InferenceFudger<'a, 'tcx> {
187187
if self.type_vars.0.contains(&vid) {
188188
// This variable was created during the fudging.
189189
// Recreate it with a fresh variable here.
190-
let idx = (vid.index - self.type_vars.0.start.index) as usize;
190+
let idx = (vid.as_usize() - self.type_vars.0.start.as_usize()) as usize;
191191
let origin = self.type_vars.1[idx];
192192
self.infcx.next_ty_var(origin)
193193
} else {

compiler/rustc_infer/src/infer/type_variable.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,15 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
196196
/// Note that this function does not return care whether
197197
/// `vid` has been unified with something else or not.
198198
pub fn var_diverges(&self, vid: ty::TyVid) -> Diverging {
199-
self.storage.values.get(vid.index as usize).diverging
199+
self.storage.values.get(vid.index()).diverging
200200
}
201201

202202
/// Returns the origin that was given when `vid` was created.
203203
///
204204
/// Note that this function does not return care whether
205205
/// `vid` has been unified with something else or not.
206206
pub fn var_origin(&self, vid: ty::TyVid) -> &TypeVariableOrigin {
207-
&self.storage.values.get(vid.index as usize).origin
207+
&self.storage.values.get(vid.as_usize()).origin
208208
}
209209

210210
/// Records that `a == b`, depending on `dir`.
@@ -269,7 +269,7 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
269269
assert_eq!(eq_key.vid, sub_key);
270270

271271
let index = self.values().push(TypeVariableData { origin, diverging });
272-
assert_eq!(eq_key.vid.index, index as u32);
272+
assert_eq!(eq_key.vid.as_u32(), index as u32);
273273

274274
debug!(
275275
"new_var(index={:?}, universe={:?}, diverging={:?}, origin={:?}",
@@ -357,11 +357,11 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
357357
&mut self,
358358
value_count: usize,
359359
) -> (Range<TyVid>, Vec<TypeVariableOrigin>) {
360-
let range = TyVid { index: value_count as u32 }..TyVid { index: self.num_vars() as u32 };
360+
let range = TyVid::from_usize(value_count)..TyVid::from_usize(self.num_vars());
361361
(
362362
range.start..range.end,
363-
(range.start.index..range.end.index)
364-
.map(|index| self.storage.values.get(index as usize).origin)
363+
(range.start.as_usize()..range.end.as_usize())
364+
.map(|index| self.storage.values.get(index).origin)
365365
.collect(),
366366
)
367367
}
@@ -371,7 +371,7 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
371371
pub fn unsolved_variables(&mut self) -> Vec<ty::TyVid> {
372372
(0..self.storage.values.len())
373373
.filter_map(|i| {
374-
let vid = ty::TyVid { index: i as u32 };
374+
let vid = ty::TyVid::from_usize(i);
375375
match self.probe(vid) {
376376
TypeVariableValue::Unknown { .. } => Some(vid),
377377
TypeVariableValue::Known { .. } => None,
@@ -424,10 +424,10 @@ impl<'tcx> ut::UnifyKey for TyVidEqKey<'tcx> {
424424
type Value = TypeVariableValue<'tcx>;
425425
#[inline(always)]
426426
fn index(&self) -> u32 {
427-
self.vid.index
427+
self.vid.as_u32()
428428
}
429429
fn from_index(i: u32) -> Self {
430-
TyVidEqKey::from(ty::TyVid { index: i })
430+
TyVidEqKey::from(ty::TyVid::from_u32(i))
431431
}
432432
fn tag() -> &'static str {
433433
"TyVidEqKey"

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1244,15 +1244,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
12441244
let sig = if let ty::Tuple(inputs) = inputs.kind() {
12451245
tcx.mk_fn_sig(
12461246
inputs.iter().map(|k| k.expect_ty()),
1247-
tcx.mk_ty_infer(ty::TyVar(ty::TyVid { index: 0 })),
1247+
tcx.mk_ty_infer(ty::TyVar(ty::TyVid::from_u32(0))),
12481248
false,
12491249
hir::Unsafety::Normal,
12501250
abi::Abi::Rust,
12511251
)
12521252
} else {
12531253
tcx.mk_fn_sig(
12541254
std::iter::once(inputs),
1255-
tcx.mk_ty_infer(ty::TyVar(ty::TyVid { index: 0 })),
1255+
tcx.mk_ty_infer(ty::TyVar(ty::TyVid::from_u32(0))),
12561256
false,
12571257
hir::Unsafety::Normal,
12581258
abi::Abi::Rust,

compiler/rustc_type_ir/src/lib.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,11 @@ pub enum IntVarValue {
363363
#[derive(Clone, Copy, PartialEq, Eq)]
364364
pub struct FloatVarValue(pub FloatTy);
365365

366-
/// A **ty**pe **v**ariable **ID**.
367-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
368-
pub struct TyVid {
369-
pub index: u32,
366+
rustc_index::newtype_index! {
367+
/// A **ty**pe **v**ariable **ID**.
368+
pub struct TyVid {
369+
DEBUG_FORMAT = "_#{}t"
370+
}
370371
}
371372

372373
/// An **int**egral (`u32`, `i32`, `usize`, etc.) type **v**ariable **ID**.
@@ -422,10 +423,10 @@ pub enum InferTy {
422423
impl UnifyKey for TyVid {
423424
type Value = ();
424425
fn index(&self) -> u32 {
425-
self.index
426+
self.as_u32()
426427
}
427428
fn from_index(i: u32) -> TyVid {
428-
TyVid { index: i }
429+
TyVid::from_u32(i)
429430
}
430431
fn tag() -> &'static str {
431432
"TyVid"
@@ -558,7 +559,7 @@ impl<CTX> HashStable<CTX> for InferTy {
558559
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
559560
use InferTy::*;
560561
match self {
561-
TyVar(v) => v.index.hash_stable(ctx, hasher),
562+
TyVar(v) => v.as_u32().hash_stable(ctx, hasher),
562563
IntVar(v) => v.index.hash_stable(ctx, hasher),
563564
FloatVar(v) => v.index.hash_stable(ctx, hasher),
564565
FreshTy(v) | FreshIntTy(v) | FreshFloatTy(v) => v.hash_stable(ctx, hasher),
@@ -587,12 +588,6 @@ impl fmt::Debug for FloatVarValue {
587588
}
588589
}
589590

590-
impl fmt::Debug for TyVid {
591-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
592-
write!(f, "_#{}t", self.index)
593-
}
594-
}
595-
596591
impl fmt::Debug for IntVid {
597592
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
598593
write!(f, "_#{}i", self.index)

compiler/rustc_typeck/src/check/method/suggest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
742742
let projection_ty = pred.skip_binder().projection_ty;
743743

744744
let substs_with_infer_self = tcx.mk_substs(
745-
iter::once(tcx.mk_ty_var(ty::TyVid { index: 0 }).into())
745+
iter::once(tcx.mk_ty_var(ty::TyVid::from_u32(0)).into())
746746
.chain(projection_ty.substs.iter().skip(1)),
747747
);
748748

0 commit comments

Comments
 (0)