Skip to content

Commit d0278f9

Browse files
committed
Add comments about predicate folding.
This explains why the predicate folding code looks different to the ty/const folding code, something I was wondering.
1 parent 9533030 commit d0278f9

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

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)