Skip to content

Commit 94cc5bb

Browse files
committed
Streamline const folding/visiting.
Type folders can only modify a few "types of interest": `Binder`, `Ty`, `Predicate`, `Clauses`, `Region`, `Const`. Likewise for type visitors, but they can also visit errors (via `ErrorGuaranteed`). Currently the impls of `try_super_fold_with`, `super_fold_with`, and `super_visit_with` do more than they need to -- they fold/visit values that cannot contain any types of interest. This commit removes those unnecessary fold/visit operations, which makes these impls more similar to the impls for `Ty`. It also removes the now-unnecessary derived impls for the no-longer-visited types.
1 parent 606dcc0 commit 94cc5bb

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ TrivialLiftImpls! {
232232
crate::mir::Promoted,
233233
crate::mir::interpret::AllocId,
234234
crate::mir::interpret::Scalar,
235+
crate::ty::ParamConst,
235236
rustc_abi::ExternAbi,
236237
rustc_abi::Size,
237238
rustc_hir::Safety,
@@ -271,10 +272,6 @@ TrivialTypeTraversalImpls! {
271272
crate::ty::AssocItem,
272273
crate::ty::AssocKind,
273274
crate::ty::BoundRegion,
274-
crate::ty::BoundVar,
275-
crate::ty::InferConst,
276-
crate::ty::Placeholder<crate::ty::BoundRegion>,
277-
crate::ty::Placeholder<ty::BoundVar>,
278275
crate::ty::UserTypeAnnotationIndex,
279276
crate::ty::ValTree<'tcx>,
280277
crate::ty::abstract_const::NotConstEvaluatable,
@@ -302,9 +299,8 @@ TrivialTypeTraversalImpls! {
302299
// interners).
303300
TrivialTypeTraversalAndLiftImpls! {
304301
// tidy-alphabetical-start
305-
crate::ty::ParamConst,
306302
crate::ty::ParamTy,
307-
crate::ty::Placeholder<crate::ty::BoundTy>,
303+
crate::ty::PlaceholderType,
308304
crate::ty::instance::ReifyReason,
309305
rustc_hir::def_id::DefId,
310306
// tidy-alphabetical-end
@@ -673,30 +669,30 @@ impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Const<'tcx> {
673669
folder: &mut F,
674670
) -> Result<Self, F::Error> {
675671
let kind = match self.kind() {
676-
ConstKind::Param(p) => ConstKind::Param(p.try_fold_with(folder)?),
677-
ConstKind::Infer(i) => ConstKind::Infer(i.try_fold_with(folder)?),
678-
ConstKind::Bound(d, b) => {
679-
ConstKind::Bound(d.try_fold_with(folder)?, b.try_fold_with(folder)?)
680-
}
681-
ConstKind::Placeholder(p) => ConstKind::Placeholder(p.try_fold_with(folder)?),
682672
ConstKind::Unevaluated(uv) => ConstKind::Unevaluated(uv.try_fold_with(folder)?),
683673
ConstKind::Value(v) => ConstKind::Value(v.try_fold_with(folder)?),
684-
ConstKind::Error(e) => ConstKind::Error(e.try_fold_with(folder)?),
685674
ConstKind::Expr(e) => ConstKind::Expr(e.try_fold_with(folder)?),
675+
676+
ConstKind::Param(_)
677+
| ConstKind::Infer(_)
678+
| ConstKind::Bound(..)
679+
| ConstKind::Placeholder(_)
680+
| ConstKind::Error(_) => return Ok(self),
686681
};
687682
if kind != self.kind() { Ok(folder.cx().mk_ct_from_kind(kind)) } else { Ok(self) }
688683
}
689684

690685
fn super_fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
691686
let kind = match self.kind() {
692-
ConstKind::Param(p) => ConstKind::Param(p.fold_with(folder)),
693-
ConstKind::Infer(i) => ConstKind::Infer(i.fold_with(folder)),
694-
ConstKind::Bound(d, b) => ConstKind::Bound(d.fold_with(folder), b.fold_with(folder)),
695-
ConstKind::Placeholder(p) => ConstKind::Placeholder(p.fold_with(folder)),
696687
ConstKind::Unevaluated(uv) => ConstKind::Unevaluated(uv.fold_with(folder)),
697688
ConstKind::Value(v) => ConstKind::Value(v.fold_with(folder)),
698-
ConstKind::Error(e) => ConstKind::Error(e.fold_with(folder)),
699689
ConstKind::Expr(e) => ConstKind::Expr(e.fold_with(folder)),
690+
691+
ConstKind::Param(_)
692+
| ConstKind::Infer(_)
693+
| ConstKind::Bound(..)
694+
| ConstKind::Placeholder(_)
695+
| ConstKind::Error(_) => return self,
700696
};
701697
if kind != self.kind() { folder.cx().mk_ct_from_kind(kind) } else { self }
702698
}
@@ -705,17 +701,15 @@ impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Const<'tcx> {
705701
impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for ty::Const<'tcx> {
706702
fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
707703
match self.kind() {
708-
ConstKind::Param(p) => p.visit_with(visitor),
709-
ConstKind::Infer(i) => i.visit_with(visitor),
710-
ConstKind::Bound(d, b) => {
711-
try_visit!(d.visit_with(visitor));
712-
b.visit_with(visitor)
713-
}
714-
ConstKind::Placeholder(p) => p.visit_with(visitor),
715704
ConstKind::Unevaluated(uv) => uv.visit_with(visitor),
716705
ConstKind::Value(v) => v.visit_with(visitor),
717-
ConstKind::Error(e) => e.visit_with(visitor),
718706
ConstKind::Expr(e) => e.visit_with(visitor),
707+
ConstKind::Error(e) => e.visit_with(visitor),
708+
709+
ConstKind::Param(_)
710+
| ConstKind::Infer(_)
711+
| ConstKind::Bound(..)
712+
| ConstKind::Placeholder(_) => V::Result::output(),
719713
}
720714
}
721715
}

0 commit comments

Comments
 (0)