Skip to content

Commit 9d93068

Browse files
committed
Replace more manual TypeFoldable and TypeVisitable impls with derives
1 parent a5ab8da commit 9d93068

File tree

5 files changed

+15
-109
lines changed

5 files changed

+15
-109
lines changed

compiler/rustc_middle/src/mir/interpret/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub use self::pointer::{Pointer, PointerArithmetic, Provenance};
137137
/// - A constant
138138
/// - A static
139139
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, TyEncodable, TyDecodable)]
140-
#[derive(HashStable, Lift)]
140+
#[derive(HashStable, Lift, TypeFoldable, TypeVisitable)]
141141
pub struct GlobalId<'tcx> {
142142
/// For a constant or static, the `Instance` of the item itself.
143143
/// For a promoted global, the `Instance` of the function they belong to.

compiler/rustc_middle/src/ty/consts/kind.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<'tcx, P: Default> Unevaluated<'tcx, P> {
5050

5151
/// Represents a constant in Rust.
5252
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)]
53-
#[derive(Hash, HashStable)]
53+
#[derive(Hash, HashStable, TypeFoldable, TypeVisitable)]
5454
pub enum ConstKind<'tcx> {
5555
/// A const generic parameter.
5656
Param(ty::ParamConst),

compiler/rustc_middle/src/ty/instance.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::fmt;
2020
/// simply couples a potentially generic `InstanceDef` with some substs, and codegen and const eval
2121
/// will do all required substitution as they run.
2222
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable)]
23-
#[derive(HashStable, Lift)]
23+
#[derive(HashStable, Lift, TypeFoldable, TypeVisitable)]
2424
pub struct Instance<'tcx> {
2525
pub def: InstanceDef<'tcx>,
2626
pub substs: SubstsRef<'tcx>,

compiler/rustc_middle/src/ty/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1526,16 +1526,15 @@ impl<'tcx> TypeFoldable<'tcx> for ParamEnv<'tcx> {
15261526
Ok(ParamEnv::new(
15271527
self.caller_bounds().try_fold_with(folder)?,
15281528
self.reveal().try_fold_with(folder)?,
1529-
self.constness().try_fold_with(folder)?,
1529+
self.constness(),
15301530
))
15311531
}
15321532
}
15331533

15341534
impl<'tcx> TypeVisitable<'tcx> for ParamEnv<'tcx> {
15351535
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
15361536
self.caller_bounds().visit_with(visitor)?;
1537-
self.reveal().visit_with(visitor)?;
1538-
self.constness().visit_with(visitor)
1537+
self.reveal().visit_with(visitor)
15391538
}
15401539
}
15411540

compiler/rustc_middle/src/ty/structural_impls.rs

+10-103
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::ty::print::{with_no_trimmed_paths, FmtPrinter, Printer};
99
use crate::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
1010
use crate::ty::{self, InferConst, Lift, Term, TermKind, Ty, TyCtxt};
1111
use rustc_data_structures::functor::IdFunctor;
12-
use rustc_hir as hir;
1312
use rustc_hir::def::Namespace;
1413
use rustc_index::vec::{Idx, IndexVec};
1514

@@ -241,6 +240,16 @@ TrivialTypeTraversalAndLiftImpls! {
241240
Field,
242241
interpret::Scalar,
243242
rustc_target::abi::Size,
243+
ty::DelaySpanBugEmitted,
244+
rustc_type_ir::DebruijnIndex,
245+
ty::BoundVar,
246+
ty::Placeholder<ty::BoundVar>,
247+
}
248+
249+
TrivialTypeTraversalAndLiftImpls! {
250+
for<'tcx> {
251+
ty::ValTree<'tcx>,
252+
}
244253
}
245254

246255
///////////////////////////////////////////////////////////////////////////
@@ -613,68 +622,6 @@ impl<'tcx> TypeVisitable<'tcx> for &'tcx ty::List<ProjectionKind> {
613622
}
614623
}
615624

