@@ -21,10 +21,13 @@ use stable_mir::abi::{
2121 ArgAbi , CallConvention , FieldsShape , IntegerLength , PassMode , Primitive , Scalar , ValueAbi ,
2222 VariantsShape ,
2323} ;
24+ use stable_mir:: mir:: MirVisitor ;
2425use stable_mir:: mir:: mono:: Instance ;
2526use stable_mir:: target:: MachineInfo ;
27+ use stable_mir:: ty:: { AdtDef , RigidTy , Ty , TyKind } ;
2628use stable_mir:: { CrateDef , CrateItem , CrateItems , ItemKind } ;
2729use std:: assert_matches:: assert_matches;
30+ use std:: collections:: HashSet ;
2831use std:: convert:: TryFrom ;
2932use std:: io:: Write ;
3033use std:: ops:: ControlFlow ;
@@ -67,6 +70,17 @@ fn test_stable_mir() -> ControlFlow<()> {
6770 assert ! ( ptr_variadic_fn_abi. c_variadic) ;
6871 assert_eq ! ( ptr_variadic_fn_abi. args. len( ) , 1 ) ;
6972
73+ let entry = stable_mir:: entry_fn ( ) . unwrap ( ) ;
74+ let main_fn = Instance :: try_from ( entry) . unwrap ( ) ;
75+ let mut visitor = AdtDefVisitor :: default ( ) ;
76+ visitor. visit_body ( & main_fn. body ( ) . unwrap ( ) ) ;
77+ let AdtDefVisitor { adt_defs } = visitor;
78+ assert_eq ! ( adt_defs. len( ) , 1 ) ;
79+
80+ // Test ADT representation options
81+ let repr_c_struct = adt_defs. iter ( ) . find ( |def| def. name ( ) == "ReprCStruct" ) . unwrap ( ) ;
82+ assert ! ( repr_c_struct. repr( ) . is_c( ) ) ;
83+
7084 ControlFlow :: Continue ( ( ) )
7185}
7286
@@ -138,6 +152,20 @@ fn get_item<'a>(
138152 items. iter ( ) . find ( |crate_item| ( item. 0 == crate_item. kind ( ) ) && crate_item. name ( ) == item. 1 )
139153}
140154
155+ #[ derive( Default ) ]
156+ struct AdtDefVisitor {
157+ adt_defs : HashSet < AdtDef > ,
158+ }
159+
160+ impl MirVisitor for AdtDefVisitor {
161+ fn visit_ty ( & mut self , ty : & Ty , _location : stable_mir:: mir:: visit:: Location ) {
162+ if let TyKind :: RigidTy ( RigidTy :: Adt ( adt, _) ) = ty. kind ( ) {
163+ self . adt_defs . insert ( adt) ;
164+ }
165+ self . super_ty ( ty)
166+ }
167+ }
168+
141169/// This test will generate and analyze a dummy crate using the stable mir.
142170/// For that, it will first write the dummy crate into a file.
143171/// Then it will create a `StableMir` using custom arguments and then
@@ -147,7 +175,7 @@ fn main() {
147175 generate_input ( & path) . unwrap ( ) ;
148176 let args = & [
149177 "rustc" . to_string ( ) ,
150- "--crate-type=lib " . to_string ( ) ,
178+ "-Cpanic=abort " . to_string ( ) ,
151179 "--crate-name" . to_string ( ) ,
152180 CRATE_NAME . to_string ( ) ,
153181 path. to_string ( ) ,
@@ -185,6 +213,13 @@ fn generate_input(path: &str) -> std::io::Result<()> {
185213 // We only care about the signature.
186214 todo!()
187215 }}
216+
217+ fn main() {{
218+ #[repr(C)]
219+ struct ReprCStruct;
220+
221+ let _s = ReprCStruct;
222+ }}
188223 "#
189224 ) ?;
190225 Ok ( ( ) )
0 commit comments