1 //! Utilities for creating generic types with bound vars in place of parameter values.
3 use rustc::ty::subst::{GenericArg, InternalSubsts, SubstsRef};
4 use rustc::ty::{self, Ty, TyCtxt};
6 use rustc_hir::def_id::DefId;
7 use rustc_target::spec::abi;
9 crate fn bound(tcx: TyCtxt<'tcx>, index: u32) -> Ty<'tcx> {
10 let ty = ty::Bound(ty::INNERMOST, ty::BoundVar::from_u32(index).into());
14 crate fn raw_ptr(tcx: TyCtxt<'tcx>, mutbl: hir::Mutability) -> Ty<'tcx> {
15 tcx.mk_ptr(ty::TypeAndMut { ty: bound(tcx, 0), mutbl })
20 arity_and_output: usize,
22 unsafety: hir::Unsafety,
25 let inputs_and_output = tcx.mk_type_list(
28 .map(|i| ty::BoundVar::from(i))
29 // DebruijnIndex(1) because we are going to inject these in a `PolyFnSig`
30 .map(|var| tcx.mk_ty(ty::Bound(ty::DebruijnIndex::from(1usize), var.into()))),
33 let fn_sig = ty::Binder::bind(ty::FnSig { inputs_and_output, c_variadic, unsafety, abi });
37 crate fn type_list(tcx: TyCtxt<'tcx>, arity: usize) -> SubstsRef<'tcx> {
41 .map(|i| ty::BoundVar::from(i))
42 .map(|var| tcx.mk_ty(ty::Bound(ty::INNERMOST, var.into())))
43 .map(|ty| GenericArg::from(ty)),
47 crate fn ref_ty(tcx: TyCtxt<'tcx>, mutbl: hir::Mutability) -> Ty<'tcx> {
48 let region = tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BoundRegion::BrAnon(0)));
50 tcx.mk_ref(region, ty::TypeAndMut { ty: bound(tcx, 1), mutbl })
53 crate fn fn_def(tcx: TyCtxt<'tcx>, def_id: DefId) -> Ty<'tcx> {
54 tcx.mk_ty(ty::FnDef(def_id, InternalSubsts::bound_vars_for_item(tcx, def_id)))
57 crate fn closure(tcx: TyCtxt<'tcx>, def_id: DefId) -> Ty<'tcx> {
58 tcx.mk_closure(def_id, InternalSubsts::bound_vars_for_item(tcx, def_id))
61 crate fn generator(tcx: TyCtxt<'tcx>, def_id: DefId) -> Ty<'tcx> {
64 InternalSubsts::bound_vars_for_item(tcx, def_id),
65 hir::Movability::Movable,