Skip to content

Commit e4b9625

Browse files
committed
Add #[derive(TypeVisitable)]
1 parent bca8949 commit e4b9625

File tree

31 files changed

+183
-221
lines changed

31 files changed

+183
-221
lines changed

compiler/rustc_infer/src/infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ pub struct InferCtxt<'a, 'tcx> {
318318
}
319319

320320
/// See the `error_reporting` module for more details.
321-
#[derive(Clone, Copy, Debug, PartialEq, Eq, TypeFoldable)]
321+
#[derive(Clone, Copy, Debug, PartialEq, Eq, TypeFoldable, TypeVisitable)]
322322
pub enum ValuePairs<'tcx> {
323323
Regions(ExpectedFound<ty::Region<'tcx>>),
324324
Terms(ExpectedFound<ty::Term<'tcx>>),

compiler/rustc_infer/src/infer/region_constraints/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ pub struct Verify<'tcx> {
165165
pub bound: VerifyBound<'tcx>,
166166
}
167167

168-
#[derive(Copy, Clone, PartialEq, Eq, Hash, TypeFoldable)]
168+
#[derive(Copy, Clone, PartialEq, Eq, Hash, TypeFoldable, TypeVisitable)]
169169
pub enum GenericKind<'tcx> {
170170
Param(ty::ParamTy),
171171
Projection(ty::ProjectionTy<'tcx>),
@@ -272,7 +272,7 @@ pub enum VerifyBound<'tcx> {
272272
/// }
273273
/// }
274274
/// ```
275-
#[derive(Debug, Copy, Clone, TypeFoldable)]
275+
#[derive(Debug, Copy, Clone, TypeFoldable, TypeVisitable)]
276276
pub struct VerifyIfEq<'tcx> {
277277
/// Type which must match the generic `G`
278278
pub ty: Ty<'tcx>,

compiler/rustc_infer/src/traits/project.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub struct MismatchedProjectionTypes<'tcx> {
2020
pub err: ty::error::TypeError<'tcx>,
2121
}
2222

23-
#[derive(Clone, TypeFoldable)]
23+
#[derive(Clone, TypeFoldable, TypeVisitable)]
2424
pub struct Normalized<'tcx, T> {
2525
pub value: T,
2626
pub obligations: Vec<PredicateObligation<'tcx>>,

compiler/rustc_macros/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod query;
1818
mod serialize;
1919
mod symbols;
2020
mod type_foldable;
21+
mod type_visitable;
2122

2223
#[proc_macro]
2324
pub fn rustc_queries(input: TokenStream) -> TokenStream {
@@ -121,6 +122,7 @@ decl_derive!([TyEncodable] => serialize::type_encodable_derive);
121122
decl_derive!([MetadataDecodable] => serialize::meta_decodable_derive);
122123
decl_derive!([MetadataEncodable] => serialize::meta_encodable_derive);
123124
decl_derive!([TypeFoldable, attributes(type_foldable)] => type_foldable::type_foldable_derive);
125+
decl_derive!([TypeVisitable, attributes(type_visitable)] => type_visitable::type_visitable_derive);
124126
decl_derive!([Lift, attributes(lift)] => lift::lift_derive);
125127
decl_derive!(
126128
[SessionDiagnostic, attributes(

compiler/rustc_macros/src/type_foldable.rs

-13
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ pub fn type_foldable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::
1111
}
1212

1313
s.add_bounds(synstructure::AddBounds::Generics);
14-
let body_visit = s.each(|bind| {
15-
quote! {
16-
::rustc_middle::ty::fold::TypeFoldable::visit_with(#bind, __folder)?;
17-
}
18-
});
1914
s.bind_with(|_| synstructure::BindStyle::Move);
2015
let body_fold = s.each_variant(|vi| {
2116
let bindings = vi.bindings();
@@ -36,14 +31,6 @@ pub fn type_foldable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::
3631
) -> Result<Self, __F::Error> {
3732
Ok(match self { #body_fold })
3833
}
39-
40-
fn visit_with<__F: ::rustc_middle::ty::fold::TypeVisitor<'tcx>>(
41-
&self,
42-
__folder: &mut __F
43-
) -> ::std::ops::ControlFlow<__F::BreakTy> {
44-
match *self { #body_visit }
45-
::std::ops::ControlFlow::CONTINUE
46-
}
4734
},
4835
)
4936
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use quote::quote;
2+
use syn::parse_quote;
3+
4+
pub fn type_visitable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
5+
if let syn::Data::Union(_) = s.ast().data {
6+
panic!("cannot derive on union")
7+
}
8+
9+
if !s.ast().generics.lifetimes().any(|lt| lt.lifetime.ident == "tcx") {
10+
s.add_impl_generic(parse_quote! { 'tcx });
11+
}
12+
13+
s.add_bounds(synstructure::AddBounds::Generics);
14+
let body_visit = s.each(|bind| {
15+
quote! {
16+
::rustc_middle::ty::visit::TypeVisitable::visit_with(#bind, __visitor)?;
17+
}
18+
});
19+
s.bind_with(|_| synstructure::BindStyle::Move);
20+
21+
s.bound_impl(
22+
quote!(::rustc_middle::ty::visit::TypeVisitable<'tcx>),
23+
quote! {
24+
fn visit_with<__V: ::rustc_middle::ty::visit::TypeVisitor<'tcx>>(
25+
&self,
26+
__visitor: &mut __V
27+
) -> ::std::ops::ControlFlow<__V::BreakTy> {
28+
match *self { #body_visit }
29+
::std::ops::ControlFlow::CONTINUE
30+
}
31+
},
32+
)
33+
}

compiler/rustc_middle/src/hir/place.rs

+10-38
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,8 @@ use crate::ty::Ty;
44
use rustc_hir::HirId;
55
use rustc_target::abi::VariantIdx;
66

7-
#[derive(
8-
Clone,
9-
Copy,
10-
Debug,
11-
PartialEq,
12-
Eq,
13-
Hash,
14-
TyEncodable,
15-
TyDecodable,
16-
TypeFoldable,
17-
HashStable
18-
)]
7+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable)]
8+
#[derive(TypeFoldable, TypeVisitable)]
199
pub enum PlaceBase {
2010
/// A temporary variable.
2111
Rvalue,
@@ -27,18 +17,8 @@ pub enum PlaceBase {
2717
Upvar(ty::UpvarId),
2818
}
2919

30-
#[derive(
31-
Clone,
32-
Copy,
33-
Debug,
34-
PartialEq,
35-
Eq,
36-
Hash,
37-
TyEncodable,
38-
TyDecodable,
39-
TypeFoldable,
40-
HashStable
41-
)]
20+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable)]
21+
#[derive(TypeFoldable, TypeVisitable)]
4222
pub enum ProjectionKind {
4323
/// A dereference of a pointer, reference or `Box<T>` of the given type.
4424
Deref,
@@ -58,18 +38,8 @@ pub enum ProjectionKind {
5838
Subslice,
5939
}
6040

61-
#[derive(
62-
Clone,
63-
Copy,
64-
Debug,
65-
PartialEq,
66-
Eq,
67-
Hash,
68-
TyEncodable,
69-
TyDecodable,
70-
TypeFoldable,
71-
HashStable
72-
)]
41+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable)]
42+
#[derive(TypeFoldable, TypeVisitable)]
7343
pub struct Projection<'tcx> {
7444
/// Type after the projection is applied.
7545
pub ty: Ty<'tcx>,
@@ -81,7 +51,8 @@ pub struct Projection<'tcx> {
8151
/// A `Place` represents how a value is located in memory.
8252
///
8353
/// This is an HIR version of [`rustc_middle::mir::Place`].
84-
#[derive(Clone, Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
54+
#[derive(Clone, Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable)]
55+
#[derive(TypeFoldable, TypeVisitable)]
8556
pub struct Place<'tcx> {
8657
/// The type of the `PlaceBase`
8758
pub base_ty: Ty<'tcx>,
@@ -94,7 +65,8 @@ pub struct Place<'tcx> {
9465
/// A `PlaceWithHirId` represents how a value is located in memory.
9566
///
9667
/// This is an HIR version of [`rustc_middle::mir::Place`].
97-
#[derive(Clone, Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
68+
#[derive(Clone, Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable)]
69+
#[derive(TypeFoldable, TypeVisitable)]
9870
pub struct PlaceWithHirId<'tcx> {
9971
/// `HirId` of the expression or pattern producing this value.
10072
pub hir_id: HirId,

compiler/rustc_middle/src/infer/canonical.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use std::ops::Index;
3434
/// variables have been rewritten to "canonical vars". These are
3535
/// numbered starting from 0 in order of first appearance.
3636
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, TyDecodable, TyEncodable)]
37-
#[derive(HashStable, TypeFoldable, Lift)]
37+
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
3838
pub struct Canonical<'tcx, V> {
3939
pub max_universe: ty::UniverseIndex,
4040
pub variables: CanonicalVarInfos<'tcx>,
@@ -53,7 +53,7 @@ pub type CanonicalVarInfos<'tcx> = &'tcx List<CanonicalVarInfo<'tcx>>;
5353
/// variables. You will need to supply it later to instantiate the
5454
/// canonicalized query response.
5555
#[derive(Clone, Debug, PartialEq, Eq, Hash, TyDecodable, TyEncodable)]
56-
#[derive(HashStable, TypeFoldable, Lift)]
56+
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
5757
pub struct CanonicalVarValues<'tcx> {
5858
pub var_values: IndexVec<BoundVar, GenericArg<'tcx>>,
5959
}
@@ -173,7 +173,7 @@ pub enum CanonicalTyVarKind {
173173
/// After we execute a query with a canonicalized key, we get back a
174174
/// `Canonical<QueryResponse<..>>`. You can use
175175
/// `instantiate_query_result` to access the data in this result.
176-
#[derive(Clone, Debug, HashStable, TypeFoldable, Lift)]
176+
#[derive(Clone, Debug, HashStable, TypeFoldable, TypeVisitable, Lift)]
177177
pub struct QueryResponse<'tcx, R> {
178178
pub var_values: CanonicalVarValues<'tcx>,
179179
pub region_constraints: QueryRegionConstraints<'tcx>,
@@ -187,7 +187,7 @@ pub struct QueryResponse<'tcx, R> {
187187
pub value: R,
188188
}
189189

