From 9fa6bb2aa0d076cffac5699643eb5f473b6eba16 Mon Sep 17 00:00:00 2001 From: Alan Egerton Date: Fri, 10 Feb 2023 15:51:28 +0000 Subject: [PATCH] Make atomic structural impls generic over Interner --- compiler/rustc_middle/src/macros.rs | 40 +++++++-- compiler/rustc_middle/src/mir/mod.rs | 6 +- .../rustc_middle/src/ty/structural_impls.rs | 90 +++++++++---------- 3 files changed, 82 insertions(+), 54 deletions(-) diff --git a/compiler/rustc_middle/src/macros.rs b/compiler/rustc_middle/src/macros.rs index 08cf12559d7..e096be16677 100644 --- a/compiler/rustc_middle/src/macros.rs +++ b/compiler/rustc_middle/src/macros.rs @@ -100,11 +100,35 @@ fn visit_with>( }; ($($ty:ty,)+) => { - TrivialTypeTraversalImpls! { - for <'tcx> { - $($ty,)+ + $( + impl $crate::ty::fold::ir::TypeFoldable for $ty { + fn try_fold_with>( + self, + _: &mut F, + ) -> ::std::result::Result { + Ok(self) + } + + #[inline] + fn fold_with>( + self, + _: &mut F, + ) -> Self { + self + } } - } + + impl $crate::ty::visit::ir::TypeVisitable for $ty { + #[inline] + fn visit_with>( + &self, + _: &mut F) + -> ::std::ops::ControlFlow + { + ::std::ops::ControlFlow::Continue(()) + } + } + )+ }; } @@ -121,10 +145,10 @@ macro_rules! EnumTypeTraversalImpl { (impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path { $($variants:tt)* } $(where $($wc:tt)*)*) => { - impl<$($p),*> $crate::ty::fold::ir::TypeFoldable<$crate::ty::TyCtxt<$tcx>> for $s + impl<$($p),*> $crate::ty::fold::ir::TypeFoldable<$tcx> for $s $(where $($wc)*)* { - fn try_fold_with>( + fn try_fold_with>( self, folder: &mut V, ) -> ::std::result::Result { @@ -136,10 +160,10 @@ fn try_fold_with>( (impl<$($p:tt),*> TypeVisitable<$tcx:tt> for $s:path { $($variants:tt)* } $(where $($wc:tt)*)*) => { - impl<$($p),*> $crate::ty::visit::ir::TypeVisitable<$crate::ty::TyCtxt<$tcx>> for $s + impl<$($p),*> $crate::ty::visit::ir::TypeVisitable<$tcx> for $s $(where $($wc)*)* { - fn visit_with>( + fn visit_with>( &self, visitor: &mut V, ) -> ::std::ops::ControlFlow { diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index e0dfc4bbddc..6f42b69633c 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -705,7 +705,11 @@ pub enum BindingForm<'tcx> { RefForGuard, } -TrivialTypeTraversalAndLiftImpls! { BindingForm<'tcx>, } +TrivialTypeTraversalAndLiftImpls! { + for<'tcx> { + BindingForm<'tcx>, + } +} mod binding_form_impl { use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 43f045c6335..351753911e5 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -7,7 +7,7 @@ use crate::ty::fold::{ir::TypeSuperFoldable, FallibleTypeFolder, TypeFoldable}; use crate::ty::print::{with_no_trimmed_paths, FmtPrinter, Printer}; use crate::ty::visit::{ir::TypeSuperVisitable, TypeVisitable, TypeVisitor}; -use crate::ty::{self, ir, AliasTy, InferConst, Lift, Term, TermKind, Ty, TyCtxt}; +use crate::ty::{self, ir, AliasTy, InferConst, Interner, Lift, Term, TermKind, Ty, TyCtxt}; use rustc_data_structures::functor::IdFunctor; use rustc_hir::def::Namespace; use rustc_index::vec::{Idx, IndexVec}; @@ -375,8 +375,8 @@ fn visit_with>(&self, _visitor: &mut V) -> ControlFlow, U: TypeFoldable<'tcx>> ir::TypeFoldable> for (T, U) { - fn try_fold_with>( +impl, U: ir::TypeFoldable> ir::TypeFoldable for (T, U) { + fn try_fold_with>( self, folder: &mut F, ) -> Result<(T, U), F::Error> { @@ -384,19 +384,19 @@ fn try_fold_with>( } } -impl<'tcx, T: TypeVisitable<'tcx>, U: TypeVisitable<'tcx>> ir::TypeVisitable> +impl, U: ir::TypeVisitable> ir::TypeVisitable for (T, U) { - fn visit_with>(&self, visitor: &mut V) -> ControlFlow { + fn visit_with>(&self, visitor: &mut V) -> ControlFlow { self.0.visit_with(visitor)?; self.1.visit_with(visitor) } } -impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> - ir::TypeFoldable> for (A, B, C) +impl, B: ir::TypeFoldable, C: ir::TypeFoldable> + ir::TypeFoldable for (A, B, C) { - fn try_fold_with>( + fn try_fold_with>( self, folder: &mut F, ) -> Result<(A, B, C), F::Error> { @@ -408,10 +408,10 @@ fn try_fold_with>( } } -impl<'tcx, A: TypeVisitable<'tcx>, B: TypeVisitable<'tcx>, C: TypeVisitable<'tcx>> - ir::TypeVisitable> for (A, B, C) +impl, B: ir::TypeVisitable, C: ir::TypeVisitable> + ir::TypeVisitable for (A, B, C) { - fn visit_with>(&self, visitor: &mut V) -> ControlFlow { + fn visit_with>(&self, visitor: &mut V) -> ControlFlow { self.0.visit_with(visitor)?; self.1.visit_with(visitor)?; self.2.visit_with(visitor) @@ -419,33 +419,33 @@ fn visit_with>(&self, visitor: &mut V) -> ControlFlow TypeFoldable<'tcx> for Option { + impl TypeFoldable for Option { (Some)(a), (None), - } where T: TypeFoldable<'tcx> + } where I: Interner, T: ir::TypeFoldable } EnumTypeTraversalImpl! { - impl<'tcx, T> TypeVisitable<'tcx> for Option { + impl TypeVisitable for Option { (Some)(a), (None), - } where T: TypeVisitable<'tcx> + } where I: Interner, T: ir::TypeVisitable } EnumTypeTraversalImpl! { - impl<'tcx, T, E> TypeFoldable<'tcx> for Result { + impl TypeFoldable for Result { (Ok)(a), (Err)(a), - } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>, + } where I: Interner, T: ir::TypeFoldable, E: ir::TypeFoldable, } EnumTypeTraversalImpl! { - impl<'tcx, T, E> TypeVisitable<'tcx> for Result { + impl TypeVisitable for Result { (Ok)(a), (Err)(a), - } where T: TypeVisitable<'tcx>, E: TypeVisitable<'tcx>, + } where I: Interner, T: ir::TypeVisitable, E: ir::TypeVisitable, } -impl<'tcx, T: TypeFoldable<'tcx>> ir::TypeFoldable> for Rc { - fn try_fold_with>( +impl> ir::TypeFoldable for Rc { + fn try_fold_with>( mut self, folder: &mut F, ) -> Result { @@ -484,14 +484,14 @@ fn try_fold_with>( } } -impl<'tcx, T: TypeVisitable<'tcx>> ir::TypeVisitable> for Rc { - fn visit_with>(&self, visitor: &mut V) -> ControlFlow { +impl> ir::TypeVisitable for Rc { + fn visit_with>(&self, visitor: &mut V) -> ControlFlow { (**self).visit_with(visitor) } } -impl<'tcx, T: TypeFoldable<'tcx>> ir::TypeFoldable> for Arc { - fn try_fold_with>( +impl> ir::TypeFoldable for Arc { + fn try_fold_with>( mut self, folder: &mut F, ) -> Result { @@ -530,50 +530,50 @@ fn try_fold_with>( } } -impl<'tcx, T: TypeVisitable<'tcx>> ir::TypeVisitable> for Arc { - fn visit_with>(&self, visitor: &mut V) -> ControlFlow { +impl> ir::TypeVisitable for Arc { + fn visit_with>(&self, visitor: &mut V) -> ControlFlow { (**self).visit_with(visitor) } } -impl<'tcx, T: TypeFoldable<'tcx>> ir::TypeFoldable> for Box { - fn try_fold_with>(self, folder: &mut F) -> Result { +impl> ir::TypeFoldable for Box { + fn try_fold_with>(self, folder: &mut F) -> Result { self.try_map_id(|value| value.try_fold_with(folder)) } } -impl<'tcx, T: TypeVisitable<'tcx>> ir::TypeVisitable> for Box { - fn visit_with>(&self, visitor: &mut V) -> ControlFlow { +impl> ir::TypeVisitable for Box { + fn visit_with>(&self, visitor: &mut V) -> ControlFlow { (**self).visit_with(visitor) } } -impl<'tcx, T: TypeFoldable<'tcx>> ir::TypeFoldable> for Vec { - fn try_fold_with>(self, folder: &mut F) -> Result { +impl> ir::TypeFoldable for Vec { + fn try_fold_with>(self, folder: &mut F) -> Result { self.try_map_id(|t| t.try_fold_with(folder)) } } -impl<'tcx, T: TypeVisitable<'tcx>> ir::TypeVisitable> for Vec { - fn visit_with>(&self, visitor: &mut V) -> ControlFlow { +impl> ir::TypeVisitable for Vec { + fn visit_with>(&self, visitor: &mut V) -> ControlFlow { self.iter().try_for_each(|t| t.visit_with(visitor)) } } -impl<'tcx, T: TypeVisitable<'tcx>> ir::TypeVisitable> for &[T] { - fn visit_with>(&self, visitor: &mut V) -> ControlFlow { +impl> ir::TypeVisitable for &[T] { + fn visit_with>(&self, visitor: &mut V) -> ControlFlow { self.iter().try_for_each(|t| t.visit_with(visitor)) } } -impl<'tcx, T: TypeFoldable<'tcx>> ir::TypeFoldable> for Box<[T]> { - fn try_fold_with>(self, folder: &mut F) -> Result { +impl> ir::TypeFoldable for Box<[T]> { + fn try_fold_with>(self, folder: &mut F) -> Result { self.try_map_id(|t| t.try_fold_with(folder)) } } -impl<'tcx, T: TypeVisitable<'tcx>> ir::TypeVisitable> for Box<[T]> { - fn visit_with>(&self, visitor: &mut V) -> ControlFlow { +impl> ir::TypeVisitable for Box<[T]> { + fn visit_with>(&self, visitor: &mut V) -> ControlFlow { self.iter().try_for_each(|t| t.visit_with(visitor)) } } @@ -790,14 +790,14 @@ fn try_fold_with>(self, folder: &mut F) -> Result, I: Idx> ir::TypeFoldable> for IndexVec { - fn try_fold_with>(self, folder: &mut F) -> Result { +impl, Ix: Idx> ir::TypeFoldable for IndexVec { + fn try_fold_with>(self, folder: &mut F) -> Result { self.try_map_id(|x| x.try_fold_with(folder)) } } -impl<'tcx, T: TypeVisitable<'tcx>, I: Idx> ir::TypeVisitable> for IndexVec { - fn visit_with>(&self, visitor: &mut V) -> ControlFlow { +impl, Ix: Idx> ir::TypeVisitable for IndexVec { + fn visit_with>(&self, visitor: &mut V) -> ControlFlow { self.iter().try_for_each(|t| t.visit_with(visitor)) } }