@@ -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,15 @@ 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+
78+ // Test ADT representation options
79+ let repr_c_struct = visitor. adt_defs . iter ( ) . find ( |def| def. name ( ) == "ReprCStruct" ) . unwrap ( ) ;
80+ assert ! ( repr_c_struct. repr( ) . is_c( ) ) ;
81+
7082 ControlFlow :: Continue ( ( ) )
7183}
7284
@@ -138,6 +150,20 @@ fn get_item<'a>(
138150 items. iter ( ) . find ( |crate_item| ( item. 0 == crate_item. kind ( ) ) && crate_item. name ( ) == item. 1 )
139151}
140152
153+ #[ derive( Default ) ]
154+ struct AdtDefVisitor {
155+ adt_defs : HashSet < AdtDef > ,
156+ }
157+
158+ impl MirVisitor for AdtDefVisitor {
159+ fn visit_ty ( & mut self , ty : & Ty , _location : stable_mir:: mir:: visit:: Location ) {
160+ if let TyKind :: RigidTy ( RigidTy :: Adt ( adt, _) ) = ty. kind ( ) {
161+ self . adt_defs . insert ( adt) ;
162+ }
163+ self . super_ty ( ty)
164+ }
165+ }
166+
141167/// This test will generate and analyze a dummy crate using the stable mir.
142168/// For that, it will first write the dummy crate into a file.
143169/// Then it will create a `StableMir` using custom arguments and then
@@ -147,7 +173,7 @@ fn main() {
147173 generate_input ( & path) . unwrap ( ) ;
148174 let args = & [
149175 "rustc" . to_string ( ) ,
150- "--crate-type=lib " . to_string ( ) ,
176+ "-Cpanic=abort " . to_string ( ) ,
151177 "--crate-name" . to_string ( ) ,
152178 CRATE_NAME . to_string ( ) ,
153179 path. to_string ( ) ,
@@ -185,6 +211,13 @@ fn generate_input(path: &str) -> std::io::Result<()> {
185211 // We only care about the signature.
186212 todo!()
187213 }}
214+
215+ fn main() {{
216+ #[repr(C)]
217+ struct ReprCStruct;
218+
219+ let _ = ReprCStruct;
220+ }}
188221 "#
189222 ) ?;
190223 Ok ( ( ) )
0 commit comments