Skip to content

Commit 2e4e2a8

Browse files
committed
Auto merge of #117139 - compiler-errors:vid-lifetimes, r=BoxyUwU
Get rid of `'tcx` lifetime on `ConstVid`, `EffectVid` These are simply newtyped numbers, so don't really have a reason (per se) to have a lifetime -- `TyVid` and `RegionVid` do not, for example. The only consequence of this is that we need to use a new key type for `UnifyKey` that mentions `'tcx`. This is already done for `RegionVid`, with `RegionVidKey<'tcx>`, but this `UnifyKey` trait implementation may have been the original reason to give `ConstVid` a lifetime. See the changes to `compiler/rustc_middle/src/infer/unify_key.rs` specifically. I consider the code cleaner this way, though -- we removed quite a few unnecessary `'tcx` in the process. This also makes it easier to uplift these two ids to `rustc_type_ir`, which I plan on doing in a follow-up PR. r? `@BoxyUwU`
2 parents 848a387 + a986ab4 commit 2e4e2a8

File tree

17 files changed

+150
-130
lines changed

17 files changed

+150
-130
lines changed

compiler/rustc_infer/src/infer/canonical/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl<'tcx> InferCtxt<'tcx> {
152152
)
153153
.into(),
154154
CanonicalVarKind::Effect => {
155-
let vid = self.inner.borrow_mut().effect_unification_table().new_key(None);
155+
let vid = self.inner.borrow_mut().effect_unification_table().new_key(None).vid;
156156
ty::Const::new_infer(self.tcx, ty::InferConst::EffectVar(vid), self.tcx.types.bool)
157157
.into()
158158
}

compiler/rustc_infer/src/infer/combine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ impl<'tcx> InferCtxt<'tcx> {
320320
#[instrument(level = "debug", skip(self))]
321321
fn unify_const_variable(
322322
&self,
323-
target_vid: ty::ConstVid<'tcx>,
323+
target_vid: ty::ConstVid,
324324
ct: ty::Const<'tcx>,
325325
param_env: ty::ParamEnv<'tcx>,
326326
) -> RelateResult<'tcx, ty::Const<'tcx>> {
@@ -381,7 +381,7 @@ impl<'tcx> InferCtxt<'tcx> {
381381
fn unify_effect_variable(
382382
&self,
383383
vid_is_expected: bool,
384-
vid: ty::EffectVid<'tcx>,
384+
vid: ty::EffectVid,
385385
val: EffectVarValue<'tcx>,
386386
) -> RelateResult<'tcx, ty::Const<'tcx>> {
387387
self.inner

compiler/rustc_infer/src/infer/freshen.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct TypeFreshener<'a, 'tcx> {
4242
ty_freshen_count: u32,
4343
const_freshen_count: u32,
4444
ty_freshen_map: FxHashMap<ty::InferTy, Ty<'tcx>>,
45-
const_freshen_map: FxHashMap<ty::InferConst<'tcx>, ty::Const<'tcx>>,
45+
const_freshen_map: FxHashMap<ty::InferConst, ty::Const<'tcx>>,
4646
}
4747

4848
impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
@@ -79,12 +79,12 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
7979
fn freshen_const<F>(
8080
&mut self,
8181
opt_ct: Option<ty::Const<'tcx>>,
82-
key: ty::InferConst<'tcx>,
82+
key: ty::InferConst,
8383
freshener: F,
8484
ty: Ty<'tcx>,
8585
) -> ty::Const<'tcx>
8686
where
87-
F: FnOnce(u32) -> ty::InferConst<'tcx>,
87+
F: FnOnce(u32) -> ty::InferConst,
8888
{
8989
if let Some(ct) = opt_ct {
9090
return ct.fold_with(self);

compiler/rustc_infer/src/infer/fudge.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_middle::infer::unify_key::ConstVidKey;
12
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
23
use rustc_middle::ty::{self, ConstVid, FloatVid, IntVid, RegionVid, Ty, TyCtxt, TyVid};
34

@@ -23,14 +24,14 @@ where
2324
}
2425

2526
fn const_vars_since_snapshot<'tcx>(
26-
table: &mut UnificationTable<'_, 'tcx, ConstVid<'tcx>>,
27+
table: &mut UnificationTable<'_, 'tcx, ConstVidKey<'tcx>>,
2728
snapshot_var_len: usize,
28-
) -> (Range<ConstVid<'tcx>>, Vec<ConstVariableOrigin>) {
29+
) -> (Range<ConstVid>, Vec<ConstVariableOrigin>) {
2930
let range = vars_since_snapshot(table, snapshot_var_len);
3031
(
31-
range.start..range.end,
32-
(range.start.index..range.end.index)
33-
.map(|index| table.probe_value(ConstVid::from_index(index)).origin)
32+
range.start.vid..range.end.vid,
33+
(range.start.index()..range.end.index())
34+
.map(|index| table.probe_value(ConstVid::from_u32(index)).origin)
3435
.collect(),
3536
)
3637
}
@@ -172,7 +173,7 @@ pub struct InferenceFudger<'a, 'tcx> {
172173
int_vars: Range<IntVid>,
173174
float_vars: Range<FloatVid>,
174175
region_vars: (Range<RegionVid>, Vec<RegionVariableOrigin>),
175-
const_vars: (Range<ConstVid<'tcx>>, Vec<ConstVariableOrigin>),
176+
const_vars: (Range<ConstVid>, Vec<ConstVariableOrigin>),
176177
}
177178

178179
impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceFudger<'a, 'tcx> {
@@ -235,7 +236,7 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceFudger<'a, 'tcx> {
235236
if self.const_vars.0.contains(&vid) {
236237
// This variable was created during the fudging.
237238
// Recreate it with a fresh variable here.
238-
let idx = (vid.index - self.const_vars.0.start.index) as usize;
239+
let idx = (vid.index() - self.const_vars.0.start.index()) as usize;
239240
let origin = self.const_vars.1[idx];
240241
self.infcx.next_const_var(ct.ty(), origin)
241242
} else {

compiler/rustc_infer/src/infer/generalize.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub(super) fn generalize<'tcx, D: GeneralizerDelegate<'tcx>, T: Into<Term<'tcx>>
1717
infcx: &InferCtxt<'tcx>,
1818
delegate: &mut D,
1919
term: T,
20-
for_vid: impl Into<ty::TermVid<'tcx>>,
20+
for_vid: impl Into<ty::TermVid>,
2121
ambient_variance: ty::Variance,
2222
) -> RelateResult<'tcx, Generalization<T>> {
2323
let (for_universe, root_vid) = match for_vid.into() {
@@ -27,7 +27,7 @@ pub(super) fn generalize<'tcx, D: GeneralizerDelegate<'tcx>, T: Into<Term<'tcx>>
2727
),
2828
ty::TermVid::Const(ct_vid) => (
2929
infcx.probe_const_var(ct_vid).unwrap_err(),
30-
ty::TermVid::Const(infcx.inner.borrow_mut().const_unification_table().find(ct_vid)),
30+
ty::TermVid::Const(infcx.inner.borrow_mut().const_unification_table().find(ct_vid).vid),
3131
),
3232
};
3333

@@ -127,7 +127,7 @@ struct Generalizer<'me, 'tcx, D> {
127127
/// The vid of the type variable that is in the process of being
128128
/// instantiated. If we find this within the value we are folding,
129129
/// that means we would have created a cyclic value.
130-
root_vid: ty::TermVid<'tcx>,
130+
root_vid: ty::TermVid,
131131

132132
/// The universe of the type variable that is in the process of being
133133
/// instantiated. If we find anything that this universe cannot name,
@@ -376,7 +376,7 @@ where
376376
// `vid` are related and we'd be inferring an infinitely
377377
// deep const.
378378
if ty::TermVid::Const(
379-
self.infcx.inner.borrow_mut().const_unification_table().find(vid),
379+
self.infcx.inner.borrow_mut().const_unification_table().find(vid).vid,
380380
) == self.root_vid
381381
{
382382
return Err(self.cyclic_term_error());
@@ -394,10 +394,14 @@ where
394394
if self.for_universe.can_name(universe) {
395395
Ok(c)
396396
} else {
397-
let new_var_id = variable_table.new_key(ConstVarValue {
398-
origin: var_value.origin,
399-
val: ConstVariableValue::Unknown { universe: self.for_universe },
400-
});
397+
let new_var_id = variable_table
398+
.new_key(ConstVarValue {
399+
origin: var_value.origin,
400+
val: ConstVariableValue::Unknown {
401+
universe: self.for_universe,
402+
},
403+
})
404+
.vid;
401405
Ok(ty::Const::new_var(self.tcx(), new_var_id, c.ty()))
402406
}
403407
}

compiler/rustc_infer/src/infer/mod.rs

+45-38
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ pub use self::RegionVariableOrigin::*;
66
pub use self::SubregionOrigin::*;
77
pub use self::ValuePairs::*;
88
pub use combine::ObligationEmittingRelation;
9+
use rustc_data_structures::captures::Captures;
910
use rustc_data_structures::undo_log::UndoLogs;
11+
use rustc_middle::infer::unify_key::{ConstVidKey, EffectVidKey};
1012

1113
use self::opaque_types::OpaqueTypeStorage;
1214
pub(crate) use self::undo_log::{InferCtxtUndoLogs, Snapshot, UndoLog};
@@ -40,7 +42,6 @@ use rustc_span::{Span, DUMMY_SP};
4042

4143
use std::cell::{Cell, RefCell};
4244
use std::fmt;
43-
use std::marker::PhantomData;
4445

4546
use self::combine::CombineFields;
4647
use self::error_reporting::TypeErrCtxt;
@@ -85,7 +86,7 @@ pub struct InferOk<'tcx, T> {
8586
pub type InferResult<'tcx, T> = Result<InferOk<'tcx, T>, TypeError<'tcx>>;
8687

8788
pub type UnitResult<'tcx> = RelateResult<'tcx, ()>; // "unify result"
88-
pub type FixupResult<'tcx, T> = Result<T, FixupError<'tcx>>; // "fixup result"
89+
pub type FixupResult<T> = Result<T, FixupError>; // "fixup result"
8990

9091
pub(crate) type UnificationTable<'a, 'tcx, T> = ut::UnificationTable<
9192
ut::InPlace<T, &'a mut ut::UnificationStorage<T>, &'a mut InferCtxtUndoLogs<'tcx>>,
@@ -108,7 +109,7 @@ pub struct InferCtxtInner<'tcx> {
108109
type_variable_storage: type_variable::TypeVariableStorage<'tcx>,
109110

110111
/// Map from const parameter variable to the kind of const it represents.
111-
const_unification_storage: ut::UnificationTableStorage<ty::ConstVid<'tcx>>,
112+
const_unification_storage: ut::UnificationTableStorage<ConstVidKey<'tcx>>,
112113

113114
/// Map from integral variable to the kind of integer it represents.
114115
int_unification_storage: ut::UnificationTableStorage<ty::IntVid>,
@@ -117,7 +118,7 @@ pub struct InferCtxtInner<'tcx> {
117118
float_unification_storage: ut::UnificationTableStorage<ty::FloatVid>,
118119

119120
/// Map from effect variable to the effect param it represents.
120-
effect_unification_storage: ut::UnificationTableStorage<ty::EffectVid<'tcx>>,
121+
effect_unification_storage: ut::UnificationTableStorage<EffectVidKey<'tcx>>,
121122

122123
/// Tracks the set of region variables and the constraints between them.
123124
///
@@ -224,11 +225,11 @@ impl<'tcx> InferCtxtInner<'tcx> {
224225
}
225226

226227
#[inline]
227-
fn const_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ty::ConstVid<'tcx>> {
228+
fn const_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ConstVidKey<'tcx>> {
228229
self.const_unification_storage.with_log(&mut self.undo_log)
229230
}
230231

231-
fn effect_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ty::EffectVid<'tcx>> {
232+
fn effect_unification_table(&mut self) -> UnificationTable<'_, 'tcx, EffectVidKey<'tcx>> {
232233
self.effect_unification_storage.with_log(&mut self.undo_log)
233234
}
234235

@@ -359,7 +360,7 @@ impl<'tcx> ty::InferCtxtLike for InferCtxt<'tcx> {
359360
}
360361
}
361362

362-
fn universe_of_ct(&self, ct: ty::InferConst<'tcx>) -> Option<ty::UniverseIndex> {
363+
fn universe_of_ct(&self, ct: ty::InferConst) -> Option<ty::UniverseIndex> {
363364
use ty::InferConst::*;
364365
match ct {
365366
// Same issue as with `universe_of_ty`
@@ -548,11 +549,11 @@ pub enum NllRegionVariableOrigin {
548549

549550
// FIXME(eddyb) investigate overlap between this and `TyOrConstInferVar`.
550551
#[derive(Copy, Clone, Debug)]
551-
pub enum FixupError<'tcx> {
552+
pub enum FixupError {
552553
UnresolvedIntTy(IntVid),
553554
UnresolvedFloatTy(FloatVid),
554555
UnresolvedTy(TyVid),
555-
UnresolvedConst(ConstVid<'tcx>),
556+
UnresolvedConst(ConstVid),
556557
}
557558

558559
/// See the `region_obligations` field for more information.
@@ -563,7 +564,7 @@ pub struct RegionObligation<'tcx> {
563564
pub origin: SubregionOrigin<'tcx>,
564565
}
565566

566-
impl<'tcx> fmt::Display for FixupError<'tcx> {
567+
impl fmt::Display for FixupError {
567568
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
568569
use self::FixupError::*;
569570

@@ -794,7 +795,7 @@ impl<'tcx> InferCtxt<'tcx> {
794795
let mut table = inner.effect_unification_table();
795796

796797
(0..table.len())
797-
.map(|i| ty::EffectVid { index: i as u32, phantom: PhantomData })
798+
.map(|i| ty::EffectVid::from_usize(i))
798799
.filter(|&vid| table.probe_value(vid).is_none())
799800
.map(|v| {
800801
ty::Const::new_infer(self.tcx, ty::InferConst::EffectVar(v), self.tcx.types.bool)
@@ -1072,15 +1073,20 @@ impl<'tcx> InferCtxt<'tcx> {
10721073
.inner
10731074
.borrow_mut()
10741075
.const_unification_table()
1075-
.new_key(ConstVarValue { origin, val: ConstVariableValue::Unknown { universe } });
1076+
.new_key(ConstVarValue { origin, val: ConstVariableValue::Unknown { universe } })
1077+
.vid;
10761078
ty::Const::new_var(self.tcx, vid, ty)
10771079
}
10781080

1079-
pub fn next_const_var_id(&self, origin: ConstVariableOrigin) -> ConstVid<'tcx> {
1080-
self.inner.borrow_mut().const_unification_table().new_key(ConstVarValue {
1081-
origin,
1082-
val: ConstVariableValue::Unknown { universe: self.universe() },
1083-
})
1081+
pub fn next_const_var_id(&self, origin: ConstVariableOrigin) -> ConstVid {
1082+
self.inner
1083+
.borrow_mut()
1084+
.const_unification_table()
1085+
.new_key(ConstVarValue {
1086+
origin,
1087+
val: ConstVariableValue::Unknown { universe: self.universe() },
1088+
})
1089+
.vid
10841090
}
10851091

10861092
fn next_int_var_id(&self) -> IntVid {
@@ -1194,11 +1200,15 @@ impl<'tcx> InferCtxt<'tcx> {
11941200
),
11951201
span,
11961202
};
1197-
let const_var_id =
1198-
self.inner.borrow_mut().const_unification_table().new_key(ConstVarValue {
1203+
let const_var_id = self
1204+
.inner
1205+
.borrow_mut()
1206+
.const_unification_table()
1207+
.new_key(ConstVarValue {
11991208
origin,
12001209
val: ConstVariableValue::Unknown { universe: self.universe() },
1201-
});
1210+
})
1211+
.vid;
12021212
ty::Const::new_var(
12031213
self.tcx,
12041214
const_var_id,
@@ -1213,7 +1223,7 @@ impl<'tcx> InferCtxt<'tcx> {
12131223
}
12141224

12151225
pub fn var_for_effect(&self, param: &ty::GenericParamDef) -> GenericArg<'tcx> {
1216-
let effect_vid = self.inner.borrow_mut().effect_unification_table().new_key(None);
1226+
let effect_vid = self.inner.borrow_mut().effect_unification_table().new_key(None).vid;
12171227
let ty = self
12181228
.tcx
12191229
.type_of(param.def_id)
@@ -1333,12 +1343,12 @@ impl<'tcx> InferCtxt<'tcx> {
13331343
self.inner.borrow_mut().type_variables().root_var(var)
13341344
}
13351345

1336-
pub fn root_const_var(&self, var: ty::ConstVid<'tcx>) -> ty::ConstVid<'tcx> {
1337-
self.inner.borrow_mut().const_unification_table().find(var)
1346+
pub fn root_const_var(&self, var: ty::ConstVid) -> ty::ConstVid {
1347+
self.inner.borrow_mut().const_unification_table().find(var).vid
13381348
}
13391349

1340-
pub fn root_effect_var(&self, var: ty::EffectVid<'tcx>) -> ty::EffectVid<'tcx> {
1341-
self.inner.borrow_mut().effect_unification_table().find(var)
1350+
pub fn root_effect_var(&self, var: ty::EffectVid) -> ty::EffectVid {
1351+
self.inner.borrow_mut().effect_unification_table().find(var).vid
13421352
}
13431353

13441354
/// Resolves an int var to a rigid int type, if it was constrained to one,
@@ -1402,17 +1412,14 @@ impl<'tcx> InferCtxt<'tcx> {
14021412
value.visit_with(&mut resolve::UnresolvedTypeOrConstFinder::new(self)).break_value()
14031413
}
14041414

1405-
pub fn probe_const_var(
1406-
&self,
1407-
vid: ty::ConstVid<'tcx>,
1408-
) -> Result<ty::Const<'tcx>, ty::UniverseIndex> {
1415+
pub fn probe_const_var(&self, vid: ty::ConstVid) -> Result<ty::Const<'tcx>, ty::UniverseIndex> {
14091416
match self.inner.borrow_mut().const_unification_table().probe_value(vid).val {
14101417
ConstVariableValue::Known { value } => Ok(value),
14111418
ConstVariableValue::Unknown { universe } => Err(universe),
14121419
}
14131420
}
14141421

1415-
pub fn probe_effect_var(&self, vid: EffectVid<'tcx>) -> Option<EffectVarValue<'tcx>> {
1422+
pub fn probe_effect_var(&self, vid: EffectVid) -> Option<EffectVarValue<'tcx>> {
14161423
self.inner.borrow_mut().effect_unification_table().probe_value(vid)
14171424
}
14181425

@@ -1423,7 +1430,7 @@ impl<'tcx> InferCtxt<'tcx> {
14231430
///
14241431
/// This method is idempotent, but it not typically not invoked
14251432
/// except during the writeback phase.
1426-
pub fn fully_resolve<T: TypeFoldable<TyCtxt<'tcx>>>(&self, value: T) -> FixupResult<'tcx, T> {
1433+
pub fn fully_resolve<T: TypeFoldable<TyCtxt<'tcx>>>(&self, value: T) -> FixupResult<T> {
14271434
match resolve::fully_resolve(self, value) {
14281435
Ok(value) => {
14291436
if value.has_non_region_infer() {
@@ -1647,11 +1654,11 @@ impl<'tcx> InferCtxt<'tcx> {
16471654
#[inline]
16481655
pub fn is_ty_infer_var_definitely_unchanged<'a>(
16491656
&'a self,
1650-
) -> (impl Fn(TyOrConstInferVar<'tcx>) -> bool + 'a) {
1657+
) -> (impl Fn(TyOrConstInferVar) -> bool + Captures<'tcx> + 'a) {
16511658
// This hoists the borrow/release out of the loop body.
16521659
let inner = self.inner.try_borrow();
16531660

1654-
return move |infer_var: TyOrConstInferVar<'tcx>| match (infer_var, &inner) {
1661+
return move |infer_var: TyOrConstInferVar| match (infer_var, &inner) {
16551662
(TyOrConstInferVar::Ty(ty_var), Ok(inner)) => {
16561663
use self::type_variable::TypeVariableValue;
16571664

@@ -1674,7 +1681,7 @@ impl<'tcx> InferCtxt<'tcx> {
16741681
/// inference variables), and it handles both `Ty` and `ty::Const` without
16751682
/// having to resort to storing full `GenericArg`s in `stalled_on`.
16761683
#[inline(always)]
1677-
pub fn ty_or_const_infer_var_changed(&self, infer_var: TyOrConstInferVar<'tcx>) -> bool {
1684+
pub fn ty_or_const_infer_var_changed(&self, infer_var: TyOrConstInferVar) -> bool {
16781685
match infer_var {
16791686
TyOrConstInferVar::Ty(v) => {
16801687
use self::type_variable::TypeVariableValue;
@@ -1781,7 +1788,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
17811788
/// Helper for [InferCtxt::ty_or_const_infer_var_changed] (see comment on that), currently
17821789
/// used only for `traits::fulfill`'s list of `stalled_on` inference variables.
17831790
#[derive(Copy, Clone, Debug)]
1784-
pub enum TyOrConstInferVar<'tcx> {
1791+
pub enum TyOrConstInferVar {
17851792
/// Equivalent to `ty::Infer(ty::TyVar(_))`.
17861793
Ty(TyVid),
17871794
/// Equivalent to `ty::Infer(ty::IntVar(_))`.
@@ -1790,12 +1797,12 @@ pub enum TyOrConstInferVar<'tcx> {
17901797
TyFloat(FloatVid),
17911798

17921799
/// Equivalent to `ty::ConstKind::Infer(ty::InferConst::Var(_))`.
1793-
Const(ConstVid<'tcx>),
1800+
Const(ConstVid),
17941801
/// Equivalent to `ty::ConstKind::Infer(ty::InferConst::EffectVar(_))`.
1795-
Effect(EffectVid<'tcx>),
1802+
Effect(EffectVid),
17961803
}
17971804

1798-
impl<'tcx> TyOrConstInferVar<'tcx> {
1805+
impl<'tcx> TyOrConstInferVar {
17991806
/// Tries to extract an inference variable from a type or a constant, returns `None`
18001807
/// for types other than `ty::Infer(_)` (or `InferTy::Fresh*`) and
18011808
/// for constants other than `ty::ConstKind::Infer(_)` (or `InferConst::Fresh`).

0 commit comments

Comments
 (0)