Skip to content

Commit 59ee434

Browse files
committed
Move visiting traits to type library
1 parent 8754bd3 commit 59ee434

File tree

9 files changed

+648
-557
lines changed

9 files changed

+648
-557
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4868,6 +4868,7 @@ dependencies = [
48684868
"rustc_macros",
48694869
"rustc_serialize",
48704870
"smallvec",
4871+
"tracing",
48714872
]
48724873

48734874
[[package]]

compiler/rustc_middle/src/macros.rs

Lines changed: 30 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ macro_rules! CloneLiftImpls {
6666
/// Used for types that are `Copy` and which **do not care arena
6767
/// allocated data** (i.e., don't need to be folded).
6868
#[macro_export]
69-
macro_rules! TrivialTypeTraversalImpls {
69+
macro_rules! TrivialTypeFoldableImpls {
7070
(for <$tcx:lifetime> { $($ty:ty,)+ }) => {
7171
$(
7272
impl<$tcx> $crate::ty::fold::TypeFoldable<$crate::ty::TyCtxt<$tcx>> for $ty {
@@ -85,18 +85,25 @@ macro_rules! TrivialTypeTraversalImpls {
8585
self
8686
}
8787
}
88+
)+
89+
};
8890

89-
impl<$tcx> $crate::ty::visit::TypeVisitable<$crate::ty::TyCtxt<$tcx>> for $ty {
90-
#[inline]
91-
fn visit_with<F: $crate::ty::visit::TypeVisitor<$crate::ty::TyCtxt<$tcx>>>(
92-
&self,
93-
_: &mut F)
94-
-> ::std::ops::ControlFlow<F::BreakTy>
95-
{
96-
::std::ops::ControlFlow::Continue(())
97-
}
91+
($($ty:ty,)+) => {
92+
TrivialTypeFoldableImpls! {
93+
for <'tcx> {
94+
$($ty,)+
9895
}
99-
)+
96+
}
97+
};
98+
}
99+
100+
/// Used for types that are `Copy` and which **do not care arena
101+
/// allocated data** (i.e., don't need to be folded).
102+
#[macro_export]
103+
macro_rules! TrivialTypeTraversalImpls {
104+
(for <$tcx:lifetime> { $($ty:ty,)+ }) => {
105+
TrivialTypeFoldableImpls!(for <$tcx> { $($ty,)+ });
106+
rustc_type_ir::TrivialTypeVisitableImpls!($(<$tcx> $crate::ty::TyCtxt<$tcx> { $ty })+);
100107
};
101108

102109
($($ty:ty,)+) => {
@@ -108,6 +115,14 @@ macro_rules! TrivialTypeTraversalImpls {
108115
};
109116
}
110117

118+
#[macro_export]
119+
macro_rules! TrivialTypeFoldableAndLiftImpls {
120+
($($t:tt)*) => {
121+
TrivialTypeFoldableImpls! { $($t)* }
122+
CloneLiftImpls! { $($t)* }
123+
}
124+
}
125+
111126
#[macro_export]
112127
macro_rules! TrivialTypeTraversalAndLiftImpls {
113128
($($t:tt)*) => {
@@ -136,15 +151,10 @@ macro_rules! EnumTypeTraversalImpl {
136151
(impl<$($p:tt),*> TypeVisitable<$tcx:ty> for $s:path {
137152
$($variants:tt)*
138153
} $(where $($wc:tt)*)*) => {
139-
impl<$($p),*> $crate::ty::visit::TypeVisitable<$tcx> for $s
140-
$(where $($wc)*)*
141-
{
142-
fn visit_with<V: $crate::ty::visit::TypeVisitor<$tcx>>(
143-
&self,
144-
visitor: &mut V,
145-
) -> ::std::ops::ControlFlow<V::BreakTy> {
146-
EnumTypeTraversalImpl!(@VisitVariants(self, visitor) input($($variants)*) output())
147-
}
154+
rustc_type_ir::EnumTypeVisitableImpl! {
155+
impl<$($p),*> TypeVisitable<$tcx> for $s {
156+
$($variants)*
157+
} $(where $($wc)*)*
148158
}
149159
};
150160

@@ -201,59 +211,4 @@ macro_rules! EnumTypeTraversalImpl {
201211
)
202212
)
203213
};
204-
205-
(@VisitVariants($this:expr, $visitor:expr) input() output($($output:tt)*)) => {
206-
match $this {
207-
$($output)*
208-
}
209-
};
210-
211-
(@VisitVariants($this:expr, $visitor:expr)
212-
input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
213-
output( $($output:tt)*) ) => {
214-
EnumTypeTraversalImpl!(
215-
@VisitVariants($this, $visitor)
216-
input($($input)*)
217-
output(
218-
$variant ( $($variant_arg),* ) => {
219-
$($crate::ty::visit::TypeVisitable::visit_with(
220-
$variant_arg, $visitor
221-
)?;)*
222-
::std::ops::ControlFlow::Continue(())
223-
}
224-
$($output)*
225-
)
226-
)
227-
};
228-
229-
(@VisitVariants($this:expr, $visitor:expr)
230-
input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
231-
output( $($output:tt)*) ) => {
232-
EnumTypeTraversalImpl!(
233-
@VisitVariants($this, $visitor)
234-
input($($input)*)
235-
output(
236-
$variant { $($variant_arg),* } => {
237-
$($crate::ty::visit::TypeVisitable::visit_with(
238-
$variant_arg, $visitor
239-
)?;)*
240-
::std::ops::ControlFlow::Continue(())
241-
}
242-
$($output)*
243-
)
244-
)
245-
};
246-
247-
(@VisitVariants($this:expr, $visitor:expr)
248-
input( ($variant:path), $($input:tt)*)
249-
output( $($output:tt)*) ) => {
250-
EnumTypeTraversalImpl!(
251-
@VisitVariants($this, $visitor)
252-
input($($input)*)
253-
output(
254-
$variant => { ::std::ops::ControlFlow::Continue(()) }
255-
$($output)*
256-
)
257-
)
258-
};
259214
}

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 6 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,19 @@ impl<'tcx> fmt::Debug for AliasTy<'tcx> {
199199
// For things that don't carry any arena-allocated data (and are
200200
// copy...), just add them to this list.
201201

202-
TrivialTypeTraversalAndLiftImpls! {
202+
TrivialTypeFoldableAndLiftImpls! {
203203
(),
204204
bool,
205205
usize,
206-
::rustc_target::abi::VariantIdx,
207206
u16,
208207
u32,
209208
u64,
210209
String,
210+
rustc_type_ir::DebruijnIndex,
211+
}
212+
213+
TrivialTypeTraversalAndLiftImpls! {
214+
::rustc_target::abi::VariantIdx,
211215
crate::middle::region::Scope,
212216
crate::ty::FloatTy,
213217
::rustc_ast::InlineAsmOptions,
@@ -259,7 +263,6 @@ TrivialTypeTraversalAndLiftImpls! {
259263
Field,
260264
interpret::Scalar,
261265
rustc_target::abi::Size,
262-
rustc_type_ir::DebruijnIndex,
263266
ty::BoundVar,
264267
ty::Placeholder<ty::BoundVar>,
265268
}
@@ -389,13 +392,6 @@ impl<I: Interner, T: TypeFoldable<I>, U: TypeFoldable<I>> TypeFoldable<I> for (T
389392
}
390393
}
391394

392-
impl<I: Interner, T: TypeVisitable<I>, U: TypeVisitable<I>> TypeVisitable<I> for (T, U) {
393-
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
394-
self.0.visit_with(visitor)?;
395-
self.1.visit_with(visitor)
396-
}
397-
}
398-
399395
impl<I: Interner, A: TypeFoldable<I>, B: TypeFoldable<I>, C: TypeFoldable<I>> TypeFoldable<I>
400396
for (A, B, C)
401397
{
@@ -411,41 +407,19 @@ impl<I: Interner, A: TypeFoldable<I>, B: TypeFoldable<I>, C: TypeFoldable<I>> Ty
411407
}
412408
}
413409

414-
impl<I: Interner, A: TypeVisitable<I>, B: TypeVisitable<I>, C: TypeVisitable<I>> TypeVisitable<I>
415-
for (A, B, C)
416-
{
417-
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
418-
self.0.visit_with(visitor)?;
419-
self.1.visit_with(visitor)?;
420-
self.2.visit_with(visitor)
421-
}
422-
}
423-
424410
EnumTypeTraversalImpl! {
425411
impl<I, T> TypeFoldable<I> for Option<T> {
426412
(Some)(a),
427413
(None),
428414
} where I: Interner, T: TypeFoldable<I>
429415
}
430-
EnumTypeTraversalImpl! {
431-
impl<I, T> TypeVisitable<I> for Option<T> {
432-
(Some)(a),
433-
(None),
434-
} where I: Interner, T: TypeVisitable<I>
435-
}
436416

437417
EnumTypeTraversalImpl! {
438418
impl<I, T, E> TypeFoldable<I> for Result<T, E> {
439419
(Ok)(a),
440420
(Err)(a),
441421
} where I: Interner, T: TypeFoldable<I>, E: TypeFoldable<I>,
442422
}
443-
EnumTypeTraversalImpl! {
444-
impl<I, T, E> TypeVisitable<I> for Result<T, E> {
445-
(Ok)(a),
446-
(Err)(a),
447-
} where I: Interner, T: TypeVisitable<I>, E: TypeVisitable<I>,
448-
}
449423

450424
impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Rc<T> {
451425
fn try_fold_with<F: FallibleTypeFolder<I>>(mut self, folder: &mut F) -> Result<Self, F::Error> {
@@ -484,12 +458,6 @@ impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Rc<T> {
484458
}
485459
}
486460

487-
impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Rc<T> {
488-
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
489-
(**self).visit_with(visitor)
490-
}
491-
}
492-
493461
impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Arc<T> {
494462
fn try_fold_with<F: FallibleTypeFolder<I>>(mut self, folder: &mut F) -> Result<Self, F::Error> {
495463
// We merely want to replace the contained `T`, if at all possible,
@@ -527,54 +495,24 @@ impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Arc<T> {
527495
}
528496
}
529497

530-
impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Arc<T> {
531-
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
532-
(**self).visit_with(visitor)
533-
}
534-
}
535-
536498
impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Box<T> {
537499
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
538500
self.try_map_id(|value| value.try_fold_with(folder))
539501
}
540502
}
541503

542-
impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Box<T> {
543-
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
544-
(**self).visit_with(visitor)
545-
}
546-
}
547-
548504
impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Vec<T> {
549505
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
550506
self.try_map_id(|t| t.try_fold_with(folder))
551507
}
552508
}
553509

