impl TypeVisitable in type traversal macros

This commit is contained in:
Alan Egerton 2022-06-17 11:05:17 +01:00
parent e4b9625b87
commit 9ffdc2de8c
No known key found for this signature in database
GPG Key ID: 07CAC3CCA7E0643F
14 changed files with 57 additions and 35 deletions

View File

@ -293,7 +293,7 @@ impl<'tcx, V> Canonical<'tcx, V> {
pub type QueryOutlivesConstraint<'tcx> = pub type QueryOutlivesConstraint<'tcx> =
ty::Binder<'tcx, ty::OutlivesPredicate<GenericArg<'tcx>, Region<'tcx>>>; ty::Binder<'tcx, ty::OutlivesPredicate<GenericArg<'tcx>, Region<'tcx>>>;
TrivialTypeFoldableAndLiftImpls! { TrivialTypeTraversalAndLiftImpls! {
for <'tcx> { for <'tcx> {
crate::infer::canonical::Certainty, crate::infer::canonical::Certainty,
crate::infer::canonical::CanonicalVarInfo<'tcx>, crate::infer::canonical::CanonicalVarInfo<'tcx>,
@ -301,7 +301,7 @@ TrivialTypeFoldableAndLiftImpls! {
} }
} }
TrivialTypeFoldableImpls! { TrivialTypeTraversalImpls! {
for <'tcx> { for <'tcx> {
crate::infer::canonical::CanonicalVarInfos<'tcx>, crate::infer::canonical::CanonicalVarInfos<'tcx>,
} }

View File

@ -18,7 +18,7 @@ macro_rules! span_bug {
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// Lift and TypeFoldable macros // Lift and TypeFoldable/TypeVisitable macros
// //
// When possible, use one of these (relatively) convenient macros to write // When possible, use one of these (relatively) convenient macros to write
// the impls for you. // the impls for you.
@ -48,7 +48,7 @@ macro_rules! CloneLiftImpls {
/// Used for types that are `Copy` and which **do not care arena /// Used for types that are `Copy` and which **do not care arena
/// allocated data** (i.e., don't need to be folded). /// allocated data** (i.e., don't need to be folded).
#[macro_export] #[macro_export]
macro_rules! TrivialTypeFoldableImpls { macro_rules! TrivialTypeTraversalImpls {
(for <$tcx:lifetime> { $($ty:ty,)+ }) => { (for <$tcx:lifetime> { $($ty:ty,)+ }) => {
$( $(
impl<$tcx> $crate::ty::fold::TypeFoldable<$tcx> for $ty { impl<$tcx> $crate::ty::fold::TypeFoldable<$tcx> for $ty {
@ -58,8 +58,10 @@ macro_rules! TrivialTypeFoldableImpls {
) -> ::std::result::Result<$ty, F::Error> { ) -> ::std::result::Result<$ty, F::Error> {
Ok(self) Ok(self)
} }
}
fn visit_with<F: $crate::ty::fold::TypeVisitor<$tcx>>( impl<$tcx> $crate::ty::visit::TypeVisitable<$tcx> for $ty {
fn visit_with<F: $crate::ty::visit::TypeVisitor<$tcx>>(
&self, &self,
_: &mut F) _: &mut F)
-> ::std::ops::ControlFlow<F::BreakTy> -> ::std::ops::ControlFlow<F::BreakTy>
@ -71,7 +73,7 @@ macro_rules! TrivialTypeFoldableImpls {
}; };
($($ty:ty,)+) => { ($($ty:ty,)+) => {
TrivialTypeFoldableImpls! { TrivialTypeTraversalImpls! {
for <'tcx> { for <'tcx> {
$($ty,)+ $($ty,)+
} }
@ -80,15 +82,15 @@ macro_rules! TrivialTypeFoldableImpls {
} }
#[macro_export] #[macro_export]
macro_rules! TrivialTypeFoldableAndLiftImpls { macro_rules! TrivialTypeTraversalAndLiftImpls {
($($t:tt)*) => { ($($t:tt)*) => {
TrivialTypeFoldableImpls! { $($t)* } TrivialTypeTraversalImpls! { $($t)* }
CloneLiftImpls! { $($t)* } CloneLiftImpls! { $($t)* }
} }
} }
#[macro_export] #[macro_export]
macro_rules! EnumTypeFoldableImpl { macro_rules! EnumTypeTraversalImpl {
(impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path { (impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
$($variants:tt)* $($variants:tt)*
} $(where $($wc:tt)*)*) => { } $(where $($wc:tt)*)*) => {
@ -99,14 +101,22 @@ macro_rules! EnumTypeFoldableImpl {
self, self,
folder: &mut V, folder: &mut V,
) -> ::std::result::Result<Self, V::Error> { ) -> ::std::result::Result<Self, V::Error> {
EnumTypeFoldableImpl!(@FoldVariants(self, folder) input($($variants)*) output()) EnumTypeTraversalImpl!(@FoldVariants(self, folder) input($($variants)*) output())
} }
}
};
fn visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>( (impl<$($p:tt),*> TypeVisitable<$tcx:tt> for $s:path {
$($variants:tt)*
} $(where $($wc:tt)*)*) => {
impl<$($p),*> $crate::ty::visit::TypeVisitable<$tcx> for $s
$(where $($wc)*)*
{
fn visit_with<V: $crate::ty::visit::TypeVisitor<$tcx>>(
&self, &self,
visitor: &mut V, visitor: &mut V,
) -> ::std::ops::ControlFlow<V::BreakTy> { ) -> ::std::ops::ControlFlow<V::BreakTy> {
EnumTypeFoldableImpl!(@VisitVariants(self, visitor) input($($variants)*) output()) EnumTypeTraversalImpl!(@VisitVariants(self, visitor) input($($variants)*) output())
} }
} }
}; };
@ -120,7 +130,7 @@ macro_rules! EnumTypeFoldableImpl {
(@FoldVariants($this:expr, $folder:expr) (@FoldVariants($this:expr, $folder:expr)
input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*) input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
output( $($output:tt)*) ) => { output( $($output:tt)*) ) => {
EnumTypeFoldableImpl!( EnumTypeTraversalImpl!(
@FoldVariants($this, $folder) @FoldVariants($this, $folder)
input($($input)*) input($($input)*)
output( output(
@ -137,7 +147,7 @@ macro_rules! EnumTypeFoldableImpl {
(@FoldVariants($this:expr, $folder:expr) (@FoldVariants($this:expr, $folder:expr)
input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*) input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
output( $($output:tt)*) ) => { output( $($output:tt)*) ) => {
EnumTypeFoldableImpl!( EnumTypeTraversalImpl!(
@FoldVariants($this, $folder) @FoldVariants($this, $folder)
input($($input)*) input($($input)*)
output( output(
@ -155,7 +165,7 @@ macro_rules! EnumTypeFoldableImpl {
(@FoldVariants($this:expr, $folder:expr) (@FoldVariants($this:expr, $folder:expr)
input( ($variant:path), $($input:tt)*) input( ($variant:path), $($input:tt)*)
output( $($output:tt)*) ) => { output( $($output:tt)*) ) => {
EnumTypeFoldableImpl!( EnumTypeTraversalImpl!(
@FoldVariants($this, $folder) @FoldVariants($this, $folder)
input($($input)*) input($($input)*)
output( output(
@ -174,12 +184,12 @@ macro_rules! EnumTypeFoldableImpl {
(@VisitVariants($this:expr, $visitor:expr) (@VisitVariants($this:expr, $visitor:expr)
input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*) input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
output( $($output:tt)*) ) => { output( $($output:tt)*) ) => {
EnumTypeFoldableImpl!( EnumTypeTraversalImpl!(
@VisitVariants($this, $visitor) @VisitVariants($this, $visitor)
input($($input)*) input($($input)*)
output( output(
$variant ( $($variant_arg),* ) => { $variant ( $($variant_arg),* ) => {
$($crate::ty::fold::TypeFoldable::visit_with( $($crate::ty::visit::TypeVisitable::visit_with(
$variant_arg, $visitor $variant_arg, $visitor
)?;)* )?;)*
::std::ops::ControlFlow::CONTINUE ::std::ops::ControlFlow::CONTINUE
@ -192,12 +202,12 @@ macro_rules! EnumTypeFoldableImpl {
(@VisitVariants($this:expr, $visitor:expr) (@VisitVariants($this:expr, $visitor:expr)
input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*) input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
output( $($output:tt)*) ) => { output( $($output:tt)*) ) => {
EnumTypeFoldableImpl!( EnumTypeTraversalImpl!(
@VisitVariants($this, $visitor) @VisitVariants($this, $visitor)
input($($input)*) input($($input)*)
output( output(
$variant { $($variant_arg),* } => { $variant { $($variant_arg),* } => {
$($crate::ty::fold::TypeFoldable::visit_with( $($crate::ty::visit::TypeVisitable::visit_with(
$variant_arg, $visitor $variant_arg, $visitor
)?;)* )?;)*
::std::ops::ControlFlow::CONTINUE ::std::ops::ControlFlow::CONTINUE
@ -210,7 +220,7 @@ macro_rules! EnumTypeFoldableImpl {
(@VisitVariants($this:expr, $visitor:expr) (@VisitVariants($this:expr, $visitor:expr)
input( ($variant:path), $($input:tt)*) input( ($variant:path), $($input:tt)*)
output( $($output:tt)*) ) => { output( $($output:tt)*) ) => {
EnumTypeFoldableImpl!( EnumTypeTraversalImpl!(
@VisitVariants($this, $visitor) @VisitVariants($this, $visitor)
input($($input)*) input($($input)*)
output( output(

View File

@ -58,6 +58,6 @@ impl<CTX> HashStable<CTX> for GraphIsCyclicCache {
} }
} }
TrivialTypeFoldableAndLiftImpls! { TrivialTypeTraversalAndLiftImpls! {
GraphIsCyclicCache, GraphIsCyclicCache,
} }

View File

@ -29,7 +29,7 @@ impl From<ErrorGuaranteed> for ErrorHandled {
} }
} }
TrivialTypeFoldableAndLiftImpls! { TrivialTypeTraversalAndLiftImpls! {
ErrorHandled, ErrorHandled,
} }

View File

@ -762,7 +762,7 @@ pub enum ImplicitSelfKind {
None, None,
} }
TrivialTypeFoldableAndLiftImpls! { BindingForm<'tcx>, } TrivialTypeTraversalAndLiftImpls! { BindingForm<'tcx>, }
mod binding_form_impl { mod binding_form_impl {
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@ -2641,7 +2641,7 @@ impl UserTypeProjection {
} }
} }
TrivialTypeFoldableAndLiftImpls! { ProjectionKind, } TrivialTypeTraversalAndLiftImpls! { ProjectionKind, }
impl<'tcx> TypeFoldable<'tcx> for UserTypeProjection { impl<'tcx> TypeFoldable<'tcx> for UserTypeProjection {
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> { fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {

View File

@ -73,6 +73,6 @@ impl<CTX> HashStable<CTX> for PredecessorCache {
} }
} }
TrivialTypeFoldableAndLiftImpls! { TrivialTypeTraversalAndLiftImpls! {
PredecessorCache, PredecessorCache,
} }

View File

@ -73,6 +73,6 @@ impl<CTX> HashStable<CTX> for SwitchSourceCache {
} }
} }
TrivialTypeFoldableAndLiftImpls! { TrivialTypeTraversalAndLiftImpls! {
SwitchSourceCache, SwitchSourceCache,
} }

View File

@ -384,6 +384,6 @@ impl<CTX> HashStable<CTX> for PostorderCache {
} }
} }
TrivialTypeFoldableAndLiftImpls! { TrivialTypeTraversalAndLiftImpls! {
PostorderCache, PostorderCache,
} }

View File

@ -4,7 +4,7 @@ use super::*;
use crate::ty; use crate::ty;
use rustc_data_structures::functor::IdFunctor; use rustc_data_structures::functor::IdFunctor;
TrivialTypeFoldableAndLiftImpls! { TrivialTypeTraversalAndLiftImpls! {
BlockTailInfo, BlockTailInfo,
MirPhase, MirPhase,
SourceInfo, SourceInfo,

View File

@ -42,7 +42,7 @@ impl From<ErrorGuaranteed> for NotConstEvaluatable {
} }
} }
TrivialTypeFoldableAndLiftImpls! { TrivialTypeTraversalAndLiftImpls! {
NotConstEvaluatable, NotConstEvaluatable,
} }

View File

@ -283,7 +283,7 @@ impl From<ErrorGuaranteed> for OverflowError {
} }
} }
TrivialTypeFoldableAndLiftImpls! { TrivialTypeTraversalAndLiftImpls! {
OverflowError, OverflowError,
} }

View File

@ -129,7 +129,7 @@ impl<N: fmt::Debug> fmt::Debug for traits::ImplSourceConstDestructData<N> {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// Lift implementations // Lift implementations
TrivialTypeFoldableAndLiftImpls! { TrivialTypeTraversalAndLiftImpls! {
super::IfExpressionCause, super::IfExpressionCause,
super::ImplSourceDiscriminantKindData, super::ImplSourceDiscriminantKindData,
super::ImplSourcePointeeData, super::ImplSourcePointeeData,

View File

@ -8,7 +8,7 @@ pub enum BindingMode {
BindByValue(Mutability), BindByValue(Mutability),
} }
TrivialTypeFoldableAndLiftImpls! { BindingMode, } TrivialTypeTraversalAndLiftImpls! { BindingMode, }
impl BindingMode { impl BindingMode {
pub fn convert(ba: BindingAnnotation) -> BindingMode { pub fn convert(ba: BindingAnnotation) -> BindingMode {

View File

@ -183,7 +183,7 @@ impl<'tcx> fmt::Debug for ty::PredicateKind<'tcx> {
// For things that don't carry any arena-allocated data (and are // For things that don't carry any arena-allocated data (and are
// copy...), just add them to this list. // copy...), just add them to this list.
TrivialTypeFoldableAndLiftImpls! { TrivialTypeTraversalAndLiftImpls! {
(), (),
bool, bool,
usize, usize,
@ -692,19 +692,31 @@ impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>>
} }
} }
EnumTypeFoldableImpl! { EnumTypeTraversalImpl! {
impl<'tcx, T> TypeFoldable<'tcx> for Option<T> { impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
(Some)(a), (Some)(a),
(None), (None),
} where T: TypeFoldable<'tcx> } where T: TypeFoldable<'tcx>
} }
EnumTypeTraversalImpl! {
impl<'tcx, T> TypeVisitable<'tcx> for Option<T> {
(Some)(a),
(None),
} where T: TypeVisitable<'tcx>
}
EnumTypeFoldableImpl! { EnumTypeTraversalImpl! {
impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> { impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
(Ok)(a), (Ok)(a),
(Err)(a), (Err)(a),
} where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>, } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
} }
EnumTypeTraversalImpl! {
impl<'tcx, T, E> TypeVisitable<'tcx> for Result<T, E> {
(Ok)(a),
(Err)(a),
} where T: TypeVisitable<'tcx>, E: TypeVisitable<'tcx>,
}
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> { impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
fn try_fold_with<F: FallibleTypeFolder<'tcx>>( fn try_fold_with<F: FallibleTypeFolder<'tcx>>(