1#![allow(rustc::usage_of_ty_tykind)]
10#![doc(test(attr(allow(unused_variables), deny(warnings), allow(internal_features))))]
11#![feature(sized_hierarchy)]
12use std::fmt::Debug;
20use std::marker::PhantomData;
21use std::{fmt, io};
22
23pub(crate) use rustc_public_bridge::IndexedVal;
24use rustc_public_bridge::Tables;
25use rustc_public_bridge::context::CompilerCtxt;
26#[cfg(feature = "rustc_internal")]
29pub mod rustc_internal;
30use serde::Serialize;
31
32use crate::compiler_interface::with;
33pub use crate::crate_def::{CrateDef, CrateDefType, DefId};
34pub use crate::error::*;
35use crate::mir::mono::StaticDef;
36use crate::mir::{Body, Mutability};
37use crate::ty::{
38 AdtDef, AssocItem, FnDef, ForeignModuleDef, ImplDef, ProvenanceMap, Span, TraitDef, Ty,
39 serialize_index_impl,
40};
41use crate::unstable::Stable;
42
43pub mod abi;
44mod alloc;
45pub(crate) mod unstable;
46#[macro_use]
47pub mod crate_def;
48pub mod compiler_interface;
49#[macro_use]
50pub mod error;
51pub mod mir;
52pub mod target;
53#[cfg(test)]
54mod tests;
55pub mod ty;
56pub mod visitor;
57
58pub type Symbol = String;
60
61#[derive(#[automatically_derived]
impl ::core::clone::Clone for CrateNum {
#[inline]
fn clone(&self) -> CrateNum {
let _: ::core::clone::AssertParamIsClone<usize>;
let _: ::core::clone::AssertParamIsClone<ThreadLocalIndex>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for CrateNum { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for CrateNum {
#[inline]
fn eq(&self, other: &CrateNum) -> bool {
self.0 == other.0 && self.1 == other.1
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for CrateNum {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<usize>;
let _: ::core::cmp::AssertParamIsEq<ThreadLocalIndex>;
}
}Eq, #[automatically_derived]
impl ::core::fmt::Debug for CrateNum {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field2_finish(f, "CrateNum",
&self.0, &&self.1)
}
}Debug)]
63pub struct CrateNum(pub(crate) usize, ThreadLocalIndex);
64impl ::serde::Serialize for CrateNum {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
S: ::serde::Serializer {
let n: usize = self.0;
::serde::Serialize::serialize(&n, serializer)
}
}serialize_index_impl!(CrateNum);
65
66impl Debug for DefId {
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 f.debug_struct("DefId").field("id", &self.0).field("name", &self.name()).finish()
69 }
70}
71
72pub type CrateItems = Vec<CrateItem>;
74
75pub type TraitDecls = Vec<TraitDef>;
77
78pub type ImplTraitDecls = Vec<ImplDef>;
80
81pub type AssocItems = Vec<AssocItem>;
83
84#[derive(#[automatically_derived]
impl ::core::clone::Clone for Crate {
#[inline]
fn clone(&self) -> Crate {
Crate {
id: ::core::clone::Clone::clone(&self.id),
name: ::core::clone::Clone::clone(&self.name),
is_local: ::core::clone::Clone::clone(&self.is_local),
}
}
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for Crate {
#[inline]
fn eq(&self, other: &Crate) -> bool {
self.is_local == other.is_local && self.id == other.id &&
self.name == other.name
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Crate {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<CrateNum>;
let _: ::core::cmp::AssertParamIsEq<Symbol>;
let _: ::core::cmp::AssertParamIsEq<bool>;
}
}Eq, #[automatically_derived]
impl ::core::fmt::Debug for Crate {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f, "Crate", "id",
&self.id, "name", &self.name, "is_local", &&self.is_local)
}
}Debug, #[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
{
#[allow(unused_extern_crates, clippy :: useless_attribute)]
extern crate serde as _serde;
;
#[automatically_derived]
impl _serde::Serialize for Crate {
fn serialize<__S>(&self, __serializer: __S)
-> _serde::__private228::Result<__S::Ok, __S::Error> where
__S: _serde::Serializer {
let mut __serde_state =
_serde::Serializer::serialize_struct(__serializer, "Crate",
false as usize + 1 + 1 + 1)?;
_serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
"id", &self.id)?;
_serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
"name", &self.name)?;
_serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
"is_local", &self.is_local)?;
_serde::ser::SerializeStruct::end(__serde_state)
}
}
};Serialize)]
86pub struct Crate {
87 pub id: CrateNum,
88 pub name: Symbol,
89 pub is_local: bool,
90}
91
92impl Crate {
93 pub fn foreign_modules(&self) -> Vec<ForeignModuleDef> {
95 with(|cx| cx.foreign_modules(self.id))
96 }
97
98 pub fn trait_decls(&self) -> TraitDecls {
100 with(|cx| cx.trait_decls(self.id))
101 }
102
103 pub fn trait_impls(&self) -> ImplTraitDecls {
105 with(|cx| cx.trait_impls(self.id))
106 }
107
108 pub fn fn_defs(&self) -> Vec<FnDef> {
110 with(|cx| cx.crate_functions(self.id))
111 }
112
113 pub fn statics(&self) -> Vec<StaticDef> {
115 with(|cx| cx.crate_statics(self.id))
116 }
117
118 pub fn adts(&self) -> Vec<AdtDef> {
120 with(|cx| cx.crate_adts(self.id))
121 }
122}
123
124#[derive(#[automatically_derived]
impl ::core::marker::Copy for ItemKind { }Copy, #[automatically_derived]
impl ::core::clone::Clone for ItemKind {
#[inline]
fn clone(&self) -> ItemKind {
let _: ::core::clone::AssertParamIsClone<CtorKind>;
*self
}
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for ItemKind {
#[inline]
fn eq(&self, other: &ItemKind) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(ItemKind::Ctor(__self_0), ItemKind::Ctor(__arg1_0)) =>
__self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for ItemKind {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<CtorKind>;
}
}Eq, #[automatically_derived]
impl ::core::fmt::Debug for ItemKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
ItemKind::Fn => ::core::fmt::Formatter::write_str(f, "Fn"),
ItemKind::Static =>
::core::fmt::Formatter::write_str(f, "Static"),
ItemKind::Const => ::core::fmt::Formatter::write_str(f, "Const"),
ItemKind::Ctor(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Ctor",
&__self_0),
}
}
}Debug, #[automatically_derived]
impl ::core::hash::Hash for ItemKind {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state);
match self {
ItemKind::Ctor(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
_ => {}
}
}
}Hash, #[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
{
#[allow(unused_extern_crates, clippy :: useless_attribute)]
extern crate serde as _serde;
;
#[automatically_derived]
impl _serde::Serialize for ItemKind {
fn serialize<__S>(&self, __serializer: __S)
-> _serde::__private228::Result<__S::Ok, __S::Error> where
__S: _serde::Serializer {
match *self {
ItemKind::Fn =>
_serde::Serializer::serialize_unit_variant(__serializer,
"ItemKind", 0u32, "Fn"),
ItemKind::Static =>
_serde::Serializer::serialize_unit_variant(__serializer,
"ItemKind", 1u32, "Static"),
ItemKind::Const =>
_serde::Serializer::serialize_unit_variant(__serializer,
"ItemKind", 2u32, "Const"),
ItemKind::Ctor(ref __field0) =>
_serde::Serializer::serialize_newtype_variant(__serializer,
"ItemKind", 3u32, "Ctor", __field0),
}
}
}
};Serialize)]
125pub enum ItemKind {
126 Fn,
127 Static,
128 Const,
129 Ctor(CtorKind),
130}
131
132#[derive(#[automatically_derived]
impl ::core::marker::Copy for CtorKind { }Copy, #[automatically_derived]
impl ::core::clone::Clone for CtorKind {
#[inline]
fn clone(&self) -> CtorKind { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for CtorKind {
#[inline]
fn eq(&self, other: &CtorKind) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for CtorKind {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::fmt::Debug for CtorKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self { CtorKind::Const => "Const", CtorKind::Fn => "Fn", })
}
}Debug, #[automatically_derived]
impl ::core::hash::Hash for CtorKind {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state)
}
}Hash, #[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
{
#[allow(unused_extern_crates, clippy :: useless_attribute)]
extern crate serde as _serde;
;
#[automatically_derived]
impl _serde::Serialize for CtorKind {
fn serialize<__S>(&self, __serializer: __S)
-> _serde::__private228::Result<__S::Ok, __S::Error> where
__S: _serde::Serializer {
match *self {
CtorKind::Const =>
_serde::Serializer::serialize_unit_variant(__serializer,
"CtorKind", 0u32, "Const"),
CtorKind::Fn =>
_serde::Serializer::serialize_unit_variant(__serializer,
"CtorKind", 1u32, "Fn"),
}
}
}
};Serialize)]
133pub enum CtorKind {
134 Const,
135 Fn,
136}
137
138pub type Filename = String;
139
140#[automatically_derived]
impl ::core::clone::Clone for CrateItem {
#[inline]
fn clone(&self) -> CrateItem {
let _: ::core::clone::AssertParamIsClone<DefId>;
*self
}
}
#[automatically_derived]
impl ::core::marker::Copy for CrateItem { }
#[automatically_derived]
impl ::core::marker::StructuralPartialEq for CrateItem { }
#[automatically_derived]
impl ::core::cmp::PartialEq for CrateItem {
#[inline]
fn eq(&self, other: &CrateItem) -> bool { self.0 == other.0 }
}
#[automatically_derived]
impl ::core::cmp::Eq for CrateItem {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<DefId>;
}
}
#[automatically_derived]
impl ::core::fmt::Debug for CrateItem {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "CrateItem",
&&self.0)
}
}
#[automatically_derived]
impl ::core::hash::Hash for CrateItem {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state)
}
}
#[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
{
#[allow(unused_extern_crates, clippy :: useless_attribute)]
extern crate serde as _serde;
;
#[automatically_derived]
impl _serde::Serialize for CrateItem {
fn serialize<__S>(&self, __serializer: __S)
-> _serde::__private228::Result<__S::Ok, __S::Error> where
__S: _serde::Serializer {
_serde::Serializer::serialize_newtype_struct(__serializer,
"CrateItem", &self.0)
}
}
};
impl CrateDef for CrateItem {
fn def_id(&self) -> DefId { self.0 }
}
impl CrateDefType for CrateItem {}crate_def_with_ty! {
141 #[derive(Serialize)]
143 pub CrateItem;
144}
145
146impl CrateItem {
147 pub fn expect_body(&self) -> mir::Body {
149 with(|cx| cx.mir_body(self.0))
150 }
151
152 pub fn body(&self) -> Option<mir::Body> {
154 with(|cx| cx.has_body(self.0).then(|| cx.mir_body(self.0)))
155 }
156
157 pub fn has_body(&self) -> bool {
159 with(|cx| cx.has_body(self.0))
160 }
161
162 pub fn span(&self) -> Span {
163 self.0.span()
164 }
165
166 pub fn kind(&self) -> ItemKind {
167 with(|cx| cx.item_kind(*self))
168 }
169
170 pub fn requires_monomorphization(&self) -> bool {
171 with(|cx| cx.requires_monomorphization(self.0))
172 }
173
174 pub fn ty(&self) -> Ty {
175 with(|cx| cx.def_ty(self.0))
176 }
177
178 pub fn is_foreign_item(&self) -> bool {
179 with(|cx| cx.is_foreign_item(self.0))
180 }
181
182 pub fn emit_mir<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
184 self.body()
185 .ok_or_else(|| io::Error::other(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("No body found for `{0}`",
self.name()))
})format!("No body found for `{}`", self.name())))?
186 .dump(w, &self.trimmed_name())
187 }
188}
189
190pub fn entry_fn() -> Option<CrateItem> {
194 with(|cx| cx.entry_fn())
195}
196
197pub fn local_crate() -> Crate {
199 with(|cx| cx.local_crate())
200}
201
202pub fn find_crates(name: &str) -> Vec<Crate> {
204 with(|cx| cx.find_crates(name))
205}
206
207pub fn external_crates() -> Vec<Crate> {
209 with(|cx| cx.external_crates())
210}
211
212pub fn all_local_items() -> CrateItems {
214 with(|cx| cx.all_local_items())
215}
216
217pub fn all_trait_decls() -> TraitDecls {
218 with(|cx| cx.all_trait_decls())
219}
220
221pub fn all_trait_impls() -> ImplTraitDecls {
222 with(|cx| cx.all_trait_impls())
223}
224
225#[derive(#[automatically_derived]
impl ::core::clone::Clone for Opaque {
#[inline]
fn clone(&self) -> Opaque { Opaque(::core::clone::Clone::clone(&self.0)) }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for Opaque {
#[inline]
fn eq(&self, other: &Opaque) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Opaque {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<String>;
}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for Opaque {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state)
}
}Hash, #[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
{
#[allow(unused_extern_crates, clippy :: useless_attribute)]
extern crate serde as _serde;
;
#[automatically_derived]
impl _serde::Serialize for Opaque {
fn serialize<__S>(&self, __serializer: __S)
-> _serde::__private228::Result<__S::Ok, __S::Error> where
__S: _serde::Serializer {
_serde::Serializer::serialize_newtype_struct(__serializer,
"Opaque", &self.0)
}
}
};Serialize)]
227pub struct Opaque(String);
228
229impl std::fmt::Display for Opaque {
230 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
231 f.write_fmt(format_args!("{0}", self.0))write!(f, "{}", self.0)
232 }
233}
234
235impl std::fmt::Debug for Opaque {
236 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
237 f.write_fmt(format_args!("{0}", self.0))write!(f, "{}", self.0)
238 }
239}
240
241pub fn opaque<T: Debug>(value: &T) -> Opaque {
242 Opaque(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", value))
})format!("{value:?}"))
243}
244
245macro_rules! bridge_impl {
246 ( $( $name:ident, $ty:ty ),* $(,)? ) => {
247 $(
248 impl rustc_public_bridge::bridge::$name<compiler_interface::BridgeTys> for $ty {
249 fn new(def: crate::DefId) -> Self {
250 Self(def)
251 }
252 }
253 )*
254 };
255}
256
257#[rustfmt::skip]
258impl rustc_public_bridge::bridge::StaticDef<compiler_interface::BridgeTys> for
crate::mir::mono::StaticDef {
fn new(def: crate::DefId) -> Self { Self(def) }
}bridge_impl!(
259 CrateItem, crate::CrateItem,
260 AdtDef, crate::ty::AdtDef,
261 ForeignModuleDef, crate::ty::ForeignModuleDef,
262 ForeignDef, crate::ty::ForeignDef,
263 FnDef, crate::ty::FnDef,
264 ClosureDef, crate::ty::ClosureDef,
265 CoroutineDef, crate::ty::CoroutineDef,
266 CoroutineClosureDef, crate::ty::CoroutineClosureDef,
267 AliasDef, crate::ty::AliasDef,
268 ParamDef, crate::ty::ParamDef,
269 BrNamedDef, crate::ty::BrNamedDef,
270 TraitDef, crate::ty::TraitDef,
271 GenericDef, crate::ty::GenericDef,
272 ConstDef, crate::ty::ConstDef,
273 ImplDef, crate::ty::ImplDef,
274 RegionDef, crate::ty::RegionDef,
275 CoroutineWitnessDef, crate::ty::CoroutineWitnessDef,
276 AssocDef, crate::ty::AssocDef,
277 OpaqueDef, crate::ty::OpaqueDef,
278 StaticDef, crate::mir::mono::StaticDef
279);
280
281impl rustc_public_bridge::bridge::Prov<compiler_interface::BridgeTys> for crate::ty::Prov {
282 fn new(aid: crate::mir::alloc::AllocId) -> Self {
283 Self(aid)
284 }
285}
286
287impl rustc_public_bridge::bridge::Allocation<compiler_interface::BridgeTys>
288 for crate::ty::Allocation
289{
290 fn new<'tcx>(
291 bytes: Vec<Option<u8>>,
292 ptrs: Vec<(usize, rustc_middle::mir::interpret::AllocId)>,
293 align: u64,
294 mutability: rustc_middle::mir::Mutability,
295 tables: &mut Tables<'tcx, compiler_interface::BridgeTys>,
296 cx: &CompilerCtxt<'tcx, compiler_interface::BridgeTys>,
297 ) -> Self {
298 Self {
299 bytes,
300 provenance: ProvenanceMap {
301 ptrs: ptrs.iter().map(|(i, aid)| (*i, tables.prov(*aid))).collect(),
302 },
303 align,
304 mutability: mutability.stable(tables, cx),
305 }
306 }
307}
308
309#[derive(#[automatically_derived]
impl ::core::clone::Clone for ThreadLocalIndex {
#[inline]
fn clone(&self) -> ThreadLocalIndex {
let _: ::core::clone::AssertParamIsClone<PhantomData<*const ()>>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for ThreadLocalIndex { }Copy, #[automatically_derived]
impl ::core::hash::Hash for ThreadLocalIndex {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self._phantom, state)
}
}Hash, #[automatically_derived]
impl ::core::cmp::PartialEq for ThreadLocalIndex {
#[inline]
fn eq(&self, other: &ThreadLocalIndex) -> bool {
self._phantom == other._phantom
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for ThreadLocalIndex {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<PhantomData<*const ()>>;
}
}Eq, #[automatically_derived]
impl ::core::default::Default for ThreadLocalIndex {
#[inline]
fn default() -> ThreadLocalIndex {
ThreadLocalIndex { _phantom: ::core::default::Default::default() }
}
}Default)]
310pub(crate) struct ThreadLocalIndex {
319 _phantom: PhantomData<*const ()>,
320}
321#[expect(non_upper_case_globals)]
322pub(crate) const ThreadLocalIndex: ThreadLocalIndex = ThreadLocalIndex { _phantom: PhantomData };
324
325impl fmt::Debug for ThreadLocalIndex {
326 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
327 f.debug_tuple("ThreadLocalIndex").finish()
328 }
329}