Skip to content

Commit 575ba3a

Browse files
committed
Make const methods consistent and expose methods to shallow resolve a tyvar.
1 parent 84075ba commit 575ba3a

1 file changed

Lines changed: 29 additions & 13 deletions

File tree

  • compiler/rustc_infer/src/infer

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,33 @@ impl<'tcx> InferCtxt<'tcx> {
13221322
}
13231323
}
13241324

1325+
/// Resolve a const variable to a const, if known.
1326+
/// Otherwise return a const with the root const vid in it.
1327+
///
1328+
/// `ct` is const type that we may already have available, which represents the `ConstVid`.
1329+
/// In cases where we do, this can aid performance.
1330+
#[inline(always)]
1331+
fn shallow_resolve_const_var_with_ct(
1332+
&self,
1333+
v: ConstVid,
1334+
ct: Option<ty::Const<'tcx>>,
1335+
) -> ty::Const<'tcx> {
1336+
let (root, value) =
1337+
self.inner.borrow_mut().const_unification_table().inlined_probe_key_value(v);
1338+
match value {
1339+
ConstVariableValue::Known { value } => value,
1340+
ConstVariableValue::Unknown { .. } => {
1341+
if root.vid == v
1342+
&& let Some(ct) = ct
1343+
{
1344+
ct
1345+
} else {
1346+
ty::Const::new_var(self.tcx, root.vid)
1347+
}
1348+
}
1349+
}
1350+
}
1351+
13251352
/// Shallow resolve a type/int infer var, panics on type variables.
13261353
///
13271354
/// See docs on [`shallow_resolve_ty_var`](Self::shallow_resolve_ty_var) for why this exists.
@@ -1392,19 +1419,9 @@ impl<'tcx> InferCtxt<'tcx> {
13921419
pub fn shallow_resolve_const(&self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
13931420
match ct.kind() {
13941421
ty::ConstKind::Infer(infer_ct) => match infer_ct {
1395-
InferConst::Var(vid) => {
1396-
let (root, value) = self
1397-
.inner
1398-
.borrow_mut()
1399-
.const_unification_table()
1400-
.inlined_probe_key_value(vid);
1401-
value.known().unwrap_or_else(|| {
1402-
if root.vid == vid { ct } else { ty::Const::new_var(self.tcx, root.vid) }
1403-
})
1404-
}
1422+
InferConst::Var(vid) => self.shallow_resolve_const_var_with_ct(vid, Some(ct)),
14051423
InferConst::Fresh(_) => ct,
14061424
},
1407-
14081425
ty::ConstKind::Param(_)
14091426
| ty::ConstKind::Bound(_, _)
14101427
| ty::ConstKind::Placeholder(_)
@@ -1475,13 +1492,12 @@ impl<'tcx> InferCtxt<'tcx> {
14751492
self.shallow_resolve_int_var_with_ty(vid, None)
14761493
}
14771494

1478-
/// Resolves a float var to a rigid int type, if it was constrained to one,
1495+
/// Resolves a float var to a rigid type, if it was constrained to one,
14791496
/// or else the root float var in the unification table.
14801497
pub fn shallow_resolve_float_var(&self, vid: ty::FloatVid) -> Ty<'tcx> {
14811498
self.shallow_resolve_float_var_with_ty(vid, None)
14821499
}
14831500

1484-
/// Where possible, replaces type/const variables in `value` with their final value.
14851501
/// If a type/const variable has not (yet) been unified, it is left as is.
14861502
///
14871503
/// This is an idempotent operation that does not affect inference state in any way,

0 commit comments

Comments
 (0)