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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)