1
Fork 0
This commit is contained in:
lcnr 2020-10-24 09:27:15 +02:00 committed by Bastian Kauschke
parent 80acfea5a7
commit a6cbd64dae
19 changed files with 87 additions and 105 deletions

View file

@ -286,7 +286,7 @@ impl<'tcx, V> Canonical<'tcx, V> {
pub type QueryOutlivesConstraint<'tcx> =
ty::Binder<ty::OutlivesPredicate<GenericArg<'tcx>, Region<'tcx>>>;
CloneTypeFoldableAndLiftImpls! {
TrivialTypeFoldableAndLiftImpls! {
for <'tcx> {
crate::infer::canonical::Certainty,
crate::infer::canonical::CanonicalVarInfo<'tcx>,
@ -294,7 +294,7 @@ CloneTypeFoldableAndLiftImpls! {
}
}
CloneTypeFoldableImpls! {
TrivialTypeFoldableImpls! {
for <'tcx> {
crate::infer::canonical::CanonicalVarInfos<'tcx>,
}

View file

@ -48,7 +48,7 @@ macro_rules! CloneLiftImpls {
/// Used for types that are `Copy` and which **do not care arena
/// allocated data** (i.e., don't need to be folded).
#[macro_export]
macro_rules! CloneTypeFoldableImpls {
macro_rules! TrivialTypeFoldableImpls {
(for <$tcx:lifetime> { $($ty:ty,)+ }) => {
$(
impl<$tcx> $crate::ty::fold::TypeFoldable<$tcx> for $ty {
@ -71,7 +71,7 @@ macro_rules! CloneTypeFoldableImpls {
};
($($ty:ty,)+) => {
CloneTypeFoldableImpls! {
TrivialTypeFoldableImpls! {
for <'tcx> {
$($ty,)+
}
@ -80,9 +80,9 @@ macro_rules! CloneTypeFoldableImpls {
}
#[macro_export]
macro_rules! CloneTypeFoldableAndLiftImpls {
macro_rules! TrivialTypeFoldableAndLiftImpls {
($($t:tt)*) => {
CloneTypeFoldableImpls! { $($t)* }
TrivialTypeFoldableImpls! { $($t)* }
CloneLiftImpls! { $($t)* }
}
}

View file

@ -29,7 +29,7 @@ impl From<ErrorReported> for ErrorHandled {
}
}
CloneTypeFoldableAndLiftImpls! {
TrivialTypeFoldableAndLiftImpls! {
ErrorHandled,
}

View file

@ -742,7 +742,7 @@ pub enum ImplicitSelfKind {
None,
}
CloneTypeFoldableAndLiftImpls! { BindingForm<'tcx>, }
TrivialTypeFoldableAndLiftImpls! { BindingForm<'tcx>, }
mod binding_form_impl {
use crate::ich::StableHashingContext;
@ -2452,29 +2452,14 @@ impl UserTypeProjection {
}
}
CloneTypeFoldableAndLiftImpls! { ProjectionKind, }
TrivialTypeFoldableAndLiftImpls! { ProjectionKind, }
impl<'tcx> TypeFoldable<'tcx> for UserTypeProjection {
fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
use crate::mir::ProjectionElem::*;
let base = self.base.fold_with(folder);
let projs: Vec<_> = self
.projs
.iter()
.map(|&elem| match elem {
Deref => Deref,
Field(f, ()) => Field(f, ()),
Index(()) => Index(()),
Downcast(symbol, variantidx) => Downcast(symbol, variantidx),
ConstantIndex { offset, min_length, from_end } => {
ConstantIndex { offset, min_length, from_end }
}
Subslice { from, to, from_end } => Subslice { from, to, from_end },
})
.collect();
UserTypeProjection { base, projs }
UserTypeProjection {
base: self.base.fold_with(folder),
projs: self.projs.fold_with(folder),
}
}
fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> ControlFlow<()> {

View file

@ -75,6 +75,6 @@ impl<CTX> HashStable<CTX> for PredecessorCache {
}
}
CloneTypeFoldableAndLiftImpls! {
TrivialTypeFoldableAndLiftImpls! {
PredecessorCache,
}

View file

@ -4,7 +4,7 @@ use super::*;
use crate::ty;
use rustc_data_structures::functor::IdFunctor;
CloneTypeFoldableAndLiftImpls! {
TrivialTypeFoldableAndLiftImpls! {
BlockTailInfo,
MirPhase,
SourceInfo,
@ -24,7 +24,7 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
SwitchInt { discr, switch_ty, targets } => SwitchInt {
discr: discr.fold_with(folder),
switch_ty: switch_ty.fold_with(folder),
targets: targets.clone(),
targets,
},
Drop { place, target, unwind } => {
Drop { place: place.fold_with(folder), target, unwind }
@ -42,7 +42,7 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
drop,
},
Call { func, args, destination, cleanup, from_hir_call, fn_span } => {
let dest = destination.as_ref().map(|&(loc, dest)| (loc.fold_with(folder), dest));
let dest = destination.map(|(loc, dest)| (loc.fold_with(folder), dest));
Call {
func: func.fold_with(folder),
@ -63,7 +63,7 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
OverflowNeg(op) => OverflowNeg(op.fold_with(folder)),
DivisionByZero(op) => DivisionByZero(op.fold_with(folder)),
RemainderByZero(op) => RemainderByZero(op.fold_with(folder)),
ResumedAfterReturn(_) | ResumedAfterPanic(_) => msg.clone(),
ResumedAfterReturn(_) | ResumedAfterPanic(_) => msg,
};
Assert { cond: cond.fold_with(folder), expected, msg, target, cleanup }
}
@ -162,8 +162,7 @@ impl<'tcx> TypeFoldable<'tcx> for Place<'tcx> {
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<PlaceElem<'tcx>> {
fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
let v = self.iter().map(|t| t.fold_with(folder)).collect::<Vec<_>>();
folder.tcx().intern_place_elems(&v)
ty::util::fold_list(self, folder, |tcx, v| tcx.intern_place_elems(v))
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
@ -322,7 +321,7 @@ impl<'tcx> TypeFoldable<'tcx> for GeneratorSavedLocal {
impl<'tcx, R: Idx, C: Idx> TypeFoldable<'tcx> for BitMatrix<R, C> {
fn super_fold_with<F: TypeFolder<'tcx>>(self, _: &mut F) -> Self {
self.clone()
self
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<()> {
ControlFlow::CONTINUE

View file

@ -105,7 +105,7 @@ impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceTraitAliasData<'tcx,
///////////////////////////////////////////////////////////////////////////
// Lift implementations
CloneTypeFoldableAndLiftImpls! {
TrivialTypeFoldableAndLiftImpls! {
super::IfExpressionCause,
super::ImplSourceDiscriminantKindData,
}

View file

@ -8,7 +8,7 @@ pub enum BindingMode {
BindByValue(Mutability),
}
CloneTypeFoldableAndLiftImpls! { BindingMode, }
TrivialTypeFoldableAndLiftImpls! { BindingMode, }
impl BindingMode {
pub fn convert(ba: BindingAnnotation) -> BindingMode {

View file

@ -13,7 +13,6 @@ use rustc_hir::def::Namespace;
use rustc_hir::def_id::CRATE_DEF_INDEX;
use rustc_index::vec::{Idx, IndexVec};
use smallvec::SmallVec;
use std::fmt;
use std::ops::ControlFlow;
use std::rc::Rc;
@ -275,7 +274,7 @@ impl fmt::Debug for ty::PredicateAtom<'tcx> {
// For things that don't carry any arena-allocated data (and are
// copy...), just add them to this list.
CloneTypeFoldableAndLiftImpls! {
TrivialTypeFoldableAndLiftImpls! {
(),
bool,
usize,
@ -846,7 +845,7 @@ impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
fold_list(self, folder, |tcx, v| tcx.intern_existential_predicates(v))
ty::util::fold_list(self, folder, |tcx, v| tcx.intern_existential_predicates(v))
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
@ -856,7 +855,7 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>>
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
fold_list(self, folder, |tcx, v| tcx.intern_type_list(v))
ty::util::fold_list(self, folder, |tcx, v| tcx.intern_type_list(v))
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
@ -866,7 +865,7 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
fold_list(self, folder, |tcx, v| tcx.intern_projs(v))
ty::util::fold_list(self, folder, |tcx, v| tcx.intern_projs(v))
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
@ -928,25 +927,25 @@ impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
let kind = match self.kind() {
let kind = match *self.kind() {
ty::RawPtr(tm) => ty::RawPtr(tm.fold_with(folder)),
ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
ty::Adt(tid, substs) => ty::Adt(*tid, substs.fold_with(folder)),
ty::Dynamic(ref trait_ty, ref region) => {
ty::Adt(tid, substs) => ty::Adt(tid, substs.fold_with(folder)),
ty::Dynamic(trait_ty, region) => {
ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder))
}
ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
ty::FnDef(def_id, substs) => ty::FnDef(*def_id, substs.fold_with(folder)),
ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.fold_with(folder)),
ty::FnPtr(f) => ty::FnPtr(f.fold_with(folder)),
ty::Ref(ref r, ty, mutbl) => ty::Ref(r.fold_with(folder), ty.fold_with(folder), *mutbl),
ty::Ref(r, ty, mutbl) => ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl),
ty::Generator(did, substs, movability) => {
ty::Generator(*did, substs.fold_with(folder), *movability)
ty::Generator(did, substs.fold_with(folder), movability)
}
ty::GeneratorWitness(types) => ty::GeneratorWitness(types.fold_with(folder)),
ty::Closure(did, substs) => ty::Closure(*did, substs.fold_with(folder)),
ty::Projection(ref data) => ty::Projection(data.fold_with(folder)),
ty::Opaque(did, substs) => ty::Opaque(*did, substs.fold_with(folder)),
ty::Closure(did, substs) => ty::Closure(did, substs.fold_with(folder)),
ty::Projection(data) => ty::Projection(data.fold_with(folder)),
ty::Opaque(did, substs) => ty::Opaque(did, substs.fold_with(folder)),
ty::Bool
| ty::Char
@ -1060,7 +1059,7 @@ impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
fold_list(self, folder, |tcx, v| tcx.intern_predicates(v))
ty::util::fold_list(self, folder, |tcx, v| tcx.intern_predicates(v))
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
@ -1140,34 +1139,3 @@ impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
ControlFlow::CONTINUE
}
}
// Does the equivalent of
// ```
// let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
// folder.tcx().intern_*(&v)
// ```
fn fold_list<'tcx, F, T>(
list: &'tcx ty::List<T>,
folder: &mut F,
intern: impl FnOnce(TyCtxt<'tcx>, &[T]) -> &'tcx ty::List<T>,
) -> &'tcx ty::List<T>
where
F: TypeFolder<'tcx>,
T: TypeFoldable<'tcx> + PartialEq + Copy,
{
let mut iter = list.iter();
// Look for the first element that changed
if let Some((i, new_t)) = iter.by_ref().enumerate().find_map(|(i, t)| {
let new_t = t.fold_with(folder);
if new_t == t { None } else { Some((i, new_t)) }
}) {
// An element changed, prepare to intern the resulting list
let mut new_list = SmallVec::<[_; 8]>::with_capacity(list.len());
new_list.extend_from_slice(&list[..i]);
new_list.push(new_t);
new_list.extend(iter.map(|t| t.fold_with(folder)));
intern(folder.tcx(), &new_list)
} else {
list
}
}

View file

@ -1130,6 +1130,37 @@ pub fn needs_drop_components(
}
}
// Does the equivalent of
// ```
// let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
// folder.tcx().intern_*(&v)
// ```
pub fn fold_list<'tcx, F, T>(
list: &'tcx ty::List<T>,
folder: &mut F,
intern: impl FnOnce(TyCtxt<'tcx>, &[T]) -> &'tcx ty::List<T>,
) -> &'tcx ty::List<T>
where
F: TypeFolder<'tcx>,
T: TypeFoldable<'tcx> + PartialEq + Copy,
{
let mut iter = list.iter();
// Look for the first element that changed
if let Some((i, new_t)) = iter.by_ref().enumerate().find_map(|(i, t)| {
let new_t = t.fold_with(folder);
if new_t == t { None } else { Some((i, new_t)) }
}) {
// An element changed, prepare to intern the resulting list
let mut new_list = SmallVec::<[_; 8]>::with_capacity(list.len());
new_list.extend_from_slice(&list[..i]);
new_list.push(new_t);
new_list.extend(iter.map(|t| t.fold_with(folder)));
intern(folder.tcx(), &new_list)
} else {
list
}
}
#[derive(Copy, Clone, Debug, HashStable, TyEncodable, TyDecodable)]
pub struct AlwaysRequiresDrop;