Skip to content

Commit df34db9

Browse files
committed
Auto merge of #101858 - oli-obk:lift_derive, r=lcnr
derive various impls instead of hand-rolling them r? `@lcnr` This may not have been what you asked for in 964b97e#r84051418 but I got carried away while following the compiler team meeting today.
2 parents 35a0407 + c6fcb1c commit df34db9

17 files changed

+90
-887
lines changed

compiler/rustc_middle/src/infer/canonical.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ pub struct Canonical<'tcx, V> {
4444

4545
pub type CanonicalVarInfos<'tcx> = &'tcx List<CanonicalVarInfo<'tcx>>;
4646

47+
impl<'tcx> ty::TypeFoldable<'tcx> for CanonicalVarInfos<'tcx> {
48+
fn try_fold_with<F: ty::FallibleTypeFolder<'tcx>>(
49+
self,
50+
folder: &mut F,
51+
) -> Result<Self, F::Error> {
52+
ty::util::fold_list(self, folder, |tcx, v| tcx.intern_canonical_var_infos(v))
53+
}
54+
}
55+
4756
/// A set of values corresponding to the canonical variables from some
4857
/// `Canonical`. You can give these values to
4958
/// `canonical_value.substitute` to substitute them into the canonical
@@ -90,6 +99,7 @@ impl<'tcx> Default for OriginalQueryValues<'tcx> {
9099
/// a copy of the canonical value in some other inference context,
91100
/// with fresh inference variables replacing the canonical values.
92101
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, TyDecodable, TyEncodable, HashStable)]
102+
#[derive(TypeFoldable, TypeVisitable)]
93103
pub struct CanonicalVarInfo<'tcx> {
94104
pub kind: CanonicalVarKind<'tcx>,
95105
}
@@ -115,6 +125,7 @@ impl<'tcx> CanonicalVarInfo<'tcx> {
115125
/// in the type-theory sense of the term -- i.e., a "meta" type system
116126
/// that analyzes type-like values.
117127
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, TyDecodable, TyEncodable, HashStable)]
128+
#[derive(TypeFoldable, TypeVisitable)]
118129
pub enum CanonicalVarKind<'tcx> {
119130
/// Some kind of type inference variable.
120131
Ty(CanonicalTyVarKind),
@@ -299,14 +310,7 @@ pub type QueryOutlivesConstraint<'tcx> = (
299310
TrivialTypeTraversalAndLiftImpls! {
300311
for <'tcx> {
301312
crate::infer::canonical::Certainty,
302-
crate::infer::canonical::CanonicalVarInfo<'tcx>,
303-
crate::infer::canonical::CanonicalVarKind<'tcx>,
304-
}
305-
}
306-
307-
TrivialTypeTraversalImpls! {
308-
for <'tcx> {
309-
crate::infer::canonical::CanonicalVarInfos<'tcx>,
313+
crate::infer::canonical::CanonicalTyVarKind,
310314
}
311315
}
312316

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/mir/interpret/value.rs

