]> git.lizzy.rs Git - rust.git/blob - src/librustc_traits/generic_types.rs
Rollup merge of #66128 - emilio:new-zeroed, r=SimonSapin
[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::{self, Ty, TyCtxt};
4 use rustc::ty::subst::{GenericArg, SubstsRef, InternalSubsts};
5 use rustc::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(
11         ty::INNERMOST,
12         ty::BoundVar::from_u32(index).into()
13     );
14     tcx.mk_ty(ty)
15 }
16
17 crate fn raw_ptr(tcx: TyCtxt<'tcx>, mutbl: hir::Mutability) -> Ty<'tcx> {
18     tcx.mk_ptr(ty::TypeAndMut {
19         ty: bound(tcx, 0),
20         mutbl,
21     })
22 }
23
24 crate fn fn_ptr(
25     tcx: TyCtxt<'tcx>,
26     arity_and_output: usize,
27     c_variadic: bool,
28     unsafety: hir::Unsafety,
29     abi: abi::Abi,
30 ) -> Ty<'tcx> {
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())))
36     );
37
38     let fn_sig = ty::Binder::bind(ty::FnSig {
39         inputs_and_output,
40         c_variadic,
41         unsafety,
42         abi,
43     });
44     tcx.mk_fn_ptr(fn_sig)
45 }
46
47 crate fn type_list(tcx: TyCtxt<'tcx>, arity: usize) -> SubstsRef<'tcx> {
48     tcx.mk_substs(
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))
53     )
54 }
55
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))
59     );
60
61     tcx.mk_ref(region, ty::TypeAndMut {
62         ty: bound(tcx, 1),
63         mutbl,
64     })
65 }
66
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)))
69 }
70
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))
73 }
74
75 crate fn generator(tcx: TyCtxt<'tcx>, def_id: DefId) -> Ty<'tcx> {
76     tcx.mk_generator(
77         def_id,
78         InternalSubsts::bound_vars_for_item(tcx, def_id),
79         hir::Movability::Movable
80     )
81 }