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.
67use std::marker::PointeeSized;
89use 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;
1415use super::compiler_interface::BridgeTys;
16use crate::{CtorKind, ItemKind};
1718pub(crate) mod convert;
19mod internal_cx;
2021/// 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 {
27fn tcx(self) -> TyCtxt<'tcx>;
2829fn lift<T: ty::Lift<TyCtxt<'tcx>>>(self, value: T) -> Option<T::Lifted>;
3031fn mk_args_from_iter<I, T>(self, iter: I) -> T::Output
32where
33I: Iterator<Item = T>,
34 T: ty::CollectAndApply<ty::GenericArg<'tcx>, ty::GenericArgsRef<'tcx>>;
3536fn mk_pat(self, v: ty::PatternKind<'tcx>) -> ty::Pattern<'tcx>;
3738fn mk_poly_existential_predicates(
39self,
40 eps: &[ty::PolyExistentialPredicate<'tcx>],
41 ) -> &'tcx List<ty::PolyExistentialPredicate<'tcx>>;
4243fn mk_type_list(self, v: &[Ty<'tcx>]) -> &'tcx List<Ty<'tcx>>;
4445fn lifetimes_re_erased(self) -> ty::Region<'tcx>;
4647fn mk_bound_variable_kinds_from_iter<I, T>(self, iter: I) -> T::Output
48where
49I: Iterator<Item = T>,
50 T: ty::CollectAndApply<
51 ty::BoundVariableKind<'tcx>,
52&'tcx List<ty::BoundVariableKind<'tcx>>,
53 >;
5455fn mk_place_elems(self, v: &[mir::PlaceElem<'tcx>]) -> &'tcx List<mir::PlaceElem<'tcx>>;
5657fn adt_def(self, def_id: rustc_hir::def_id::DefId) -> ty::AdtDef<'tcx>;
5859fn mk_patterns_from_iter<I, T>(self, iter: I) -> T::Output
60where
61I: Iterator<Item = T>,
62 T: ty::CollectAndApply<ty::Pattern<'tcx>, &'tcx List<ty::Pattern<'tcx>>>;
63}
6465/// Trait used to convert between an internal MIR type to a rustc_public's IR type.
66///
67/// This trait is currently exposed to users so they can have interoperability
68/// between internal MIR and rustc_public's IR constructs.
69/// However, they should be used seldom and they have no influence in this crate semver.
70#[doc(hidden)]
71#[cfg_attr(not(feature = "rustc_internal"), allow(unreachable_pub))]
72pub trait Stable<'tcx>: PointeeSized {
73/// The stable representation of the type implementing Stable.
74type T;
75/// Converts an object to the equivalent rustc_public's IR representation.
76fn stable<'cx>(
77&self,
78 tables: &mut Tables<'cx, BridgeTys>,
79 cx: &CompilerCtxt<'cx, BridgeTys>,
80 ) -> Self::T;
81}
8283/// Trait used to translate a rustc_public's IR construct to its rustc counterpart.
84///
85/// This is basically a mirror of [Stable].
86///
87/// This trait is currently exposed to users so they can have interoperability
88/// between internal MIR and rustc_public's IR constructs.
89/// They should be used seldom as they have no stability guarantees.
90#[doc(hidden)]
91#[cfg_attr(not(feature = "rustc_internal"), allow(unreachable_pub))]
92pub trait RustcInternal {
93type T<'tcx>;
94fn internal<'tcx>(
95&self,
96 tables: &mut Tables<'_, BridgeTys>,
97 tcx: impl InternalCx<'tcx>,
98 ) -> Self::T<'tcx>;
99}
100101pub(crate) fn new_item_kind(kind: DefKind) -> ItemKind {
102match kind {
103 DefKind::Mod104 | DefKind::Struct105 | DefKind::Union106 | DefKind::Enum107 | DefKind::Variant108 | DefKind::Trait109 | DefKind::TyAlias110 | DefKind::ForeignTy111 | DefKind::TraitAlias112 | DefKind::AssocTy113 | DefKind::TyParam114 | DefKind::ConstParam115 | DefKind::Macro(_)
116 | DefKind::ExternCrate117 | DefKind::Use118 | DefKind::ForeignMod119 | DefKind::OpaqueTy120 | DefKind::Field121 | DefKind::LifetimeParam122 | DefKind::Impl { .. }
123 | DefKind::GlobalAsm => {
124{
::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:?}");
125 }
126 DefKind::Closure | DefKind::AssocFn | DefKind::Fn | DefKind::SyntheticCoroutineBody => {
127 ItemKind::Fn128 }
129 DefKind::Const { .. }
130 | DefKind::InlineConst131 | DefKind::AssocConst { .. }
132 | DefKind::AnonConst => ItemKind::Const,
133 DefKind::Static { .. } => ItemKind::Static,
134 DefKind::Ctor(_, rustc_hir::def::CtorKind::Const) => ItemKind::Ctor(CtorKind::Const),
135 DefKind::Ctor(_, rustc_hir::def::CtorKind::Fn) => ItemKind::Ctor(CtorKind::Fn),
136 }
137}