616-
impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
617-
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
618-
use crate::ty::InstanceDef::*;
619-
Ok(Self {
620-
substs: self.substs.try_fold_with(folder)?,
621-
def: match self.def {
622-
Item(def) => Item(def.try_fold_with(folder)?),
623-
VTableShim(did) => VTableShim(did.try_fold_with(folder)?),
624-
ReifyShim(did) => ReifyShim(did.try_fold_with(folder)?),
625-
Intrinsic(did) => Intrinsic(did.try_fold_with(folder)?),
626-
FnPtrShim(did, ty) => {
627-
FnPtrShim(did.try_fold_with(folder)?, ty.try_fold_with(folder)?)
628-
}
629-
Virtual(did, i) => Virtual(did.try_fold_with(folder)?, i),
630-
ClosureOnceShim { call_once, track_caller } => {
631-
ClosureOnceShim { call_once: call_once.try_fold_with(folder)?, track_caller }
632-
}
633-
DropGlue(did, ty) => {
634-
DropGlue(did.try_fold_with(folder)?, ty.try_fold_with(folder)?)
635-
}
636-
CloneShim(did, ty) => {
637-
CloneShim(did.try_fold_with(folder)?, ty.try_fold_with(folder)?)
638-
}
639-
},
640-
})
641-
}
642-
}
643-
644-
impl<'tcx> TypeVisitable<'tcx> for ty::instance::Instance<'tcx> {
645-
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
646-
use crate::ty::InstanceDef::*;
647-
self.substs.visit_with(visitor)?;
648-
match self.def {
649-
Item(def) => def.visit_with(visitor),
650-
VTableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
651-
did.visit_with(visitor)
652-
}
653-
FnPtrShim(did, ty) | CloneShim(did, ty) => {
654-
did.visit_with(visitor)?;
655-
ty.visit_with(visitor)
656-
}
657-
DropGlue(did, ty) => {
658-
did.visit_with(visitor)?;
659-
ty.visit_with(visitor)
660-
}
661-
ClosureOnceShim { call_once, track_caller: _ } => call_once.visit_with(visitor),
662-
}
663-
}
664-
}
665-
666-
impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
667-
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
668-
Ok(Self { instance: self.instance.try_fold_with(folder)?, promoted: self.promoted })
669-
}
670-
}
671-
672-
impl<'tcx> TypeVisitable<'tcx> for interpret::GlobalId<'tcx> {
673-
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
674-
self.instance.visit_with(visitor)
675-
}
676-
}
677-
678625
impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
679626
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
680627
folder.try_fold_ty(self)
@@ -902,34 +849,6 @@ impl<'tcx> TypeSuperVisitable<'tcx> for ty::Const<'tcx> {
902849
}
903850
}
904851

905-
impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
906-
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
907-
Ok(match self {
908-
ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.try_fold_with(folder)?),
909-
ty::ConstKind::Param(p) => ty::ConstKind::Param(p.try_fold_with(folder)?),
910-
ty::ConstKind::Unevaluated(uv) => ty::ConstKind::Unevaluated(uv.try_fold_with(folder)?),
911-
ty::ConstKind::Value(_)
912-
| ty::ConstKind::Bound(..)
913-
| ty::ConstKind::Placeholder(..)
914-
| ty::ConstKind::Error(_) => self,
915-
})
916-
}
917-
}
918-
919-
impl<'tcx> TypeVisitable<'tcx> for ty::ConstKind<'tcx> {
920-
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
921-
match *self {
922-
ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
923-
ty::ConstKind::Param(p) => p.visit_with(visitor),
924-
ty::ConstKind::Unevaluated(uv) => uv.visit_with(visitor),
925-
ty::ConstKind::Value(_)
926-
| ty::ConstKind::Bound(..)
927-
| ty::ConstKind::Placeholder(_)
928-
| ty::ConstKind::Error(_) => ControlFlow::CONTINUE,
929-
}
930-
}
931-
}
932-
933852
impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
934853
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _folder: &mut F) -> Result<Self, F::Error> {
935854
Ok(self)
@@ -984,15 +903,3 @@ impl<'tcx> TypeVisitable<'tcx> for ty::Unevaluated<'tcx, ()> {
984903
self.expand().visit_with(visitor)
985904
}
986905
}
987-
988-
impl<'tcx> TypeFoldable<'tcx> for hir::Constness {
989-
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _: &mut F) -> Result<Self, F::Error> {
990-
Ok(self)
991-
}
992-
}
993-
994-
impl<'tcx> TypeVisitable<'tcx> for hir::Constness {
995-
fn visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
996-
ControlFlow::CONTINUE
997-
}
998-
}

0 commit comments

Comments
 (0)