Skip to content

Commit 44a5b55

Browse files
Auto merge of rust-lang#150748 - nnethercote:canonicalizer-cleanups, r=lcnr
Canonicalizer cleanups Some cleanups in and around the canonicalizers, found while I was looking closely at this code. r? @lcnr
2 parents b68e16c + b222cf3 commit 44a5b55

28 files changed

+163
-154
lines changed

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ struct Canonicalizer<'cx, 'tcx> {
289289
/// Set to `None` to disable the resolution of inference variables.
290290
infcx: Option<&'cx InferCtxt<'tcx>>,
291291
tcx: TyCtxt<'tcx>,
292-
variables: SmallVec<[CanonicalVarKind<'tcx>; 8]>,
292+
var_kinds: SmallVec<[CanonicalVarKind<'tcx>; 8]>,
293293
query_state: &'cx mut OriginalQueryValues<'tcx>,
294294
// Note that indices is only used once `var_values` is big enough to be
295295
// heap-allocated.
@@ -507,7 +507,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
507507
{
508508
let base = Canonical {
509509
max_universe: ty::UniverseIndex::ROOT,
510-
variables: List::empty(),
510+
var_kinds: List::empty(),
511511
value: (),
512512
};
513513
Canonicalizer::canonicalize_with_base(
@@ -548,7 +548,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
548548
tcx,
549549
canonicalize_mode: canonicalize_region_mode,
550550
needs_canonical_flags,
551-
variables: SmallVec::from_slice(base.variables),
551+
var_kinds: SmallVec::from_slice(base.var_kinds),
552552
query_state,
553553
indices: FxHashMap::default(),
554554
sub_root_lookup_table: Default::default(),
@@ -569,16 +569,16 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
569569
// anymore.
570570
debug_assert!(!out_value.has_infer() && !out_value.has_placeholders());
571571

572-
let canonical_variables =
573-
tcx.mk_canonical_var_kinds(&canonicalizer.universe_canonicalized_variables());
572+
let canonical_var_kinds =
573+
tcx.mk_canonical_var_kinds(&canonicalizer.universe_canonicalized_var_kinds());
574574

575-
let max_universe = canonical_variables
575+
let max_universe = canonical_var_kinds
576576
.iter()
577577
.map(|cvar| cvar.universe())
578578
.max()
579579
.unwrap_or(ty::UniverseIndex::ROOT);
580580

581-
Canonical { max_universe, variables: canonical_variables, value: (base.value, out_value) }
581+
Canonical { max_universe, var_kinds: canonical_var_kinds, value: (base.value, out_value) }
582582
}
583583

584584
/// Creates a canonical variable replacing `kind` from the input,
@@ -590,7 +590,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
590590
var_kind: CanonicalVarKind<'tcx>,
591591
value: GenericArg<'tcx>,
592592
) -> BoundVar {
593-
let Canonicalizer { variables, query_state, indices, .. } = self;
593+
let Canonicalizer { var_kinds, query_state, indices, .. } = self;
594594

595595
let var_values = &mut query_state.var_values;
596596

@@ -607,7 +607,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
607607
}
608608
}
609609

610-
// This code is hot. `variables` and `var_values` are usually small
610+
// This code is hot. `var_kinds` and `var_values` are usually small
611611
// (fewer than 8 elements ~95% of the time). They are SmallVec's to
612612
// avoid allocations in those cases. We also don't use `indices` to
613613
// determine if a kind has been seen before until the limit of 8 has
@@ -620,10 +620,10 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
620620
BoundVar::new(idx)
621621
} else {
622622
// `kind` isn't present in `var_values`. Append it. Likewise
623-
// for `var_kind` and `variables`.
624-
variables.push(var_kind);
623+
// for `var_kind` and `var_kinds`.
624+
var_kinds.push(var_kind);
625625
var_values.push(value);
626-
assert_eq!(variables.len(), var_values.len());
626+
assert_eq!(var_kinds.len(), var_values.len());
627627

628628
// If `var_values` has become big enough to be heap-allocated,
629629
// fill up `indices` to facilitate subsequent lookups.
@@ -641,27 +641,27 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
641641
} else {
642642
// `var_values` is large. Do a hashmap search via `indices`.
643643
*indices.entry(value).or_insert_with(|| {
644-
variables.push(var_kind);
644+
var_kinds.push(var_kind);
645645
var_values.push(value);
646-
assert_eq!(variables.len(), var_values.len());
647-
BoundVar::new(variables.len() - 1)
646+
assert_eq!(var_kinds.len(), var_values.len());
647+
BoundVar::new(var_kinds.len() - 1)
648648
})
649649
}
650650
}
651651

652652
fn get_or_insert_sub_root(&mut self, vid: ty::TyVid) -> ty::BoundVar {
653653
let root_vid = self.infcx.unwrap().sub_unification_table_root_var(vid);
654654
let idx =
655-
*self.sub_root_lookup_table.entry(root_vid).or_insert_with(|| self.variables.len());
655+
*self.sub_root_lookup_table.entry(root_vid).or_insert_with(|| self.var_kinds.len());
656656
ty::BoundVar::from(idx)
657657
}
658658

