33//! For that, we define APIs that will temporarily be public to 3P that exposes rustc internal APIs
44//! until stable MIR is complete.
55
6- use std:: fmt:: Debug ;
76use std:: ops:: { ControlFlow , Index } ;
87
98use crate :: rustc_internal;
10- use crate :: stable_mir:: CompilerError ;
11- use crate :: {
12- rustc_smir:: Tables ,
13- stable_mir:: { self , with} ,
14- } ;
9+ use crate :: rustc_smir:: Tables ;
1510use rustc_driver:: { Callbacks , Compilation , RunCompiler } ;
1611use rustc_interface:: { interface, Queries } ;
1712use rustc_middle:: mir:: interpret:: AllocId ;
1813use rustc_middle:: ty:: TyCtxt ;
1914pub use rustc_span:: def_id:: { CrateNum , DefId } ;
2015use rustc_span:: Span ;
21-
22- fn with_tables < R > ( mut f : impl FnMut ( & mut Tables < ' _ > ) -> R ) -> R {
23- let mut ret = None ;
24- with ( |tables| tables. rustc_tables ( & mut |t| ret = Some ( f ( t) ) ) ) ;
25- ret. unwrap ( )
26- }
27-
28- pub fn item_def_id ( item : & stable_mir:: CrateItem ) -> DefId {
29- with_tables ( |t| t[ item. 0 ] )
30- }
31-
32- pub fn crate_item ( did : DefId ) -> stable_mir:: CrateItem {
33- with_tables ( |t| t. crate_item ( did) )
34- }
35-
36- pub fn adt_def ( did : DefId ) -> stable_mir:: ty:: AdtDef {
37- with_tables ( |t| t. adt_def ( did) )
38- }
39-
40- pub fn foreign_def ( did : DefId ) -> stable_mir:: ty:: ForeignDef {
41- with_tables ( |t| t. foreign_def ( did) )
42- }
43-
44- pub fn fn_def ( did : DefId ) -> stable_mir:: ty:: FnDef {
45- with_tables ( |t| t. fn_def ( did) )
46- }
47-
48- pub fn closure_def ( did : DefId ) -> stable_mir:: ty:: ClosureDef {
49- with_tables ( |t| t. closure_def ( did) )
50- }
51-
52- pub fn generator_def ( did : DefId ) -> stable_mir:: ty:: GeneratorDef {
53- with_tables ( |t| t. generator_def ( did) )
54- }
55-
56- pub fn alias_def ( did : DefId ) -> stable_mir:: ty:: AliasDef {
57- with_tables ( |t| t. alias_def ( did) )
58- }
59-
60- pub fn param_def ( did : DefId ) -> stable_mir:: ty:: ParamDef {
61- with_tables ( |t| t. param_def ( did) )
62- }
63-
64- pub fn br_named_def ( did : DefId ) -> stable_mir:: ty:: BrNamedDef {
65- with_tables ( |t| t. br_named_def ( did) )
66- }
67-
68- pub fn trait_def ( did : DefId ) -> stable_mir:: ty:: TraitDef {
69- with_tables ( |t| t. trait_def ( did) )
70- }
71-
72- pub fn impl_def ( did : DefId ) -> stable_mir:: ty:: ImplDef {
73- with_tables ( |t| t. impl_def ( did) )
74- }
16+ use stable_mir:: CompilerError ;
7517
7618impl < ' tcx > Index < stable_mir:: DefId > for Tables < ' tcx > {
7719 type Output = DefId ;
@@ -82,6 +24,15 @@ impl<'tcx> Index<stable_mir::DefId> for Tables<'tcx> {
8224 }
8325}
8426
27+ impl < ' tcx > Index < stable_mir:: ty:: Span > for Tables < ' tcx > {
28+ type Output = Span ;
29+
30+ #[ inline( always) ]
31+ fn index ( & self , index : stable_mir:: ty:: Span ) -> & Self :: Output {
32+ & self . spans [ index. 0 ]
33+ }
34+ }
35+
8536impl < ' tcx > Tables < ' tcx > {
8637 pub fn crate_item ( & mut self , did : DefId ) -> stable_mir:: CrateItem {
8738 stable_mir:: CrateItem ( self . create_def_id ( did) )
@@ -178,32 +129,12 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
178129}
179130
180131pub fn run ( tcx : TyCtxt < ' _ > , f : impl FnOnce ( ) ) {
181- crate :: stable_mir:: run (
132+ stable_mir:: run (
182133 Tables { tcx, def_ids : vec ! [ ] , alloc_ids : vec ! [ ] , spans : vec ! [ ] , types : vec ! [ ] } ,
183134 f,
184135 ) ;
185136}
186137
187- /// A type that provides internal information but that can still be used for debug purpose.
188- #[ derive( Clone ) ]
189- pub struct Opaque ( String ) ;
190-
191- impl std:: fmt:: Display for Opaque {
192- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
193- write ! ( f, "{}" , self . 0 )
194- }
195- }
196-
197- impl std:: fmt:: Debug for Opaque {
198- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
199- write ! ( f, "{:?}" , self . 0 )
200- }
201- }
202-
203- pub ( crate ) fn opaque < T : Debug > ( value : & T ) -> Opaque {
204- Opaque ( format ! ( "{value:?}" ) )
205- }
206-
207138pub struct StableMir < B = ( ) , C = ( ) >
208139where
209140 B : Send ,
0 commit comments