]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/pass/dst-field-align.rs
Auto merge of #104915 - weihanglo:update-cargo, r=ehuss
[rust.git] / src / tools / miri / tests / pass / dst-field-align.rs
1 #[allow(dead_code)]
2 struct Foo<T: ?Sized> {
3     a: u16,
4     b: T,
5 }
6
7 trait Bar {
8     fn get(&self) -> usize;
9 }
10
11 impl Bar for usize {
12     fn get(&self) -> usize {
13         *self
14     }
15 }
16
17 struct Baz<T: ?Sized> {
18     a: T,
19 }
20
21 #[allow(dead_code)]
22 struct HasDrop<T: ?Sized> {
23     ptr: Box<usize>,
24     data: T,
25 }
26
27 fn main() {
28     // Test that zero-offset works properly
29     let b: Baz<usize> = Baz { a: 7 };
30     assert_eq!(b.a.get(), 7);
31     let b: &Baz<dyn Bar> = &b;
32     assert_eq!(b.a.get(), 7);
33
34     // Test that the field is aligned properly
35     let f: Foo<usize> = Foo { a: 0, b: 11 };
36     assert_eq!(f.b.get(), 11);
37     let ptr1: *const u8 = &f.b as *const _ as *const u8;
38
39     let f: &Foo<dyn Bar> = &f;
40     let ptr2: *const u8 = &f.b as *const _ as *const u8;
41     assert_eq!(f.b.get(), 11);
42
43     // The pointers should be the same
44     assert_eq!(ptr1, ptr2);
45
46     // Test that nested DSTs work properly
47     let f: Foo<Foo<usize>> = Foo { a: 0, b: Foo { a: 1, b: 17 } };
48     assert_eq!(f.b.b.get(), 17);
49     let f: &Foo<Foo<dyn Bar>> = &f;
50     assert_eq!(f.b.b.get(), 17);
51
52     // Test that get the pointer via destructuring works
53
54     let f: Foo<usize> = Foo { a: 0, b: 11 };
55     let f: &Foo<dyn Bar> = &f;
56     let &Foo { a: _, b: ref bar } = f;
57     assert_eq!(bar.get(), 11);
58
59     // Make sure that drop flags don't screw things up
60
61     let d: HasDrop<Baz<[i32; 4]>> = HasDrop { ptr: Box::new(0), data: Baz { a: [1, 2, 3, 4] } };
62     assert_eq!([1, 2, 3, 4], d.data.a);
63
64     let d: &HasDrop<Baz<[i32]>> = &d;
65     assert_eq!(&[1, 2, 3, 4], &d.data.a);
66 }