Skip to content

Commit de3bf35

Browse files
committed
make TyVid implement the Idx trait
1 parent a4f357d commit de3bf35

File tree

6 files changed

+20
-19
lines changed

6 files changed

+20
-19
lines changed

compiler/rustc_infer/src/infer/fudge.rs

Lines changed: 1 addition & 1 deletion
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();
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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,15 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
198198
/// Note that this function does not return care whether
199199
/// `vid` has been unified with something else or not.
200200
pub fn var_diverges(&self, vid: ty::TyVid) -> Diverging {
201-
self.storage.values.get(vid.index as usize).diverging
201+
self.storage.values.get(vid.as_usize()).diverging
202202
}
203203

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

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

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

276276
debug!(
277277
"new_var(index={:?}, universe={:?}, diverging={:?}, origin={:?}",
@@ -359,11 +359,11 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
359359
&mut self,
360360
value_count: usize,
361361
) -> (Range<TyVid>, Vec<TypeVariableOrigin>) {
362-
let range = TyVid { index: value_count as u32 }..TyVid { index: self.num_vars() as u32 };
362+
let range = TyVid::from(value_count)..TyVid::from(self.num_vars());
363363
(
364364
range.start..range.end,
365-
(range.start.index..range.end.index)
366-
.map(|index| self.storage.values.get(index as usize).origin)
365+
(range.start.as_usize()..range.end.as_usize())
366+
.map(|index| self.storage.values.get(index).origin)
367367
.collect(),
368368
)
369369
}
@@ -395,7 +395,7 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
395395
super::UndoLog::TypeVariables(UndoLog::Values(sv::UndoLog::Other(
396396
Instantiate { vid, .. },
397397
))) => {
398-
if vid.index < new_elem_threshold {
398+
if vid.as_u32() < new_elem_threshold {
399399
// quick check to see if this variable was
400400
// created since the snapshot started or not.
401401
let mut eq_relations = ut::UnificationTable::with_log(
@@ -423,7 +423,7 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
423423
pub fn unsolved_variables(&mut self) -> Vec<ty::TyVid> {
424424
(0..self.storage.values.len())
425425
.filter_map(|i| {
426-
let vid = ty::TyVid { index: i as u32 };
426+
let vid = ty::TyVid::from(i);
427427
match self.probe(vid) {
428428
TypeVariableValue::Unknown { .. } => Some(vid),
429429
TypeVariableValue::Known { .. } => None,
@@ -475,10 +475,10 @@ impl<'tcx> From<ty::TyVid> for TyVidEqKey<'tcx> {
475475
impl<'tcx> ut::UnifyKey for TyVidEqKey<'tcx> {
476476
type Value = TypeVariableValue<'tcx>;
477477
fn index(&self) -> u32 {
478-
self.vid.index
478+
self.vid.as_u32()
479479
}
480480
fn from_index(i: u32) -> Self {
481-
TyVidEqKey::from(ty::TyVid { index: i })
481+
TyVidEqKey::from(ty::TyVid::from(i))
482482
}
483483
fn tag() -> &'static str {
484484
"TyVidEqKey"

compiler/rustc_middle/src/infer/unify_key.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ pub trait ToType {
2020
impl UnifyKey for ty::TyVid {
2121
type Value = ();
2222
fn index(&self) -> u32 {
23-
self.index
23+
u32::from(*self)
2424
}
2525
fn from_index(i: u32) -> ty::TyVid {
26-
ty::TyVid { index: i }
26+
ty::TyVid::from(i)
2727
}
2828
fn tag() -> &'static str {
2929
"TyVid"

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl fmt::Debug for ty::FnSig<'tcx> {
130130

131131
impl fmt::Debug for ty::TyVid {
132132
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133-
write!(f, "_#{}t", self.index)
133+
write!(f, "_#{}t", u32::from(*self))
134134
}
135135
}
136136

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,9 +1489,10 @@ pub struct EarlyBoundRegion {
14891489
pub name: Symbol,
14901490
}
14911491

1492-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)]
1493-
pub struct TyVid {
1494-
pub index: u32,
1492+
rustc_index::newtype_index! {
1493+
pub struct TyVid {
1494+
DEBUG_FORMAT = custom,
1495+
}
14951496
}
14961497

14971498
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,15 +1157,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
11571157
let sig = if let ty::Tuple(inputs) = inputs.kind() {
11581158
tcx.mk_fn_sig(
11591159
inputs.iter().map(|k| k.expect_ty()),
1160-
tcx.mk_ty_infer(ty::TyVar(ty::TyVid { index: 0 })),
1160+
tcx.mk_ty_infer(ty::TyVar(ty::TyVid::from(0_u32))),
11611161
false,
11621162
hir::Unsafety::Normal,
11631163
abi::Abi::Rust,
11641164
)
11651165
} else {
11661166
tcx.mk_fn_sig(
11671167
std::iter::once(inputs),
1168-
tcx.mk_ty_infer(ty::TyVar(ty::TyVid { index: 0 })),
1168+
tcx.mk_ty_infer(ty::TyVar(ty::TyVid::from(0_u32))),
11691169
false,
11701170
hir::Unsafety::Normal,
11711171
abi::Abi::Rust,

0 commit comments

Comments
 (0)