2 use core::mem::size_of;
3 use std::boxed::ThinBox;
6 fn want_niche_optimization() {
7 fn uses_niche<T: ?Sized>() -> bool {
8 size_of::<*const ()>() == size_of::<Option<ThinBox<T>>>()
12 assert!(uses_niche::<dyn Tr>());
13 assert!(uses_niche::<[i32]>());
14 assert!(uses_niche::<i32>());
19 fn is_thin<T: ?Sized>() -> bool {
20 size_of::<*const ()>() == size_of::<ThinBox<T>>()
24 assert!(is_thin::<dyn Tr>());
25 assert!(is_thin::<[i32]>());
26 assert!(is_thin::<i32>());
30 fn verify_aligned<T>(ptr: *const T) {
31 // Use `black_box` to attempt to obscure the fact that we're calling this
32 // function on pointers that come from box/references, which the compiler
33 // would otherwise realize is impossible (because it would mean we've
34 // already executed UB).
36 // That is, we'd *like* it to be possible for the asserts in this function
37 // to detect brokenness in the ThinBox impl.
39 // It would probably be better if we instead had these as debug_asserts
40 // inside `ThinBox`, prior to the point where we do the UB. Anyway, in
41 // practice these checks are mostly just smoke-detectors for an extremely
42 // broken `ThinBox` impl, since it's an extremely subtle piece of code.
43 let ptr = core::hint::black_box(ptr);
44 let align = core::mem::align_of::<T>();
46 (ptr.addr() & (align - 1)) == 0 && !ptr.is_null(),
47 "misaligned ThinBox data; valid pointers to `{}` should be aligned to {align}: {ptr:p}",
48 core::any::type_name::<T>(),
53 fn check_thin_sized<T: Debug + PartialEq + Clone>(make: impl FnOnce() -> T) {
55 let boxed = ThinBox::new(value.clone());
57 verify_aligned(val as *const T);
58 assert_eq!(val, &value);
62 fn check_thin_dyn<T: Debug + PartialEq + Clone>(make: impl FnOnce() -> T) {
64 let wanted_debug = format!("{value:?}");
65 let boxed: ThinBox<dyn Debug> = ThinBox::new_unsize(value.clone());
67 // wide reference -> wide pointer -> thin pointer
68 verify_aligned(val as *const dyn Debug as *const T);
69 let got_debug = format!("{val:?}");
70 assert_eq!(wanted_debug, got_debug);
73 macro_rules! define_test {
75 @test_name: $testname:ident;
78 struct $Type:ident($inner:ty);
84 use core::sync::atomic::{AtomicIsize, Ordering};
85 // Define the type, and implement new/clone/drop in such a way that
86 // the number of live instances will be counted.
88 #[derive(Debug, PartialEq)]
93 impl Clone for $Type {
94 fn clone(&self) -> Self {
96 Self::new(self._priv.clone())
100 impl Drop for $Type {
102 verify_aligned(self);
103 Self::modify_live(-1);
108 fn new(i: $inner) -> Self {
109 Self::modify_live(1);
113 fn modify_live(n: isize) -> isize {
114 static COUNTER: AtomicIsize = AtomicIsize::new(0);
115 COUNTER.fetch_add(n, Ordering::Relaxed) + n
118 fn live_objects() -> isize {
122 // Run the test statements
123 let _: () = { $($test_stmts)* };
124 // Check that we didn't leak anything, or call drop too many times.
126 $Type::live_objects(), 0,
127 "Wrong number of drops of {}, `initializations - drops` should be 0.",
135 @test_name: align1zst;
136 struct Align1Zst(());
138 check_thin_sized(|| Align1Zst::new(()));
139 check_thin_dyn(|| Align1Zst::new(()));
143 @test_name: align1small;
144 struct Align1Small(u8);
146 check_thin_sized(|| Align1Small::new(50));
147 check_thin_dyn(|| Align1Small::new(50));
151 @test_name: align1_size_not_pow2;
152 struct Align64NotPow2Size([u8; 79]);
154 check_thin_sized(|| Align64NotPow2Size::new([100; 79]));
155 check_thin_dyn(|| Align64NotPow2Size::new([100; 79]));
159 @test_name: align1big;
160 struct Align1Big([u8; 256]);
162 check_thin_sized(|| Align1Big::new([5u8; 256]));
163 check_thin_dyn(|| Align1Big::new([5u8; 256]));
166 // Note: `#[repr(align(2))]` is worth testing because
167 // - can have pointers which are misaligned, unlike align(1)
168 // - is still expected to have an alignment less than the alignment of a vtable.
170 @test_name: align2zst;
172 struct Align2Zst(());
174 check_thin_sized(|| Align2Zst::new(()));
175 check_thin_dyn(|| Align2Zst::new(()));
179 @test_name: align2small;
181 struct Align2Small(u8);
183 check_thin_sized(|| Align2Small::new(60));
184 check_thin_dyn(|| Align2Small::new(60));
188 @test_name: align2full;
190 struct Align2Full([u8; 2]);
191 check_thin_sized(|| Align2Full::new([3u8; 2]));
192 check_thin_dyn(|| Align2Full::new([3u8; 2]));
196 @test_name: align2_size_not_pow2;
198 struct Align2NotPower2Size([u8; 6]);
200 check_thin_sized(|| Align2NotPower2Size::new([3; 6]));
201 check_thin_dyn(|| Align2NotPower2Size::new([3; 6]));
205 @test_name: align2big;
207 struct Align2Big([u8; 256]);
209 check_thin_sized(|| Align2Big::new([5u8; 256]));
210 check_thin_dyn(|| Align2Big::new([5u8; 256]));
214 @test_name: align64zst;
216 struct Align64Zst(());
218 check_thin_sized(|| Align64Zst::new(()));
219 check_thin_dyn(|| Align64Zst::new(()));
223 @test_name: align64small;
225 struct Align64Small(u8);
227 check_thin_sized(|| Align64Small::new(50));
228 check_thin_dyn(|| Align64Small::new(50));
232 @test_name: align64med;
234 struct Align64Med([u8; 64]);
235 check_thin_sized(|| Align64Med::new([10; 64]));
236 check_thin_dyn(|| Align64Med::new([10; 64]));
240 @test_name: align64_size_not_pow2;
242 struct Align64NotPow2Size([u8; 192]);
244 check_thin_sized(|| Align64NotPow2Size::new([10; 192]));
245 check_thin_dyn(|| Align64NotPow2Size::new([10; 192]));
249 @test_name: align64big;
251 struct Align64Big([u8; 256]);
253 check_thin_sized(|| Align64Big::new([10; 256]));
254 check_thin_dyn(|| Align64Big::new([10; 256]));