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