Skip to content

Commit 4460a1d

Browse files
committed
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.
1 parent d558796 commit 4460a1d

File tree

11 files changed

+51
-66
lines changed

11 files changed

+51
-66
lines changed

compiler/rustc_hir_analysis/src/variance/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ fn variance_of_opaque(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Varianc
105105
if let ty::RegionKind::ReEarlyBound(ebr) = r.kind() {
106106
self.variances[ebr.index as usize] = ty::Invariant;
107107
}
108-
r.super_visit_with(self)
108+
ControlFlow::Continue(())
109109
}
110110

111111
#[instrument(level = "trace", skip(self), ret)]

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_hir::intravisit::Visitor;
1313
use rustc_middle::hir::nested_filter;
1414
use rustc_middle::ty::error::ExpectedFound;
1515
use rustc_middle::ty::print::RegionHighlightMode;
16-
use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor};
16+
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitor};
1717
use rustc_span::Span;
1818

1919
use std::ops::ControlFlow;
@@ -81,7 +81,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
8181
self.highlight.highlighting_region(r, self.counter);
8282
self.counter += 1;
8383
}
84-
r.super_visit_with(self)
84+
ControlFlow::Continue(())
8585
}
8686
}
8787

compiler/rustc_middle/src/ty/fold.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ where
3737
}
3838

3939
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
40-
let r = r.super_fold_with(self);
40+
// This one is a little different, because `super_fold_with` is not
41+
// implemented on non-recursive `Region`.
4142
(self.lt_op)(r)
4243
}
4344

compiler/rustc_middle/src/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2518,7 +2518,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
25182518
self.used_region_names.insert(name);
25192519
}
25202520

2521-
r.super_visit_with(self)
2521+
ControlFlow::Continue(())
25222522
}
25232523

25242524
// We collect types in order to prevent really large types from compiling for

compiler/rustc_middle/src/ty/structural_impls.rs

-18
Original file line numberDiff line numberDiff line change
@@ -583,24 +583,6 @@ impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Region<'tcx> {
583583
}
584584
}
585585

586-
impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Region<'tcx> {
587-
fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
588-
self,
589-
_folder: &mut F,
590-
) -> Result<Self, F::Error> {
591-
Ok(self)
592-
}
593-
}
594-
595-
impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for ty::Region<'tcx> {
596-
fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(
597-
&self,
598-
_visitor: &mut V,
599-
) -> ControlFlow<V::BreakTy> {
600-
ControlFlow::Continue(())
601-
}
602-
}
603-
604586
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::Predicate<'tcx> {
605587
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
606588
self,

compiler/rustc_middle/src/ty/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ValidateBoundVars<'tcx> {
364364
_ => (),
365365
};
366366

367-
r.super_visit_with(self)
367+
ControlFlow::Continue(())
368368
}
369369
}
370370

compiler/rustc_trait_selection/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ fn is_impossible_method(tcx: TyCtxt<'_>, (impl_def_id, trait_item_def_id): (DefI
450450
{
451451
return ControlFlow::Break(());
452452
}
453-
r.super_visit_with(self)
453+
ControlFlow::Continue(())
454454
}
455455
fn visit_const(&mut self, ct: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
456456
if let ty::ConstKind::Param(param) = ct.kind()

compiler/rustc_traits/src/chalk/lowering.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for BoundVarsCollector<'tcx> {
998998
_ => (),
999999
};
10001000

1001-
r.super_visit_with(self)
1001+
ControlFlow::Continue(())
10021002
}
10031003
}
10041004

@@ -1048,7 +1048,7 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for NamedBoundVarSubstitutor<'a, 'tcx> {
10481048
_ => (),
10491049
};
10501050

1051-
r.super_fold_with(self)
1051+
r
10521052
}
10531053
}
10541054

@@ -1142,7 +1142,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ParamsSubstitutor<'tcx> {
11421142
}
11431143
},
11441144

