Skip to content

Commit 8ab82c1

Browse files
committed
introduce from_ref helper for replacement
1 parent d046ffd commit 8ab82c1

File tree

6 files changed

+86
-13
lines changed

6 files changed

+86
-13
lines changed

src/librustc/infer/error_reporting/need_type_info.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
220220

221221
let ty_msg = match local_visitor.found_ty {
222222
Some(ty::TyS { kind: ty::Closure(def_id, substs), .. }) => {
223-
let fn_sig = substs.closure_sig(*def_id, self.tcx);
223+
let fn_sig = ty::ClosureSubsts::from_ref(substs).closure_sig(*def_id, self.tcx);
224224
let args = closure_args(&fn_sig);
225225
let ret = fn_sig.output().skip_binder().to_string();
226226
format!(" for the closure `fn({}) -> {}`", args, ret)

src/librustc/infer/opaque_types/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ where
722722
ty::Closure(def_id, ref substs) => {
723723
// Skip lifetime parameters of the enclosing item(s)
724724

725-
for upvar_ty in substs.upvar_tys(def_id, self.tcx) {
725+
for upvar_ty in ty::ClosureSubsts::from_ref(substs).upvar_tys(def_id, self.tcx) {
726726
upvar_ty.visit_with(self);
727727
}
728728

@@ -886,7 +886,7 @@ impl TypeFolder<'tcx> for ReverseMapper<'tcx> {
886886

887887
let generics = self.tcx.generics_of(def_id);
888888
let substs =
889-
self.tcx.mk_substs(substs.substs.iter().enumerate().map(|(index, &kind)| {
889+
self.tcx.mk_substs(substs.iter().enumerate().map(|(index, &kind)| {
890890
if index < generics.parent_count {
891891
// Accommodate missing regions in the parent kinds...
892892
self.fold_kind_mapping_missing_regions_to_empty(kind)

src/librustc/middle/mem_categorization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
745745
// During upvar inference we may not know the
746746
// closure kind, just use the LATTICE_BOTTOM value.
747747
Some(infcx) =>
748-
infcx.closure_kind(closure_def_id, closure_substs)
748+
infcx.closure_kind(closure_def_id, ty::ClosureSubsts::from_ref(closure_substs))
749749
.unwrap_or(ty::ClosureKind::LATTICE_BOTTOM),
750750

751751
None =>

src/librustc/traits/select.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -3370,17 +3370,18 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
33703370
)?);
33713371

33723372
// FIXME: chalk
3373+
33733374
if !self.tcx().sess.opts.debugging_opts.chalk {
33743375
obligations.push(Obligation::new(
33753376
obligation.cause.clone(),
33763377
obligation.param_env,
3377-
ty::Predicate::ClosureKind(closure_def_id, substs, kind),
3378+
ty::Predicate::ClosureKind(closure_def_id, ty::ClosureSubsts::from_ref(substs.clone()), kind),
33783379
));
33793380
}
33803381

33813382
Ok(VtableClosureData {
33823383
closure_def_id,
3383-
substs: substs.clone(),
3384+
substs: ty::ClosureSubsts::from_ref(substs),
33843385
nested: obligations,
33853386
})
33863387
}
@@ -3869,7 +3870,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
38693870
&mut self,
38703871
obligation: &TraitObligation<'tcx>,
38713872
closure_def_id: DefId,
3872-
substs: ty::ClosureSubsts<'tcx>,
3873+
substs: SubstsRef<'tcx>,
38733874
) -> ty::PolyTraitRef<'tcx> {
38743875
debug!(
38753876
"closure_trait_ref_unnormalized(obligation={:?}, closure_def_id={:?}, substs={:?})",

src/librustc/ty/sty.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pub enum TyKind<'tcx> {
158158

159159
/// The anonymous type of a closure. Used to represent the type of
160160
/// `|a| a`.
161-
Closure(DefId, ClosureSubsts<'tcx>),
161+
Closure(DefId, SubstsRef<'tcx>),
162162

163163
/// The anonymous type of a generator. Used to represent the type of
164164
/// `|a| yield a`.
@@ -317,13 +317,18 @@ pub struct ClosureSubsts<'tcx> {
317317

318318
/// Struct returned by `split()`. Note that these are subslices of the
319319
/// parent slice and not canonical substs themselves.
320-
struct SplitClosureSubsts<'tcx> {
321-
closure_kind_ty: Ty<'tcx>,
322-
closure_sig_ty: Ty<'tcx>,
323-
upvar_kinds: &'tcx [GenericArg<'tcx>],
320+
pub(crate) struct SplitClosureSubsts<'tcx> {
321+
pub(crate) closure_kind_ty: Ty<'tcx>,
322+
pub(crate) closure_sig_ty: Ty<'tcx>,
323+
pub(crate) upvar_kinds: &'tcx [GenericArg<'tcx>],
324324
}
325325

326326
impl<'tcx> ClosureSubsts<'tcx> {
327+
// FIXME(csmoe): remove this method once the migration is done.
328+
pub fn from_ref(substs: SubstsRef<'tcx>) -> Self {
329+
Self { substs }
330+
}
331+
327332
/// Divides the closure substs into their respective
328333
/// components. Single source of truth with respect to the
329334
/// ordering.
@@ -2147,7 +2152,7 @@ impl<'tcx> TyS<'tcx> {
21472152
Adt(_, substs) | Opaque(_, substs) => {
21482153
out.extend(substs.regions())
21492154
}
2150-
Closure(_, ClosureSubsts { ref substs }) |
2155+
Closure(_, ref substs ) |
21512156
Generator(_, GeneratorSubsts { ref substs }, _) => {
21522157
out.extend(substs.regions())
21532158
}

src/librustc/ty/subst.rs

+67
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::infer::canonical::Canonical;
55
use crate::ty::{self, Lift, List, Ty, TyCtxt, InferConst, ParamConst};
66
use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
77
use crate::mir::interpret::ConstValue;
8+
use crate::ty::sty::SplitClosureSubsts;
89

910
use rustc_serialize::{self, Encodable, Encoder, Decodable, Decoder};
1011
use syntax_pos::{Span, DUMMY_SP};
@@ -379,6 +380,72 @@ impl<'a, 'tcx> InternalSubsts<'tcx> {
379380
pub fn truncate_to(&self, tcx: TyCtxt<'tcx>, generics: &ty::Generics) -> SubstsRef<'tcx> {
380381
tcx.mk_substs(self.iter().take(generics.count()).cloned())
381382
}
383+
384+
/// Divides the closure substs into their respective
385+
/// components. Single source of truth with respect to the
386+
/// ordering.
387+
fn split(self, def_id: DefId, tcx: TyCtxt<'_>) -> SplitClosureSubsts<'tcx> {
388+
let generics = tcx.generics_of(def_id);
389+
let parent_len = generics.parent_count;
390+
SplitClosureSubsts {
391+
closure_kind_ty: self.substs.type_at(parent_len),
392+
closure_sig_ty: self.substs.type_at(parent_len + 1),
393+
upvar_kinds: &self.substs[parent_len + 2..],
394+
}
395+
}
396+
397+
#[inline]
398+
pub fn upvar_tys(
399+
&self,
400+
def_id: DefId,
401+
tcx: TyCtxt<'_>,
402+
) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
403+
let SplitClosureSubsts { upvar_kinds, .. } = self.split(def_id, tcx);
404+
upvar_kinds.iter().map(|t| {
405+
if let UnpackedKind::Type(ty) = t.unpack() {
406+
ty
407+
} else {
408+
bug!("upvar should be type")
409+
}
410+
})
411+
}
412+
413+
/// Returns the closure kind for this closure; may return a type
414+
/// variable during inference. To get the closure kind during
415+
/// inference, use `infcx.closure_kind(def_id, substs)`.
416+
pub fn closure_kind_ty(self, def_id: DefId, tcx: TyCtxt<'_>) -> Ty<'tcx> {
417+
self.split(def_id, tcx).closure_kind_ty
418+
}
419+
420+
/// Returns the type representing the closure signature for this
421+
/// closure; may contain type variables during inference. To get
422+
/// the closure signature during inference, use
423+
/// `infcx.fn_sig(def_id)`.
424+
pub fn closure_sig_ty(self, def_id: DefId, tcx: TyCtxt<'_>) -> Ty<'tcx> {
425+
self.split(def_id, tcx).closure_sig_ty
426+
}
427+
428+
/// Returns the closure kind for this closure; only usable outside
429+
/// of an inference context, because in that context we know that
430+
/// there are no type variables.
431+
///
432+
/// If you have an inference context, use `infcx.closure_kind()`.
433+
pub fn closure_kind(self, def_id: DefId, tcx: TyCtxt<'tcx>) -> ty::ClosureKind {
434+
self.split(def_id, tcx).closure_kind_ty.to_opt_closure_kind().unwrap()
435+
}
436+
437+
/// Extracts the signature from the closure; only usable outside
438+
/// of an inference context, because in that context we know that
439+
/// there are no type variables.
440+
///
441+
/// If you have an inference context, use `infcx.closure_sig()`.
442+
pub fn closure_sig(self, def_id: DefId, tcx: TyCtxt<'tcx>) -> ty::PolyFnSig<'tcx> {
443+
let ty = self.closure_sig_ty(def_id, tcx);
444+
match ty.kind {
445+
ty::FnPtr(sig) => sig,
446+
_ => bug!("closure_sig_ty is not a fn-ptr: {:?}", ty.kind),
447+
}
448+
}
382449
}
383450

384451
impl<'tcx> TypeFoldable<'tcx> for SubstsRef<'tcx> {

0 commit comments

Comments
 (0)