190-
#[derive(Clone, Debug, Default, HashStable, TypeFoldable, Lift)]
190+
#[derive(Clone, Debug, Default, HashStable, TypeFoldable, TypeVisitable, Lift)]
191191
pub struct QueryRegionConstraints<'tcx> {
192192
pub outlives: Vec<QueryOutlivesConstraint<'tcx>>,
193193
pub member_constraints: Vec<MemberConstraint<'tcx>>,

compiler/rustc_middle/src/infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_span::Span;
1313
/// ```text
1414
/// R0 member of [O1..On]
1515
/// ```
16-
#[derive(Debug, Clone, HashStable, TypeFoldable, Lift)]
16+
#[derive(Debug, Clone, HashStable, TypeFoldable, TypeVisitable, Lift)]
1717
pub struct MemberConstraint<'tcx> {
1818
/// The `DefId` of the opaque type causing this constraint: used for error reporting.
1919
pub opaque_type_def_id: DefId,

compiler/rustc_middle/src/mir/coverage.rs

+5-14
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl From<InjectedExpressionId> for ExpressionOperandId {
100100
}
101101
}
102102

103-
#[derive(Clone, PartialEq, TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable)]
103+
#[derive(Clone, PartialEq, TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
104104
pub enum CoverageKind {
105105
Counter {
106106
function_source_hash: u64,
@@ -148,18 +148,8 @@ impl Debug for CoverageKind {
148148
}
149149
}
150150

151-
#[derive(
152-
Clone,
153-
TyEncodable,
154-
TyDecodable,
155-
Hash,
156-
HashStable,
157-
TypeFoldable,
158-
PartialEq,
159-
Eq,
160-
PartialOrd,
161-
Ord
162-
)]
151+
#[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq, Eq, PartialOrd, Ord)]
152+
#[derive(TypeFoldable, TypeVisitable)]
163153
pub struct CodeRegion {
164154
pub file_name: Symbol,
165155
pub start_line: u32,
@@ -178,7 +168,8 @@ impl Debug for CodeRegion {
178168
}
179169
}
180170

181-
#[derive(Copy, Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable)]
171+
#[derive(Copy, Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)]
172+
#[derive(TypeFoldable, TypeVisitable)]
182173
pub enum Op {
183174
Subtract,
184175
Add,

compiler/rustc_middle/src/mir/mod.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl MirPhase {
136136

137137
/// Where a specific `mir::Body` comes from.
138138
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
139-
#[derive(HashStable, TyEncodable, TyDecodable, TypeFoldable)]
139+
#[derive(HashStable, TyEncodable, TyDecodable, TypeFoldable, TypeVisitable)]
140140
pub struct MirSource<'tcx> {
141141
pub instance: InstanceDef<'tcx>,
142142

@@ -166,7 +166,7 @@ impl<'tcx> MirSource<'tcx> {
166166
}
167167
}
168168

169-
#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable)]
169+
#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable, TypeVisitable)]
170170
pub struct GeneratorInfo<'tcx> {
171171
/// The yield type of the function, if it is a generator.
172172
pub yield_ty: Option<Ty<'tcx>>,
@@ -183,7 +183,7 @@ pub struct GeneratorInfo<'tcx> {
183183
}
184184

