Skip to content

Commit 90db033

Browse files
committed
Folding revamp.
This commit makes type folding more like the way chalk does it. Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods. - `fold_with` is the standard entry point, and defaults to calling `super_fold_with`. - `super_fold_with` does the actual work of traversing a type. - For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead calls into a `TypeFolder`, which can then call back into `super_fold_with`. With the new approach, `TypeFoldable` has `fold_with` and `TypeSuperFoldable` has `super_fold_with`. - `fold_with` is still the standard entry point, *and* it does the actual work of traversing a type, for all types except types of interest. - `super_fold_with` is only implemented for the types of interest. Benefits of the new model. - I find it easier to understand. The distinction between types of interest and other types is clearer, and `super_fold_with` doesn't exist for most types. - With the current model is easy to get confused and implement a `super_fold_with` method that should be left defaulted. (Some of the precursor commits fixed such cases.) - With the current model it's easy to call `super_fold_with` within `TypeFolder` impls where `fold_with` should be called. The new approach makes this mistake impossible, and this commit fixes a number of such cases. - It's potentially faster, because it avoids the `fold_with` -> `super_fold_with` call in all cases except types of interest. A lot of the time the compile would inline those away, but not necessarily always.
1 parent 7480b50 commit 90db033

File tree

47 files changed

+287
-332
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+287
-332
lines changed

compiler/rustc_const_eval/src/interpret/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_middle::mir::interpret::InterpResult;
2-
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeVisitor};
2+
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable, TypeVisitor};
33
use std::convert::TryInto;
44
use std::ops::ControlFlow;
55

@@ -47,7 +47,7 @@ where
4747
match (is_used, subst.needs_subst()) {
4848
// Just in case there are closures or generators within this subst,
4949
// recurse.
50-
(true, true) => return subst.super_visit_with(self),
50+
(true, true) => return subst.visit_with(self),
5151
// Confirm that polymorphization replaced the parameter with
5252
// `ty::Param`/`ty::ConstKind::Param`.
5353
(false, true) if cfg!(debug_assertions) => match subst.unpack() {

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::infer::canonical::{
1111
};
1212
use crate::infer::InferCtxt;
1313
use rustc_middle::ty::flags::FlagComputation;
14-
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
14+
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
1515
use rustc_middle::ty::subst::GenericArg;
1616
use rustc_middle::ty::{self, BoundVar, InferConst, List, Ty, TyCtxt, TypeFlags};
1717
use std::sync::atomic::Ordering;

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use rustc_middle::ty::{
7070
self,
7171
error::TypeError,
7272
subst::{GenericArgKind, Subst, SubstsRef},
73-
Binder, EarlyBinder, List, Region, Ty, TyCtxt, TypeFoldable,
73+
Binder, EarlyBinder, List, Region, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable,
7474
};
7575
use rustc_span::{sym, symbol::kw, BytePos, DesugaringKind, Pos, Span};
7676
use rustc_target::spec::abi;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hir::def_id::DefId;
1010
use rustc_hir::intravisit::{walk_ty, Visitor};
1111
use rustc_hir::{self as hir, GenericBound, Item, ItemKind, Lifetime, LifetimeName, Node, TyKind};
1212
use rustc_middle::ty::{
13-
self, AssocItemContainer, StaticLifetimeVisitor, Ty, TyCtxt, TypeFoldable, TypeVisitor,
13+
self, AssocItemContainer, StaticLifetimeVisitor, Ty, TyCtxt, TypeSuperFoldable, TypeVisitor,
1414
};
1515
use rustc_span::symbol::Ident;
1616
use rustc_span::Span;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
1111
use rustc_hir::intravisit::Visitor;
1212
use rustc_middle::hir::nested_filter;
1313
use rustc_middle::ty::print::RegionHighlightMode;
14-
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeVisitor};
14+
use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperFoldable, TypeVisitor};
1515
use rustc_span::{Span, Symbol};
1616

1717
use std::ops::ControlFlow;

