@@ -21,10 +21,13 @@ use stable_mir::abi::{
21
21
ArgAbi , CallConvention , FieldsShape , IntegerLength , PassMode , Primitive , Scalar , ValueAbi ,
22
22
VariantsShape ,
23
23
} ;
24
+ use stable_mir:: mir:: MirVisitor ;
24
25
use stable_mir:: mir:: mono:: Instance ;
25
26
use stable_mir:: target:: MachineInfo ;
27
+ use stable_mir:: ty:: { AdtDef , RigidTy , Ty , TyKind } ;
26
28
use stable_mir:: { CrateDef , CrateItem , CrateItems , ItemKind } ;
27
29
use std:: assert_matches:: assert_matches;
30
+ use std:: collections:: HashSet ;
28
31
use std:: convert:: TryFrom ;
29
32
use std:: io:: Write ;
30
33
use std:: ops:: ControlFlow ;
@@ -67,6 +70,15 @@ fn test_stable_mir() -> ControlFlow<()> {
67
70
assert ! ( ptr_variadic_fn_abi. c_variadic) ;
68
71
assert_eq ! ( ptr_variadic_fn_abi. args. len( ) , 1 ) ;
69
72
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
+
70
82
ControlFlow :: Continue ( ( ) )
71
83
}
72
84
@@ -138,6 +150,20 @@ fn get_item<'a>(
138
150
items. iter ( ) . find ( |crate_item| ( item. 0 == crate_item. kind ( ) ) && crate_item. name ( ) == item. 1 )
139
151
}
140
152
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
+
141
167
/// This test will generate and analyze a dummy crate using the stable mir.
142
168
/// For that, it will first write the dummy crate into a file.
143
169
/// Then it will create a `StableMir` using custom arguments and then
@@ -185,6 +211,13 @@ fn generate_input(path: &str) -> std::io::Result<()> {
185
211
// We only care about the signature.
186
212
todo!()
187
213
}}
214
+
215
+ fn main() {{
216
+ #[repr(C)]
217
+ struct ReprCStruct;
218
+
219
+ let _ = ReprCStruct;
220
+ }}
188
221
"#
189
222
) ?;
190
223
Ok ( ( ) )
0 commit comments