stable_mir/
compiler_interface.rs

1//! Define the interface with the Rust compiler.
2//!
3//! StableMIR users should not use any of the items in this module directly.
4//! These APIs have no stability guarantee.
5
6use std::cell::Cell;
7
8use rustc_hir::def::DefKind;
9use rustc_smir::context::SmirCtxt;
10use rustc_smir::{Bridge, SmirContainer};
11use tracing::debug;
12
13use crate::abi::{FnAbi, Layout, LayoutShape, ReprOptions};
14use crate::crate_def::Attribute;
15use crate::mir::alloc::{AllocId, GlobalAlloc};
16use crate::mir::mono::{Instance, InstanceDef, StaticDef};
17use crate::mir::{BinOp, Body, Place, UnOp};
18use crate::target::{MachineInfo, MachineSize};
19use crate::ty::{
20    AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, CoroutineDef, Discr, FieldDef, FnDef,
21    ForeignDef, ForeignItemKind, ForeignModule, ForeignModuleDef, GenericArgs, GenericPredicates,
22    Generics, ImplDef, ImplTrait, IntrinsicDef, LineInfo, MirConst, PolyFnSig, RigidTy, Span,
23    TraitDecl, TraitDef, Ty, TyConst, TyConstId, TyKind, UintTy, VariantDef, VariantIdx,
24};
25use crate::unstable::{RustcInternal, Stable, new_item_kind};
26use crate::{
27    AssocItems, Crate, CrateDef, CrateItem, CrateItems, CrateNum, DefId, Error, Filename,
28    ImplTraitDecls, ItemKind, Symbol, TraitDecls, alloc, mir,
29};
30
31pub struct BridgeTys;
32
33impl Bridge for BridgeTys {
34    type DefId = crate::DefId;
35    type AllocId = crate::mir::alloc::AllocId;
36    type Span = crate::ty::Span;
37    type Ty = crate::ty::Ty;
38    type InstanceDef = crate::mir::mono::InstanceDef;
39    type TyConstId = crate::ty::TyConstId;
40    type MirConstId = crate::ty::MirConstId;
41    type Layout = crate::abi::Layout;
42
43    type Error = crate::Error;
44    type CrateItem = crate::CrateItem;
45    type AdtDef = crate::ty::AdtDef;
46    type ForeignModuleDef = crate::ty::ForeignModuleDef;
47    type ForeignDef = crate::ty::ForeignDef;
48    type FnDef = crate::ty::FnDef;
49    type ClosureDef = crate::ty::ClosureDef;
50    type CoroutineDef = crate::ty::CoroutineDef;
51    type CoroutineClosureDef = crate::ty::CoroutineClosureDef;
52    type AliasDef = crate::ty::AliasDef;
53    type ParamDef = crate::ty::ParamDef;
54    type BrNamedDef = crate::ty::BrNamedDef;
55    type TraitDef = crate::ty::TraitDef;
56    type GenericDef = crate::ty::GenericDef;
57    type ConstDef = crate::ty::ConstDef;
58    type ImplDef = crate::ty::ImplDef;
59    type RegionDef = crate::ty::RegionDef;
60    type CoroutineWitnessDef = crate::ty::CoroutineWitnessDef;
61    type AssocDef = crate::ty::AssocDef;
62    type OpaqueDef = crate::ty::OpaqueDef;
63    type Prov = crate::ty::Prov;
64    type StaticDef = crate::mir::mono::StaticDef;
65
66    type Allocation = crate::ty::Allocation;
67}
68
69/// Stable public API for querying compiler information.
70///
71/// All queries are delegated to [`rustc_smir::context::SmirCtxt`] that provides
72/// similar APIs but based on internal rustc constructs.
73///
74/// Do not use this directly. This is currently used in the macro expansion.
75pub(crate) trait SmirInterface {
76    fn entry_fn(&self) -> Option<CrateItem>;
77    /// Retrieve all items of the local crate that have a MIR associated with them.
78    fn all_local_items(&self) -> CrateItems;
79    /// Retrieve the body of a function.
80    /// This function will panic if the body is not available.
81    fn mir_body(&self, item: DefId) -> mir::Body;
82    /// Check whether the body of a function is available.
83    fn has_body(&self, item: DefId) -> bool;
84    fn foreign_modules(&self, crate_num: CrateNum) -> Vec<ForeignModuleDef>;
85
86    /// Retrieve all functions defined in this crate.
87    fn crate_functions(&self, crate_num: CrateNum) -> Vec<FnDef>;
88
89    /// Retrieve all static items defined in this crate.
90    fn crate_statics(&self, crate_num: CrateNum) -> Vec<StaticDef>;
91    fn foreign_module(&self, mod_def: ForeignModuleDef) -> ForeignModule;
92    fn foreign_items(&self, mod_def: ForeignModuleDef) -> Vec<ForeignDef>;
93    fn all_trait_decls(&self) -> TraitDecls;
94    fn trait_decls(&self, crate_num: CrateNum) -> TraitDecls;
95    fn trait_decl(&self, trait_def: &TraitDef) -> TraitDecl;
96    fn all_trait_impls(&self) -> ImplTraitDecls;
97    fn trait_impls(&self, crate_num: CrateNum) -> ImplTraitDecls;
98    fn trait_impl(&self, trait_impl: &ImplDef) -> ImplTrait;
99    fn generics_of(&self, def_id: DefId) -> Generics;
100    fn predicates_of(&self, def_id: DefId) -> GenericPredicates;
101    fn explicit_predicates_of(&self, def_id: DefId) -> GenericPredicates;
102
103    /// Get information about the local crate.
104    fn local_crate(&self) -> Crate;
105    /// Retrieve a list of all external crates.
106    fn external_crates(&self) -> Vec<Crate>;
107
108    /// Find a crate with the given name.
109    fn find_crates(&self, name: &str) -> Vec<Crate>;
110
111    /// Returns the name of given `DefId`
112    fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol;
113
114    /// Return registered tool attributes with the given attribute name.
115    ///
116    /// FIXME(jdonszelmann): may panic on non-tool attributes. After more attribute work, non-tool
117    /// attributes will simply return an empty list.
118    ///
119    /// Single segmented name like `#[clippy]` is specified as `&["clippy".to_string()]`.
120    /// Multi-segmented name like `#[rustfmt::skip]` is specified as `&["rustfmt".to_string(), "skip".to_string()]`.
121    fn tool_attrs(&self, def_id: DefId, attr: &[Symbol]) -> Vec<Attribute>;
122
123    /// Get all tool attributes of a definition.
124    fn all_tool_attrs(&self, def_id: DefId) -> Vec<Attribute>;
125
126    /// Returns printable, human readable form of `Span`
127    fn span_to_string(&self, span: Span) -> String;
128
129    /// Return filename from given `Span`, for diagnostic purposes
130    fn get_filename(&self, span: &Span) -> Filename;
131
132    /// Return lines corresponding to this `Span`
133    fn get_lines(&self, span: &Span) -> LineInfo;
134
135    /// Returns the `kind` of given `DefId`
136    fn item_kind(&self, item: CrateItem) -> ItemKind;
137
138    /// Returns whether this is a foreign item.
139    fn is_foreign_item(&self, item: DefId) -> bool;
140
141    /// Returns the kind of a given foreign item.
142    fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind;
143
144    /// Returns the kind of a given algebraic data type
145    fn adt_kind(&self, def: AdtDef) -> AdtKind;
146
147    /// Returns if the ADT is a box.
148    fn adt_is_box(&self, def: AdtDef) -> bool;
149
150    /// Returns whether this ADT is simd.
151    fn adt_is_simd(&self, def: AdtDef) -> bool;
152
153    /// Returns whether this definition is a C string.
154    fn adt_is_cstr(&self, def: AdtDef) -> bool;
155
156    /// Returns the representation options for this ADT.
157    fn adt_repr(&self, def: AdtDef) -> ReprOptions;
158
159    /// Retrieve the function signature for the given generic arguments.
160    fn fn_sig(&self, def: FnDef, args: &GenericArgs) -> PolyFnSig;
161
162    /// Retrieve the intrinsic definition if the item corresponds one.
163    fn intrinsic(&self, item: DefId) -> Option<IntrinsicDef>;
164
165    /// Retrieve the plain function name of an intrinsic.
166    fn intrinsic_name(&self, def: IntrinsicDef) -> Symbol;
167
168    /// Retrieve the closure signature for the given generic arguments.
169    fn closure_sig(&self, args: &GenericArgs) -> PolyFnSig;
170
171    /// The number of variants in this ADT.
172    fn adt_variants_len(&self, def: AdtDef) -> usize;
173
174    /// Discriminant for a given variant index of AdtDef.
175    fn adt_discr_for_variant(&self, adt: AdtDef, variant: VariantIdx) -> Discr;
176
177    /// Discriminant for a given variand index and args of a coroutine.
178    fn coroutine_discr_for_variant(
179        &self,
180        coroutine: CoroutineDef,
181        args: &GenericArgs,
182        variant: VariantIdx,
183    ) -> Discr;
184
185    /// The name of a variant.
186    fn variant_name(&self, def: VariantDef) -> Symbol;
187    fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef>;
188
189    /// Evaluate constant as a target usize.
190    fn eval_target_usize(&self, cnst: &MirConst) -> Result<u64, Error>;
191    fn eval_target_usize_ty(&self, cnst: &TyConst) -> Result<u64, Error>;
192
193    /// Create a new zero-sized constant.
194    fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error>;
195
196    /// Create a new constant that represents the given string value.
197    fn new_const_str(&self, value: &str) -> MirConst;
198
199    /// Create a new constant that represents the given boolean value.
200    fn new_const_bool(&self, value: bool) -> MirConst;
201
202    /// Create a new constant that represents the given value.
203    fn try_new_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<MirConst, Error>;
204    fn try_new_ty_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<TyConst, Error>;
205
206    /// Create a new type from the given kind.
207    fn new_rigid_ty(&self, kind: RigidTy) -> Ty;
208
209    /// Create a new box type, `Box<T>`, for the given inner type `T`.
210    fn new_box_ty(&self, ty: Ty) -> Ty;
211
212    /// Returns the type of given crate item.
213    fn def_ty(&self, item: DefId) -> Ty;
214
215    /// Returns the type of given definition instantiated with the given arguments.
216    fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty;
217
218    /// Returns literal value of a const as a string.
219    fn mir_const_pretty(&self, cnst: &MirConst) -> String;
220
221    /// `Span` of an item
222    fn span_of_an_item(&self, def_id: DefId) -> Span;
223
224    fn ty_const_pretty(&self, ct: TyConstId) -> String;
225
226    /// Obtain the representation of a type.
227    fn ty_pretty(&self, ty: Ty) -> String;
228
229    /// Obtain the kind of a type.
230    fn ty_kind(&self, ty: Ty) -> TyKind;
231
232    // Get the discriminant Ty for this Ty if there's one.
233    fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> Ty;
234
235    /// Get the body of an Instance which is already monomorphized.
236    fn instance_body(&self, instance: InstanceDef) -> Option<Body>;
237
238    /// Get the instance type with generic instantiations applied and lifetimes erased.
239    fn instance_ty(&self, instance: InstanceDef) -> Ty;
240
241    /// Get the instantiation types.
242    fn instance_args(&self, def: InstanceDef) -> GenericArgs;
243
244    /// Get the instance.
245    fn instance_def_id(&self, instance: InstanceDef) -> DefId;
246
247    /// Get the instance mangled name.
248    fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol;
249
250    /// Check if this is an empty DropGlue shim.
251    fn is_empty_drop_shim(&self, def: InstanceDef) -> bool;
252
253    /// Convert a non-generic crate item into an instance.
254    /// This function will panic if the item is generic.
255    fn mono_instance(&self, def_id: DefId) -> Instance;
256
257    /// Item requires monomorphization.
258    fn requires_monomorphization(&self, def_id: DefId) -> bool;
259
260    /// Resolve an instance from the given function definition and generic arguments.
261    fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
262
263    /// Resolve an instance for drop_in_place for the given type.
264    fn resolve_drop_in_place(&self, ty: Ty) -> Instance;
265
266    /// Resolve instance for a function pointer.
267    fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
268
269    /// Resolve instance for a closure with the requested type.
270    fn resolve_closure(
271        &self,
272        def: ClosureDef,
273        args: &GenericArgs,
274        kind: ClosureKind,
275    ) -> Option<Instance>;
276
277    /// Evaluate a static's initializer.
278    fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error>;
279
280    /// Try to evaluate an instance into a constant.
281    fn eval_instance(&self, def: InstanceDef, const_ty: Ty) -> Result<Allocation, Error>;
282
283    /// Retrieve global allocation for the given allocation ID.
284    fn global_alloc(&self, id: AllocId) -> GlobalAlloc;
285
286    /// Retrieve the id for the virtual table.
287    fn vtable_allocation(&self, global_alloc: &GlobalAlloc) -> Option<AllocId>;
288    fn krate(&self, def_id: DefId) -> Crate;
289    fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol;
290
291    /// Return information about the target machine.
292    fn target_info(&self) -> MachineInfo;
293
294    /// Get an instance ABI.
295    fn instance_abi(&self, def: InstanceDef) -> Result<FnAbi, Error>;
296
297    /// Get the ABI of a function pointer.
298    fn fn_ptr_abi(&self, fn_ptr: PolyFnSig) -> Result<FnAbi, Error>;
299
300    /// Get the layout of a type.
301    fn ty_layout(&self, ty: Ty) -> Result<Layout, Error>;
302
303    /// Get the layout shape.
304    fn layout_shape(&self, id: Layout) -> LayoutShape;
305
306    /// Get a debug string representation of a place.
307    fn place_pretty(&self, place: &Place) -> String;
308
309    /// Get the resulting type of binary operation.
310    fn binop_ty(&self, bin_op: BinOp, rhs: Ty, lhs: Ty) -> Ty;
311
312    /// Get the resulting type of unary operation.
313    fn unop_ty(&self, un_op: UnOp, arg: Ty) -> Ty;
314
315    /// Get all associated items of a definition.
316    fn associated_items(&self, def_id: DefId) -> AssocItems;
317}
318
319impl<'tcx> SmirInterface for SmirContainer<'tcx, BridgeTys> {
320    fn entry_fn(&self) -> Option<CrateItem> {
321        let mut tables = self.tables.borrow_mut();
322        let cx = &*self.cx.borrow();
323        let did = cx.entry_fn();
324        Some(tables.crate_item(did?))
325    }
326
327    /// Retrieve all items of the local crate that have a MIR associated with them.
328    fn all_local_items(&self) -> CrateItems {
329        let mut tables = self.tables.borrow_mut();
330        let cx = &*self.cx.borrow();
331        cx.all_local_items().iter().map(|did| tables.crate_item(*did)).collect()
332    }
333
334    /// Retrieve the body of a function.
335    /// This function will panic if the body is not available.
336    fn mir_body(&self, item: DefId) -> mir::Body {
337        let mut tables = self.tables.borrow_mut();
338        let cx = &*self.cx.borrow();
339        let did = tables[item];
340        cx.mir_body(did).stable(&mut *tables, cx)
341    }
342
343    /// Check whether the body of a function is available.
344    fn has_body(&self, item: DefId) -> bool {
345        let mut tables = self.tables.borrow_mut();
346        let cx = &*self.cx.borrow();
347        let def = item.internal(&mut *tables, cx.tcx);
348        cx.has_body(def)
349    }
350
351    fn foreign_modules(&self, crate_num: CrateNum) -> Vec<ForeignModuleDef> {
352        let mut tables = self.tables.borrow_mut();
353        let cx = &*self.cx.borrow();
354        cx.foreign_modules(crate_num.internal(&mut *tables, cx.tcx))
355            .iter()
356            .map(|did| tables.foreign_module_def(*did))
357            .collect()
358    }
359
360    /// Retrieve all functions defined in this crate.
361    fn crate_functions(&self, crate_num: CrateNum) -> Vec<FnDef> {
362        let mut tables = self.tables.borrow_mut();
363        let cx = &*self.cx.borrow();
364        let krate = crate_num.internal(&mut *tables, cx.tcx);
365        cx.crate_functions(krate).iter().map(|did| tables.fn_def(*did)).collect()
366    }
367
368    /// Retrieve all static items defined in this crate.
369    fn crate_statics(&self, crate_num: CrateNum) -> Vec<StaticDef> {
370        let mut tables = self.tables.borrow_mut();
371        let cx = &*self.cx.borrow();
372        let krate = crate_num.internal(&mut *tables, cx.tcx);
373        cx.crate_statics(krate).iter().map(|did| tables.static_def(*did)).collect()
374    }
375
376    fn foreign_module(&self, mod_def: ForeignModuleDef) -> ForeignModule {
377        let mut tables = self.tables.borrow_mut();
378        let cx = &*self.cx.borrow();
379        let did = tables[mod_def.def_id()];
380        cx.foreign_module(did).stable(&mut *tables, cx)
381    }
382
383    fn foreign_items(&self, mod_def: ForeignModuleDef) -> Vec<ForeignDef> {
384        let mut tables = self.tables.borrow_mut();
385        let cx = &*self.cx.borrow();
386        let did = tables[mod_def.def_id()];
387        cx.foreign_items(did).iter().map(|did| tables.foreign_def(*did)).collect()
388    }
389
390    fn all_trait_decls(&self) -> TraitDecls {
391        let mut tables = self.tables.borrow_mut();
392        let cx = &*self.cx.borrow();
393        cx.all_trait_decls().map(|did| tables.trait_def(did)).collect()
394    }
395
396    fn trait_decls(&self, crate_num: CrateNum) -> TraitDecls {
397        let mut tables = self.tables.borrow_mut();
398        let cx = &*self.cx.borrow();
399        let krate = crate_num.internal(&mut *tables, cx.tcx);
400        cx.trait_decls(krate).iter().map(|did| tables.trait_def(*did)).collect()
401    }
402
403    fn trait_decl(&self, trait_def: &TraitDef) -> TraitDecl {
404        let mut tables = self.tables.borrow_mut();
405        let cx = &*self.cx.borrow();
406        let did = tables[trait_def.0];
407        cx.trait_decl(did).stable(&mut *tables, cx)
408    }
409
410    fn all_trait_impls(&self) -> ImplTraitDecls {
411        let mut tables = self.tables.borrow_mut();
412        let cx = &*self.cx.borrow();
413        cx.all_trait_impls().iter().map(|did| tables.impl_def(*did)).collect()
414    }
415
416    fn trait_impls(&self, crate_num: CrateNum) -> ImplTraitDecls {
417        let mut tables = self.tables.borrow_mut();
418        let cx = &*self.cx.borrow();
419        let krate = crate_num.internal(&mut *tables, cx.tcx);
420        cx.trait_impls(krate).iter().map(|did| tables.impl_def(*did)).collect()
421    }
422
423    fn trait_impl(&self, trait_impl: &ImplDef) -> ImplTrait {
424        let mut tables = self.tables.borrow_mut();
425        let cx = &*self.cx.borrow();
426        let did = tables[trait_impl.0];
427        cx.trait_impl(did).stable(&mut *tables, cx)
428    }
429
430    fn generics_of(&self, def_id: DefId) -> Generics {
431        let mut tables = self.tables.borrow_mut();
432        let cx = &*self.cx.borrow();
433        let did = tables[def_id];
434        cx.generics_of(did).stable(&mut *tables, cx)
435    }
436
437    fn predicates_of(&self, def_id: DefId) -> GenericPredicates {
438        let mut tables = self.tables.borrow_mut();
439        let cx = &*self.cx.borrow();
440        let did = tables[def_id];
441        let (parent, kinds) = cx.predicates_of(did);
442        crate::ty::GenericPredicates {
443            parent: parent.map(|did| tables.trait_def(did)),
444            predicates: kinds
445                .iter()
446                .map(|(kind, span)| (kind.stable(&mut *tables, cx), span.stable(&mut *tables, cx)))
447                .collect(),
448        }
449    }
450
451    fn explicit_predicates_of(&self, def_id: DefId) -> GenericPredicates {
452        let mut tables = self.tables.borrow_mut();
453        let cx = &*self.cx.borrow();
454        let did = tables[def_id];
455        let (parent, kinds) = cx.explicit_predicates_of(did);
456        crate::ty::GenericPredicates {
457            parent: parent.map(|did| tables.trait_def(did)),
458            predicates: kinds
459                .iter()
460                .map(|(kind, span)| (kind.stable(&mut *tables, cx), span.stable(&mut *tables, cx)))
461                .collect(),
462        }
463    }
464
465    /// Get information about the local crate.
466    fn local_crate(&self) -> Crate {
467        let cx = &*self.cx.borrow();
468        smir_crate(cx, cx.local_crate_num())
469    }
470
471    /// Retrieve a list of all external crates.
472    fn external_crates(&self) -> Vec<Crate> {
473        let cx = &*self.cx.borrow();
474        cx.external_crates().iter().map(|crate_num| smir_crate(cx, *crate_num)).collect()
475    }
476
477    /// Find a crate with the given name.
478    fn find_crates(&self, name: &str) -> Vec<Crate> {
479        let cx = &*self.cx.borrow();
480        cx.find_crates(name).iter().map(|crate_num| smir_crate(cx, *crate_num)).collect()
481    }
482
483    /// Returns the name of given `DefId`.
484    fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol {
485        let tables = self.tables.borrow();
486        let cx = &*self.cx.borrow();
487        let did = tables[def_id];
488        cx.def_name(did, trimmed)
489    }
490
491    /// Return registered tool attributes with the given attribute name.
492    ///
493    /// FIXME(jdonszelmann): may panic on non-tool attributes. After more attribute work, non-tool
494    /// attributes will simply return an empty list.
495    ///
496    /// Single segmented name like `#[clippy]` is specified as `&["clippy".to_string()]`.
497    /// Multi-segmented name like `#[rustfmt::skip]` is specified as `&["rustfmt".to_string(), "skip".to_string()]`.
498    fn tool_attrs(&self, def_id: DefId, attr: &[Symbol]) -> Vec<Attribute> {
499        let mut tables = self.tables.borrow_mut();
500        let cx = &*self.cx.borrow();
501        let did = tables[def_id];
502        cx.tool_attrs(did, attr)
503            .into_iter()
504            .map(|(attr_str, span)| Attribute::new(attr_str, span.stable(&mut *tables, cx)))
505            .collect()
506    }
507
508    /// Get all tool attributes of a definition.
509    fn all_tool_attrs(&self, def_id: DefId) -> Vec<Attribute> {
510        let mut tables = self.tables.borrow_mut();
511        let cx = &*self.cx.borrow();
512        let did = tables[def_id];
513        cx.all_tool_attrs(did)
514            .into_iter()
515            .map(|(attr_str, span)| Attribute::new(attr_str, span.stable(&mut *tables, cx)))
516            .collect()
517    }
518
519    /// Returns printable, human readable form of `Span`.
520    fn span_to_string(&self, span: Span) -> String {
521        let tables = self.tables.borrow_mut();
522        let cx = &*self.cx.borrow();
523        let sp = tables.spans[span];
524        cx.span_to_string(sp)
525    }
526
527    /// Return filename from given `Span`, for diagnostic purposes.
528    fn get_filename(&self, span: &Span) -> Filename {
529        let tables = self.tables.borrow_mut();
530        let cx = &*self.cx.borrow();
531        let sp = tables.spans[*span];
532        cx.get_filename(sp)
533    }
534
535    /// Return lines corresponding to this `Span`.
536    fn get_lines(&self, span: &Span) -> LineInfo {
537        let tables = self.tables.borrow_mut();
538        let cx = &*self.cx.borrow();
539        let sp = tables.spans[*span];
540        let lines = cx.get_lines(sp);
541        LineInfo::from(lines)
542    }
543
544    /// Returns the `kind` of given `DefId`.
545    fn item_kind(&self, item: CrateItem) -> ItemKind {
546        let tables = self.tables.borrow();
547        let cx = &*self.cx.borrow();
548        let did = tables[item.0];
549        new_item_kind(cx.def_kind(did))
550    }
551
552    /// Returns whether this is a foreign item.
553    fn is_foreign_item(&self, item: DefId) -> bool {
554        let tables = self.tables.borrow();
555        let cx = &*self.cx.borrow();
556        let did = tables[item];
557        cx.is_foreign_item(did)
558    }
559
560    /// Returns the kind of a given foreign item.
561    fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind {
562        let mut tables = self.tables.borrow_mut();
563        let cx = &*self.cx.borrow();
564        let def_id = tables[def.def_id()];
565        let def_kind = cx.foreign_item_kind(def_id);
566        match def_kind {
567            DefKind::Fn => ForeignItemKind::Fn(tables.fn_def(def_id)),
568            DefKind::Static { .. } => ForeignItemKind::Static(tables.static_def(def_id)),
569            DefKind::ForeignTy => {
570                use rustc_smir::context::SmirTy;
571                ForeignItemKind::Type(tables.intern_ty(cx.new_foreign(def_id)))
572            }
573            def_kind => unreachable!("Unexpected kind for a foreign item: {:?}", def_kind),
574        }
575    }
576
577    /// Returns the kind of a given algebraic data type.
578    fn adt_kind(&self, def: AdtDef) -> AdtKind {
579        let mut tables = self.tables.borrow_mut();
580        let cx = &*self.cx.borrow();
581        cx.adt_kind(def.internal(&mut *tables, cx.tcx)).stable(&mut *tables, cx)
582    }
583
584    /// Returns if the ADT is a box.
585    fn adt_is_box(&self, def: AdtDef) -> bool {
586        let mut tables = self.tables.borrow_mut();
587        let cx = &*self.cx.borrow();
588        cx.adt_is_box(def.internal(&mut *tables, cx.tcx))
589    }
590
591    /// Returns whether this ADT is simd.
592    fn adt_is_simd(&self, def: AdtDef) -> bool {
593        let mut tables = self.tables.borrow_mut();
594        let cx = &*self.cx.borrow();
595        cx.adt_is_simd(def.internal(&mut *tables, cx.tcx))
596    }
597
598    /// Returns whether this definition is a C string.
599    fn adt_is_cstr(&self, def: AdtDef) -> bool {
600        let mut tables = self.tables.borrow_mut();
601        let cx = &*self.cx.borrow();
602        cx.adt_is_cstr(def.0.internal(&mut *tables, cx.tcx))
603    }
604
605    /// Returns the representation options for this ADT
606    fn adt_repr(&self, def: AdtDef) -> ReprOptions {
607        let mut tables = self.tables.borrow_mut();
608        let cx = &*self.cx.borrow();
609        cx.adt_repr(def.internal(&mut *tables, cx.tcx)).stable(&mut *tables, cx)
610    }
611
612    /// Retrieve the function signature for the given generic arguments.
613    fn fn_sig(&self, def: FnDef, args: &GenericArgs) -> PolyFnSig {
614        let mut tables = self.tables.borrow_mut();
615        let cx = &*self.cx.borrow();
616        let def_id = def.0.internal(&mut *tables, cx.tcx);
617        let args_ref = args.internal(&mut *tables, cx.tcx);
618        cx.fn_sig(def_id, args_ref).stable(&mut *tables, cx)
619    }
620
621    /// Retrieve the intrinsic definition if the item corresponds one.
622    fn intrinsic(&self, item: DefId) -> Option<IntrinsicDef> {
623        let mut tables = self.tables.borrow_mut();
624        let cx = &*self.cx.borrow();
625        let def_id = item.internal(&mut *tables, cx.tcx);
626        cx.intrinsic(def_id).map(|_| IntrinsicDef(item))
627    }
628
629    /// Retrieve the plain function name of an intrinsic.
630    fn intrinsic_name(&self, def: IntrinsicDef) -> Symbol {
631        let mut tables = self.tables.borrow_mut();
632        let cx = &*self.cx.borrow();
633        let def_id = def.0.internal(&mut *tables, cx.tcx);
634        cx.intrinsic_name(def_id)
635    }
636
637    /// Retrieve the closure signature for the given generic arguments.
638    fn closure_sig(&self, args: &GenericArgs) -> PolyFnSig {
639        let mut tables = self.tables.borrow_mut();
640        let cx = &*self.cx.borrow();
641        let args_ref = args.internal(&mut *tables, cx.tcx);
642        cx.closure_sig(args_ref).stable(&mut *tables, cx)
643    }
644
645    /// The number of variants in this ADT.
646    fn adt_variants_len(&self, def: AdtDef) -> usize {
647        let mut tables = self.tables.borrow_mut();
648        let cx = &*self.cx.borrow();
649        cx.adt_variants_len(def.internal(&mut *tables, cx.tcx))
650    }
651
652    /// Discriminant for a given variant index of AdtDef.
653    fn adt_discr_for_variant(&self, adt: AdtDef, variant: VariantIdx) -> Discr {
654        let mut tables = self.tables.borrow_mut();
655        let cx = &*self.cx.borrow();
656        cx.adt_discr_for_variant(
657            adt.internal(&mut *tables, cx.tcx),
658            variant.internal(&mut *tables, cx.tcx),
659        )
660        .stable(&mut *tables, cx)
661    }
662
663    /// Discriminant for a given variand index and args of a coroutine.
664    fn coroutine_discr_for_variant(
665        &self,
666        coroutine: CoroutineDef,
667        args: &GenericArgs,
668        variant: VariantIdx,
669    ) -> Discr {
670        let mut tables = self.tables.borrow_mut();
671        let cx = &*self.cx.borrow();
672        let tcx = cx.tcx;
673        let def = coroutine.def_id().internal(&mut *tables, tcx);
674        let args_ref = args.internal(&mut *tables, tcx);
675        cx.coroutine_discr_for_variant(def, args_ref, variant.internal(&mut *tables, tcx))
676            .stable(&mut *tables, cx)
677    }
678
679    /// The name of a variant.
680    fn variant_name(&self, def: VariantDef) -> Symbol {
681        let mut tables = self.tables.borrow_mut();
682        let cx = &*self.cx.borrow();
683        cx.variant_name(def.internal(&mut *tables, cx.tcx))
684    }
685
686    fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef> {
687        let mut tables = self.tables.borrow_mut();
688        let cx = &*self.cx.borrow();
689        def.internal(&mut *tables, cx.tcx)
690            .fields
691            .iter()
692            .map(|f| f.stable(&mut *tables, cx))
693            .collect()
694    }
695
696    /// Evaluate constant as a target usize.
697    fn eval_target_usize(&self, mir_const: &MirConst) -> Result<u64, Error> {
698        let mut tables = self.tables.borrow_mut();
699        let cx = &*self.cx.borrow();
700        let cnst = mir_const.internal(&mut *tables, cx.tcx);
701        cx.eval_target_usize(cnst)
702    }
703
704    fn eval_target_usize_ty(&self, ty_const: &TyConst) -> Result<u64, Error> {
705        let mut tables = self.tables.borrow_mut();
706        let cx = &*self.cx.borrow();
707        let cnst = ty_const.internal(&mut *tables, cx.tcx);
708        cx.eval_target_usize_ty(cnst)
709    }
710
711    /// Create a new zero-sized constant.
712    fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error> {
713        let mut tables = self.tables.borrow_mut();
714        let cx = &*self.cx.borrow();
715        let ty_internal = ty.internal(&mut *tables, cx.tcx);
716        cx.try_new_const_zst(ty_internal).map(|cnst| cnst.stable(&mut *tables, cx))
717    }
718
719    /// Create a new constant that represents the given string value.
720    fn new_const_str(&self, value: &str) -> MirConst {
721        let mut tables = self.tables.borrow_mut();
722        let cx = &*self.cx.borrow();
723        cx.new_const_str(value).stable(&mut *tables, cx)
724    }
725
726    /// Create a new constant that represents the given boolean value.
727    fn new_const_bool(&self, value: bool) -> MirConst {
728        let mut tables = self.tables.borrow_mut();
729        let cx = &*self.cx.borrow();
730        cx.new_const_bool(value).stable(&mut *tables, cx)
731    }
732
733    /// Create a new constant that represents the given value.
734    fn try_new_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<MirConst, Error> {
735        let mut tables = self.tables.borrow_mut();
736        let cx = &*self.cx.borrow();
737        let ty = cx.ty_new_uint(uint_ty.internal(&mut *tables, cx.tcx));
738        cx.try_new_const_uint(value, ty).map(|cnst| cnst.stable(&mut *tables, cx))
739    }
740
741    fn try_new_ty_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<TyConst, Error> {
742        let mut tables = self.tables.borrow_mut();
743        let cx = &*self.cx.borrow();
744        let ty = cx.ty_new_uint(uint_ty.internal(&mut *tables, cx.tcx));
745        cx.try_new_ty_const_uint(value, ty).map(|cnst| cnst.stable(&mut *tables, cx))
746    }
747
748    /// Create a new type from the given kind.
749    fn new_rigid_ty(&self, kind: RigidTy) -> Ty {
750        let mut tables = self.tables.borrow_mut();
751        let cx = &*self.cx.borrow();
752        let internal_kind = kind.internal(&mut *tables, cx.tcx);
753        cx.new_rigid_ty(internal_kind).stable(&mut *tables, cx)
754    }
755
756    /// Create a new box type, `Box<T>`, for the given inner type `T`.
757    fn new_box_ty(&self, ty: Ty) -> Ty {
758        let mut tables = self.tables.borrow_mut();
759        let cx = &*self.cx.borrow();
760        let inner = ty.internal(&mut *tables, cx.tcx);
761        cx.new_box_ty(inner).stable(&mut *tables, cx)
762    }
763
764    /// Returns the type of given crate item.
765    fn def_ty(&self, item: DefId) -> Ty {
766        let mut tables = self.tables.borrow_mut();
767        let cx = &*self.cx.borrow();
768        let inner = item.internal(&mut *tables, cx.tcx);
769        cx.def_ty(inner).stable(&mut *tables, cx)
770    }
771
772    /// Returns the type of given definition instantiated with the given arguments.
773    fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty {
774        let mut tables = self.tables.borrow_mut();
775        let cx = &*self.cx.borrow();
776        let inner = item.internal(&mut *tables, cx.tcx);
777        let args_ref = args.internal(&mut *tables, cx.tcx);
778        cx.def_ty_with_args(inner, args_ref).stable(&mut *tables, cx)
779    }
780
781    /// Returns literal value of a const as a string.
782    fn mir_const_pretty(&self, cnst: &MirConst) -> String {
783        let mut tables = self.tables.borrow_mut();
784        let cx = &*self.cx.borrow();
785        cnst.internal(&mut *tables, cx.tcx).to_string()
786    }
787
788    /// `Span` of an item.
789    fn span_of_an_item(&self, def_id: DefId) -> Span {
790        let mut tables = self.tables.borrow_mut();
791        let cx = &*self.cx.borrow();
792        let did = tables[def_id];
793        cx.span_of_an_item(did).stable(&mut *tables, cx)
794    }
795
796    fn ty_const_pretty(&self, ct: TyConstId) -> String {
797        let tables = self.tables.borrow_mut();
798        let cx = &*self.cx.borrow();
799        cx.ty_const_pretty(tables.ty_consts[ct])
800    }
801
802    /// Obtain the representation of a type.
803    fn ty_pretty(&self, ty: Ty) -> String {
804        let tables = self.tables.borrow_mut();
805        let cx = &*self.cx.borrow();
806        cx.ty_pretty(tables.types[ty])
807    }
808
809    /// Obtain the kind of a type.
810    fn ty_kind(&self, ty: Ty) -> TyKind {
811        let mut tables = self.tables.borrow_mut();
812        let cx = &*self.cx.borrow();
813        cx.ty_kind(tables.types[ty]).stable(&mut *tables, cx)
814    }
815
816    /// Get the discriminant Ty for this Ty if there's one.
817    fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> Ty {
818        let mut tables = self.tables.borrow_mut();
819        let cx = &*self.cx.borrow();
820        let internal_kind = ty.internal(&mut *tables, cx.tcx);
821        cx.rigid_ty_discriminant_ty(internal_kind).stable(&mut *tables, cx)
822    }
823
824    /// Get the body of an Instance which is already monomorphized.
825    fn instance_body(&self, instance: InstanceDef) -> Option<Body> {
826        let mut tables = self.tables.borrow_mut();
827        let cx = &*self.cx.borrow();
828        let instance = tables.instances[instance];
829        cx.instance_body(instance).map(|body| body.stable(&mut *tables, cx))
830    }
831
832    /// Get the instance type with generic instantiations applied and lifetimes erased.
833    fn instance_ty(&self, instance: InstanceDef) -> Ty {
834        let mut tables = self.tables.borrow_mut();
835        let cx = &*self.cx.borrow();
836        let instance = tables.instances[instance];
837        cx.instance_ty(instance).stable(&mut *tables, cx)
838    }
839
840    /// Get the instantiation types.
841    fn instance_args(&self, def: InstanceDef) -> GenericArgs {
842        let mut tables = self.tables.borrow_mut();
843        let cx = &*self.cx.borrow();
844        let instance = tables.instances[def];
845        cx.instance_args(instance).stable(&mut *tables, cx)
846    }
847
848    /// Get the instance.
849    fn instance_def_id(&self, instance: InstanceDef) -> DefId {
850        let mut tables = self.tables.borrow_mut();
851        let cx = &*self.cx.borrow();
852        let instance = tables.instances[instance];
853        cx.instance_def_id(instance, &mut *tables)
854    }
855
856    /// Get the instance mangled name.
857    fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol {
858        let tables = self.tables.borrow_mut();
859        let cx = &*self.cx.borrow();
860        let instance = tables.instances[instance];
861        cx.instance_mangled_name(instance)
862    }
863
864    /// Check if this is an empty DropGlue shim.
865    fn is_empty_drop_shim(&self, def: InstanceDef) -> bool {
866        let tables = self.tables.borrow_mut();
867        let cx = &*self.cx.borrow();
868        let instance = tables.instances[def];
869        cx.is_empty_drop_shim(instance)
870    }
871
872    /// Convert a non-generic crate item into an instance.
873    /// This function will panic if the item is generic.
874    fn mono_instance(&self, def_id: DefId) -> Instance {
875        let mut tables = self.tables.borrow_mut();
876        let cx = &*self.cx.borrow();
877        let did = tables[def_id];
878        cx.mono_instance(did).stable(&mut *tables, cx)
879    }
880
881    /// Item requires monomorphization.
882    fn requires_monomorphization(&self, def_id: DefId) -> bool {
883        let tables = self.tables.borrow();
884        let cx = &*self.cx.borrow();
885        let did = tables[def_id];
886        cx.requires_monomorphization(did)
887    }
888
889    /// Resolve an instance from the given function definition and generic arguments.
890    fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance> {
891        let mut tables = self.tables.borrow_mut();
892        let cx = &*self.cx.borrow();
893        let def_id = def.0.internal(&mut *tables, cx.tcx);
894        let args_ref = args.internal(&mut *tables, cx.tcx);
895        cx.resolve_instance(def_id, args_ref).map(|inst| inst.stable(&mut *tables, cx))
896    }
897
898    /// Resolve an instance for drop_in_place for the given type.
899    fn resolve_drop_in_place(&self, ty: Ty) -> Instance {
900        let mut tables = self.tables.borrow_mut();
901        let cx = &*self.cx.borrow();
902        let internal_ty = ty.internal(&mut *tables, cx.tcx);
903
904        cx.resolve_drop_in_place(internal_ty).stable(&mut *tables, cx)
905    }
906
907    /// Resolve instance for a function pointer.
908    fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance> {
909        let mut tables = self.tables.borrow_mut();
910        let cx = &*self.cx.borrow();
911        let def_id = def.0.internal(&mut *tables, cx.tcx);
912        let args_ref = args.internal(&mut *tables, cx.tcx);
913        cx.resolve_for_fn_ptr(def_id, args_ref).stable(&mut *tables, cx)
914    }
915
916    /// Resolve instance for a closure with the requested type.
917    fn resolve_closure(
918        &self,
919        def: ClosureDef,
920        args: &GenericArgs,
921        kind: ClosureKind,
922    ) -> Option<Instance> {
923        let mut tables = self.tables.borrow_mut();
924        let cx = &*self.cx.borrow();
925        let def_id = def.0.internal(&mut *tables, cx.tcx);
926        let args_ref = args.internal(&mut *tables, cx.tcx);
927        let closure_kind = kind.internal(&mut *tables, cx.tcx);
928        cx.resolve_closure(def_id, args_ref, closure_kind).map(|inst| inst.stable(&mut *tables, cx))
929    }
930
931    /// Evaluate a static's initializer.
932    fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error> {
933        let mut tables = self.tables.borrow_mut();
934        let cx = &*self.cx.borrow();
935        let def_id = def.0.internal(&mut *tables, cx.tcx);
936
937        cx.eval_static_initializer(def_id).stable(&mut *tables, cx)
938    }
939
940    /// Try to evaluate an instance into a constant.
941    fn eval_instance(&self, def: InstanceDef, const_ty: Ty) -> Result<Allocation, Error> {
942        let mut tables = self.tables.borrow_mut();
943        let instance = tables.instances[def];
944        let cx = &*self.cx.borrow();
945        let const_ty = const_ty.internal(&mut *tables, cx.tcx);
946        cx.eval_instance(instance)
947            .map(|const_val| alloc::try_new_allocation(const_ty, const_val, &mut *tables, cx))
948            .map_err(|e| e.stable(&mut *tables, cx))?
949    }
950
951    /// Retrieve global allocation for the given allocation ID.
952    fn global_alloc(&self, id: AllocId) -> GlobalAlloc {
953        let mut tables = self.tables.borrow_mut();
954        let cx = &*self.cx.borrow();
955        let alloc_id = id.internal(&mut *tables, cx.tcx);
956        cx.global_alloc(alloc_id).stable(&mut *tables, cx)
957    }
958
959    /// Retrieve the id for the virtual table.
960    fn vtable_allocation(&self, global_alloc: &GlobalAlloc) -> Option<AllocId> {
961        let mut tables = self.tables.borrow_mut();
962        let GlobalAlloc::VTable(ty, trait_ref) = global_alloc else {
963            return None;
964        };
965        let cx = &*self.cx.borrow();
966        let ty = ty.internal(&mut *tables, cx.tcx);
967        let trait_ref = trait_ref.internal(&mut *tables, cx.tcx);
968        let alloc_id = cx.vtable_allocation(ty, trait_ref);
969        Some(alloc_id.stable(&mut *tables, cx))
970    }
971
972    fn krate(&self, def_id: DefId) -> Crate {
973        let tables = self.tables.borrow();
974        let cx = &*self.cx.borrow();
975        smir_crate(cx, tables[def_id].krate)
976    }
977
978    fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol {
979        let tables = self.tables.borrow_mut();
980        let cx = &*self.cx.borrow();
981        let instance = tables.instances[def];
982        cx.instance_name(instance, trimmed)
983    }
984
985    /// Return information about the target machine.
986    fn target_info(&self) -> MachineInfo {
987        let mut tables = self.tables.borrow_mut();
988        let cx = &*self.cx.borrow();
989        MachineInfo {
990            endian: cx.target_endian().stable(&mut *tables, cx),
991            pointer_width: MachineSize::from_bits(cx.target_pointer_size()),
992        }
993    }
994
995    /// Get an instance ABI.
996    fn instance_abi(&self, def: InstanceDef) -> Result<FnAbi, Error> {
997        let mut tables = self.tables.borrow_mut();
998        let cx = &*self.cx.borrow();
999        let instance = tables.instances[def];
1000        cx.instance_abi(instance).map(|fn_abi| fn_abi.stable(&mut *tables, cx))
1001    }
1002
1003    /// Get the ABI of a function pointer.
1004    fn fn_ptr_abi(&self, fn_ptr: PolyFnSig) -> Result<FnAbi, Error> {
1005        let mut tables = self.tables.borrow_mut();
1006        let cx = &*self.cx.borrow();
1007        let sig = fn_ptr.internal(&mut *tables, cx.tcx);
1008        cx.fn_ptr_abi(sig).map(|fn_abi| fn_abi.stable(&mut *tables, cx))
1009    }
1010
1011    /// Get the layout of a type.
1012    fn ty_layout(&self, ty: Ty) -> Result<Layout, Error> {
1013        let mut tables = self.tables.borrow_mut();
1014        let cx = &*self.cx.borrow();
1015        let internal_ty = ty.internal(&mut *tables, cx.tcx);
1016        cx.ty_layout(internal_ty).map(|layout| layout.stable(&mut *tables, cx))
1017    }
1018
1019    /// Get the layout shape.
1020    fn layout_shape(&self, id: Layout) -> LayoutShape {
1021        let mut tables = self.tables.borrow_mut();
1022        let cx = &*self.cx.borrow();
1023        id.internal(&mut *tables, cx.tcx).0.stable(&mut *tables, cx)
1024    }
1025
1026    /// Get a debug string representation of a place.
1027    fn place_pretty(&self, place: &Place) -> String {
1028        let mut tables = self.tables.borrow_mut();
1029        let cx = &*self.cx.borrow();
1030
1031        format!("{:?}", place.internal(&mut *tables, cx.tcx))
1032    }
1033
1034    /// Get the resulting type of binary operation.
1035    fn binop_ty(&self, bin_op: BinOp, rhs: Ty, lhs: Ty) -> Ty {
1036        let mut tables = self.tables.borrow_mut();
1037        let cx = &*self.cx.borrow();
1038        let rhs_internal = rhs.internal(&mut *tables, cx.tcx);
1039        let lhs_internal = lhs.internal(&mut *tables, cx.tcx);
1040        let bin_op_internal = bin_op.internal(&mut *tables, cx.tcx);
1041        cx.binop_ty(bin_op_internal, rhs_internal, lhs_internal).stable(&mut *tables, cx)
1042    }
1043
1044    /// Get the resulting type of unary operation.
1045    fn unop_ty(&self, un_op: UnOp, arg: Ty) -> Ty {
1046        let mut tables = self.tables.borrow_mut();
1047        let cx = &*self.cx.borrow();
1048        let un_op = un_op.internal(&mut *tables, cx.tcx);
1049        let arg = arg.internal(&mut *tables, cx.tcx);
1050        cx.unop_ty(un_op, arg).stable(&mut *tables, cx)
1051    }
1052
1053    /// Get all associated items of a definition.
1054    fn associated_items(&self, def_id: DefId) -> AssocItems {
1055        let mut tables = self.tables.borrow_mut();
1056        let cx = &*self.cx.borrow();
1057        let did = tables[def_id];
1058        cx.associated_items(did).iter().map(|assoc| assoc.stable(&mut *tables, cx)).collect()
1059    }
1060}
1061
1062// A thread local variable that stores a pointer to [`SmirInterface`].
1063scoped_tls::scoped_thread_local!(static TLV: Cell<*const ()>);
1064
1065pub(crate) fn run<F, T>(interface: &dyn SmirInterface, f: F) -> Result<T, Error>
1066where
1067    F: FnOnce() -> T,
1068{
1069    if TLV.is_set() {
1070        Err(Error::from("StableMIR already running"))
1071    } else {
1072        let ptr: *const () = (&raw const interface) as _;
1073        TLV.set(&Cell::new(ptr), || Ok(f()))
1074    }
1075}
1076
1077/// Execute the given function with access the [`SmirInterface`].
1078///
1079/// I.e., This function will load the current interface and calls a function with it.
1080/// Do not nest these, as that will ICE.
1081pub(crate) fn with<R>(f: impl FnOnce(&dyn SmirInterface) -> R) -> R {
1082    assert!(TLV.is_set());
1083    TLV.with(|tlv| {
1084        let ptr = tlv.get();
1085        assert!(!ptr.is_null());
1086        f(unsafe { *(ptr as *const &dyn SmirInterface) })
1087    })
1088}
1089
1090fn smir_crate<'tcx>(
1091    cx: &SmirCtxt<'tcx, BridgeTys>,
1092    crate_num: rustc_span::def_id::CrateNum,
1093) -> Crate {
1094    let name = cx.crate_name(crate_num);
1095    let is_local = cx.crate_is_local(crate_num);
1096    let id = cx.crate_num_id(crate_num);
1097    debug!(?name, ?crate_num, "smir_crate");
1098    Crate { id, name, is_local }
1099}