compiler/rustc_infer/src/infer/freshen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use super::InferCtxt;
3434
use rustc_data_structures::fx::FxHashMap;
3535
use rustc_middle::infer::unify_key::ToType;
3636
use rustc_middle::ty::fold::TypeFolder;
37-
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
37+
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable};
3838
use std::collections::hash_map::Entry;
3939

4040
pub struct TypeFreshener<'a, 'tcx> {

compiler/rustc_infer/src/infer/fudge.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
1+
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
22
use rustc_middle::ty::{self, ConstVid, FloatVid, IntVid, RegionVid, Ty, TyCtxt, TyVid};
33

44
use super::type_variable::TypeVariableOrigin;

compiler/rustc_infer/src/infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKin
2323
use rustc_middle::mir::interpret::{ErrorHandled, EvalToConstValueResult};
2424
use rustc_middle::traits::select;
2525
use rustc_middle::ty::error::{ExpectedFound, TypeError};
26-
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
26+
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
2727
use rustc_middle::ty::relate::RelateResult;
2828
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, SubstsRef};
2929
pub use rustc_middle::ty::IntVarValue;

compiler/rustc_infer/src/infer/nll_relate/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::infer::{ConstVarValue, ConstVariableValue};
2727
use crate::infer::{TypeVariableOrigin, TypeVariableOriginKind};
2828
use rustc_data_structures::fx::FxHashMap;
2929
use rustc_middle::ty::error::TypeError;
30-
use rustc_middle::ty::fold::{TypeFoldable, TypeVisitor};
30+
use rustc_middle::ty::fold::{TypeFoldable, TypeSuperFoldable, TypeVisitor};
3131
use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation};
3232
use rustc_middle::ty::{self, InferConst, Ty, TyCtxt};
3333
use rustc_span::Span;

compiler/rustc_infer/src/infer/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::traits::ObligationCause;
99
use rustc_middle::ty::fold::BottomUpFolder;
1010
use rustc_middle::ty::subst::{GenericArgKind, Subst};
1111
use rustc_middle::ty::{
12-
self, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable, TypeVisitor,
12+
self, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable, TypeVisitor,
1313
};
1414
use rustc_span::Span;
1515

compiler/rustc_infer/src/infer/resolve.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
22
use super::{FixupError, FixupResult, InferCtxt, Span};
33
use rustc_middle::mir;
4-
use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFolder, TypeVisitor};
4+
use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFolder, TypeSuperFoldable, TypeVisitor};
55
use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable};
66

77
use std::ops::ControlFlow;

compiler/rustc_infer/src/traits/structural_impls.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ impl<'tcx> fmt::Debug for traits::MismatchedProjectionTypes<'tcx> {
6060
// TypeFoldable implementations.
6161

6262
impl<'tcx, O: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::Obligation<'tcx, O> {
63-
fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
64-
self,
65-
folder: &mut F,
66-
) -> Result<Self, F::Error> {
63+
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
6764
Ok(traits::Obligation {
6865
cause: self.cause,
6966
recursion_depth: self.recursion_depth,
@@ -72,7 +69,7 @@ impl<'tcx, O: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::Obligation<'tcx
7269
})
7370
}
7471

75-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
72+
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
7673
self.predicate.visit_with(visitor)?;
7774
self.param_env.visit_with(visitor)
7875
}

compiler/rustc_lint/src/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::def_id::DefId;
88
use rustc_hir::{is_range_literal, Expr, ExprKind, Node};
99
use rustc_middle::ty::layout::{IntegerExt, LayoutOf, SizeSkeleton};
1010
use rustc_middle::ty::subst::SubstsRef;
11-
use rustc_middle::ty::{self, AdtKind, DefIdTree, Ty, TyCtxt, TypeFoldable};
11+
use rustc_middle::ty::{self, AdtKind, DefIdTree, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable};
1212
use rustc_span::source_map;
1313
use rustc_span::symbol::sym;
1414
use rustc_span::{Span, Symbol, DUMMY_SP};

