Skip to content

Commit 5221322

Browse files
committed
Auto merge of #141499 - compiler-errors:cache-ct-var, r=<try>
Pre-intern some const infer vars Cache const vars like we do for ty/region vars
2 parents 5af801b + 5771471 commit 5221322

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> {
139139
variance,
140140
ty,
141141
)?;
142-
Ok(infcx.resolve_vars_if_possible(Ty::new_infer(infcx.tcx, ty::TyVar(ty_vid))))
142+
Ok(infcx.resolve_vars_if_possible(Ty::new_var(infcx.tcx, ty_vid)))
143143
};
144144

145145
let (a, b) = match (a.kind(), b.kind()) {

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ impl<'tcx> Const<'tcx> {
7575
}
7676

7777
#[inline]
78-
pub fn new_var(tcx: TyCtxt<'tcx>, infer: ty::ConstVid) -> Const<'tcx> {
79-
Const::new(tcx, ty::ConstKind::Infer(ty::InferConst::Var(infer)))
78+
pub fn new_var(tcx: TyCtxt<'tcx>, v: ty::ConstVid) -> Const<'tcx> {
79+
// Use a pre-interned one when possible.
80+
tcx.consts
81+
.ct_vars
82+
.get(v.as_usize())
83+
.copied()
84+
.unwrap_or_else(|| Const::new(tcx, ty::ConstKind::Infer(ty::InferConst::Var(v))))
8085
}
8186

8287
#[inline]

compiler/rustc_middle/src/ty/context.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,8 @@ const NUM_PREINTERNED_FRESH_TYS: u32 = 20;
10141014
const NUM_PREINTERNED_FRESH_INT_TYS: u32 = 3;
10151015
const NUM_PREINTERNED_FRESH_FLOAT_TYS: u32 = 3;
10161016

1017+
const NUM_PREINTERNED_CT_VARS: u32 = 100;
1018+
10171019
// This number may seem high, but it is reached in all but the smallest crates.
10181020
const NUM_PREINTERNED_RE_VARS: u32 = 500;
10191021
const NUM_PREINTERNED_RE_LATE_BOUNDS_I: u32 = 2;
@@ -1084,6 +1086,8 @@ pub struct CommonConsts<'tcx> {
10841086
pub false_: Const<'tcx>,
10851087
/// Use [`ty::ValTree::zst`] instead.
10861088
pub(crate) valtree_zst: ValTree<'tcx>,
1089+
/// Pre-interned `ConstKind::Infer(InferConst::Var(n))` for small values of `n`.
1090+
pub ct_vars: Vec<Const<'tcx>>,
10871091
}
10881092

10891093
impl<'tcx> CommonTypes<'tcx> {
@@ -1197,6 +1201,10 @@ impl<'tcx> CommonConsts<'tcx> {
11971201
let valtree_true = mk_valtree(ty::ValTreeKind::Leaf(ty::ScalarInt::TRUE));
11981202
let valtree_false = mk_valtree(ty::ValTreeKind::Leaf(ty::ScalarInt::FALSE));
11991203

1204+
let ct_vars = (0..NUM_PREINTERNED_CT_VARS)
1205+
.map(|n| mk_const(ty::ConstKind::Infer(ty::InferConst::Var(ty::ConstVid::from(n)))))
1206+
.collect();
1207+
12001208
CommonConsts {
12011209
unit: mk_const(ty::ConstKind::Value(ty::Value {
12021210
ty: types.unit,
@@ -1210,6 +1218,7 @@ impl<'tcx> CommonConsts<'tcx> {
12101218
ty: types.bool,
12111219
valtree: valtree_false,
12121220
})),
1221+
ct_vars,
12131222
valtree_zst,
12141223
}
12151224
}

0 commit comments

Comments
 (0)