@@ -25,9 +25,9 @@ use rustc_middle::ty::TyCtxt;
2525use rustc_smir:: rustc_internal;
2626use stable_mir:: crate_def:: CrateDef ;
2727use 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 } ;
3131use stable_mir:: { CrateItem , CrateItems , ItemKind } ;
3232use std:: ascii:: Char ;
3333use std:: assert_matches:: assert_matches;
@@ -46,6 +46,7 @@ fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
4646 check_bar ( * get_item ( & items, ( ItemKind :: Static , "BAR" ) ) . unwrap ( ) ) ;
4747 check_len ( * get_item ( & items, ( ItemKind :: Static , "LEN" ) ) . unwrap ( ) ) ;
4848 check_other_consts ( * get_item ( & items, ( ItemKind :: Fn , "other_consts" ) ) . unwrap ( ) ) ;
49+ check_type_id ( * get_item ( & items, ( ItemKind :: Fn , "check_type_id" ) ) . unwrap ( ) ) ;
4950 ControlFlow :: Continue ( ( ) )
5051}
5152
@@ -139,6 +140,30 @@ fn check_other_consts(item: CrateItem) {
139140 }
140141}
141142
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+
142167/// Collects all the constant assignments.
143168pub fn collect_consts ( body : & Body ) -> HashMap < String , & Allocation > {
144169 body. var_debug_info
@@ -193,6 +218,9 @@ fn generate_input(path: &str) -> std::io::Result<()> {
193218 write ! (
194219 file,
195220 r#"
221+ #![feature(core_intrinsics)]
222+ use std::intrinsics::type_id;
223+
196224 static LEN: usize = 2;
197225 static FOO: [&str; 2] = ["hi", "there"];
198226 static BAR: &str = "Bar";
@@ -211,6 +239,11 @@ fn generate_input(path: &str) -> std::io::Result<()> {
211239 let _tuple = TUPLE;
212240 }}
213241
242+ fn check_type_id() {{
243+ let _char_id = type_id::<char>();
244+ let _bool_id = type_id::<bool>();
245+ }}
246+
214247 pub fn main() {{
215248 println!("{{FOO:?}}! {{BAR}}");
216249 assert_eq!(FOO.len(), LEN);
0 commit comments