]> git.lizzy.rs Git - rust.git/blob - src/librustc_traits/generic_types.rs
Rollup merge of #69477 - Pulkit07:issue69298, r=cramertj
[rust.git] / src / librustc_traits / generic_types.rs
1 //! Utilities for creating generic types with bound vars in place of parameter values.
2
3 use rustc::ty::subst::{GenericArg, InternalSubsts, SubstsRef};
4 use rustc::ty::{self, Ty, TyCtxt};
5 use rustc_hir as hir;
6 use rustc_hir::def_id::DefId;
7 use rustc_target::spec::abi;
8
9 crate fn bound(tcx: TyCtxt<'tcx>, index: u32) -> Ty<'tcx> {
10     let ty = ty::Bound(ty::INNERMOST, ty::BoundVar::from_u32(index).into());
11     tcx.mk_ty(ty)
12 }
13
14 crate fn raw_ptr(tcx: TyCtxt<'tcx>, mutbl: hir::Mutability) -> Ty<'tcx> {
15     tcx.mk_ptr(ty::TypeAndMut { ty: bound(tcx, 0), mutbl })
16 }
17
18 crate fn fn_ptr(
19     tcx: TyCtxt<'tcx>,
20     arity_and_output: usize,
21     c_variadic: bool,
22     unsafety: hir::Unsafety,
23     abi: abi::Abi,
24 ) -> Ty<'tcx> {
25     let inputs_and_output = tcx.mk_type_list(
26         (0..arity_and_output)
27             .map(|i| ty::BoundVar::from(i))
28             // DebruijnIndex(1) because we are going to inject these in a `PolyFnSig`
29             .map(|var| tcx.mk_ty(ty::Bound(ty::DebruijnIndex::from(1usize), var.into()))),
30     );
31
32     let fn_sig = ty::Binder::bind(ty::FnSig { inputs_and_output, c_variadic, unsafety, abi });
33     tcx.mk_fn_ptr(fn_sig)
34 }
35
36 crate fn type_list(tcx: TyCtxt<'tcx>, arity: usize) -> SubstsRef<'tcx> {
37     tcx.mk_substs(
38         (0..arity)
39             .map(|i| ty::BoundVar::from(i))
40             .map(|var| tcx.mk_ty(ty::Bound(ty::INNERMOST, var.into())))
41             .map(|ty| GenericArg::from(ty)),
42     )
43 }
44
45 crate fn ref_ty(tcx: TyCtxt<'tcx>, mutbl: hir::Mutability) -> Ty<'tcx> {
46     let region = tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BoundRegion::BrAnon(0)));
47
48     tcx.mk_ref(region, ty::TypeAndMut { ty: bound(tcx, 1), mutbl })
49 }
50
51 crate fn fn_def(tcx: TyCtxt<'tcx>, def_id: DefId) -> Ty<'tcx> {
52     tcx.mk_ty(ty::FnDef(def_id, InternalSubsts::bound_vars_for_item(tcx, def_id)))
53 }
54
55 crate fn closure(tcx: TyCtxt<'tcx>, def_id: DefId) -> Ty<'tcx> {
56     tcx.mk_closure(def_id, InternalSubsts::bound_vars_for_item(tcx, def_id))
57 }
58
59 crate fn generator(tcx: TyCtxt<'tcx>, def_id: DefId) -> Ty<'tcx> {
60     tcx.mk_generator(
61         def_id,
62         InternalSubsts::bound_vars_for_item(tcx, def_id),
63         hir::Movability::Movable,
64     )
65 }