]> git.lizzy.rs Git - rust.git/blob - src/librustc_traits/generic_types.rs
Rollup merge of #58269 - taeguk:add-some-sources-to-rust-src-distribution, r=Mark...
[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::InternalSubsts;
5 use rustc::hir;
6 use rustc::hir::def_id::DefId;
7 use rustc_target::spec::abi;
8
9 crate fn bound(tcx: ty::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: ty::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: ty::TyCtxt<'_, '_, 'tcx>, arity: usize) -> &'tcx ty::List<Ty<'tcx>> {
48     tcx.mk_type_list(
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     )
53 }
54
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))
58     );
59
60     tcx.mk_ref(region, ty::TypeAndMut {
61         ty: bound(tcx, 1),
62         mutbl,
63     })
64 }
65
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)))
68 }
69
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),
73     })
74 }
75
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)
80 }