compiler/rustc_macros/src/type_foldable.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ pub fn type_foldable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::
3030
s.bound_impl(
3131
quote!(::rustc_middle::ty::fold::TypeFoldable<'tcx>),
3232
quote! {
33-
fn try_super_fold_with<__F: ::rustc_middle::ty::fold::FallibleTypeFolder<'tcx>>(
33+
fn try_fold_with<__F: ::rustc_middle::ty::fold::FallibleTypeFolder<'tcx>>(
3434
self,
3535
__folder: &mut __F
3636
) -> Result<Self, __F::Error> {
3737
Ok(match self { #body_fold })
3838
}
3939

40-
fn super_visit_with<__F: ::rustc_middle::ty::fold::TypeVisitor<'tcx>>(
40+
fn visit_with<__F: ::rustc_middle::ty::fold::TypeVisitor<'tcx>>(
4141
&self,
4242
__folder: &mut __F
4343
) -> ::std::ops::ControlFlow<__F::BreakTy> {

compiler/rustc_middle/src/macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ macro_rules! TrivialTypeFoldableImpls {
5252
(for <$tcx:lifetime> { $($ty:ty,)+ }) => {
5353
$(
5454
impl<$tcx> $crate::ty::fold::TypeFoldable<$tcx> for $ty {
55-
fn try_super_fold_with<F: $crate::ty::fold::FallibleTypeFolder<$tcx>>(
55+
fn try_fold_with<F: $crate::ty::fold::FallibleTypeFolder<$tcx>>(
5656
self,
5757
_: &mut F
5858
) -> ::std::result::Result<$ty, F::Error> {
5959
Ok(self)
6060
}
6161

62-
fn super_visit_with<F: $crate::ty::fold::TypeVisitor<$tcx>>(
62+
fn visit_with<F: $crate::ty::fold::TypeVisitor<$tcx>>(
6363
&self,
6464
_: &mut F)
6565
-> ::std::ops::ControlFlow<F::BreakTy>
@@ -95,14 +95,14 @@ macro_rules! EnumTypeFoldableImpl {
9595
impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s
9696
$(where $($wc)*)*
9797
{
98-
fn try_super_fold_with<V: $crate::ty::fold::FallibleTypeFolder<$tcx>>(
98+
fn try_fold_with<V: $crate::ty::fold::FallibleTypeFolder<$tcx>>(
9999
self,
100100
folder: &mut V,
101101
) -> ::std::result::Result<Self, V::Error> {
102102
EnumTypeFoldableImpl!(@FoldVariants(self, folder) input($($variants)*) output())
103103
}
104104

105-
fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
105+
fn visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
106106
&self,
107107
visitor: &mut V,
108108
) -> ::std::ops::ControlFlow<V::BreakTy> {

compiler/rustc_middle/src/mir/mod.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::mir::interpret::{ConstAllocation, ConstValue, GlobalAlloc, LitToConst
77
use crate::mir::visit::MirVisitable;
88
use crate::ty::adjustment::PointerCast;
99
use crate::ty::codec::{TyDecoder, TyEncoder};
10-
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeVisitor};
10+
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable, TypeVisitor};
1111
use crate::ty::print::{FmtPrinter, Printer};
1212
use crate::ty::subst::{GenericArg, InternalSubsts, Subst, SubstsRef};
1313
use crate::ty::{self, List, Ty, TyCtxt};
@@ -3399,20 +3399,14 @@ impl UserTypeProjection {
33993399
TrivialTypeFoldableAndLiftImpls! { ProjectionKind, }
34003400

34013401
impl<'tcx> TypeFoldable<'tcx> for UserTypeProjection {
3402-
fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
3403-
self,
3404-
folder: &mut F,
3405-
) -> Result<Self, F::Error> {
3402+
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
34063403
Ok(UserTypeProjection {
34073404
base: self.base.try_fold_with(folder)?,
34083405
projs: self.projs.try_fold_with(folder)?,
34093406
})
34103407
}
34113408

3412-
fn super_visit_with<Vs: TypeVisitor<'tcx>>(
3413-
&self,
3414-
visitor: &mut Vs,
3415-
) -> ControlFlow<Vs::BreakTy> {
3409+
fn visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> ControlFlow<Vs::BreakTy> {
34163410
self.base.visit_with(visitor)
34173411
// Note: there's nothing in `self.proj` to visit.
34183412
}

0 commit comments

Comments
 (0)