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::{GenericArg, SubstsRef, InternalSubsts};
6 use rustc::hir::def_id::DefId;
7 use rustc_target::spec::abi;
9 crate fn bound(tcx: 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 {
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: TyCtxt<'tcx>, arity: usize) -> SubstsRef<'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())))
52 .map(|ty| GenericArg::from(ty))
56 crate fn ref_ty(tcx: TyCtxt<'tcx>, mutbl: hir::Mutability) -> Ty<'tcx> {
57 let region = tcx.mk_region(
58 ty::ReLateBound(ty::INNERMOST, ty::BoundRegion::BrAnon(0))
61 tcx.mk_ref(region, ty::TypeAndMut {
67 crate fn fn_def(tcx: TyCtxt<'tcx>, def_id: DefId) -> Ty<'tcx> {
68 tcx.mk_ty(ty::FnDef(def_id, InternalSubsts::bound_vars_for_item(tcx, def_id)))
71 crate fn closure(tcx: TyCtxt<'tcx>, def_id: DefId) -> Ty<'tcx> {
72 tcx.mk_closure(def_id, InternalSubsts::bound_vars_for_item(tcx, def_id))
75 crate fn generator(tcx: TyCtxt<'tcx>, def_id: DefId) -> Ty<'tcx> {
78 InternalSubsts::bound_vars_for_item(tcx, def_id),
79 hir::Movability::Movable