@@ -25,9 +25,9 @@ use rustc_middle::ty::TyCtxt;
25
25
use rustc_smir:: rustc_internal;
26
26
use stable_mir:: crate_def:: CrateDef ;
27
27
use stable_mir:: mir:: alloc:: GlobalAlloc ;
28
- use stable_mir:: mir:: mono:: { Instance , StaticDef } ;
29
- use stable_mir:: mir:: Body ;
30
- use stable_mir:: ty:: { Allocation , ConstantKind } ;
28
+ use stable_mir:: mir:: mono:: { Instance , InstanceKind , StaticDef } ;
29
+ use stable_mir:: mir:: { Body , TerminatorKind } ;
30
+ use stable_mir:: ty:: { Allocation , ConstantKind , RigidTy , TyKind } ;
31
31
use stable_mir:: { CrateItem , CrateItems , ItemKind } ;
32
32
use std:: ascii:: Char ;
33
33
use std:: assert_matches:: assert_matches;
@@ -46,6 +46,7 @@ fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
46
46
check_bar ( * get_item ( & items, ( ItemKind :: Static , "BAR" ) ) . unwrap ( ) ) ;
47
47
check_len ( * get_item ( & items, ( ItemKind :: Static , "LEN" ) ) . unwrap ( ) ) ;
48
48
check_other_consts ( * get_item ( & items, ( ItemKind :: Fn , "other_consts" ) ) . unwrap ( ) ) ;
49
+ check_type_id ( * get_item ( & items, ( ItemKind :: Fn , "check_type_id" ) ) . unwrap ( ) ) ;
49
50
ControlFlow :: Continue ( ( ) )
50
51
}
51
52
@@ -139,6 +140,30 @@ fn check_other_consts(item: CrateItem) {
139
140
}
140
141
}
141
142
143
+ /// Check that we can retrieve the type id of char and bool, and that they have different values.
144
+ fn check_type_id ( item : CrateItem ) {
145
+ let body = Instance :: try_from ( item) . unwrap ( ) . body ( ) . unwrap ( ) ;
146
+ let mut ids: Vec < u128 > = vec ! [ ] ;
147
+ for term in body. blocks . iter ( ) . map ( |bb| & bb. terminator ) {
148
+ match & term. kind {
149
+ TerminatorKind :: Call { func, destination, .. } => {
150
+ let TyKind :: RigidTy ( ty) = func. ty ( body. locals ( ) ) . unwrap ( ) . kind ( ) else {
151
+ unreachable ! ( )
152
+ } ;
153
+ let RigidTy :: FnDef ( def, args) = ty else { unreachable ! ( ) } ;
154
+ let instance = Instance :: resolve ( def, & args) . unwrap ( ) ;
155
+ assert_eq ! ( instance. kind, InstanceKind :: Intrinsic ) ;
156
+ let dest_ty = destination. ty ( body. locals ( ) ) . unwrap ( ) ;
157
+ let alloc = instance. try_const_eval ( dest_ty) . unwrap ( ) ;
158
+ ids. push ( alloc. read_uint ( ) . unwrap ( ) ) ;
159
+ }
160
+ _ => { /* Do nothing */ }
161
+ }
162
+ }
163
+ assert_eq ! ( ids. len( ) , 2 ) ;
164
+ assert_ne ! ( ids[ 0 ] , ids[ 1 ] ) ;
165
+ }
166
+
142
167
/// Collects all the constant assignments.
143
168
pub fn collect_consts ( body : & Body ) -> HashMap < String , & Allocation > {
144
169
body. var_debug_info
@@ -193,6 +218,9 @@ fn generate_input(path: &str) -> std::io::Result<()> {
193
218
write ! (
194
219
file,
195
220
r#"
221
+ #![feature(core_intrinsics)]
222
+ use std::intrinsics::type_id;
223
+
196
224
static LEN: usize = 2;
197
225
static FOO: [&str; 2] = ["hi", "there"];
198
226
static BAR: &str = "Bar";
@@ -211,6 +239,11 @@ fn generate_input(path: &str) -> std::io::Result<()> {
211
239
let _tuple = TUPLE;
212
240
}}
213
241
242
+ fn check_type_id() {{
243
+ let _char_id = type_id::<char>();
244
+ let _bool_id = type_id::<bool>();
245
+ }}
246
+
214
247
pub fn main() {{
215
248
println!("{{FOO:?}}! {{BAR}}");
216
249
assert_eq!(FOO.len(), LEN);
0 commit comments