659659
/// Replaces the universe indexes used in `var_values` with their index in
660660
/// `query_state.universe_map`. This minimizes the maximum universe used in
661661
/// the canonicalized value.
662-
fn universe_canonicalized_variables(self) -> SmallVec<[CanonicalVarKind<'tcx>; 8]> {
662+
fn universe_canonicalized_var_kinds(self) -> SmallVec<[CanonicalVarKind<'tcx>; 8]> {
663663
if self.query_state.universe_map.len() == 1 {
664-
return self.variables;
664+
return self.var_kinds;
665665
}
666666

667667
let reverse_universe_map: FxHashMap<ty::UniverseIndex, ty::UniverseIndex> = self
@@ -672,7 +672,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
672672
.map(|(idx, universe)| (*universe, ty::UniverseIndex::from_usize(idx)))
673673
.collect();
674674

675-
self.variables
675+
self.var_kinds
676676
.iter()
677677
.map(|&kind| match kind {
678678
CanonicalVarKind::Int | CanonicalVarKind::Float => {

‎compiler/rustc_infer/src/infer/canonical/instantiate.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<'tcx, V> Canonical<'tcx, V> {
4343
where
4444
T: TypeFoldable<TyCtxt<'tcx>>,
4545
{
46-
assert_eq!(self.variables.len(), var_values.len());
46+
assert_eq!(self.var_kinds.len(), var_values.len());
4747
let value = projection_fn(&self.value);
4848
instantiate_value(tcx, var_values, value)
4949
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl<'tcx> InferCtxt<'tcx> {
6868
.collect();
6969

7070
let var_values =
71-
CanonicalVarValues::instantiate(self.tcx, &canonical.variables, |var_values, info| {
71+
CanonicalVarValues::instantiate(self.tcx, &canonical.var_kinds, |var_values, info| {
7272
self.instantiate_canonical_var(span, info, &var_values, |ui| universes[ui])
7373
});
7474
let result = canonical.instantiate(self.tcx, &var_values);

‎compiler/rustc_infer/src/infer/canonical/query_response.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ impl<'tcx> InferCtxt<'tcx> {
430430
// result, then we can type the corresponding value from the
431431
// input. See the example above.
432432
let mut opt_values: IndexVec<BoundVar, Option<GenericArg<'tcx>>> =
433-
IndexVec::from_elem_n(None, query_response.variables.len());
433+
IndexVec::from_elem_n(None, query_response.var_kinds.len());
434434

435435
for (original_value, result_value) in iter::zip(&original_values.var_values, result_values)
436436
{
@@ -442,7 +442,7 @@ impl<'tcx> InferCtxt<'tcx> {
442442
// more involved. They are also a lot rarer than region variables.
443443
if let ty::Bound(index_kind, b) = *result_value.kind()
444444
&& !matches!(
445-
query_response.variables[b.var.as_usize()],
445+
query_response.var_kinds[b.var.as_usize()],
446446
CanonicalVarKind::Ty { .. }
447447
)
448448
{
@@ -472,8 +472,8 @@ impl<'tcx> InferCtxt<'tcx> {
472472
// given variable in the loop above, use that. Otherwise, use
473473
// a fresh inference variable.
474474
let tcx = self.tcx;
475-
let variables = query_response.variables;
476-
let var_values = CanonicalVarValues::instantiate(tcx, variables, |var_values, kind| {
475+
let var_kinds = query_response.var_kinds;
476+
let var_values = CanonicalVarValues::instantiate(tcx, var_kinds, |var_values, kind| {
477477
if kind.universe() != ty::UniverseIndex::ROOT {
478478
// A variable from inside a binder of the query. While ideally these shouldn't
479479
// exist at all, we have to deal with them for now.

‎compiler/rustc_middle/src/infer/canonical.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl<'tcx> CanonicalParamEnvCache<'tcx> {
169169
) {
170170
return Canonical {
171171
max_universe: ty::UniverseIndex::ROOT,
172-
variables: List::empty(),
172+
var_kinds: List::empty(),
173173
value: key,
174174
};
175175
}

‎compiler/rustc_middle/src/ty/structural_impls.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,18 +586,26 @@ impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Predicate<'tcx> {
586586
self,
587587
folder: &mut F,
588588
) -> Result<Self, F::Error> {
589+
// This method looks different to `Ty::try_super_fold_with` and `Const::super_fold_with`.
590+
// Why is that? `PredicateKind` provides little scope for optimized folding, unlike
591+
// `TyKind` and `ConstKind` (which have common variants that don't require recursive
592+
// `fold_with` calls on their fields). So we just derive the `TypeFoldable` impl for
593+
// `PredicateKind` and call it here because the derived code is as fast as hand-written
594+
// code would be.
589595
let new = self.kind().try_fold_with(folder)?;
590596
Ok(folder.cx().reuse_or_mk_predicate(self, new))
591597
}
592598

593599
fn super_fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
600+
// See comment in `Predicate::try_super_fold_with`.
594601
let new = self.kind().fold_with(folder);
595602
folder.cx().reuse_or_mk_predicate(self, new)
596603
}
597604
}
598605

599606
impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for ty::Predicate<'tcx> {
600607
fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
608+
// See comment in `Predicate::try_super_fold_with`.
601609
self.kind().visit_with(visitor)
602610
}
603611
}

0 commit comments

Comments
 (0)