Skip to content

Commit 3dde145

Browse files
committed
fixup various sites that use root_{ty,const}_var that can skip it after a shallow_resolve
1 parent 15c5ff4 commit 3dde145

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
@@ -285,7 +285,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
285285
}
286286

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

398398
/// If `ty` is an unresolved type variable, returns its root vid.
399399
fn root_vid(&self, ty: Ty<'tcx>) -> Option<ty::TyVid> {
400-
Some(self.root_var(self.shallow_resolve(ty).ty_vid()?))
400+
Some(self.shallow_resolve(ty).ty_vid()?)
401401
}
402402

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

408408
/// 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
@@ -101,7 +101,7 @@ impl<'tcx> rustc_type_ir::InferCtxtLike for InferCtxt<'tcx> {
101101
}
102102

103103
fn root_ty_var(&self, var: ty::TyVid) -> ty::TyVid {
104-
self.root_var(var)
104+
self.root_ty_var(var)
105105
}
106106

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

499499
let folded = match t.kind() {
500500
ty::Infer(ty::TyVar(vid)) => {
501-
let vid = self.infcx.root_var(*vid);
501+
let vid = self.infcx.root_ty_var(*vid);
502502
let probe = self.infcx.inner.borrow_mut().type_variables().probe(vid);
503503
match probe {
504504
TypeVariableValue::Known { value: u } => u.super_fold_with(self),
@@ -530,8 +530,8 @@ impl<'a, 'tcx> ty::TypeFolder<TyCtxt<'tcx>> for LowerUniverseFolder<'a, 'tcx> {
530530

531531
match c.kind() {
532532
ty::ConstKind::Infer(ty::InferConst::Var(vid)) => {
533-
let vid = self.infcx.root_const_var(vid);
534-
let universe = match self.infcx.try_resolve_const_var(vid) {
533+
let (res, vid) = self.infcx.try_resolve_const_var_with_root(vid);
534+
let universe = match res {
535535
Ok(value) => return value.fold_with(self),
536536
Err(universe) => universe,
537537
};

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,8 +1211,29 @@ impl<'tcx> InferCtxt<'tcx> {
12111211
self.deep_resolve_non_region_vars(t).to_string()
12121212
}
12131213

1214-
/// If `TyVar(vid)` resolves to a type, return that type. Else, return the
1215-
/// universe index of `TyVar(vid)`.
1214+
/// If `TyVar(vid)` resolves to a type, return that type.
1215+
/// Else, return the universe index of `TyVar(vid)`.
1216+
///
1217+
/// Also return the root `TyVid` of `vid`.
1218+
/// This is more efficient than calling [`try_resolve_ty_var`](Self::try_resolve_ty_var)
1219+
/// followed by [`root_ty_var`](Self::root_ty_var).
1220+
pub fn try_resolve_ty_var_with_root(
1221+
&self,
1222+
vid: TyVid,
1223+
) -> (Result<Ty<'tcx>, ty::UniverseIndex>, TyVid) {
1224+
let (root, value) = self.inner.borrow_mut().type_variables().probe_with_root_vid(vid);
1225+
1226+
(
1227+
match value {
1228+
TypeVariableValue::Known { value } => Ok(self.shallow_resolve_non_recursive(value)),
1229+
TypeVariableValue::Unknown { universe } => Err(universe),
1230+
},
1231+
root,
1232+
)
1233+
}
1234+
1235+
/// If `TyVar(vid)` resolves to a type, return that type.
1236+
/// Else, return the universe index of `TyVar(vid)`.
12161237
pub fn try_resolve_ty_var(&self, vid: TyVid) -> Result<Ty<'tcx>, ty::UniverseIndex> {
12171238
let value = self.inner.borrow_mut().type_variables().probe(vid);
12181239

@@ -1224,12 +1245,8 @@ impl<'tcx> InferCtxt<'tcx> {
12241245

12251246
/// If `vid` resolves to a type, return that type. Otherwise return the root variable id for `vid`.
12261247
pub fn shallow_resolve_ty_var_or_get_root(&self, vid: TyVid) -> Result<Ty<'tcx>, TyVid> {
1227-
let (root, value) = self.inner.borrow_mut().type_variables().probe_with_root_vid(vid);
1228-
1229-
match value {
1230-
TypeVariableValue::Known { value } => Ok(self.shallow_resolve_non_recursive(value)),
1231-
TypeVariableValue::Unknown { universe: _ } => Err(root),
1232-
}
1248+
let (res, root) = self.try_resolve_ty_var_with_root(vid);
1249+
res.map_err(|_| root)
12331250
}
12341251

12351252
/// Resolve a type variable to a type, if known.
@@ -1430,7 +1447,7 @@ impl<'tcx> InferCtxt<'tcx> {
14301447
}
14311448
}
14321449

1433-
pub fn root_var(&self, var: ty::TyVid) -> ty::TyVid {
1450+
pub fn root_ty_var(&self, var: ty::TyVid) -> ty::TyVid {
14341451
self.inner.borrow_mut().type_variables().root_var(var)
14351452
}
14361453

@@ -1513,6 +1530,29 @@ impl<'tcx> InferCtxt<'tcx> {
15131530
value.fold_with(&mut r)
15141531
}
15151532

1533+
/// If `ConstVar(vid)` resolves to a const, return that const.
1534+
/// Else, return the universe index of `ConstVar(vid)`.
1535+
///
1536+
/// Also return the root `ConstVid` of `vid`.
1537+
/// This is more efficient than calling [`try_resolve_const_var`](Self::try_resolve_const_var)
1538+
/// followed by [`root_const_var`](Self::root_const_var).
1539+
pub fn try_resolve_const_var_with_root(
1540+
&self,
1541+
vid: ty::ConstVid,
1542+
) -> (Result<ty::Const<'tcx>, ty::UniverseIndex>, ty::ConstVid) {
1543+
let (root, value) =
1544+
self.inner.borrow_mut().const_unification_table().inlined_probe_key_value(vid);
1545+
(
1546+
match value {
1547+
ConstVariableValue::Known { value } => Ok(value),
1548+
ConstVariableValue::Unknown { origin: _, universe } => Err(universe),
1549+
},
1550+
root.vid,
1551+
)
1552+
}
1553+
1554+
/// If `ConstVar(vid)` resolves to a const, return that const.
1555+
/// Else, return the universe index of `ConstVar(vid)`.
15161556
pub fn try_resolve_const_var(
15171557
&self,
15181558
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)