1
Fork 0
rust/compiler/rustc_middle/src/ty/structural_impls.rs

1171 lines
42 KiB
Rust
Raw Normal View History

//! This module contains implements of the `Lift` and `TypeFoldable`
//! traits for various types in the Rust compiler. Most are written by
2019-11-15 18:19:52 +01:00
//! hand, though we've recently added some macros and proc-macros to help with the tedium.
use crate::mir::interpret;
2019-12-22 17:42:04 -05:00
use crate::mir::ProjectionKind;
2019-02-05 11:20:45 -06:00
use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
use crate::ty::print::{with_no_trimmed_paths, FmtPrinter, Printer};
2019-12-22 17:42:04 -05:00
use crate::ty::{self, InferConst, Lift, Ty, TyCtxt};
use rustc_hir as hir;
use rustc_hir::def::Namespace;
use rustc_hir::def_id::CRATE_DEF_INDEX;
2019-12-22 17:42:04 -05:00
use rustc_index::vec::{Idx, IndexVec};
2015-09-06 21:51:58 +03:00
use smallvec::SmallVec;
use std::fmt;
2015-09-06 21:51:58 +03:00
use std::rc::Rc;
2019-06-18 08:15:27 -04:00
use std::sync::Arc;
2015-09-06 21:51:58 +03:00
impl fmt::Debug for ty::TraitDef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::tls::with(|tcx| {
with_no_trimmed_paths(|| {
FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.def_id, &[])
})?;
Ok(())
})
}
}
impl fmt::Debug for ty::AdtDef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::tls::with(|tcx| {
with_no_trimmed_paths(|| {
FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.did, &[])
})?;
Ok(())
})
}
}
impl fmt::Debug for ty::UpvarId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-12-22 17:42:04 -05:00
let name = ty::tls::with(|tcx| tcx.hir().name(self.var_path.hir_id));
write!(f, "UpvarId({:?};`{}`;{:?})", self.var_path.hir_id, name, self.closure_expr_id)
}
}
impl fmt::Debug for ty::UpvarBorrow<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-12-22 17:42:04 -05:00
write!(f, "UpvarBorrow({:?}, {:?})", self.kind, self.region)
}
}
impl fmt::Debug for ty::ExistentialTraitRef<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
}
}
impl fmt::Debug for ty::adjustment::Adjustment<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?} -> {}", self.kind, self.target)
}
}
impl fmt::Debug for ty::BoundRegion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
ty::BrAnon(n) => write!(f, "BrAnon({:?})", n),
ty::BrNamed(did, name) => {
if did.index == CRATE_DEF_INDEX {
write!(f, "BrNamed({})", name)
} else {
write!(f, "BrNamed({:?}, {})", did, name)
}
}
ty::BrEnv => write!(f, "BrEnv"),
}
}
}
impl fmt::Debug for ty::RegionKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
2019-12-22 17:42:04 -05:00
ty::ReEarlyBound(ref data) => write!(f, "ReEarlyBound({}, {})", data.index, data.name),
ty::ReLateBound(binder_id, ref bound_region) => {
write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region)
}
ty::ReFree(ref fr) => fr.fmt(f),
ty::ReStatic => write!(f, "ReStatic"),
ty::ReVar(ref vid) => vid.fmt(f),
2019-12-22 17:42:04 -05:00
ty::RePlaceholder(placeholder) => write!(f, "RePlaceholder({:?})", placeholder),
ty::ReEmpty(ui) => write!(f, "ReEmpty({:?})", ui),
ty::ReErased => write!(f, "ReErased"),
}
}
}
impl fmt::Debug for ty::FreeRegion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region)
}
}
impl fmt::Debug for ty::Variance {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match *self {
ty::Covariant => "+",
ty::Contravariant => "-",
ty::Invariant => "o",
ty::Bivariant => "*",
})
}
}
impl fmt::Debug for ty::FnSig<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-12-22 17:42:04 -05:00
write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
}
}
impl fmt::Debug for ty::TyVid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "_#{}t", self.index)
}
}
impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "_#{}c", self.index)
}
}
impl fmt::Debug for ty::IntVid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "_#{}i", self.index)
}
}
impl fmt::Debug for ty::FloatVid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "_#{}f", self.index)
}
}
impl fmt::Debug for ty::RegionVid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "'_#{}r", self.index())
}
}
impl fmt::Debug for ty::InferTy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
ty::TyVar(ref v) => v.fmt(f),
ty::IntVar(ref v) => v.fmt(f),
ty::FloatVar(ref v) => v.fmt(f),
ty::FreshTy(v) => write!(f, "FreshTy({:?})", v),
ty::FreshIntTy(v) => write!(f, "FreshIntTy({:?})", v),
ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({:?})", v),
}
}
}
impl fmt::Debug for ty::IntVarValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
ty::IntType(ref v) => v.fmt(f),
ty::UintType(ref v) => v.fmt(f),
}
}
}
impl fmt::Debug for ty::FloatVarValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl fmt::Debug for ty::TraitRef<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
}
}
impl fmt::Debug for Ty<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
}
}
impl fmt::Debug for ty::ParamTy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}/#{}", self.name, self.index)
}
}
impl fmt::Debug for ty::ParamConst {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}/#{}", self.name, self.index)
}
}
impl fmt::Debug for ty::TraitPredicate<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "TraitPredicate({:?})", self.trait_ref)
}
}
impl fmt::Debug for ty::ProjectionPredicate<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.ty)
}
}
2020-05-11 22:06:41 +02:00
impl fmt::Debug for ty::Predicate<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.kind())
}
}
impl fmt::Debug for ty::PredicateKind<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
2020-07-09 00:35:55 +02:00
ty::PredicateKind::ForAll(binder) => write!(f, "ForAll({:?})", binder),
ty::PredicateKind::Atom(atom) => write!(f, "{:?}", atom),
}
}
}
impl fmt::Debug for ty::PredicateAtom<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
ty::PredicateAtom::Trait(ref a, constness) => {
if let hir::Constness::Const = constness {
write!(f, "const ")?;
}
a.fmt(f)
}
2020-07-09 00:35:55 +02:00
ty::PredicateAtom::Subtype(ref pair) => pair.fmt(f),
ty::PredicateAtom::RegionOutlives(ref pair) => pair.fmt(f),
ty::PredicateAtom::TypeOutlives(ref pair) => pair.fmt(f),
ty::PredicateAtom::Projection(ref pair) => pair.fmt(f),
ty::PredicateAtom::WellFormed(data) => write!(f, "WellFormed({:?})", data),
ty::PredicateAtom::ObjectSafe(trait_def_id) => {
write!(f, "ObjectSafe({:?})", trait_def_id)
}
2020-07-09 00:35:55 +02:00
ty::PredicateAtom::ClosureKind(closure_def_id, closure_substs, kind) => {
2019-12-22 17:42:04 -05:00
write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
}
2020-07-09 00:35:55 +02:00
ty::PredicateAtom::ConstEvaluatable(def_id, substs) => {
write!(f, "ConstEvaluatable({:?}, {:?})", def_id, substs)
}
2020-07-09 00:35:55 +02:00
ty::PredicateAtom::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
}
}
}
///////////////////////////////////////////////////////////////////////////
// Atomic structs
//
// For things that don't carry any arena-allocated data (and are
// copy...), just add them to this list.
CloneTypeFoldableAndLiftImpls! {
(),
bool,
usize,
::rustc_target::abi::VariantIdx,
u32,
u64,
2018-11-26 17:30:19 +01:00
String,
2019-02-05 11:20:45 -06:00
crate::middle::region::Scope,
2020-04-27 23:26:11 +05:30
::rustc_ast::FloatTy,
::rustc_ast::InlineAsmOptions,
::rustc_ast::InlineAsmTemplatePiece,
::rustc_ast::NodeId,
::rustc_span::symbol::Symbol,
::rustc_hir::def::Res,
::rustc_hir::def_id::DefId,
2020-07-08 01:03:19 +02:00
::rustc_hir::def_id::LocalDefId,
::rustc_hir::HirId,
::rustc_hir::LlvmInlineAsmInner,
::rustc_hir::MatchSource,
::rustc_hir::Mutability,
::rustc_hir::Unsafety,
2020-02-14 18:17:50 +00:00
::rustc_target::asm::InlineAsmRegOrRegClass,
::rustc_target::spec::abi::Abi,
crate::mir::coverage::ExpressionOperandId,
crate::mir::coverage::CounterValueReference,
crate::mir::coverage::InjectedExpressionIndex,
crate::mir::coverage::MappedExpressionIndex,
2019-02-05 11:20:45 -06:00
crate::mir::Local,
crate::mir::Promoted,
crate::traits::Reveal,
crate::ty::adjustment::AutoBorrowMutability,
crate::ty::AdtKind,
// Including `BoundRegion` is a *bit* dubious, but direct
// references to bound region appear in `ty::Error`, and aren't
// really meant to be folded. In general, we can only fold a fully
// general `Region`.
2019-02-05 11:20:45 -06:00
crate::ty::BoundRegion,
crate::ty::AssocItem,
crate::ty::Placeholder<crate::ty::BoundRegion>,
2019-02-05 11:20:45 -06:00
crate::ty::ClosureKind,
crate::ty::FreeRegion,
crate::ty::InferTy,
2019-02-05 11:20:45 -06:00
crate::ty::IntVarValue,
crate::ty::ParamConst,
2019-02-05 11:20:45 -06:00
crate::ty::ParamTy,
crate::ty::adjustment::PointerCast,
crate::ty::RegionVid,
2019-02-05 11:20:45 -06:00
crate::ty::UniverseIndex,
crate::ty::Variance,
::rustc_span::Span,
}
2015-09-06 21:51:58 +03:00
///////////////////////////////////////////////////////////////////////////
// Lift implementations
// FIXME(eddyb) replace all the uses of `Option::map` with `?`.
2015-09-06 21:51:58 +03:00
impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
type Lifted = (A::Lifted, B::Lifted);
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
2015-09-06 21:51:58 +03:00
tcx.lift(&self.0).and_then(|a| tcx.lift(&self.1).map(|b| (a, b)))
}
}
2016-12-26 14:34:03 +01:00
impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
type Lifted = (A::Lifted, B::Lifted, C::Lifted);
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
2019-12-22 17:42:04 -05:00
tcx.lift(&self.0)
.and_then(|a| tcx.lift(&self.1).and_then(|b| tcx.lift(&self.2).map(|c| (a, b, c))))
}
2016-12-26 14:34:03 +01:00
}
impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
type Lifted = Option<T::Lifted>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
match *self {
Some(ref x) => tcx.lift(x).map(Some),
2019-12-22 17:42:04 -05:00
None => Some(None),
}
}
}
impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
type Lifted = Result<T::Lifted, E::Lifted>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
match *self {
Ok(ref x) => tcx.lift(x).map(Ok),
2019-12-22 17:42:04 -05:00
Err(ref e) => tcx.lift(e).map(Err),
}
}
}
impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
type Lifted = Box<T::Lifted>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
tcx.lift(&**self).map(Box::new)
}
}
impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Rc<T> {
type Lifted = Rc<T::Lifted>;
2019-06-24 16:06:17 -04:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
tcx.lift(&**self).map(Rc::new)
}
}
2019-06-18 08:15:27 -04:00
impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Arc<T> {
type Lifted = Arc<T::Lifted>;
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
tcx.lift(&**self).map(Arc::new)
}
}
2015-09-06 21:51:58 +03:00
impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for [T] {
type Lifted = Vec<T::Lifted>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
// type annotation needed to inform `projection_must_outlive`
2019-12-22 17:42:04 -05:00
let mut result: Vec<<T as Lift<'tcx>>::Lifted> = Vec::with_capacity(self.len());
2015-09-06 21:51:58 +03:00
for x in self {
if let Some(value) = tcx.lift(x) {
result.push(value);
} else {
return None;
}
}
Some(result)
}
}
impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
type Lifted = Vec<T::Lifted>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
tcx.lift(&self[..])
}
}
impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
type Lifted = IndexVec<I, T::Lifted>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
2019-12-22 17:42:04 -05:00
self.iter().map(|e| tcx.lift(e)).collect()
}
}
impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
type Lifted = ty::TraitRef<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
2019-12-22 17:42:04 -05:00
tcx.lift(&self.substs).map(|substs| ty::TraitRef { def_id: self.def_id, substs })
}
}
impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
type Lifted = ty::ExistentialTraitRef<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
2019-12-22 17:42:04 -05:00
tcx.lift(&self.substs).map(|substs| ty::ExistentialTraitRef { def_id: self.def_id, substs })
2015-09-06 21:51:58 +03:00
}
}
impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialPredicate<'a> {
type Lifted = ty::ExistentialPredicate<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
match self {
2019-12-22 17:42:04 -05:00
ty::ExistentialPredicate::Trait(x) => tcx.lift(x).map(ty::ExistentialPredicate::Trait),
ty::ExistentialPredicate::Projection(x) => {
tcx.lift(x).map(ty::ExistentialPredicate::Projection)
}
ty::ExistentialPredicate::AutoTrait(def_id) => {
Some(ty::ExistentialPredicate::AutoTrait(*def_id))
}
}
}
}
2015-09-06 21:51:58 +03:00
impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
type Lifted = ty::TraitPredicate<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::TraitPredicate<'tcx>> {
2019-12-22 17:42:04 -05:00
tcx.lift(&self.trait_ref).map(|trait_ref| ty::TraitPredicate { trait_ref })
2015-09-06 21:51:58 +03:00
}
}
2017-03-09 21:47:09 -05:00
impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
type Lifted = ty::SubtypePredicate<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::SubtypePredicate<'tcx>> {
2017-03-09 21:47:09 -05:00
tcx.lift(&(self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
a_is_expected: self.a_is_expected,
a,
b,
2017-03-09 21:47:09 -05:00
})
}
}
impl<'tcx, A: Copy + Lift<'tcx>, B: Copy + Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
2015-09-06 21:51:58 +03:00
type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
2015-09-06 21:51:58 +03:00
tcx.lift(&(self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
}
}
impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
type Lifted = ty::ProjectionTy<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionTy<'tcx>> {
2019-12-22 17:42:04 -05:00
tcx.lift(&self.substs)
.map(|substs| ty::ProjectionTy { item_def_id: self.item_def_id, substs })
}
}
2015-09-06 21:51:58 +03:00
impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
type Lifted = ty::ProjectionPredicate<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionPredicate<'tcx>> {
2019-12-22 17:42:04 -05:00
tcx.lift(&(self.projection_ty, self.ty))
.map(|(projection_ty, ty)| ty::ProjectionPredicate { projection_ty, ty })
2015-09-06 21:51:58 +03:00
}
}
impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
type Lifted = ty::ExistentialProjection<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
2019-12-22 17:42:04 -05:00
tcx.lift(&self.substs).map(|substs| ty::ExistentialProjection {
substs,
ty: tcx.lift(&self.ty).expect("type must lift when substs do"),
item_def_id: self.item_def_id,
})
}
}
2020-05-11 22:06:41 +02:00
impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> {
type Lifted = ty::PredicateKind<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
2020-07-18 14:37:36 +02:00
match self {
ty::PredicateKind::ForAll(binder) => tcx.lift(binder).map(ty::PredicateKind::ForAll),
ty::PredicateKind::Atom(atom) => tcx.lift(atom).map(ty::PredicateKind::Atom),
2020-07-09 00:35:55 +02:00
}
}
}
impl<'a, 'tcx> Lift<'tcx> for ty::PredicateAtom<'a> {
type Lifted = ty::PredicateAtom<'tcx>;
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
match *self {
ty::PredicateAtom::Trait(ref data, constness) => {
tcx.lift(data).map(|data| ty::PredicateAtom::Trait(data, constness))
}
2020-07-09 00:35:55 +02:00
ty::PredicateAtom::Subtype(ref data) => tcx.lift(data).map(ty::PredicateAtom::Subtype),
ty::PredicateAtom::RegionOutlives(ref data) => {
tcx.lift(data).map(ty::PredicateAtom::RegionOutlives)
}
2020-07-09 00:35:55 +02:00
ty::PredicateAtom::TypeOutlives(ref data) => {
tcx.lift(data).map(ty::PredicateAtom::TypeOutlives)
}
2020-07-09 00:35:55 +02:00
ty::PredicateAtom::Projection(ref data) => {
tcx.lift(data).map(ty::PredicateAtom::Projection)
}
2020-07-09 00:35:55 +02:00
ty::PredicateAtom::WellFormed(ty) => tcx.lift(&ty).map(ty::PredicateAtom::WellFormed),
ty::PredicateAtom::ClosureKind(closure_def_id, closure_substs, kind) => {
2019-12-22 17:42:04 -05:00
tcx.lift(&closure_substs).map(|closure_substs| {
2020-07-09 00:35:55 +02:00
ty::PredicateAtom::ClosureKind(closure_def_id, closure_substs, kind)
2019-12-22 17:42:04 -05:00
})
}
2020-07-09 00:35:55 +02:00
ty::PredicateAtom::ObjectSafe(trait_def_id) => {
Some(ty::PredicateAtom::ObjectSafe(trait_def_id))
}
2020-07-09 00:35:55 +02:00
ty::PredicateAtom::ConstEvaluatable(def_id, substs) => {
tcx.lift(&substs).map(|substs| ty::PredicateAtom::ConstEvaluatable(def_id, substs))
}
2020-07-09 00:35:55 +02:00
ty::PredicateAtom::ConstEquate(c1, c2) => {
tcx.lift(&(c1, c2)).map(|(c1, c2)| ty::PredicateAtom::ConstEquate(c1, c2))
}
}
}
}
2015-09-06 21:51:58 +03:00
impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
type Lifted = ty::Binder<T::Lifted>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
2020-06-24 23:40:33 +02:00
tcx.lift(self.as_ref().skip_binder()).map(ty::Binder::bind)
2015-09-06 21:51:58 +03:00
}
}
impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
type Lifted = ty::ParamEnv<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
2020-07-02 20:52:40 -04:00
tcx.lift(&self.caller_bounds())
.map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal(), self.def_id))
}
}
impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
tcx.lift(&self.param_env).and_then(|param_env| {
2019-12-22 17:42:04 -05:00
tcx.lift(&self.value).map(|value| ty::ParamEnvAnd { param_env, value })
})
}
}
impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
type Lifted = ty::ClosureSubsts<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
2019-12-22 17:42:04 -05:00
tcx.lift(&self.substs).map(|substs| ty::ClosureSubsts { substs })
}
}
impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
type Lifted = ty::GeneratorSubsts<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
2019-12-22 17:42:04 -05:00
tcx.lift(&self.substs).map(|substs| ty::GeneratorSubsts { substs })
2016-12-26 14:34:03 +01:00
}
}
impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
type Lifted = ty::adjustment::Adjustment<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
tcx.lift(&self.kind).and_then(|kind| {
2019-12-22 17:42:04 -05:00
tcx.lift(&self.target).map(|target| ty::adjustment::Adjustment { kind, target })
})
}
}
impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
type Lifted = ty::adjustment::Adjust<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
match *self {
2019-12-22 17:42:04 -05:00
ty::adjustment::Adjust::NeverToAny => Some(ty::adjustment::Adjust::NeverToAny),
ty::adjustment::Adjust::Pointer(ptr) => Some(ty::adjustment::Adjust::Pointer(ptr)),
ty::adjustment::Adjust::Deref(ref overloaded) => {
tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
}
ty::adjustment::Adjust::Borrow(ref autoref) => {
tcx.lift(autoref).map(ty::adjustment::Adjust::Borrow)
}
}
}
}
impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::OverloadedDeref<'a> {
type Lifted = ty::adjustment::OverloadedDeref<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
2019-12-22 17:42:04 -05:00
tcx.lift(&self.region)
.map(|region| ty::adjustment::OverloadedDeref { region, mutbl: self.mutbl })
}
}
impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
type Lifted = ty::adjustment::AutoBorrow<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
match *self {
ty::adjustment::AutoBorrow::Ref(r, m) => {
tcx.lift(&r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
}
2019-12-22 17:42:04 -05:00
ty::adjustment::AutoBorrow::RawPtr(m) => Some(ty::adjustment::AutoBorrow::RawPtr(m)),
}
}
}
2016-12-26 14:34:03 +01:00
impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
type Lifted = ty::GenSig<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
tcx.lift(&(self.resume_ty, self.yield_ty, self.return_ty))
.map(|(resume_ty, yield_ty, return_ty)| ty::GenSig { resume_ty, yield_ty, return_ty })
2016-12-26 14:34:03 +01:00
}
}
impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
type Lifted = ty::FnSig<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
2019-12-22 17:42:04 -05:00
tcx.lift(&self.inputs_and_output).map(|x| ty::FnSig {
inputs_and_output: x,
c_variadic: self.c_variadic,
unsafety: self.unsafety,
abi: self.abi,
})
}
}
impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
type Lifted = ty::error::ExpectedFound<T::Lifted>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
tcx.lift(&self.expected).and_then(|expected| {
2019-12-22 17:42:04 -05:00
tcx.lift(&self.found).map(|found| ty::error::ExpectedFound { expected, found })
})
}
}
impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
type Lifted = ty::error::TypeError<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
2019-02-05 11:20:45 -06:00
use crate::ty::error::TypeError::*;
Some(match *self {
Mismatch => Mismatch,
UnsafetyMismatch(x) => UnsafetyMismatch(x),
AbiMismatch(x) => AbiMismatch(x),
Mutability => Mutability,
TupleSize(x) => TupleSize(x),
FixedArraySize(x) => FixedArraySize(x),
ArgCount => ArgCount,
RegionsDoesNotOutlive(a, b) => {
2019-12-22 17:42:04 -05:00
return tcx.lift(&(a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b));
}
2019-02-20 05:39:04 -05:00
RegionsInsufficientlyPolymorphic(a, b) => {
2019-12-22 17:42:04 -05:00
return tcx.lift(&b).map(|b| RegionsInsufficientlyPolymorphic(a, b));
2019-02-20 05:39:04 -05:00
}
RegionsOverlyPolymorphic(a, b) => {
2019-12-22 17:42:04 -05:00
return tcx.lift(&b).map(|b| RegionsOverlyPolymorphic(a, b));
2019-02-20 05:39:04 -05:00
}
RegionsPlaceholderMismatch => RegionsPlaceholderMismatch,
IntMismatch(x) => IntMismatch(x),
FloatMismatch(x) => FloatMismatch(x),
Traits(x) => Traits(x),
VariadicMismatch(x) => VariadicMismatch(x),
CyclicTy(t) => return tcx.lift(&t).map(|t| CyclicTy(t)),
ProjectionMismatched(x) => ProjectionMismatched(x),
Sorts(ref x) => return tcx.lift(x).map(Sorts),
ExistentialMismatch(ref x) => return tcx.lift(x).map(ExistentialMismatch),
2019-03-12 19:27:06 +00:00
ConstMismatch(ref x) => return tcx.lift(x).map(ConstMismatch),
IntrinsicCast => IntrinsicCast,
TargetFeatureCast(ref x) => TargetFeatureCast(*x),
ObjectUnsafeCoercion(ref x) => return tcx.lift(x).map(ObjectUnsafeCoercion),
})
}
}
2018-01-02 23:22:09 +00:00
impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
type Lifted = ty::InstanceDef<'tcx>;
2019-06-14 00:48:52 +03:00
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
2018-01-02 23:22:09 +00:00
match *self {
2019-12-22 17:42:04 -05:00
ty::InstanceDef::Item(def_id) => Some(ty::InstanceDef::Item(def_id)),
ty::InstanceDef::VtableShim(def_id) => Some(ty::InstanceDef::VtableShim(def_id)),
ty::InstanceDef::ReifyShim(def_id) => Some(ty::InstanceDef::ReifyShim(def_id)),
ty::InstanceDef::Intrinsic(def_id) => Some(ty::InstanceDef::Intrinsic(def_id)),
ty::InstanceDef::FnPtrShim(def_id, ref ty) => {
Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?))
}
ty::InstanceDef::Virtual(def_id, n) => Some(ty::InstanceDef::Virtual(def_id, n)),
ty::InstanceDef::ClosureOnceShim { call_once } => {
Some(ty::InstanceDef::ClosureOnceShim { call_once })
}
ty::InstanceDef::DropGlue(def_id, ref ty) => {
Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?))
}
ty::InstanceDef::CloneShim(def_id, ref ty) => {
Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?))
}
2018-01-02 23:22:09 +00:00
}
}
}
2015-09-06 21:51:58 +03:00
///////////////////////////////////////////////////////////////////////////
// TypeFoldable implementations.
//
// Ideally, each type should invoke `folder.fold_foo(self)` and
// nothing else. In some cases, though, we haven't gotten around to
// adding methods on the `folder` yet, and thus the folding is
// hard-coded here. This is less-flexible, because folders cannot
// override the behavior, but there are a lot of random types and one
// can easily refactor the folding into the TypeFolder trait as
// needed.
/// AdtDefs are basically the same as a DefId.
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::AdtDef {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
*self
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
false
}
}
impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> (T, U) {
2015-09-06 21:51:58 +03:00
(self.0.fold_with(folder), self.1.fold_with(folder))
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
self.0.visit_with(visitor) || self.1.visit_with(visitor)
}
2015-09-06 21:51:58 +03:00
}
2020-07-08 01:03:19 +02:00
impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
for (A, B, C)
{
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> (A, B, C) {
(self.0.fold_with(folder), self.1.fold_with(folder), self.2.fold_with(folder))
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
self.0.visit_with(visitor) || self.1.visit_with(visitor) || self.2.visit_with(visitor)
}
}
EnumTypeFoldableImpl! {
impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
(Some)(a),
(None),
} where T: TypeFoldable<'tcx>
2015-09-06 21:51:58 +03:00
}
2019-05-31 10:23:22 +02:00
EnumTypeFoldableImpl! {
impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
(Ok)(a),
(Err)(a),
} where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
}
2015-09-06 21:51:58 +03:00
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
2015-09-06 21:51:58 +03:00
Rc::new((**self).fold_with(folder))
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
(**self).visit_with(visitor)
}
2015-09-06 21:51:58 +03:00
}
2019-06-18 19:15:31 -04:00
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
Arc::new((**self).fold_with(folder))
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
(**self).visit_with(visitor)
}
}
2015-09-06 21:51:58 +03:00
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
2015-09-06 21:51:58 +03:00
let content: T = (**self).fold_with(folder);
box content
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
(**self).visit_with(visitor)
}
2015-09-06 21:51:58 +03:00
}
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
2015-09-06 21:51:58 +03:00
self.iter().map(|t| t.fold_with(folder)).collect()
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
self.iter().any(|t| t.visit_with(visitor))
}
2015-09-06 21:51:58 +03:00
}
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
self.iter().map(|t| t.fold_with(folder)).collect::<Vec<_>>().into_boxed_slice()
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
self.iter().any(|t| t.visit_with(visitor))
}
}
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
self.map_bound_ref(|ty| ty.fold_with(folder))
}
2019-06-14 00:48:52 +03:00
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
folder.fold_binder(self)
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2020-06-24 23:40:33 +02:00
self.as_ref().skip_binder().visit_with(visitor)
}
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
visitor.visit_binder(self)
}
2015-09-06 21:51:58 +03:00
}
2018-08-22 00:35:01 +01:00
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
fold_list(*self, folder, |tcx, v| tcx.intern_existential_predicates(v))
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
self.iter().any(|p| p.visit_with(visitor))
}
}
2018-08-22 00:35:01 +01:00
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
fold_list(*self, folder, |tcx, v| tcx.intern_type_list(v))
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
self.iter().any(|t| t.visit_with(visitor))
}
}
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
fold_list(*self, folder, |tcx, v| tcx.intern_projs(v))
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
self.iter().any(|t| t.visit_with(visitor))
}
}
2018-01-16 09:24:38 +01:00
impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
2019-02-05 11:20:45 -06:00
use crate::ty::InstanceDef::*;
2018-01-16 09:24:38 +01:00
Self {
substs: self.substs.fold_with(folder),
def: match self.def {
2020-07-03 19:13:39 +02:00
Item(def) => Item(def.fold_with(folder)),
2018-09-10 22:54:48 +09:00
VtableShim(did) => VtableShim(did.fold_with(folder)),
ReifyShim(did) => ReifyShim(did.fold_with(folder)),
2018-01-16 09:24:38 +01:00
Intrinsic(did) => Intrinsic(did.fold_with(folder)),
2019-12-22 17:42:04 -05:00
FnPtrShim(did, ty) => FnPtrShim(did.fold_with(folder), ty.fold_with(folder)),
Virtual(did, i) => Virtual(did.fold_with(folder), i),
ClosureOnceShim { call_once } => {
ClosureOnceShim { call_once: call_once.fold_with(folder) }
}
DropGlue(did, ty) => DropGlue(did.fold_with(folder), ty.fold_with(folder)),
CloneShim(did, ty) => CloneShim(did.fold_with(folder), ty.fold_with(folder)),
2018-01-16 09:24:38 +01:00
},
}
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2019-02-05 11:20:45 -06:00
use crate::ty::InstanceDef::*;
2019-12-22 17:42:04 -05:00
self.substs.visit_with(visitor)
|| match self.def {
2020-07-03 19:13:39 +02:00
Item(def) => def.visit_with(visitor),
VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
2019-12-22 17:42:04 -05:00
did.visit_with(visitor)
}
FnPtrShim(did, ty) | CloneShim(did, ty) => {
did.visit_with(visitor) || ty.visit_with(visitor)
}
DropGlue(did, ty) => did.visit_with(visitor) || ty.visit_with(visitor),
ClosureOnceShim { call_once } => call_once.visit_with(visitor),
}
2018-01-16 09:24:38 +01:00
}
}
2018-01-02 23:22:09 +00:00
impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
2019-12-22 17:42:04 -05:00
Self { instance: self.instance.fold_with(folder), promoted: self.promoted }
2018-01-02 23:22:09 +00:00
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
self.instance.visit_with(visitor)
}
}
2015-09-06 21:51:58 +03:00
impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
2019-09-16 19:11:57 +01:00
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)),
2019-12-22 17:42:04 -05:00
ty::Dynamic(ref trait_ty, ref region) => {
ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder))
}
2019-12-22 17:42:04 -05:00
ty::Tuple(ts) => ty::Tuple(ts.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)),
2019-12-22 17:42:04 -05:00
ty::Ref(ref r, ty, mutbl) => ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl),
ty::Generator(did, substs, movability) => {
2019-12-22 17:42:04 -05:00
ty::Generator(did, substs.fold_with(folder), movability)
2017-07-05 14:57:26 -07:00
}
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)),
2018-10-22 20:37:56 +02:00
2019-12-22 17:42:04 -05:00
ty::Bool
| ty::Char
| ty::Str
| ty::Int(_)
| ty::Uint(_)
| ty::Float(_)
| ty::Error(_)
2019-12-22 17:42:04 -05:00
| ty::Infer(_)
| ty::Param(..)
| ty::Bound(..)
| ty::Placeholder(..)
| ty::Never
| ty::Foreign(..) => return self,
};
2019-12-22 17:42:04 -05:00
if self.kind == kind { self } else { folder.tcx().mk_ty(kind) }
}
2019-06-14 00:48:52 +03:00
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
folder.fold_ty(*self)
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2019-09-16 19:08:35 +01:00
match self.kind {
ty::RawPtr(ref tm) => tm.visit_with(visitor),
ty::Array(typ, sz) => typ.visit_with(visitor) || sz.visit_with(visitor),
ty::Slice(typ) => typ.visit_with(visitor),
ty::Adt(_, substs) => substs.visit_with(visitor),
2019-12-22 17:42:04 -05:00
ty::Dynamic(ref trait_ty, ref reg) => {
trait_ty.visit_with(visitor) || reg.visit_with(visitor)
}
ty::Tuple(ts) => ts.visit_with(visitor),
ty::FnDef(_, substs) => substs.visit_with(visitor),
ty::FnPtr(ref f) => f.visit_with(visitor),
ty::Ref(r, ty, _) => r.visit_with(visitor) || ty.visit_with(visitor),
2019-12-22 17:42:04 -05:00
ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
ty::GeneratorWitness(ref types) => types.visit_with(visitor),
ty::Closure(_did, ref substs) => substs.visit_with(visitor),
2020-05-12 01:56:29 -04:00
ty::Projection(ref data) => data.visit_with(visitor),
ty::Opaque(_, ref substs) => substs.visit_with(visitor),
2018-10-22 20:37:56 +02:00
2019-12-22 17:42:04 -05:00
ty::Bool
| ty::Char
| ty::Str
| ty::Int(_)
| ty::Uint(_)
| ty::Float(_)
| ty::Error(_)
2019-12-22 17:42:04 -05:00
| ty::Infer(_)
| ty::Bound(..)
| ty::Placeholder(..)
| ty::Param(..)
| ty::Never
| ty::Foreign(..) => false,
}
}
2015-09-06 21:51:58 +03:00
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
visitor.visit_ty(self)
2015-09-06 21:51:58 +03:00
}
}
impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
*self
}
2019-06-14 00:48:52 +03:00
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
2015-09-06 21:51:58 +03:00
folder.fold_region(*self)
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
false
}
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
visitor.visit_region(*self)
}
2015-09-06 21:51:58 +03:00
}
2020-05-11 22:04:22 +02:00
impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
let new = ty::PredicateKind::super_fold_with(&self.inner.kind, folder);
2020-06-24 18:06:04 +02:00
folder.tcx().reuse_or_mk_predicate(*self, new)
2020-05-11 22:04:22 +02:00
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
ty::PredicateKind::super_visit_with(&self.inner.kind, visitor)
}
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
visitor.visit_predicate(*self)
}
fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
self.inner.outer_exclusive_binder > binder
}
fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
self.inner.flags.intersects(flags)
}
}
pub(super) trait PredicateVisitor<'tcx>: TypeVisitor<'tcx> {
fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> bool;
}
impl<T: TypeVisitor<'tcx>> PredicateVisitor<'tcx> for T {
default fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> bool {
predicate.super_visit_with(self)
2020-05-11 22:04:22 +02:00
}
}
2018-08-22 00:35:01 +01:00
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
fold_list(*self, folder, |tcx, v| tcx.intern_predicates(v))
2017-05-23 04:19:47 -04:00
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
self.iter().any(|p| p.visit_with(visitor))
}
}
impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
self.iter().map(|x| x.fold_with(folder)).collect()
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
self.iter().any(|t| t.visit_with(visitor))
}
}
2019-03-14 10:19:31 +01:00
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
2019-03-14 10:19:31 +01:00
let ty = self.ty.fold_with(folder);
let val = self.val.fold_with(folder);
2020-05-31 17:16:44 +02:00
if ty != self.ty || val != self.val {
folder.tcx().mk_const(ty::Const { ty, val })
} else {
*self
}
}
2019-06-14 00:48:52 +03:00
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
folder.fold_const(*self)
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2019-03-14 10:19:31 +01:00
self.ty.visit_with(visitor) || self.val.visit_with(visitor)
}
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
visitor.visit_const(self)
}
}
impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
2019-03-14 10:19:31 +01:00
match *self {
ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.fold_with(folder)),
ty::ConstKind::Param(p) => ty::ConstKind::Param(p.fold_with(folder)),
ty::ConstKind::Unevaluated(did, substs, promoted) => {
ty::ConstKind::Unevaluated(did, substs.fold_with(folder), promoted)
2019-12-22 17:42:04 -05:00
}
ty::ConstKind::Value(_)
| ty::ConstKind::Bound(..)
| ty::ConstKind::Placeholder(..)
| ty::ConstKind::Error(_) => *self,
2018-12-13 11:11:12 +01:00
}
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2019-03-14 10:19:31 +01:00
match *self {
ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
ty::ConstKind::Param(p) => p.visit_with(visitor),
ty::ConstKind::Unevaluated(_, substs, _) => substs.visit_with(visitor),
ty::ConstKind::Value(_)
| ty::ConstKind::Bound(..)
| ty::ConstKind::Placeholder(_)
| ty::ConstKind::Error(_) => false,
2019-03-14 10:19:31 +01:00
}
}
}
impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
2019-06-14 00:48:52 +03:00
fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
*self
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
false
}
}
2020-01-13 09:17:10 +01:00
// 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);
2020-05-23 11:49:24 +02:00
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
}
}