Skip to content

Commit 51b5454

Browse files
committed
Derive traversable impls for ConstKind
1 parent bc4df58 commit 51b5454

File tree

7 files changed

+20
-62
lines changed

7 files changed

+20
-62
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ static_assert_size!(super::ConstKind<'_>, 32);
8080

8181
/// An inference variable for a const, for use in const generics.
8282
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Hash)]
83+
#[derive(TypeFoldable, TypeVisitable)]
84+
#[skip_traversal(
85+
but_impl_despite_trivial_because = "traversed generically in `rustc_type_ir::ConstKind<TyCtxt>`"
86+
)]
8387
pub enum InferConst {
8488
/// Infer the value of the const.
8589
Var(ty::ConstVid),

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

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_macros::{HashStable, TyDecodable, TyEncodable};
1717
///
1818
/// `ValTree` does not have this problem with representation, as it only contains integers or
1919
/// lists of (nested) `ValTree`.
20+
#[derive(TypeFoldable, TypeVisitable)]
2021
pub enum ValTree<'tcx> {
2122
/// integers, `bool`, `char` are represented as scalars.
2223
/// See the `ScalarInt` documentation for how `ScalarInt` guarantees that equal values

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,7 @@ impl<'tcx> OpaqueHiddenType<'tcx> {
15271527
/// regions/types/consts within the same universe simply have an unknown relationship to one
15281528
/// another.
15291529
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
1530-
#[derive(HashStable, TyEncodable, TyDecodable)]
1530+
#[derive(HashStable, TyEncodable, TyDecodable, TypeFoldable, TypeVisitable)]
15311531
pub struct Placeholder<T> {
15321532
pub universe: UniverseIndex,
15331533
pub bound: T,

compiler/rustc_middle/src/ty/structural_impls.rs

+3-57
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ use crate::mir::interpret;
77
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable};
88
use crate::ty::print::{with_no_trimmed_paths, FmtPrinter, Printer};
99
use crate::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
10-
use crate::ty::{
11-
self, noop_if_trivially_traversable, AliasTy, InferConst, Lift, Term, TermKind, Ty, TyCtxt,
12-
};
10+
use crate::ty::{self, AliasTy, InferConst, Lift, Term, TermKind, Ty, TyCtxt};
1311
use rustc_hir::def::Namespace;
1412
use rustc_span::source_map::Spanned;
1513
use rustc_type_ir::{ConstKind, DebugWithInfcx, InferCtxtLike, WithInfcx};
@@ -714,33 +712,7 @@ impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Const<'tcx> {
714712
folder: &mut F,
715713
) -> Result<Self, F::Error> {
716714
let ty = self.ty().try_fold_with(folder)?;
717-
let kind = match self.kind() {
718-
ConstKind::Param(p) => ConstKind::Param(noop_if_trivially_traversable!(
719-
p.try_fold_with::<TyCtxt<'tcx>>(folder)
720-
)?),
721-
ConstKind::Infer(i) => ConstKind::Infer(noop_if_trivially_traversable!(
722-
i.try_fold_with::<TyCtxt<'tcx>>(folder)
723-
)?),
724-
ConstKind::Bound(d, b) => ConstKind::Bound(
725-
noop_if_trivially_traversable!(d.try_fold_with::<TyCtxt<'tcx>>(folder))?,
726-
noop_if_trivially_traversable!(b.try_fold_with::<TyCtxt<'tcx>>(folder))?,
727-
),
728-
ConstKind::Placeholder(p) => ConstKind::Placeholder(noop_if_trivially_traversable!(
729-
p.try_fold_with::<TyCtxt<'tcx>>(folder)
730-
)?),
731-
ConstKind::Unevaluated(uv) => ConstKind::Unevaluated(noop_if_trivially_traversable!(
732-
uv.try_fold_with::<TyCtxt<'tcx>>(folder)
733-
)?),
734-
ConstKind::Value(v) => ConstKind::Value(noop_if_trivially_traversable!(
735-
v.try_fold_with::<TyCtxt<'tcx>>(folder)
736-
)?),
737-
ConstKind::Error(e) => ConstKind::Error(noop_if_trivially_traversable!(
738-
e.try_fold_with::<TyCtxt<'tcx>>(folder)
739-
)?),
740-
ConstKind::Expr(e) => ConstKind::Expr(noop_if_trivially_traversable!(
741-
e.try_fold_with::<TyCtxt<'tcx>>(folder)
742-
)?),
743-
};
715+
let kind = self.kind().try_fold_with(folder)?;
744716
if ty != self.ty() || kind != self.kind() {
745717
Ok(folder.interner().mk_ct_from_kind(kind, ty))
746718
} else {
@@ -755,33 +727,7 @@ impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for ty::Const<'tcx> {
755727
visitor: &mut V,
756728
) -> ControlFlow<V::BreakTy> {
757729
self.ty().visit_with(visitor)?;
758-
match &self.kind() {
759-
ConstKind::Param(p) => {
760-
noop_if_trivially_traversable!(p.visit_with::<TyCtxt<'tcx>>(visitor))
761-
}
762-
ConstKind::Infer(i) => {
763-
noop_if_trivially_traversable!(i.visit_with::<TyCtxt<'tcx>>(visitor))
764-
}
765-
ConstKind::Bound(d, b) => {
766-
noop_if_trivially_traversable!(d.visit_with::<TyCtxt<'tcx>>(visitor))?;
767-
noop_if_trivially_traversable!(b.visit_with::<TyCtxt<'tcx>>(visitor))
768-
}
769-
ConstKind::Placeholder(p) => {
770-
noop_if_trivially_traversable!(p.visit_with::<TyCtxt<'tcx>>(visitor))
771-
}
772-
ConstKind::Unevaluated(uv) => {
773-
noop_if_trivially_traversable!(uv.visit_with::<TyCtxt<'tcx>>(visitor))
774-
}
775-
ConstKind::Value(v) => {
776-
noop_if_trivially_traversable!(v.visit_with::<TyCtxt<'tcx>>(visitor))
777-
}
778-
ConstKind::Error(e) => {
779-
noop_if_trivially_traversable!(e.visit_with::<TyCtxt<'tcx>>(visitor))
780-
}
781-
ConstKind::Expr(e) => {
782-
noop_if_trivially_traversable!(e.visit_with::<TyCtxt<'tcx>>(visitor))
783-
}
784-
}
730+
self.kind().visit_with(visitor)
785731
}
786732
}
787733

compiler/rustc_middle/src/ty/sty.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,10 @@ impl<'tcx> ParamTy {
14451445
}
14461446

14471447
#[derive(Copy, Clone, Hash, TyEncodable, TyDecodable, Eq, PartialEq, Ord, PartialOrd)]
1448-
#[derive(HashStable)]
1448+
#[derive(HashStable, TypeFoldable, TypeVisitable)]
1449+
#[skip_traversal(
1450+
but_impl_despite_trivial_because = "traversed generically in `rustc_type_ir::ConstKind<TyCtxt>`"
1451+
)]
14491452
pub struct ParamConst {
14501453
pub index: u32,
14511454
pub name: Symbol,
@@ -1618,7 +1621,8 @@ impl Atom for RegionVid {
16181621
}
16191622

16201623
rustc_index::newtype_index! {
1621-
#[derive(HashStable)]
1624+
#[derive(HashStable, TypeFoldable, TypeVisitable)]
1625+
#[skip_traversal(but_impl_despite_trivial_because = "traversed generically in `rustc_type_ir::ConstKind<TyCtxt>`")]
16221626
#[debug_format = "{}"]
16231627
pub struct BoundVar {}
16241628
}

compiler/rustc_span/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2247,7 +2247,10 @@ where
22472247
/// The `()` field is necessary: it is non-`pub`, which means values of this
22482248
/// type cannot be constructed outside of this crate.
22492249
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
2250-
#[derive(HashStable_Generic)]
2250+
#[derive(HashStable_Generic, TypeFoldable, TypeVisitable)]
2251+
#[skip_traversal(
2252+
but_impl_despite_trivial_because = "traversed generically in `rustc_type_ir::ConstKind<TyCtxt>`"
2253+
)]
22512254
pub struct ErrorGuaranteed(());
22522255

22532256
impl ErrorGuaranteed {

compiler/rustc_type_ir/src/const_kind.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{DebruijnIndex, DebugWithInfcx, HashStableContext, InferCtxtLike, Int
77
use self::ConstKind::*;
88

99
/// Represents a constant in Rust.
10-
#[derive(derivative::Derivative)]
10+
#[derive(derivative::Derivative, TypeFoldable, TypeVisitable)]
1111
#[derivative(
1212
Clone(bound = ""),
1313
PartialOrd(bound = ""),

0 commit comments

Comments
 (0)