+2-18
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_apfloat::{
88
use rustc_macros::HashStable;
99
use rustc_target::abi::{HasDataLayout, Size};
1010

11-
use crate::ty::{Lift, ParamEnv, ScalarInt, Ty, TyCtxt};
11+
use crate::ty::{ParamEnv, ScalarInt, Ty, TyCtxt};
1212

1313
use super::{
1414
AllocId, AllocRange, ConstAllocation, InterpResult, Pointer, PointerArithmetic, Provenance,
@@ -27,7 +27,7 @@ pub struct ConstAlloc<'tcx> {
2727
/// Represents a constant value in Rust. `Scalar` and `Slice` are optimizations for
2828
/// array length computations, enum discriminants and the pattern matching logic.
2929
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Hash)]
30-
#[derive(HashStable)]
30+
#[derive(HashStable, Lift)]
3131
pub enum ConstValue<'tcx> {
3232
/// Used only for types with `layout::abi::Scalar` ABI.
3333
///
@@ -53,22 +53,6 @@ pub enum ConstValue<'tcx> {
5353
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
5454
static_assert_size!(ConstValue<'_>, 32);
5555

56-
impl<'a, 'tcx> Lift<'tcx> for ConstValue<'a> {
57-
type Lifted = ConstValue<'tcx>;
58-
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ConstValue<'tcx>> {
59-
Some(match self {
60-
ConstValue::Scalar(s) => ConstValue::Scalar(s),
61-
ConstValue::ZeroSized => ConstValue::ZeroSized,
62-
ConstValue::Slice { data, start, end } => {
63-
ConstValue::Slice { data: tcx.lift(data)?, start, end }
64-
}
65-
ConstValue::ByRef { alloc, offset } => {
66-
ConstValue::ByRef { alloc: tcx.lift(alloc)?, offset }
67-
}
68-
})
69-
}
70-
}
71-
7256
impl<'tcx> ConstValue<'tcx> {
7357
#[inline]
7458
pub fn try_to_scalar(&self) -> Option<Scalar<AllocId>> {

compiler/rustc_middle/src/mir/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
20282028
/// particular, one must be wary of `NaN`!
20292029
20302030
#[derive(Clone, Copy, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)]
2031+
#[derive(TypeFoldable, TypeVisitable)]
20312032
pub struct Constant<'tcx> {
20322033
pub span: Span,
20332034

@@ -2551,8 +2552,6 @@ impl UserTypeProjection {
25512552
}
25522553
}
25532554

2554-
TrivialTypeTraversalAndLiftImpls! { ProjectionKind, }
2555-
25562555
impl<'tcx> TypeFoldable<'tcx> for UserTypeProjection {
25572556
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
25582557
Ok(UserTypeProjection {

compiler/rustc_middle/src/mir/syntax.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ pub struct CopyNonOverlapping<'tcx> {
488488
/// must also be `cleanup`. This is a part of the type system and checked statically, so it is
489489
/// still an error to have such an edge in the CFG even if it's known that it won't be taken at
490490
/// runtime.
491-
#[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq)]
491+
#[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq, TypeFoldable, TypeVisitable)]
492492
pub enum TerminatorKind<'tcx> {
493493
/// Block has one successor; we continue execution there.
494494
Goto { target: BasicBlock },
@@ -741,7 +741,7 @@ pub enum TerminatorKind<'tcx> {
741741
}
742742

743743
/// Information about an assertion failure.
744-
#[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq)]
744+
#[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq, TypeFoldable, TypeVisitable)]
745745
pub enum AssertKind<O> {
746746
BoundsCheck { len: O, index: O },
747747
Overflow(BinOp, O, O),
@@ -863,7 +863,7 @@ pub type AssertMessage<'tcx> = AssertKind<Operand<'tcx>>;
863863
///
864864
/// Rust currently requires that every place obey those two rules. This is checked by MIRI and taken
865865
/// advantage of by codegen (via `gep inbounds`). That is possibly subject to change.
866-
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, HashStable)]
866+
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, HashStable, TypeFoldable, TypeVisitable)]
867867
pub struct Place<'tcx> {
868868
pub local: Local,
869869

@@ -872,7 +872,7 @@ pub struct Place<'tcx> {
872872
}
873873

874874
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
875-
#[derive(TyEncodable, TyDecodable, HashStable)]
875+
#[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
876876
pub enum ProjectionElem<V, T> {
877877
Deref,
878878
Field(Field, T),
@@ -955,7 +955,7 @@ pub type PlaceElem<'tcx> = ProjectionElem<Local, Ty<'tcx>>;
955955
/// **Needs clarifiation:** Is loading a place that has its variant index set well-formed? Miri
956956
/// currently implements it, but it seems like this may be something to check against in the
957957
/// validator.
958-
#[derive(Clone, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)]
958+
#[derive(Clone, PartialEq, TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
959959
pub enum Operand<'tcx> {
960960
/// Creates a value by loading the given place.
961961
///
@@ -986,7 +986,7 @@ pub enum Operand<'tcx> {
986986
/// Computing any rvalue begins by evaluating the places and operands in some order (**Needs
987987
/// clarification**: Which order?). These are then used to produce a "value" - the same kind of
988988
/// value that an [`Operand`] produces.
989-
#[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq)]
989+
#[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq, TypeFoldable, TypeVisitable)]
990990
pub enum Rvalue<'tcx> {
991991
/// Yields the operand unchanged
992992
Use(Operand<'tcx>),
@@ -1146,6 +1146,7 @@ pub enum CastKind {
11461146
}
11471147

11481148
#[derive(Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
1149+
#[derive(TypeFoldable, TypeVisitable)]
11491150
pub enum AggregateKind<'tcx> {
11501151
/// The type is of the element
11511152
Array(Ty<'tcx>),

compiler/rustc_middle/src/mir/terminator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<'a> Iterator for SwitchTargetsIter<'a> {
102102

103103
impl<'a> ExactSizeIterator for SwitchTargetsIter<'a> {}
104104

105-
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
105+
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
106106
pub struct Terminator<'tcx> {
107107
pub source_info: SourceInfo,
108108
pub kind: TerminatorKind<'tcx>,

0 commit comments

Comments
 (0)