1145-
_ => r.super_fold_with(self),
1145+
_ => r,
11461146
}
11471147
}
11481148
}
@@ -1223,6 +1223,6 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for PlaceholdersCollector {
12231223
_ => (),
12241224
};
12251225

1226-
r.super_visit_with(self)
1226+
ControlFlow::Continue(())
12271227
}
12281228
}

compiler/rustc_type_ir/src/fold.rs

+18-19
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
//! - Types of interest, for which the methods delegate to the folder.
1717
//! - All other types, including generic containers like `Vec` and `Option`.
1818
//! It defines a "skeleton" of how they should be folded.
19-
//! - `TypeSuperFoldable`. This is implemented only for each type of interest,
20-
//! and defines the folding "skeleton" for these types.
19+
//! - `TypeSuperFoldable`. This is implemented only for recursive types of
20+
//! interest, and defines the folding "skeleton" for these types. (This
21+
//! excludes `Region` because it is non-recursive, i.e. it never contains
22+
//! other types of interest.)
2123
//! - `TypeFolder`/`FallibleTypeFolder`. One of these is implemented for each
2224
//! folder. This defines how types of interest are folded.
2325
//!
@@ -72,9 +74,9 @@ pub trait TypeFoldable<I: Interner>: TypeVisitable<I> {
7274

7375
// This trait is implemented for types of interest.
7476
pub trait TypeSuperFoldable<I: Interner>: TypeFoldable<I> {
75-
/// Provides a default fold for a type of interest. This should only be
76-
/// called within `TypeFolder` methods, when a non-custom traversal is
77-
/// desired for the value of the type of interest passed to that method.
77+
/// Provides a default fold for a recursive type of interest. This should
78+
/// only be called within `TypeFolder` methods, when a non-custom traversal
79+
/// is desired for the value of the type of interest passed to that method.
7880
/// For example, in `MyFolder::try_fold_ty(ty)`, it is valid to call
7981
/// `ty.try_super_fold_with(self)`, but any other folding should be done
8082
/// with `xyz.try_fold_with(self)`.
@@ -118,11 +120,11 @@ pub trait TypeFolder<I: Interner>: FallibleTypeFolder<I, Error = !> {
118120
t.super_fold_with(self)
119121
}
120122

121-
fn fold_region(&mut self, r: I::Region) -> I::Region
122-
where
123-
I::Region: TypeSuperFoldable<I>,
124-
{
125-
r.super_fold_with(self)
123+
// The default region folder is a no-op because `Region` is non-recursive
124+
// and has no `super_visit_with` method to call. That also explains the
125+
// lack of `I::Region: TypeSuperFoldable<I>` bound on this method.
126+
fn fold_region(&mut self, r: I::Region) -> I::Region {
127+
r
126128
}
127129

128130
fn fold_const(&mut self, c: I::Const) -> I::Const
@@ -167,11 +169,11 @@ pub trait FallibleTypeFolder<I: Interner>: Sized {
167169
t.try_super_fold_with(self)
168170
}
169171

170-
fn try_fold_region(&mut self, r: I::Region) -> Result<I::Region, Self::Error>
171-
where
172-
I::Region: TypeSuperFoldable<I>,
173-
{
174-
r.try_super_fold_with(self)
172+
// The default region folder is a no-op because `Region` is non-recursive
173+
// and has no `super_visit_with` method to call. That also explains the
174+
// lack of `I::Region: TypeSuperFoldable<I>` bound on this method.
175+
fn try_fold_region(&mut self, r: I::Region) -> Result<I::Region, Self::Error> {
176+
Ok(r)
175177
}
176178

177179
fn try_fold_const(&mut self, c: I::Const) -> Result<I::Const, Self::Error>
@@ -216,10 +218,7 @@ where
216218
Ok(self.fold_ty(t))
217219
}
218220

219-
fn try_fold_region(&mut self, r: I::Region) -> Result<I::Region, !>
220-
where
221-
I::Region: TypeSuperFoldable<I>,
222-
{
221+
fn try_fold_region(&mut self, r: I::Region) -> Result<I::Region, !> {
223222
Ok(self.fold_region(r))
224223
}
225224

compiler/rustc_type_ir/src/visit.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
//! - Types of interest, for which the methods delegate to the visitor.
1414
//! - All other types, including generic containers like `Vec` and `Option`.
1515
//! It defines a "skeleton" of how they should be visited.
16-
//! - `TypeSuperVisitable`. This is implemented only for each type of interest,
17-
//! and defines the visiting "skeleton" for these types.
16+
//! - `TypeSuperVisitable`. This is implemented only for recursive types of
17+
//! interest, and defines the visiting "skeleton" for these types. (This
18+
//! excludes `Region` because it is non-recursive, i.e. it never contains
19+
//! other types of interest.)
20+
//!
1821
//! - `TypeVisitor`. This is implemented for each visitor. This defines how
1922
//! types of interest are visited.
2023
//!
@@ -62,12 +65,13 @@ pub trait TypeVisitable<I: Interner>: fmt::Debug + Clone {
6265
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy>;
6366
}
6467

68+
// This trait is implemented for types of interest.
6569
pub trait TypeSuperVisitable<I: Interner>: TypeVisitable<I> {
66-
/// Provides a default visit for a type of interest. This should only be
67-
/// called within `TypeVisitor` methods, when a non-custom traversal is
68-
/// desired for the value of the type of interest passed to that method.
69-
/// For example, in `MyVisitor::visit_ty(ty)`, it is valid to call
70-
/// `ty.super_visit_with(self)`, but any other visiting should be done
70+
/// Provides a default visit for a recursive type of interest. This should
71+
/// only be called within `TypeVisitor` methods, when a non-custom
72+
/// traversal is desired for the value of the type of interest passed to
73+
/// that method. For example, in `MyVisitor::visit_ty(ty)`, it is valid to
74+
/// call `ty.super_visit_with(self)`, but any other visiting should be done
7175
/// with `xyz.visit_with(self)`.
7276
fn super_visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy>;
7377
}
@@ -92,11 +96,11 @@ pub trait TypeVisitor<I: Interner>: Sized {
9296
t.super_visit_with(self)
9397
}
9498

95-
fn visit_region(&mut self, r: I::Region) -> ControlFlow<Self::BreakTy>
96-
where
97-
I::Region: TypeSuperVisitable<I>,
98-
{
99-
r.super_visit_with(self)
99+
// The default region visitor is a no-op because `Region` is non-recursive
100+
// and has no `super_visit_with` method to call. That also explains the
101+
// lack of `I::Region: TypeSuperVisitable<I>` bound.
102+
fn visit_region(&mut self, _r: I::Region) -> ControlFlow<Self::BreakTy> {
103+
ControlFlow::Continue(())
100104
}
101105

102106
fn visit_const(&mut self, c: I::Const) -> ControlFlow<Self::BreakTy>

src/librustdoc/clean/auto_trait.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_data_structures::fx::FxHashSet;
22
use rustc_hir as hir;
33
use rustc_hir::lang_items::LangItem;
4-
use rustc_middle::ty::{self, Region, RegionVid, TypeFoldable, TypeSuperFoldable};
4+
use rustc_middle::ty::{self, Region, RegionVid, TypeFoldable};
55
use rustc_trait_selection::traits::auto_trait::{self, AutoTraitResult};
66
use thin_vec::ThinVec;
77

@@ -740,10 +740,9 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for RegionReplacer<'a, 'tcx> {
740740
}
741741

742742
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
743-
(match *r {
744-
ty::ReVar(vid) => self.vid_to_region.get(&vid).cloned(),
745-
_ => None,
746-
})
747-
.unwrap_or_else(|| r.super_fold_with(self))
743+
match *r {
744+
ty::ReVar(vid) => self.vid_to_region.get(&vid).cloned().unwrap_or(r),
745+
_ => r,
746+
}
748747
}
749748
}

0 commit comments

Comments
 (0)