Skip to content

Commit da6f90a

Browse files
committed
Derive traversable impls for ConstKind
1 parent 09db709 commit da6f90a

File tree

3 files changed

+11
-54
lines changed

3 files changed

+11
-54
lines changed

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 3 additions & 47 deletions
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_traversal_if_boring, 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_target::abi::TyAndLayout;
1513
use rustc_type_ir::{ConstKind, DebugWithInfcx, InferCtxtLike, WithInfcx};
@@ -721,33 +719,7 @@ impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Const<'tcx> {
721719
folder: &mut F,
722720
) -> Result<Self, F::Error> {
723721
let ty = self.ty().try_fold_with(folder)?;
724-
let kind = match self.kind() {
725-
ConstKind::Param(p) => {
726-
ConstKind::Param(noop_traversal_if_boring!(p.try_fold_with(folder))?)
727-
}
728-
ConstKind::Infer(i) => {
729-
ConstKind::Infer(noop_traversal_if_boring!(i.try_fold_with(folder))?)
730-
}
731-
ConstKind::Bound(d, b) => ConstKind::Bound(
732-
noop_traversal_if_boring!(d.try_fold_with(folder))?,
733-
noop_traversal_if_boring!(b.try_fold_with(folder))?,
734-
),
735-
ConstKind::Placeholder(p) => {
736-
ConstKind::Placeholder(noop_traversal_if_boring!(p.try_fold_with(folder))?)
737-
}
738-
ConstKind::Unevaluated(uv) => {
739-
ConstKind::Unevaluated(noop_traversal_if_boring!(uv.try_fold_with(folder))?)
740-
}
741-
ConstKind::Value(v) => {
742-
ConstKind::Value(noop_traversal_if_boring!(v.try_fold_with(folder))?)
743-
}
744-
ConstKind::Error(e) => {
745-
ConstKind::Error(noop_traversal_if_boring!(e.try_fold_with(folder))?)
746-
}
747-
ConstKind::Expr(e) => {
748-
ConstKind::Expr(noop_traversal_if_boring!(e.try_fold_with(folder))?)
749-
}
750-
};
722+
let kind = self.kind().try_fold_with(folder)?;
751723
if ty != self.ty() || kind != self.kind() {
752724
Ok(folder.interner().mk_ct_from_kind(kind, ty))
753725
} else {
@@ -762,23 +734,7 @@ impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for ty::Const<'tcx> {
762734
visitor: &mut V,
763735
) -> ControlFlow<V::BreakTy> {
764736
self.ty().visit_with(visitor)?;
765-
match &self.kind() {
766-
ConstKind::Param(p) => noop_traversal_if_boring!(p.visit_with(visitor)),
767-
ConstKind::Infer(i) => noop_traversal_if_boring!(i.visit_with(visitor)),
768-
ConstKind::Bound(d, b) => {
769-
noop_traversal_if_boring!(d.visit_with(visitor))?;
770-
noop_traversal_if_boring!(b.visit_with(visitor))
771-
}
772-
ConstKind::Placeholder(p) => {
773-
noop_traversal_if_boring!(p.visit_with(visitor))
774-
}
775-
ConstKind::Unevaluated(uv) => {
776-
noop_traversal_if_boring!(uv.visit_with(visitor))
777-
}
778-
ConstKind::Value(v) => noop_traversal_if_boring!(v.visit_with(visitor)),
779-
ConstKind::Error(e) => noop_traversal_if_boring!(e.visit_with(visitor)),
780-
ConstKind::Expr(e) => noop_traversal_if_boring!(e.visit_with(visitor)),
781-
}
737+
self.kind().visit_with(visitor)
782738
}
783739
}
784740

compiler/rustc_type_ir/src/const_kind.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
use self::ConstKind::*;
1212

1313
/// Represents a constant in Rust.
14-
#[derive(derivative::Derivative)]
14+
#[derive(derivative::Derivative, TypeFoldable, TypeVisitable)]
1515
#[derivative(
1616
Clone(bound = ""),
1717
PartialOrd(bound = ""),
@@ -22,28 +22,28 @@ use self::ConstKind::*;
2222
)]
2323
pub enum ConstKind<I: Interner> {
2424
/// A const generic parameter.
25-
Param(I::ParamConst),
25+
Param(#[skip_traversal(because_boring)] I::ParamConst),
2626

2727
/// Infer the value of the const.
28-
Infer(I::InferConst),
28+
Infer(#[skip_traversal(because_boring)] I::InferConst),
2929

3030
/// Bound const variable, used only when preparing a trait query.
31-
Bound(DebruijnIndex, I::BoundConst),
31+
Bound(DebruijnIndex, #[skip_traversal(because_boring)] I::BoundConst),
3232

3333
/// A placeholder const - universally quantified higher-ranked const.
34-
Placeholder(I::PlaceholderConst),
34+
Placeholder(#[skip_traversal(because_boring)] I::PlaceholderConst),
3535

3636
/// An unnormalized const item such as an anon const or assoc const or free const item.
3737
/// Right now anything other than anon consts does not actually work properly but this
3838
/// should
3939
Unevaluated(I::AliasConst),
4040

4141
/// Used to hold computed value.
42-
Value(I::ValueConst),
42+
Value(#[skip_traversal(because_boring)] I::ValueConst),
4343

4444
/// A placeholder for a const which could not be computed; this is
4545
/// propagated to avoid useless error messages.
46-
Error(I::ErrorGuaranteed),
46+
Error(#[skip_traversal(because_boring)] I::ErrorGuaranteed),
4747

4848
/// Unevaluated non-const-item, used by `feature(generic_const_exprs)` to represent
4949
/// const arguments such as `N + 1` or `foo(N)`

compiler/rustc_type_ir/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
extern crate bitflags;
1616
#[macro_use]
1717
extern crate rustc_macros;
18+
extern crate self as rustc_type_ir;
1819

1920
use std::fmt;
2021
use std::hash::Hash;

0 commit comments

Comments
 (0)