554-
impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Vec<T> {
555-
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
556-
self.iter().try_for_each(|t| t.visit_with(visitor))
557-
}
558-
}
559-
560-
impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for &[T] {
561-
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
562-
self.iter().try_for_each(|t| t.visit_with(visitor))
563-
}
564-
}
565-
566510
impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Box<[T]> {
567511
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
568512
self.try_map_id(|t| t.try_fold_with(folder))
569513
}
570514
}
571515

572-
impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Box<[T]> {
573-
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
574-
self.iter().try_for_each(|t| t.visit_with(visitor))
575-
}
576-
}
577-
578516
impl<'tcx, T: TypeFoldable<TyCtxt<'tcx>>> TypeFoldable<TyCtxt<'tcx>> for ty::Binder<'tcx, T> {
579517
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
580518
self,
@@ -841,12 +779,6 @@ impl<I: Interner, T: TypeFoldable<I>, Ix: Idx> TypeFoldable<I> for IndexVec<Ix,
841779
}
842780
}
843781

844-
impl<I: Interner, T: TypeVisitable<I>, Ix: Idx> TypeVisitable<I> for IndexVec<Ix, T> {
845-
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
846-
self.iter().try_for_each(|t| t.visit_with(visitor))
847-
}
848-
}
849-
850782
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::Const<'tcx> {
851783
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
852784
self,

0 commit comments

Comments
 (0)