Skip to content

Commit 25e4653

Browse files
committed
fixup various sites that use root_{ty,const}_var that can skip it after a shallow_resolve
1 parent 575ba3a commit 25e4653

7 files changed

Lines changed: 77 additions & 30 deletions

File tree

compiler/rustc_hir_typeck/src/closure.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
286286
}
287287

288288
/// Given the expected type, figures out what it can about this closure we
289-
/// are about to type check:
289+
/// are about to type check.
290+
///
291+
/// WARNING: `expected_ty` must be resolved, to ensure that tyvars refer to root vids.
290292
#[instrument(skip(self), level = "debug", ret)]
291293
fn deduce_closure_signature(
292294
&self,
@@ -313,13 +315,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
313315
.and_then(|did| self.tcx.fn_trait_kind_from_def_id(did));
314316
(sig, kind)
315317
}
316-
ty::Infer(ty::TyVar(vid)) => self.deduce_closure_signature_from_predicates(
317-
Ty::new_var(self.tcx, self.root_var(vid)),
318-
closure_kind,
319-
self.obligations_for_self_ty(vid, UseSubtyping::No)
320-
.into_iter()
321-
.filter_map(|obl| Some((obl.predicate.as_clause()?, obl.cause.span))),
322-
),
318+
ty::Infer(ty::TyVar(vid)) => {
319+
// assert that the precondition (documented in the doc comments) is maintained.
320+
debug_assert_eq!(self.root_ty_var(vid), vid);
321+
self.deduce_closure_signature_from_predicates(
322+
Ty::new_var(self.tcx, vid),
323+
closure_kind,
324+
self.obligations_for_self_ty(vid, UseSubtyping::No)
325+
.into_iter()
326+
.filter_map(|obl| Some((obl.predicate.as_clause()?, obl.cause.span))),
327+
)
328+
}
323329
ty::FnPtr(sig_tys, hdr) => match closure_kind {
324330
hir::ClosureKind::Closure => {
325331
let expected_sig = ExpectedSig { cause_span: None, sig: sig_tys.with(hdr) };

compiler/rustc_hir_typeck/src/fallback.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,12 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
315315

316316
/// If `ty` is an unresolved type variable, returns its root vid.
317317
fn root_vid(&self, ty: Ty<'tcx>) -> Option<ty::TyVid> {
318-
Some(self.root_var(self.shallow_resolve(ty).ty_vid()?))
318+
Some(self.shallow_resolve(ty).ty_vid()?)
319319
}
320320

321321
/// If `ty` is an unresolved float type variable, returns its root vid.
322322
pub(crate) fn root_float_vid(&self, ty: Ty<'tcx>) -> Option<ty::FloatVid> {
323-
Some(self.root_float_var(self.shallow_resolve(ty).float_vid()?))
323+
Some(self.shallow_resolve(ty).float_vid()?)
324324
}
325325

326326
/// Given a set of diverging vids and coercions, walk the HIR to gather a

compiler/rustc_hir_typeck/src/fn_ctxt/inspect_obligations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9595

9696
match *ty.kind() {
9797
ty::Infer(ty::TyVar(found_vid)) => match subtyping {
98-
UseSubtyping::No => self.root_var(expected_vid) == self.root_var(found_vid),
98+
UseSubtyping::No => self.root_ty_var(expected_vid) == found_vid,
9999
UseSubtyping::Yes => {
100100
self.sub_unification_table_root_var(expected_vid)
101101
== self.sub_unification_table_root_var(found_vid)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'cx, 'tcx> {
334334
// We need to canonicalize the *root* of our ty var.
335335
// This is so that our canonical response correctly reflects
336336
// any equated inference vars correctly!
337-
let root_vid = self.infcx.unwrap().root_var(vid);
337+
let root_vid = self.infcx.unwrap().root_ty_var(vid);
338338
if root_vid != vid {
339339
t = Ty::new_var(self.tcx, root_vid);
340340
vid = root_vid;

compiler/rustc_infer/src/infer/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl<'tcx> rustc_type_ir::InferCtxtLike for InferCtxt<'tcx> {
9797
}
9898

9999
fn root_ty_var(&self, var: ty::TyVid) -> ty::TyVid {
100-
self.root_var(var)
100+
self.root_ty_var(var)
101101
}
102102

103103
fn sub_unification_table_root_var(&self, var: ty::TyVid) -> ty::TyVid {
@@ -449,7 +449,7 @@ impl<'a, 'tcx> ty::TypeFolder<TyCtxt<'tcx>> for LowerUniverseFolder<'a, 'tcx> {
449449

450450
let folded = match t.kind() {
451451
ty::Infer(ty::TyVar(vid)) => {
452-
let vid = self.infcx.root_var(*vid);
452+
let vid = self.infcx.root_ty_var(*vid);
453453
let probe = self.infcx.inner.borrow_mut().type_variables().probe(vid);
454454
match probe {
455455
TypeVariableValue::Known { value: u } => u.super_fold_with(self),
@@ -481,8 +481,8 @@ impl<'a, 'tcx> ty::TypeFolder<TyCtxt<'tcx>> for LowerUniverseFolder<'a, 'tcx> {
481481

482482
match c.kind() {
483483
ty::ConstKind::Infer(ty::InferConst::Var(vid)) => {
484-
let vid = self.infcx.root_const_var(vid);
485-
let universe = match self.infcx.try_resolve_const_var(vid) {
484+
let (res, vid) = self.infcx.try_resolve_const_var_with_root(vid);
485+
let universe = match res {
486486
Ok(value) => return value.fold_with(self),
487487
Err(universe) => universe,
488488
};

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,8 +1222,29 @@ impl<'tcx> InferCtxt<'tcx> {
12221222
self.deeply_resolve_ignoring_regions(t).to_string()
12231223
}
12241224

1225-
/// If `TyVar(vid)` resolves to a type, return that type. Else, return the
1226-
/// universe index of `TyVar(vid)`.
1225+
/// If `TyVar(vid)` resolves to a type, return that type.
1226+
/// Else, return the universe index of `TyVar(vid)`.
1227+
///
1228+
/// Also return the root `TyVid` of `vid`.
1229+
/// This is more efficient than calling [`try_resolve_ty_var`](Self::try_resolve_ty_var)
1230+
/// followed by [`root_ty_var`](Self::root_ty_var).
1231+
pub fn try_resolve_ty_var_with_root(
1232+
&self,
1233+
vid: TyVid,
1234+
) -> (Result<Ty<'tcx>, ty::UniverseIndex>, TyVid) {
1235+
let (root, value) = self.inner.borrow_mut().type_variables().probe_with_root_vid(vid);
1236+
1237+
(
1238+
match value {
1239+
TypeVariableValue::Known { value } => Ok(self.shallow_resolve_non_recursive(value)),
1240+
TypeVariableValue::Unknown { universe } => Err(universe),
1241+
},
1242+
root,
1243+
)
1244+
}
1245+
1246+
/// If `TyVar(vid)` resolves to a type, return that type.
1247+
/// Else, return the universe index of `TyVar(vid)`.
12271248
pub fn try_resolve_ty_var(&self, vid: TyVid) -> Result<Ty<'tcx>, ty::UniverseIndex> {
12281249
let value = self.inner.borrow_mut().type_variables().probe(vid);
12291250

@@ -1235,12 +1256,8 @@ impl<'tcx> InferCtxt<'tcx> {
12351256

12361257
/// If `vid` resolves to a type, return that type. Otherwise return the root variable id for `vid`.
12371258
pub fn shallow_resolve_ty_var_or_get_root(&self, vid: TyVid) -> Result<Ty<'tcx>, TyVid> {
1238-
let (root, value) = self.inner.borrow_mut().type_variables().probe_with_root_vid(vid);
1239-
1240-
match value {
1241-
TypeVariableValue::Known { value } => Ok(self.shallow_resolve_non_recursive(value)),
1242-
TypeVariableValue::Unknown { universe: _ } => Err(root),
1243-
}
1259+
let (res, root) = self.try_resolve_ty_var_with_root(vid);
1260+
res.map_err(|_| root)
12441261
}
12451262

12461263
/// Resolve a type variable to a type, if known.
@@ -1441,7 +1458,7 @@ impl<'tcx> InferCtxt<'tcx> {
14411458
}
14421459
}
14431460

1444-
pub fn root_var(&self, var: ty::TyVid) -> ty::TyVid {
1461+
pub fn root_ty_var(&self, var: ty::TyVid) -> ty::TyVid {
14451462
self.inner.borrow_mut().type_variables().root_var(var)
14461463
}
14471464

@@ -1529,6 +1546,29 @@ impl<'tcx> InferCtxt<'tcx> {
15291546
value.fold_with(&mut r)
15301547
}
15311548

1549+
/// If `ConstVar(vid)` resolves to a const, return that const.
1550+
/// Else, return the universe index of `ConstVar(vid)`.
1551+
///
1552+
/// Also return the root `ConstVid` of `vid`.
1553+
/// This is more efficient than calling [`try_resolve_const_var`](Self::try_resolve_const_var)
1554+
/// followed by [`root_const_var`](Self::root_const_var).
1555+
pub fn try_resolve_const_var_with_root(
1556+
&self,
1557+
vid: ty::ConstVid,
1558+
) -> (Result<ty::Const<'tcx>, ty::UniverseIndex>, ty::ConstVid) {
1559+
let (root, value) =
1560+
self.inner.borrow_mut().const_unification_table().inlined_probe_key_value(vid);
1561+
(
1562+
match value {
1563+
ConstVariableValue::Known { value } => Ok(value),
1564+
ConstVariableValue::Unknown { origin: _, universe } => Err(universe),
1565+
},
1566+
root.vid,
1567+
)
1568+
}
1569+
1570+
/// If `ConstVar(vid)` resolves to a const, return that const.
1571+
/// Else, return the universe index of `ConstVar(vid)`.
15321572
pub fn try_resolve_const_var(
15331573
&self,
15341574
vid: ty::ConstVid,

compiler/rustc_infer/src/infer/relate/generalize.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,13 @@ impl<'tcx> InferCtxt<'tcx> {
287287
assert!(!source_term.has_escaping_bound_vars());
288288
let (for_universe, root_vid) = match target_vid {
289289
TermVid::Ty(ty_vid) => {
290-
(self.try_resolve_ty_var(ty_vid).unwrap_err(), TermVid::Ty(self.root_var(ty_vid)))
290+
let (res, root) = self.try_resolve_ty_var_with_root(ty_vid);
291+
(res.unwrap_err(), TermVid::Ty(root))
292+
}
293+
TermVid::Const(ct_vid) => {
294+
let (res, root) = self.try_resolve_const_var_with_root(ct_vid);
295+
(res.unwrap_err(), TermVid::Const(root))
291296
}
292-
TermVid::Const(ct_vid) => (
293-
self.try_resolve_const_var(ct_vid).unwrap_err(),
294-
TermVid::Const(self.inner.borrow_mut().const_unification_table().find(ct_vid).vid),
295-
),
296297
};
297298

298299
let mut generalizer = Generalizer {

0 commit comments

Comments
 (0)