3
3
//! For that, we define APIs that will temporarily be public to 3P that exposes rustc internal APIs
4
4
//! until stable MIR is complete.
5
5
6
- use std:: fmt:: Debug ;
7
6
use std:: ops:: { ControlFlow , Index } ;
8
7
9
8
use 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 ;
15
10
use rustc_driver:: { Callbacks , Compilation , RunCompiler } ;
16
11
use rustc_interface:: { interface, Queries } ;
17
12
use rustc_middle:: mir:: interpret:: AllocId ;
18
13
use rustc_middle:: ty:: TyCtxt ;
19
14
pub use rustc_span:: def_id:: { CrateNum , DefId } ;
20
15
use 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 ;
75
17
76
18
impl < ' tcx > Index < stable_mir:: DefId > for Tables < ' tcx > {
77
19
type Output = DefId ;
@@ -82,6 +24,15 @@ impl<'tcx> Index<stable_mir::DefId> for Tables<'tcx> {
82
24
}
83
25
}
84
26
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
+
85
36
impl < ' tcx > Tables < ' tcx > {
86
37
pub fn crate_item ( & mut self , did : DefId ) -> stable_mir:: CrateItem {
87
38
stable_mir:: CrateItem ( self . create_def_id ( did) )
@@ -178,32 +129,12 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
178
129
}
179
130
180
131
pub fn run ( tcx : TyCtxt < ' _ > , f : impl FnOnce ( ) ) {
181
- crate :: stable_mir:: run (
132
+ stable_mir:: run (
182
133
Tables { tcx, def_ids : vec ! [ ] , alloc_ids : vec ! [ ] , spans : vec ! [ ] , types : vec ! [ ] } ,
183
134
f,
184
135
) ;
185
136
}
186
137
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
-
207
138
pub struct StableMir < B = ( ) , C = ( ) >
208
139
where
209
140
B : Send ,
0 commit comments