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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 0 additions & 13 deletions
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
}
Lines changed: 33 additions & 0 deletions
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

Lines changed: 10 additions & 38 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 14 deletions
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,

0 commit comments

Comments
 (0)