1 //! Utilities for creating generic types with bound vars in place of parameter values.
3 use rustc::ty::{self, Ty, TyCtxt};
4 use rustc::ty::subst::InternalSubsts;
6 use rustc::hir::def_id::DefId;
7 use rustc_target::spec::abi;
9 crate fn bound(tcx: ty::TyCtxt<'_, '_, 'tcx>, index: u32) -> Ty<'tcx> {
12 ty::BoundVar::from_u32(index).into()
17 crate fn raw_ptr(tcx: TyCtxt<'_, '_, 'tcx>, mutbl: hir::Mutability) -> Ty<'tcx> {
18 tcx.mk_ptr(ty::TypeAndMut {
25 tcx: ty::TyCtxt<'_, '_, 'tcx>,
26 arity_and_output: usize,
28 unsafety: hir::Unsafety,
31 let inputs_and_output = tcx.mk_type_list(
32 (0..arity_and_output).into_iter()
33 .map(|i| ty::BoundVar::from(i))
34 // DebruijnIndex(1) because we are going to inject these in a `PolyFnSig`
35 .map(|var| tcx.mk_ty(ty::Bound(ty::DebruijnIndex::from(1usize), var.into())))
38 let fn_sig = ty::Binder::bind(ty::FnSig {
47 crate fn type_list(tcx: ty::TyCtxt<'_, '_, 'tcx>, arity: usize) -> &'tcx ty::List<Ty<'tcx>> {
49 (0..arity).into_iter()
50 .map(|i| ty::BoundVar::from(i))
51 .map(|var| tcx.mk_ty(ty::Bound(ty::INNERMOST, var.into())))
55 crate fn ref_ty(tcx: ty::TyCtxt<'_, '_, 'tcx>, mutbl: hir::Mutability) -> Ty<'tcx> {
56 let region = tcx.mk_region(
57 ty::ReLateBound(ty::INNERMOST, ty::BoundRegion::BrAnon(0))
60 tcx.mk_ref(region, ty::TypeAndMut {
66 crate fn fn_def(tcx: ty::TyCtxt<'_, '_, 'tcx>, def_id: DefId) -> Ty<'tcx> {
67 tcx.mk_ty(ty::FnDef(def_id, InternalSubsts::bound_vars_for_item(tcx, def_id)))
70 crate fn closure(tcx: ty::TyCtxt<'_, '_, 'tcx>, def_id: DefId) -> Ty<'tcx> {
71 tcx.mk_closure(def_id, ty::ClosureSubsts {
72 substs: InternalSubsts::bound_vars_for_item(tcx, def_id),
76 crate fn generator(tcx: ty::TyCtxt<'_, '_, 'tcx>, def_id: DefId) -> Ty<'tcx> {
77 tcx.mk_generator(def_id, ty::GeneratorSubsts {
78 substs: InternalSubsts::bound_vars_for_item(tcx, def_id),
79 }, hir::GeneratorMovability::Movable)