]> git.lizzy.rs Git - rust.git/blob - src/librustc_traits/generic_types.rs
Rollup merge of #68424 - estebank:suggest-borrow-for-non-copy-vec, r=davidtwco
[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             .into_iter()
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()))),
31     );
32
33     let fn_sig = ty::Binder::bind(ty::FnSig { inputs_and_output, c_variadic, unsafety, abi });
34     tcx.mk_fn_ptr(fn_sig)
35 }
36
37 crate fn type_list(tcx: TyCtxt<'tcx>, arity: usize) -> SubstsRef<'tcx> {
38     tcx.mk_substs(
39         (0..arity)
40             .into_iter()
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)),
44     )
45 }
46
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)));
49
50     tcx.mk_ref(region, ty::TypeAndMut { ty: bound(tcx, 1), mutbl })
51 }
52
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)))
55 }
56
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))
59 }
60
61 crate fn generator(tcx: TyCtxt<'tcx>, def_id: DefId) -> Ty<'tcx> {
62     tcx.mk_generator(
63         def_id,
64         InternalSubsts::bound_vars_for_item(tcx, def_id),
65         hir::Movability::Movable,
66     )
67 }