From 4460a1dc28a10cd74a6e5636d6e2cd8c8f06ba4b Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Sat, 15 Apr 2023 09:31:11 +1000 Subject: [PATCH] Remove `TypeSuper{Foldable,Visitable}` impls for `Region`. These traits exist so that folders/visitors can recurse into types of interest: binders, types, regions, predicates, and consts. But `Region` is non-recursive and cannot contain other types of interest, so its methods in these traits are trivial. This commit inlines and removes those trivial methods. --- .../rustc_hir_analysis/src/variance/mod.rs | 2 +- .../trait_impl_difference.rs | 4 +- compiler/rustc_middle/src/ty/fold.rs | 3 +- compiler/rustc_middle/src/ty/print/pretty.rs | 2 +- .../rustc_middle/src/ty/structural_impls.rs | 18 --------- compiler/rustc_middle/src/ty/visit.rs | 2 +- .../rustc_trait_selection/src/traits/mod.rs | 2 +- compiler/rustc_traits/src/chalk/lowering.rs | 8 ++-- compiler/rustc_type_ir/src/fold.rs | 37 +++++++++---------- compiler/rustc_type_ir/src/visit.rs | 28 ++++++++------ src/librustdoc/clean/auto_trait.rs | 11 +++--- 11 files changed, 51 insertions(+), 66 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/variance/mod.rs b/compiler/rustc_hir_analysis/src/variance/mod.rs index 0a45119ff055b..4d240e90b143d 100644 --- a/compiler/rustc_hir_analysis/src/variance/mod.rs +++ b/compiler/rustc_hir_analysis/src/variance/mod.rs @@ -105,7 +105,7 @@ fn variance_of_opaque(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Varianc if let ty::RegionKind::ReEarlyBound(ebr) = r.kind() { self.variances[ebr.index as usize] = ty::Invariant; } - r.super_visit_with(self) + ControlFlow::Continue(()) } #[instrument(level = "trace", skip(self), ret)] diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs index 2875448ee157c..ce70bcc5c8511 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs @@ -13,7 +13,7 @@ use rustc_hir::intravisit::Visitor; use rustc_middle::hir::nested_filter; use rustc_middle::ty::error::ExpectedFound; use rustc_middle::ty::print::RegionHighlightMode; -use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor}; +use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitor}; use rustc_span::Span; use std::ops::ControlFlow; @@ -81,7 +81,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { self.highlight.highlighting_region(r, self.counter); self.counter += 1; } - r.super_visit_with(self) + ControlFlow::Continue(()) } } diff --git a/compiler/rustc_middle/src/ty/fold.rs b/compiler/rustc_middle/src/ty/fold.rs index 203e16bea27fe..25890eb15cde4 100644 --- a/compiler/rustc_middle/src/ty/fold.rs +++ b/compiler/rustc_middle/src/ty/fold.rs @@ -37,7 +37,8 @@ where } fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { - let r = r.super_fold_with(self); + // This one is a little different, because `super_fold_with` is not + // implemented on non-recursive `Region`. (self.lt_op)(r) } diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 72caadaf66199..af76cf7cc4e0d 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -2518,7 +2518,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> { self.used_region_names.insert(name); } - r.super_visit_with(self) + ControlFlow::Continue(()) } // We collect types in order to prevent really large types from compiling for diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 5c604bb6db274..619fcea8b7d4b 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -583,24 +583,6 @@ impl<'tcx> TypeVisitable> for ty::Region<'tcx> { } } -impl<'tcx> TypeSuperFoldable> for ty::Region<'tcx> { - fn try_super_fold_with>>( - self, - _folder: &mut F, - ) -> Result { - Ok(self) - } -} - -impl<'tcx> TypeSuperVisitable> for ty::Region<'tcx> { - fn super_visit_with>>( - &self, - _visitor: &mut V, - ) -> ControlFlow { - ControlFlow::Continue(()) - } -} - impl<'tcx> TypeFoldable> for ty::Predicate<'tcx> { fn try_fold_with>>( self, diff --git a/compiler/rustc_middle/src/ty/visit.rs b/compiler/rustc_middle/src/ty/visit.rs index 08a62c900f972..24a1f04c7e388 100644 --- a/compiler/rustc_middle/src/ty/visit.rs +++ b/compiler/rustc_middle/src/ty/visit.rs @@ -364,7 +364,7 @@ impl<'tcx> TypeVisitor> for ValidateBoundVars<'tcx> { _ => (), }; - r.super_visit_with(self) + ControlFlow::Continue(()) } } diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index 8a203dec86bab..ed82b9c015277 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -450,7 +450,7 @@ fn is_impossible_method(tcx: TyCtxt<'_>, (impl_def_id, trait_item_def_id): (DefI { return ControlFlow::Break(()); } - r.super_visit_with(self) + ControlFlow::Continue(()) } fn visit_const(&mut self, ct: ty::Const<'tcx>) -> ControlFlow { if let ty::ConstKind::Param(param) = ct.kind() diff --git a/compiler/rustc_traits/src/chalk/lowering.rs b/compiler/rustc_traits/src/chalk/lowering.rs index 2be72879b7b13..31eea22d72b88 100644 --- a/compiler/rustc_traits/src/chalk/lowering.rs +++ b/compiler/rustc_traits/src/chalk/lowering.rs @@ -998,7 +998,7 @@ impl<'tcx> TypeVisitor> for BoundVarsCollector<'tcx> { _ => (), }; - r.super_visit_with(self) + ControlFlow::Continue(()) } } @@ -1048,7 +1048,7 @@ impl<'a, 'tcx> TypeFolder> for NamedBoundVarSubstitutor<'a, 'tcx> { _ => (), }; - r.super_fold_with(self) + r } } @@ -1142,7 +1142,7 @@ impl<'tcx> TypeFolder> for ParamsSubstitutor<'tcx> { } }, - _ => r.super_fold_with(self), + _ => r, } } } @@ -1223,6 +1223,6 @@ impl<'tcx> TypeVisitor> for PlaceholdersCollector { _ => (), }; - r.super_visit_with(self) + ControlFlow::Continue(()) } } diff --git a/compiler/rustc_type_ir/src/fold.rs b/compiler/rustc_type_ir/src/fold.rs index 3a053d4c6a997..371c611912286 100644 --- a/compiler/rustc_type_ir/src/fold.rs +++ b/compiler/rustc_type_ir/src/fold.rs @@ -16,8 +16,10 @@ //! - Types of interest, for which the methods delegate to the folder. //! - All other types, including generic containers like `Vec` and `Option`. //! It defines a "skeleton" of how they should be folded. -//! - `TypeSuperFoldable`. This is implemented only for each type of interest, -//! and defines the folding "skeleton" for these types. +//! - `TypeSuperFoldable`. This is implemented only for recursive types of +//! interest, and defines the folding "skeleton" for these types. (This +//! excludes `Region` because it is non-recursive, i.e. it never contains +//! other types of interest.) //! - `TypeFolder`/`FallibleTypeFolder`. One of these is implemented for each //! folder. This defines how types of interest are folded. //! @@ -72,9 +74,9 @@ pub trait TypeFoldable: TypeVisitable { // This trait is implemented for types of interest. pub trait TypeSuperFoldable: TypeFoldable { - /// Provides a default fold for a type of interest. This should only be - /// called within `TypeFolder` methods, when a non-custom traversal is - /// desired for the value of the type of interest passed to that method. + /// Provides a default fold for a recursive type of interest. This should + /// only be called within `TypeFolder` methods, when a non-custom traversal + /// is desired for the value of the type of interest passed to that method. /// For example, in `MyFolder::try_fold_ty(ty)`, it is valid to call /// `ty.try_super_fold_with(self)`, but any other folding should be done /// with `xyz.try_fold_with(self)`. @@ -118,11 +120,11 @@ pub trait TypeFolder: FallibleTypeFolder { t.super_fold_with(self) } - fn fold_region(&mut self, r: I::Region) -> I::Region - where - I::Region: TypeSuperFoldable, - { - r.super_fold_with(self) + // The default region folder is a no-op because `Region` is non-recursive + // and has no `super_visit_with` method to call. That also explains the + // lack of `I::Region: TypeSuperFoldable` bound on this method. + fn fold_region(&mut self, r: I::Region) -> I::Region { + r } fn fold_const(&mut self, c: I::Const) -> I::Const @@ -167,11 +169,11 @@ pub trait FallibleTypeFolder: Sized { t.try_super_fold_with(self) } - fn try_fold_region(&mut self, r: I::Region) -> Result - where - I::Region: TypeSuperFoldable, - { - r.try_super_fold_with(self) + // The default region folder is a no-op because `Region` is non-recursive + // and has no `super_visit_with` method to call. That also explains the + // lack of `I::Region: TypeSuperFoldable` bound on this method. + fn try_fold_region(&mut self, r: I::Region) -> Result { + Ok(r) } fn try_fold_const(&mut self, c: I::Const) -> Result @@ -216,10 +218,7 @@ where Ok(self.fold_ty(t)) } - fn try_fold_region(&mut self, r: I::Region) -> Result - where - I::Region: TypeSuperFoldable, - { + fn try_fold_region(&mut self, r: I::Region) -> Result { Ok(self.fold_region(r)) } diff --git a/compiler/rustc_type_ir/src/visit.rs b/compiler/rustc_type_ir/src/visit.rs index 62239fd20066a..878c7aec6c18e 100644 --- a/compiler/rustc_type_ir/src/visit.rs +++ b/compiler/rustc_type_ir/src/visit.rs @@ -13,8 +13,11 @@ //! - Types of interest, for which the methods delegate to the visitor. //! - All other types, including generic containers like `Vec` and `Option`. //! It defines a "skeleton" of how they should be visited. -//! - `TypeSuperVisitable`. This is implemented only for each type of interest, -//! and defines the visiting "skeleton" for these types. +//! - `TypeSuperVisitable`. This is implemented only for recursive types of +//! interest, and defines the visiting "skeleton" for these types. (This +//! excludes `Region` because it is non-recursive, i.e. it never contains +//! other types of interest.) +//! //! - `TypeVisitor`. This is implemented for each visitor. This defines how //! types of interest are visited. //! @@ -62,12 +65,13 @@ pub trait TypeVisitable: fmt::Debug + Clone { fn visit_with>(&self, visitor: &mut V) -> ControlFlow; } +// This trait is implemented for types of interest. pub trait TypeSuperVisitable: TypeVisitable { - /// Provides a default visit for a type of interest. This should only be - /// called within `TypeVisitor` methods, when a non-custom traversal is - /// desired for the value of the type of interest passed to that method. - /// For example, in `MyVisitor::visit_ty(ty)`, it is valid to call - /// `ty.super_visit_with(self)`, but any other visiting should be done + /// Provides a default visit for a recursive type of interest. This should + /// only be called within `TypeVisitor` methods, when a non-custom + /// traversal is desired for the value of the type of interest passed to + /// that method. For example, in `MyVisitor::visit_ty(ty)`, it is valid to + /// call `ty.super_visit_with(self)`, but any other visiting should be done /// with `xyz.visit_with(self)`. fn super_visit_with>(&self, visitor: &mut V) -> ControlFlow; } @@ -92,11 +96,11 @@ pub trait TypeVisitor: Sized { t.super_visit_with(self) } - fn visit_region(&mut self, r: I::Region) -> ControlFlow - where - I::Region: TypeSuperVisitable, - { - r.super_visit_with(self) + // The default region visitor is a no-op because `Region` is non-recursive + // and has no `super_visit_with` method to call. That also explains the + // lack of `I::Region: TypeSuperVisitable` bound. + fn visit_region(&mut self, _r: I::Region) -> ControlFlow { + ControlFlow::Continue(()) } fn visit_const(&mut self, c: I::Const) -> ControlFlow diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index 9479b3ee0369a..f6088f4e64a41 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -1,7 +1,7 @@ use rustc_data_structures::fx::FxHashSet; use rustc_hir as hir; use rustc_hir::lang_items::LangItem; -use rustc_middle::ty::{self, Region, RegionVid, TypeFoldable, TypeSuperFoldable}; +use rustc_middle::ty::{self, Region, RegionVid, TypeFoldable}; use rustc_trait_selection::traits::auto_trait::{self, AutoTraitResult}; use thin_vec::ThinVec; @@ -740,10 +740,9 @@ impl<'a, 'tcx> TypeFolder> for RegionReplacer<'a, 'tcx> { } fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { - (match *r { - ty::ReVar(vid) => self.vid_to_region.get(&vid).cloned(), - _ => None, - }) - .unwrap_or_else(|| r.super_fold_with(self)) + match *r { + ty::ReVar(vid) => self.vid_to_region.get(&vid).cloned().unwrap_or(r), + _ => r, + } } }