185185
/// The lowered representation of a single function.
186-
#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable)]
186+
#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable, TypeVisitable)]
187187
pub struct Body<'tcx> {
188188
/// A list of basic blocks. References to basic block use a newtyped index type [`BasicBlock`]
189189
/// that indexes into this vector.
@@ -601,7 +601,7 @@ impl<'tcx> IndexMut<BasicBlock> for Body<'tcx> {
601601
}
602602
}
603603

604-
#[derive(Copy, Clone, Debug, HashStable, TypeFoldable)]
604+
#[derive(Copy, Clone, Debug, HashStable, TypeFoldable, TypeVisitable)]
605605
pub enum ClearCrossCrate<T> {
606606
Clear,
607607
Set(T),
@@ -807,7 +807,7 @@ pub struct BlockTailInfo {
807807
///
808808
/// This can be a binding declared by the user, a temporary inserted by the compiler, a function
809809
/// argument, or the return place.
810-
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
810+
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
811811
pub struct LocalDecl<'tcx> {
812812
/// Whether this is a mutable binding (i.e., `let x` or `let mut x`).
813813
///
@@ -942,7 +942,7 @@ static_assert_size!(LocalDecl<'_>, 56);
942942
///
943943
/// Not used for non-StaticRef temporaries, the return place, or anonymous
944944
/// function parameters.
945-
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
945+
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
946946
pub enum LocalInfo<'tcx> {
947947
/// A user-defined local variable or function parameter
948948
///
@@ -1081,7 +1081,7 @@ impl<'tcx> LocalDecl<'tcx> {
10811081
}
10821082
}
10831083

1084-
#[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1084+
#[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
10851085
pub enum VarDebugInfoContents<'tcx> {
10861086
/// NOTE(eddyb) There's an unenforced invariant that this `Place` is
10871087
/// based on a `Local`, not a `Static`, and contains no indexing.
@@ -1099,7 +1099,7 @@ impl<'tcx> Debug for VarDebugInfoContents<'tcx> {
10991099
}
11001100

11011101
/// Debug information pertaining to a user variable.
1102-
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1102+
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
11031103
pub struct VarDebugInfo<'tcx> {
11041104
pub name: Symbol,
11051105

@@ -1155,7 +1155,7 @@ impl BasicBlock {
11551155
// BasicBlockData
11561156

11571157
/// See [`BasicBlock`] for documentation on what basic blocks are at a high level.
1158-
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1158+
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
11591159
pub struct BasicBlockData<'tcx> {
11601160
/// List of statements in this block.
11611161
pub statements: Vec<Statement<'tcx>>,
@@ -1392,7 +1392,7 @@ impl<O: fmt::Debug> fmt::Debug for AssertKind<O> {
13921392
///////////////////////////////////////////////////////////////////////////
13931393
// Statements
13941394

1395-
#[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1395+
#[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
13961396
pub struct Statement<'tcx> {
13971397
pub source_info: SourceInfo,
13981398
pub kind: StatementKind<'tcx>,
@@ -1758,7 +1758,7 @@ impl SourceScope {
17581758
}
17591759
}
17601760

1761-
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1761+
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
17621762
pub struct SourceScopeData<'tcx> {
17631763
pub span: Span,
17641764
pub parent_scope: Option<SourceScope>,
@@ -2524,7 +2524,7 @@ impl<'tcx> ConstantKind<'tcx> {
25242524
/// The first will lead to the constraint `w: &'1 str` (for some
25252525
/// inferred region `'1`). The second will lead to the constraint `w:
25262526
/// &'static str`.
2527-
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
2527+
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
25282528
pub struct UserTypeProjections {
25292529
pub contents: Vec<(UserTypeProjection, Span)>,
25302530
}

compiler/rustc_middle/src/mir/query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ rustc_index::newtype_index! {
161161
}
162162

163163
/// The layout of generator state.
164-
#[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
164+
#[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
165165
pub struct GeneratorLayout<'tcx> {
166166
/// The type of every local stored inside the generator.
167167
pub field_tys: IndexVec<GeneratorSavedLocal, Ty<'tcx>>,

0 commit comments

Comments
 (0)