Skip to main content

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`].
25#[cfg_attr(not(feature = "rustc_internal"), allow(unreachable_pub))]
26pub trait InternalCx<'tcx>: Copy + Clone {
27    fn tcx(self) -> TyCtxt<'tcx>;
28
29    fn lift<T: ty::Lift<TyCtxt<'tcx>>>(self, value: T) -> Option<T::Lifted>;
30
31    fn mk_args_from_iter<I, T>(self, iter: I) -> T::Output
32    where
33        I: Iterator<Item = T>,
34        T: ty::CollectAndApply<ty::GenericArg<'tcx>, ty::GenericArgsRef<'tcx>>;
35
36    fn mk_pat(self, v: ty::PatternKind<'tcx>) -> ty::Pattern<'tcx>;
37
38    fn mk_poly_existential_predicates(
39        self,
40        eps: &[ty::PolyExistentialPredicate<'tcx>],
41    ) -> &'tcx List<ty::PolyExistentialPredicate<'tcx>>;
42
43    fn mk_type_list(self, v: &[Ty<'tcx>]) -> &'tcx List<Ty<'tcx>>;
44
45    fn lifetimes_re_erased(self) -> ty::Region<'tcx>;
46
47    fn mk_bound_variable_kinds_from_iter<I, T>(self, iter: I) -> T::Output
48    where
49        I: Iterator<Item = T>,
50        T: ty::CollectAndApply<
51                ty::BoundVariableKind<'tcx>,
52                &'tcx List<ty::BoundVariableKind<'tcx>>,
53            >;
54
55    fn mk_place_elems(self, v: &[mir::PlaceElem<'tcx>]) -> &'tcx List<mir::PlaceElem<'tcx>>;
56
57    fn adt_def(self, def_id: rustc_hir::def_id::DefId) -> ty::AdtDef<'tcx>;
58}
59
60/// Trait used to convert between an internal MIR type to a rustc_public's IR type.
61///
62/// This trait is currently exposed to users so they can have interoperability
63/// between internal MIR and rustc_public's IR constructs.
64/// However, they should be used seldom and they have no influence in this crate semver.
65#[doc(hidden)]
66#[cfg_attr(not(feature = "rustc_internal"), allow(unreachable_pub))]
67pub trait Stable<'tcx>: PointeeSized {
68    /// The stable representation of the type implementing Stable.
69    type T;
70    /// Converts an object to the equivalent rustc_public's IR representation.
71    fn stable<'cx>(
72        &self,
73        tables: &mut Tables<'cx, BridgeTys>,
74        cx: &CompilerCtxt<'cx, BridgeTys>,
75    ) -> Self::T;
76}
77
78/// Trait used to translate a rustc_public's IR construct to its rustc counterpart.
79///
80/// This is basically a mirror of [Stable].
81///
82/// This trait is currently exposed to users so they can have interoperability
83/// between internal MIR and rustc_public's IR constructs.
84/// They should be used seldom as they have no stability guarantees.
85#[doc(hidden)]
86#[cfg_attr(not(feature = "rustc_internal"), allow(unreachable_pub))]
87pub trait RustcInternal {
88    type T<'tcx>;
89    fn internal<'tcx>(
90        &self,
91        tables: &mut Tables<'_, BridgeTys>,
92        tcx: impl InternalCx<'tcx>,
93    ) -> Self::T<'tcx>;
94}
95
96pub(crate) fn new_item_kind(kind: DefKind) -> ItemKind {
97    match kind {
98        DefKind::Mod
99        | DefKind::Struct
100        | DefKind::Union
101        | DefKind::Enum
102        | DefKind::Variant
103        | DefKind::Trait
104        | DefKind::TyAlias
105        | DefKind::ForeignTy
106        | DefKind::TraitAlias
107        | DefKind::AssocTy
108        | DefKind::TyParam
109        | DefKind::ConstParam
110        | DefKind::Macro(_)
111        | DefKind::ExternCrate
112        | DefKind::Use
113        | DefKind::ForeignMod
114        | DefKind::OpaqueTy
115        | DefKind::Field
116        | DefKind::LifetimeParam
117        | DefKind::Impl { .. }
118        | DefKind::GlobalAsm => {
119            {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("Not a valid item kind: {0:?}", kind)));
};unreachable!("Not a valid item kind: {kind:?}");
120        }
121        DefKind::Closure | DefKind::AssocFn | DefKind::Fn | DefKind::SyntheticCoroutineBody => {
122            ItemKind::Fn
123        }
124        DefKind::Const | DefKind::InlineConst | DefKind::AssocConst | DefKind::AnonConst => {
125            ItemKind::Const
126        }
127        DefKind::Static { .. } => ItemKind::Static,
128        DefKind::Ctor(_, rustc_hir::def::CtorKind::Const) => ItemKind::Ctor(CtorKind::Const),
129        DefKind::Ctor(_, rustc_hir::def::CtorKind::Fn) => ItemKind::Ctor(CtorKind::Fn),
130    }
131}