Skip to content

Commit 35e3019

Browse files
committed
Inline visiting of GenericArgs
1 parent dce79e4 commit 35e3019

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

compiler/rustc_macros/src/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ decl_derive!([TyEncodable] => serialize::type_encodable_derive);
7474
decl_derive!([MetadataDecodable] => serialize::meta_decodable_derive);
7575
decl_derive!([MetadataEncodable] => serialize::meta_encodable_derive);
7676
decl_derive!(
77-
[TypeFoldable, attributes(type_foldable)] =>
77+
[TypeFoldable, attributes(type_foldable, inline_traversals)] =>
7878
/// Derives `TypeFoldable` for the annotated `struct` or `enum` (`union` is not supported).
7979
///
8080
/// Folds will produce a value of the same struct or enum variant as the input, with each field
@@ -97,10 +97,13 @@ decl_derive!(
9797
/// and
9898
///
9999
/// `impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for Bar<'tcx>`
100+
///
101+
/// The annotated item may be decorated with an `#[inline_traversals]` attribute to cause the
102+
/// generated folding method to be marked `#[inline]`.
100103
traversable::traversable_derive::<traversable::Foldable>
101104
);
102105
decl_derive!(
103-
[TypeVisitable, attributes(type_visitable)] =>
106+
[TypeVisitable, attributes(type_visitable, inline_traversals)] =>
104107
/// Derives `TypeVisitable` for the annotated `struct` or `enum` (`union` is not supported).
105108
///
106109
/// Each field of the struct or enum variant will be visited (in definition order) using the
@@ -123,6 +126,9 @@ decl_derive!(
123126
/// and
124127
///
125128
/// `impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for Bar<'tcx>`
129+
///
130+
/// The annotated item may be decorated with an `#[inline_traversals]` attribute to cause the
131+
/// generated folding method to be marked `#[inline]`.
126132
traversable::traversable_derive::<traversable::Visitable>
127133
);
128134
decl_derive!([Lift, attributes(lift)] => lift::lift_derive);

compiler/rustc_macros/src/traversable.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub trait Traversable {
1414
fn arms(structure: &mut synstructure::Structure<'_>) -> TokenStream;
1515

1616
/// The body of an implementation given the match `arms`.
17-
fn impl_body(arms: impl ToTokens) -> TokenStream;
17+
fn impl_body(arms: impl ToTokens, attrs: impl ToTokens) -> TokenStream;
1818
}
1919

2020
impl Traversable for Foldable {
@@ -52,8 +52,9 @@ impl Traversable for Foldable {
5252
})
5353
})
5454
}
55-
fn impl_body(arms: impl ToTokens) -> TokenStream {
55+
fn impl_body(arms: impl ToTokens, attrs: impl ToTokens) -> TokenStream {
5656
quote! {
57+
#attrs
5758
fn try_fold_with<__F: ::rustc_middle::ty::fold::FallibleTypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(
5859
self,
5960
__folder: &mut __F
@@ -94,8 +95,9 @@ impl Traversable for Visitable {
9495
}
9596
})
9697
}
97-
fn impl_body(arms: impl ToTokens) -> TokenStream {
98+
fn impl_body(arms: impl ToTokens, attrs: impl ToTokens) -> TokenStream {
9899
quote! {
100+
#attrs
99101
fn visit_with<__V: ::rustc_middle::ty::visit::TypeVisitor<::rustc_middle::ty::TyCtxt<'tcx>>>(
100102
&self,
101103
__visitor: &mut __V
@@ -122,5 +124,12 @@ pub fn traversable_derive<T: Traversable>(
122124
}
123125

124126
let arms = T::arms(&mut structure);
125-
structure.bound_impl(T::traversable(), T::impl_body(arms))
127+
let attrs = structure
128+
.ast()
129+
.attrs
130+
.iter()
131+
.any(|attr| attr.path().is_ident("inline_traversals"))
132+
.then_some(quote! { #[inline] });
133+
134+
structure.bound_impl(T::traversable(), T::impl_body(arms, attrs))
126135
}

compiler/rustc_middle/src/ty/generic_args.rs

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const CONST_TAG: usize = 0b10;
4848

4949
#[derive(Debug, TyEncodable, TyDecodable, PartialEq, Eq, PartialOrd, Ord, HashStable)]
5050
#[derive(Clone, TypeFoldable, TypeVisitable)]
51+
#[inline_traversals]
5152
pub enum GenericArgKind<'tcx> {
5253
Lifetime(ty::Region<'tcx>),
5354
Type(Ty<'tcx>),
@@ -216,6 +217,7 @@ impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for GenericArg<'tcx> {
216217
}
217218

218219
impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for GenericArg<'tcx> {
220+
#[inline]
219221
fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
220222
self.unpack().visit_with(visitor)
221223
}

0 commit comments

Comments
 (0)