generate ClosureSubsts from SubstsRef
This commit is contained in:
parent
1f8e1d8aea
commit
9b91bef78b
45 changed files with 139 additions and 216 deletions
|
|
@ -220,7 +220,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
|||
|
||||
let ty_msg = match local_visitor.found_ty {
|
||||
Some(ty::TyS { kind: ty::Closure(def_id, substs), .. }) => {
|
||||
let fn_sig = ty::ClosureSubsts::from_ref(substs).closure_sig(*def_id, self.tcx);
|
||||
let fn_sig = substs.as_closure().sig(*def_id, self.tcx);
|
||||
let args = closure_args(&fn_sig);
|
||||
let ret = fn_sig.output().skip_binder().to_string();
|
||||
format!(" for the closure `fn({}) -> {}`", args, ret)
|
||||
|
|
@ -255,7 +255,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
|||
|
||||
let suffix = match local_visitor.found_ty {
|
||||
Some(ty::TyS { kind: ty::Closure(def_id, substs), .. }) => {
|
||||
let fn_sig = substs.closure_sig(*def_id, self.tcx);
|
||||
let fn_sig = substs.as_closure().sig(*def_id, self.tcx);
|
||||
let ret = fn_sig.output().skip_binder().to_string();
|
||||
|
||||
if let Some(ExprKind::Closure(_, decl, body_id, ..)) = local_visitor.found_closure {
|
||||
|
|
|
|||
|
|
@ -1481,9 +1481,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
|||
pub fn closure_kind(
|
||||
&self,
|
||||
closure_def_id: DefId,
|
||||
closure_substs: ty::ClosureSubsts<'tcx>,
|
||||
closure_substs: SubstsRef<'tcx>,
|
||||
) -> Option<ty::ClosureKind> {
|
||||
let closure_kind_ty = closure_substs.closure_kind_ty(closure_def_id, self.tcx);
|
||||
let closure_kind_ty = closure_substs.as_closure().kind_ty(closure_def_id, self.tcx);
|
||||
let closure_kind_ty = self.shallow_resolve(closure_kind_ty);
|
||||
closure_kind_ty.to_opt_closure_kind()
|
||||
}
|
||||
|
|
@ -1495,9 +1495,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
|||
pub fn closure_sig(
|
||||
&self,
|
||||
def_id: DefId,
|
||||
substs: ty::ClosureSubsts<'tcx>,
|
||||
substs: SubstsRef<'tcx>,
|
||||
) -> ty::PolyFnSig<'tcx> {
|
||||
let closure_sig_ty = substs.closure_sig_ty(def_id, self.tcx);
|
||||
let closure_sig_ty = substs.as_closure().sig_ty(def_id, self.tcx);
|
||||
let closure_sig_ty = self.shallow_resolve(closure_sig_ty);
|
||||
closure_sig_ty.fn_sig(self.tcx)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -722,11 +722,11 @@ where
|
|||
ty::Closure(def_id, ref substs) => {
|
||||
// Skip lifetime parameters of the enclosing item(s)
|
||||
|
||||
for upvar_ty in ty::ClosureSubsts::from_ref(substs).upvar_tys(def_id, self.tcx) {
|
||||
for upvar_ty in substs.as_closure().upvar_tys(def_id, self.tcx) {
|
||||
upvar_ty.visit_with(self);
|
||||
}
|
||||
|
||||
substs.closure_sig_ty(def_id, self.tcx).visit_with(self);
|
||||
substs.as_closure().sig_ty(def_id, self.tcx).visit_with(self);
|
||||
}
|
||||
|
||||
ty::Generator(def_id, ref substs, _) => {
|
||||
|
|
|
|||
|
|
@ -740,17 +740,18 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
|
|||
let ty = self.node_ty(fn_hir_id)?;
|
||||
let kind = match ty.kind {
|
||||
ty::Generator(..) => ty::ClosureKind::FnOnce,
|
||||
ty::Closure(closure_def_id, closure_substs) => {
|
||||
ty::Closure(closure_def_id, substs) => {
|
||||
match self.infcx {
|
||||
// During upvar inference we may not know the
|
||||
// closure kind, just use the LATTICE_BOTTOM value.
|
||||
Some(infcx) =>
|
||||
infcx.closure_kind(closure_def_id,
|
||||
ty::ClosureSubsts::from_ref(closure_substs))
|
||||
.unwrap_or(ty::ClosureKind::LATTICE_BOTTOM),
|
||||
infcx.closure_kind(
|
||||
closure_def_id,
|
||||
substs
|
||||
).unwrap_or(ty::ClosureKind::LATTICE_BOTTOM),
|
||||
|
||||
None =>
|
||||
closure_substs.closure_kind(closure_def_id, self.tcx),
|
||||
substs.as_closure().kind(closure_def_id, self.tcx),
|
||||
}
|
||||
}
|
||||
_ => span_bug!(span, "unexpected type for fn in mem_categorization: {:?}", ty),
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ use crate::ty::layout::VariantIdx;
|
|||
use crate::ty::print::{FmtPrinter, Printer};
|
||||
use crate::ty::subst::{Subst, SubstsRef};
|
||||
use crate::ty::{
|
||||
self, AdtDef, CanonicalUserTypeAnnotations, ClosureSubsts, GeneratorSubsts, Region, Ty, TyCtxt,
|
||||
self, AdtDef, CanonicalUserTypeAnnotations, GeneratorSubsts, Region, Ty, TyCtxt,
|
||||
UserTypeAnnotationIndex,
|
||||
};
|
||||
|
||||
|
|
@ -2188,7 +2188,7 @@ pub enum AggregateKind<'tcx> {
|
|||
/// active field index would identity the field `c`
|
||||
Adt(&'tcx AdtDef, VariantIdx, SubstsRef<'tcx>, Option<UserTypeAnnotationIndex>, Option<usize>),
|
||||
|
||||
Closure(DefId, ClosureSubsts<'tcx>),
|
||||
Closure(DefId, SubstsRef<'tcx>),
|
||||
Generator(DefId, GeneratorSubsts<'tcx>, hir::GeneratorMovability),
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -218,7 +218,7 @@ impl<'tcx> Rvalue<'tcx> {
|
|||
tcx.type_of(def.did).subst(tcx, substs)
|
||||
}
|
||||
AggregateKind::Closure(did, substs) => {
|
||||
tcx.mk_closure(did, &substs.substs)
|
||||
tcx.mk_closure(did, substs)
|
||||
}
|
||||
AggregateKind::Generator(did, substs, movability) => {
|
||||
tcx.mk_generator(did, substs, movability)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::ty::subst::SubstsRef;
|
||||
use crate::ty::{CanonicalUserTypeAnnotation, ClosureSubsts, GeneratorSubsts, Ty};
|
||||
use crate::ty::{CanonicalUserTypeAnnotation, GeneratorSubsts, Ty};
|
||||
use crate::mir::*;
|
||||
use syntax_pos::Span;
|
||||
|
||||
|
|
@ -221,12 +221,6 @@ macro_rules! make_mir_visitor {
|
|||
self.super_substs(substs);
|
||||
}
|
||||
|
||||
fn visit_closure_substs(&mut self,
|
||||
substs: & $($mutability)? ClosureSubsts<'tcx>,
|
||||
_: Location) {
|
||||
self.super_closure_substs(substs);
|
||||
}
|
||||
|
||||
fn visit_generator_substs(&mut self,
|
||||
substs: & $($mutability)? GeneratorSubsts<'tcx>,
|
||||
_: Location) {
|
||||
|
|
@ -618,7 +612,7 @@ macro_rules! make_mir_visitor {
|
|||
_,
|
||||
closure_substs
|
||||
) => {
|
||||
self.visit_closure_substs(closure_substs, location);
|
||||
self.visit_substs(closure_substs, location);
|
||||
}
|
||||
AggregateKind::Generator(
|
||||
_,
|
||||
|
|
@ -838,10 +832,6 @@ macro_rules! make_mir_visitor {
|
|||
_substs: & $($mutability)? GeneratorSubsts<'tcx>) {
|
||||
}
|
||||
|
||||
fn super_closure_substs(&mut self,
|
||||
_substs: & $($mutability)? ClosureSubsts<'tcx>) {
|
||||
}
|
||||
|
||||
// Convenience methods
|
||||
|
||||
fn visit_location(&mut self, body: & $($mutability)? Body<'tcx>, location: Location) {
|
||||
|
|
|
|||
|
|
@ -619,7 +619,7 @@ pub struct VtableGeneratorData<'tcx, N> {
|
|||
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
|
||||
pub struct VtableClosureData<'tcx, N> {
|
||||
pub closure_def_id: DefId,
|
||||
pub substs: ty::ClosureSubsts<'tcx>,
|
||||
pub substs: SubstsRef<'tcx>,
|
||||
/// Nested obligations. This can be non-empty if the closure
|
||||
/// signature contains associated types.
|
||||
pub nested: Vec<N>
|
||||
|
|
|
|||
|
|
@ -1334,7 +1334,8 @@ fn confirm_closure_candidate<'cx, 'tcx>(
|
|||
) -> Progress<'tcx> {
|
||||
let tcx = selcx.tcx();
|
||||
let infcx = selcx.infcx();
|
||||
let closure_sig_ty = vtable.substs.closure_sig_ty(vtable.closure_def_id, tcx);
|
||||
let closure_sig_ty = vtable.substs
|
||||
.as_closure().sig_ty(vtable.closure_def_id, tcx);
|
||||
let closure_sig = infcx.shallow_resolve(closure_sig_ty).fn_sig(tcx);
|
||||
let Normalized {
|
||||
value: closure_sig,
|
||||
|
|
|
|||
|
|
@ -213,6 +213,7 @@ pub fn trivial_dropck_outlives<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
|
|||
// check if *any* of those are trivial.
|
||||
ty::Tuple(ref tys) => tys.iter().all(|t| trivial_dropck_outlives(tcx, t.expect_ty())),
|
||||
ty::Closure(def_id, ref substs) => substs
|
||||
.as_closure()
|
||||
.upvar_tys(def_id, tcx)
|
||||
.all(|t| trivial_dropck_outlives(tcx, t)),
|
||||
|
||||
|
|
|
|||
|
|
@ -2051,8 +2051,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
"assemble_unboxed_candidates: kind={:?} obligation={:?}",
|
||||
kind, obligation
|
||||
);
|
||||
match self.infcx.closure_kind(closure_def_id,
|
||||
ty::ClosureSubsts::from_ref(closure_substs)) {
|
||||
match self.infcx.closure_kind(
|
||||
closure_def_id,
|
||||
closure_substs
|
||||
) {
|
||||
Some(closure_kind) => {
|
||||
debug!(
|
||||
"assemble_unboxed_candidates: closure_kind = {:?}",
|
||||
|
|
@ -2670,7 +2672,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
ty::Closure(def_id, substs) => {
|
||||
// (*) binder moved here
|
||||
Where(ty::Binder::bind(
|
||||
substs.upvar_tys(def_id, self.tcx()).collect(),
|
||||
substs.as_closure().upvar_tys(def_id, self.tcx()).collect(),
|
||||
))
|
||||
}
|
||||
|
||||
|
|
@ -2754,7 +2756,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
tys.iter().map(|k| k.expect_ty()).collect()
|
||||
}
|
||||
|
||||
ty::Closure(def_id, ref substs) => substs.upvar_tys(def_id, self.tcx()).collect(),
|
||||
ty::Closure(def_id, ref substs) => substs.as_closure()
|
||||
.upvar_tys(def_id, self.tcx())
|
||||
.collect(),
|
||||
|
||||
ty::Generator(def_id, ref substs, _) => {
|
||||
let witness = substs.witness(def_id, self.tcx());
|
||||
|
|
@ -3376,14 +3380,17 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
obligations.push(Obligation::new(
|
||||
obligation.cause.clone(),
|
||||
obligation.param_env,
|
||||
ty::Predicate::ClosureKind(closure_def_id,
|
||||
ty::ClosureSubsts::from_ref(substs.clone()), kind),
|
||||
ty::Predicate::ClosureKind(
|
||||
closure_def_id,
|
||||
substs,
|
||||
kind
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
Ok(VtableClosureData {
|
||||
closure_def_id,
|
||||
substs: ty::ClosureSubsts::from_ref(substs),
|
||||
substs: substs,
|
||||
nested: obligations,
|
||||
})
|
||||
}
|
||||
|
|
@ -3878,8 +3885,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
"closure_trait_ref_unnormalized(obligation={:?}, closure_def_id={:?}, substs={:?})",
|
||||
obligation, closure_def_id, substs,
|
||||
);
|
||||
let closure_type = self.infcx.closure_sig(closure_def_id,
|
||||
ty::ClosureSubsts::from_ref(substs));
|
||||
let closure_type = self.infcx.closure_sig(closure_def_id, substs);
|
||||
|
||||
debug!(
|
||||
"closure_trait_ref_unnormalized: closure_type = {:?}",
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ impl<'tcx> Instance<'tcx> {
|
|||
// Shims currently have type FnPtr. Not sure this should remain.
|
||||
ty::FnPtr(_) => ty.fn_sig(tcx),
|
||||
ty::Closure(def_id, substs) => {
|
||||
let sig = substs.closure_sig(def_id, tcx);
|
||||
let sig = substs.as_closure().sig(def_id, tcx);
|
||||
|
||||
let env_ty = tcx.closure_env_ty(def_id, substs).unwrap();
|
||||
sig.map_bound(|sig| tcx.mk_fn_sig(
|
||||
|
|
@ -315,14 +315,14 @@ impl<'tcx> Instance<'tcx> {
|
|||
pub fn resolve_closure(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
def_id: DefId,
|
||||
substs: ty::ClosureSubsts<'tcx>,
|
||||
substs: ty::SubstsRef<'tcx>,
|
||||
requested_kind: ty::ClosureKind,
|
||||
) -> Instance<'tcx> {
|
||||
let actual_kind = substs.closure_kind(def_id, tcx);
|
||||
let actual_kind = substs.as_closure().kind(def_id, tcx);
|
||||
|
||||
match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
|
||||
Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, substs.substs),
|
||||
_ => Instance::new(def_id, substs.substs)
|
||||
Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, substs),
|
||||
_ => Instance::new(def_id, substs)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -348,7 +348,7 @@ impl<'tcx> Instance<'tcx> {
|
|||
|
||||
let self_ty = tcx.mk_closure(closure_did, substs);
|
||||
|
||||
let sig = substs.closure_sig(closure_did, tcx);
|
||||
let sig = substs.as_closure().sig(closure_did, tcx);
|
||||
let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
|
||||
assert_eq!(sig.inputs().len(), 1);
|
||||
let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]);
|
||||
|
|
|
|||
|
|
@ -674,7 +674,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
|
|||
ty::Generator(def_id, substs, _) => self.generator_layout(ty, def_id, &substs)?,
|
||||
|
||||
ty::Closure(def_id, ref substs) => {
|
||||
let tys = substs.upvar_tys(def_id, tcx);
|
||||
let tys = substs.as_closure().upvar_tys(def_id, tcx);
|
||||
univariant(&tys.map(|ty| self.layout_of(ty)).collect::<Result<Vec<_>, _>>()?,
|
||||
&ReprOptions::default(),
|
||||
StructKind::AlwaysSized)?
|
||||
|
|
@ -2147,7 +2147,7 @@ where
|
|||
|
||||
// Tuples, generators and closures.
|
||||
ty::Closure(def_id, ref substs) => {
|
||||
substs.upvar_tys(def_id, tcx).nth(i).unwrap()
|
||||
substs.as_closure().upvar_tys(def_id, tcx).nth(i).unwrap()
|
||||
}
|
||||
|
||||
ty::Generator(def_id, ref substs, _) => {
|
||||
|
|
|
|||
|
|
@ -1110,7 +1110,7 @@ pub enum Predicate<'tcx> {
|
|||
/// No direct syntax. May be thought of as `where T: FnFoo<...>`
|
||||
/// for some substitutions `...` and `T` being a closure type.
|
||||
/// Satisfied (or refuted) once we know the closure's kind.
|
||||
ClosureKind(DefId, ClosureSubsts<'tcx>, ClosureKind),
|
||||
ClosureKind(DefId, SubstsRef<'tcx>, ClosureKind),
|
||||
|
||||
/// `T1 <: T2`
|
||||
Subtype(PolySubtypePredicate<'tcx>),
|
||||
|
|
@ -1457,7 +1457,7 @@ impl<'tcx> Predicate<'tcx> {
|
|||
WalkTysIter::None
|
||||
}
|
||||
ty::Predicate::ClosureKind(_closure_def_id, closure_substs, _kind) => {
|
||||
WalkTysIter::Types(closure_substs.substs.types())
|
||||
WalkTysIter::Types(closure_substs.types())
|
||||
}
|
||||
ty::Predicate::ConstEvaluatable(_, substs) => {
|
||||
WalkTysIter::Types(substs.types())
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
// projection).
|
||||
match ty.kind {
|
||||
ty::Closure(def_id, ref substs) => {
|
||||
for upvar_ty in substs.upvar_tys(def_id, *self) {
|
||||
for upvar_ty in substs.as_closure().upvar_tys(def_id, *self) {
|
||||
self.compute_components(upvar_ty, out);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -154,13 +154,8 @@ impl DefPathBasedNames<'tcx> {
|
|||
self.push_type_name(sig.output(), output, debug);
|
||||
}
|
||||
}
|
||||
ty::Generator(def_id, GeneratorSubsts { ref substs }, _) => {
|
||||
self.push_def_path(def_id, output);
|
||||
let generics = self.tcx.generics_of(self.tcx.closure_base_def_id(def_id));
|
||||
let substs = substs.truncate_to(self.tcx, generics);
|
||||
self.push_generic_params(substs, iter::empty(), output, debug);
|
||||
}
|
||||
ty::Closure(def_id, substs) => {
|
||||
ty::Generator(def_id, GeneratorSubsts { substs }, _)
|
||||
| ty::Closure(def_id, substs) => {
|
||||
self.push_def_path(def_id, output);
|
||||
let generics = self.tcx.generics_of(self.tcx.closure_base_def_id(def_id));
|
||||
let substs = substs.truncate_to(self.tcx, generics);
|
||||
|
|
|
|||
|
|
@ -649,7 +649,7 @@ pub trait PrettyPrinter<'tcx>:
|
|||
p!(in_binder(&types));
|
||||
}
|
||||
ty::Closure(did, substs) => {
|
||||
let upvar_tys = substs.upvar_tys(did, self.tcx());
|
||||
let upvar_tys = substs.as_closure().upvar_tys(did, self.tcx());
|
||||
p!(write("[closure"));
|
||||
|
||||
// FIXME(eddyb) should use `def_span`.
|
||||
|
|
@ -689,8 +689,8 @@ pub trait PrettyPrinter<'tcx>:
|
|||
if self.tcx().sess.verbose() {
|
||||
p!(write(
|
||||
" closure_kind_ty={:?} closure_sig_ty={:?}",
|
||||
substs.closure_kind_ty(did, self.tcx()),
|
||||
substs.closure_sig_ty(did, self.tcx())
|
||||
substs.as_closure().kind(did, self.tcx()),
|
||||
substs.as_closure().sig_ty(did, self.tcx())
|
||||
));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -304,8 +304,8 @@ static_assert_size!(TyKind<'_>, 24);
|
|||
/// type parameters is similar, but the role of CK and CS are
|
||||
/// different. CK represents the "yield type" and CS represents the
|
||||
/// "return type" of the generator.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash,
|
||||
Debug, RustcEncodable, RustcDecodable, HashStable)]
|
||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug,
|
||||
RustcEncodable, RustcDecodable, HashStable)]
|
||||
pub struct ClosureSubsts<'tcx> {
|
||||
/// Lifetime and type parameters from the enclosing function,
|
||||
/// concatenated with the types of the upvars.
|
||||
|
|
@ -317,18 +317,13 @@ pub struct ClosureSubsts<'tcx> {
|
|||
|
||||
/// Struct returned by `split()`. Note that these are subslices of the
|
||||
/// parent slice and not canonical substs themselves.
|
||||
pub(crate) struct SplitClosureSubsts<'tcx> {
|
||||
pub(crate) closure_kind_ty: Ty<'tcx>,
|
||||
pub(crate) closure_sig_ty: Ty<'tcx>,
|
||||
pub(crate) upvar_kinds: &'tcx [GenericArg<'tcx>],
|
||||
struct SplitClosureSubsts<'tcx> {
|
||||
closure_kind_ty: Ty<'tcx>,
|
||||
closure_sig_ty: Ty<'tcx>,
|
||||
upvar_kinds: &'tcx [GenericArg<'tcx>],
|
||||
}
|
||||
|
||||
impl<'tcx> ClosureSubsts<'tcx> {
|
||||
// FIXME(csmoe): remove this method once the migration is done.
|
||||
pub fn from_ref(substs: SubstsRef<'tcx>) -> Self {
|
||||
Self { substs }
|
||||
}
|
||||
|
||||
/// Divides the closure substs into their respective
|
||||
/// components. Single source of truth with respect to the
|
||||
/// ordering.
|
||||
|
|
@ -361,7 +356,7 @@ impl<'tcx> ClosureSubsts<'tcx> {
|
|||
/// Returns the closure kind for this closure; may return a type
|
||||
/// variable during inference. To get the closure kind during
|
||||
/// inference, use `infcx.closure_kind(def_id, substs)`.
|
||||
pub fn closure_kind_ty(self, def_id: DefId, tcx: TyCtxt<'_>) -> Ty<'tcx> {
|
||||
pub fn kind_ty(self, def_id: DefId, tcx: TyCtxt<'_>) -> Ty<'tcx> {
|
||||
self.split(def_id, tcx).closure_kind_ty
|
||||
}
|
||||
|
||||
|
|
@ -369,7 +364,7 @@ impl<'tcx> ClosureSubsts<'tcx> {
|
|||
/// closure; may contain type variables during inference. To get
|
||||
/// the closure signature during inference, use
|
||||
/// `infcx.fn_sig(def_id)`.
|
||||
pub fn closure_sig_ty(self, def_id: DefId, tcx: TyCtxt<'_>) -> Ty<'tcx> {
|
||||
pub fn sig_ty(self, def_id: DefId, tcx: TyCtxt<'_>) -> Ty<'tcx> {
|
||||
self.split(def_id, tcx).closure_sig_ty
|
||||
}
|
||||
|
||||
|
|
@ -378,7 +373,7 @@ impl<'tcx> ClosureSubsts<'tcx> {
|
|||
/// there are no type variables.
|
||||
///
|
||||
/// If you have an inference context, use `infcx.closure_kind()`.
|
||||
pub fn closure_kind(self, def_id: DefId, tcx: TyCtxt<'tcx>) -> ty::ClosureKind {
|
||||
pub fn kind(self, def_id: DefId, tcx: TyCtxt<'tcx>) -> ty::ClosureKind {
|
||||
self.split(def_id, tcx).closure_kind_ty.to_opt_closure_kind().unwrap()
|
||||
}
|
||||
|
||||
|
|
@ -387,8 +382,8 @@ impl<'tcx> ClosureSubsts<'tcx> {
|
|||
/// there are no type variables.
|
||||
///
|
||||
/// If you have an inference context, use `infcx.closure_sig()`.
|
||||
pub fn closure_sig(self, def_id: DefId, tcx: TyCtxt<'tcx>) -> ty::PolyFnSig<'tcx> {
|
||||
let ty = self.closure_sig_ty(def_id, tcx);
|
||||
pub fn sig(&self, def_id: DefId, tcx: TyCtxt<'tcx>) -> ty::PolyFnSig<'tcx> {
|
||||
let ty = self.sig_ty(def_id, tcx);
|
||||
match ty.kind {
|
||||
ty::FnPtr(sig) => sig,
|
||||
_ => bug!("closure_sig_ty is not a fn-ptr: {:?}", ty.kind),
|
||||
|
|
@ -573,7 +568,7 @@ impl<'tcx> GeneratorSubsts<'tcx> {
|
|||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub enum UpvarSubsts<'tcx> {
|
||||
Closure(ClosureSubsts<'tcx>),
|
||||
Closure(SubstsRef<'tcx>),
|
||||
Generator(GeneratorSubsts<'tcx>),
|
||||
}
|
||||
|
||||
|
|
@ -582,10 +577,10 @@ impl<'tcx> UpvarSubsts<'tcx> {
|
|||
pub fn upvar_tys(
|
||||
self,
|
||||
def_id: DefId,
|
||||
tcx: TyCtxt<'_>,
|
||||
tcx: TyCtxt<'tcx>,
|
||||
) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
|
||||
let upvar_kinds = match self {
|
||||
UpvarSubsts::Closure(substs) => substs.split(def_id, tcx).upvar_kinds,
|
||||
UpvarSubsts::Closure(substs) => substs.as_closure().split(def_id, tcx).upvar_kinds,
|
||||
UpvarSubsts::Generator(substs) => substs.split(def_id, tcx).upvar_kinds,
|
||||
};
|
||||
upvar_kinds.iter().map(|t| {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use crate::infer::canonical::Canonical;
|
|||
use crate::ty::{self, Lift, List, Ty, TyCtxt, InferConst, ParamConst};
|
||||
use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
|
||||
use crate::mir::interpret::ConstValue;
|
||||
use crate::ty::sty::SplitClosureSubsts;
|
||||
use crate::ty::sty::ClosureSubsts;
|
||||
|
||||
use rustc_serialize::{self, Encodable, Encoder, Decodable, Decoder};
|
||||
use syntax_pos::{Span, DUMMY_SP};
|
||||
|
|
@ -184,6 +184,16 @@ pub type InternalSubsts<'tcx> = List<GenericArg<'tcx>>;
|
|||
pub type SubstsRef<'tcx> = &'tcx InternalSubsts<'tcx>;
|
||||
|
||||
impl<'a, 'tcx> InternalSubsts<'tcx> {
|
||||
/// Interpret these substitutions as the substitutions of a closure type.
|
||||
/// Closure substitutions have a particular structure controlled by the
|
||||
/// compiler that encodes information like the signature and closure kind;
|
||||
/// see `ty::ClosureSubsts` struct for more comments.
|
||||
pub fn as_closure(&'a self) -> ClosureSubsts<'a> {
|
||||
ClosureSubsts {
|
||||
substs: self,
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a `InternalSubsts` that maps each generic parameter to itself.
|
||||
pub fn identity_for_item(tcx: TyCtxt<'tcx>, def_id: DefId) -> SubstsRef<'tcx> {
|
||||
Self::for_item(tcx, def_id, |param, _| {
|
||||
|
|
@ -380,72 +390,6 @@ impl<'a, 'tcx> InternalSubsts<'tcx> {
|
|||
pub fn truncate_to(&self, tcx: TyCtxt<'tcx>, generics: &ty::Generics) -> SubstsRef<'tcx> {
|
||||
tcx.mk_substs(self.iter().take(generics.count()).cloned())
|
||||
}
|
||||
|
||||
/// Divides the closure substs into their respective
|
||||
/// components. Single source of truth with respect to the
|
||||
/// ordering.
|
||||
fn split(&self, def_id: DefId, tcx: TyCtxt<'_>) -> SplitClosureSubsts<'_> {
|
||||
let generics = tcx.generics_of(def_id);
|
||||
let parent_len = generics.parent_count;
|
||||
SplitClosureSubsts {
|
||||
closure_kind_ty: self.type_at(parent_len),
|
||||
closure_sig_ty: self.type_at(parent_len + 1),
|
||||
upvar_kinds: &self[parent_len + 2..],
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn upvar_tys(
|
||||
&'a self,
|
||||
def_id: DefId,
|
||||
tcx: TyCtxt<'_>,
|
||||
) -> impl Iterator<Item = Ty<'a>> + 'a {
|
||||
let SplitClosureSubsts { upvar_kinds, .. } = self.split(def_id, tcx);
|
||||
upvar_kinds.iter().map(|t| {
|
||||
if let GenericArgKind::Type(ty) = t.unpack() {
|
||||
ty
|
||||
} else {
|
||||
bug!("upvar should be type")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the closure kind for this closure; may return a type
|
||||
/// variable during inference. To get the closure kind during
|
||||
/// inference, use `infcx.closure_kind(def_id, substs)`.
|
||||
pub fn closure_kind_ty(&'a self, def_id: DefId, tcx: TyCtxt<'_>) -> Ty<'a> {
|
||||
self.split(def_id, tcx).closure_kind_ty
|
||||
}
|
||||
|
||||
/// Returns the type representing the closure signature for this
|
||||
/// closure; may contain type variables during inference. To get
|
||||
/// the closure signature during inference, use
|
||||
/// `infcx.fn_sig(def_id)`.
|
||||
pub fn closure_sig_ty(&'a self, def_id: DefId, tcx: TyCtxt<'_>) -> Ty<'a> {
|
||||
self.split(def_id, tcx).closure_sig_ty
|
||||
}
|
||||
|
||||
/// Returns the closure kind for this closure; only usable outside
|
||||
/// of an inference context, because in that context we know that
|
||||
/// there are no type variables.
|
||||
///
|
||||
/// If you have an inference context, use `infcx.closure_kind()`.
|
||||
pub fn closure_kind(&self, def_id: DefId, tcx: TyCtxt<'tcx>) -> ty::ClosureKind {
|
||||
self.split(def_id, tcx).closure_kind_ty.to_opt_closure_kind().unwrap()
|
||||
}
|
||||
|
||||
/// Extracts the signature from the closure; only usable outside
|
||||
/// of an inference context, because in that context we know that
|
||||
/// there are no type variables.
|
||||
///
|
||||
/// If you have an inference context, use `infcx.closure_sig()`.
|
||||
pub fn closure_sig(&'a self, def_id: DefId, tcx: TyCtxt<'tcx>) -> ty::PolyFnSig<'a> {
|
||||
let ty = self.closure_sig_ty(def_id, tcx);
|
||||
match ty.kind {
|
||||
ty::FnPtr(sig) => sig,
|
||||
_ => bug!("closure_sig_ty is not a fn-ptr: {:?}", ty.kind),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeFoldable<'tcx> for SubstsRef<'tcx> {
|
||||
|
|
|
|||
|
|
@ -647,7 +647,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
{
|
||||
let closure_ty = self.mk_closure(closure_def_id, closure_substs);
|
||||
let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
|
||||
let closure_kind_ty = closure_substs.closure_kind_ty(closure_def_id, self);
|
||||
let closure_kind_ty = closure_substs.as_closure().kind_ty(closure_def_id, self);
|
||||
let closure_kind = closure_kind_ty.to_opt_closure_kind()?;
|
||||
let env_ty = match closure_kind {
|
||||
ty::ClosureKind::Fn => self.mk_imm_ref(self.mk_region(env_region), closure_ty),
|
||||
|
|
@ -1108,7 +1108,9 @@ fn needs_drop_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::ParamEnvAnd<'tcx, Ty<'tcx>
|
|||
// Structural recursion.
|
||||
ty::Array(ty, _) | ty::Slice(ty) => needs_drop(ty),
|
||||
|
||||
ty::Closure(def_id, ref substs) => substs.upvar_tys(def_id, tcx).any(needs_drop),
|
||||
ty::Closure(def_id, ref substs) => {
|
||||
substs.as_closure().upvar_tys(def_id, tcx).any(needs_drop)
|
||||
}
|
||||
|
||||
// Pessimistically assume that all generators will require destructors
|
||||
// as we don't know if a destructor is a noop or not until after the MIR
|
||||
|
|
|
|||
|
|
@ -347,7 +347,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
|
|||
// anyway, except via auto trait matching (which
|
||||
// only inspects the upvar types).
|
||||
subtys.skip_current_subtree(); // subtree handled by compute_projection
|
||||
for upvar_ty in substs.upvar_tys(def_id, self.infcx.tcx) {
|
||||
for upvar_ty in substs.as_closure().upvar_tys(def_id, self.infcx.tcx) {
|
||||
self.compute(upvar_ty);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue