rustc_public/unstable/
mod.rs

1//! Module that collects the things that have no stability guarantees.
2//!
3//! We want to keep rustc_public's IR definitions and logic separate from
4//! any sort of conversion and usage of internal rustc code. So we
5//! restrict the usage of internal items to be inside this module.
6
7use std::marker::PointeeSized;
8
9use rustc_hir::def::DefKind;
10use rustc_middle::ty::{List, Ty, TyCtxt};
11use rustc_middle::{mir, ty};
12use rustc_public_bridge::Tables;
13use rustc_public_bridge::context::CompilerCtxt;
14
15use super::compiler_interface::BridgeTys;
16use crate::{CtorKind, ItemKind};
17
18pub(crate) mod convert;
19mod internal_cx;
20
21/// Trait that defines the methods that are fine to call from [`RustcInternal`].
22///
23/// This trait is only for [`RustcInternal`]. Any other other access to rustc's internals
24/// should go through [`rustc_public_bridge::context::CompilerCtxt`].
25pub trait InternalCx<'tcx>: Copy + Clone {
26    fn tcx(self) -> TyCtxt<'tcx>;
27
28    fn lift<T: ty::Lift<TyCtxt<'tcx>>>(self, value: T) -> Option<T::Lifted>;
29
30    fn mk_args_from_iter<I, T>(self, iter: I) -> T::Output
31    where
32        I: Iterator<Item = T>,
33        T: ty::CollectAndApply<ty::GenericArg<'tcx>, ty::GenericArgsRef<'tcx>>;
34
35    fn mk_pat(self, v: ty::PatternKind<'tcx>) -> ty::Pattern<'tcx>;
36
37    fn mk_poly_existential_predicates(
38        self,
39        eps: &[ty::PolyExistentialPredicate<'tcx>],
40    ) -> &'tcx List<ty::PolyExistentialPredicate<'tcx>>;
41
42    fn mk_type_list(self, v: &[Ty<'tcx>]) -> &'tcx List<Ty<'tcx>>;
43
44    fn lifetimes_re_erased(self) -> ty::Region<'tcx>;
45
46    fn mk_bound_variable_kinds_from_iter<I, T>(self, iter: I) -> T::Output
47    where
48        I: Iterator<Item = T>,
49        T: ty::CollectAndApply<ty::BoundVariableKind, &'tcx List<ty::BoundVariableKind>>;
50
51    fn mk_place_elems(self, v: &[mir::PlaceElem<'tcx>]) -> &'tcx List<mir::PlaceElem<'tcx>>;
52
53    fn adt_def(self, def_id: rustc_hir::def_id::DefId) -> ty::AdtDef<'tcx>;
54}
55
56/// Trait used to convert between an internal MIR type to a rustc_public's IR type.
57///
58/// This trait is currently exposed to users so they can have interoperability
59/// between internal MIR and rustc_public's IR constructs.
60/// However, they should be used seldom and they have no influence in this crate semver.
61#[doc(hidden)]
62pub trait Stable<'tcx>: PointeeSized {
63    /// The stable representation of the type implementing Stable.
64    type T;
65    /// Converts an object to the equivalent rustc_public's IR representation.
66    fn stable<'cx>(
67        &self,
68        tables: &mut Tables<'cx, BridgeTys>,
69        cx: &CompilerCtxt<'cx, BridgeTys>,
70    ) -> Self::T;
71}
72
73/// Trait used to translate a rustc_public's IR construct to its rustc counterpart.
74///
75/// This is basically a mirror of [Stable].
76///
77/// This trait is currently exposed to users so they can have interoperability
78/// between internal MIR and rustc_public's IR constructs.
79/// They should be used seldom as they have no stability guarantees.
80#[doc(hidden)]
81pub trait RustcInternal {
82    type T<'tcx>;
83    fn internal<'tcx>(
84        &self,
85        tables: &mut Tables<'_, BridgeTys>,
86        tcx: impl InternalCx<'tcx>,
87    ) -> Self::T<'tcx>;
88}
89
90pub(crate) fn new_item_kind(kind: DefKind) -> ItemKind {
91    match kind {
92        DefKind::Mod
93        | DefKind::Struct
94        | DefKind::Union
95        | DefKind::Enum
96        | DefKind::Variant
97        | DefKind::Trait
98        | DefKind::TyAlias
99        | DefKind::ForeignTy
100        | DefKind::TraitAlias
101        | DefKind::AssocTy
102        | DefKind::TyParam
103        | DefKind::ConstParam
104        | DefKind::Macro(_)
105        | DefKind::ExternCrate
106        | DefKind::Use
107        | DefKind::ForeignMod
108        | DefKind::OpaqueTy
109        | DefKind::Field
110        | DefKind::LifetimeParam
111        | DefKind::Impl { .. }
112        | DefKind::GlobalAsm => {
113            unreachable!("Not a valid item kind: {kind:?}");
114        }
115        DefKind::Closure | DefKind::AssocFn | DefKind::Fn | DefKind::SyntheticCoroutineBody => {
116            ItemKind::Fn
117        }
118        DefKind::Const | DefKind::InlineConst | DefKind::AssocConst | DefKind::AnonConst => {
119            ItemKind::Const
120        }
121        DefKind::Static { .. } => ItemKind::Static,
122        DefKind::Ctor(_, rustc_hir::def::CtorKind::Const) => ItemKind::Ctor(CtorKind::Const),
123        DefKind::Ctor(_, rustc_hir::def::CtorKind::Fn) => ItemKind::Ctor(CtorKind::Fn),
124    }
125}