]> git.lizzy.rs Git - rust.git/blob - tests/ui/mir/mir_fat_ptr.rs
Rollup merge of #107015 - cuviper:ra-riscv64, r=Mark-Simulacrum
[rust.git] / tests / ui / mir / mir_fat_ptr.rs
1 // run-pass
2 // test that ordinary fat pointer operations work.
3
4 struct Wrapper<T: ?Sized>(#[allow(unused_tuple_struct_fields)] u32, T);
5
6 struct FatPtrContainer<'a> {
7     ptr: &'a [u8]
8 }
9
10 fn fat_ptr_project(a: &Wrapper<[u8]>) -> &[u8] {
11     &a.1
12 }
13
14 fn fat_ptr_simple(a: &[u8]) -> &[u8] {
15     a
16 }
17
18 fn fat_ptr_via_local(a: &[u8]) -> &[u8] {
19     let x = a;
20     x
21 }
22
23 fn fat_ptr_from_struct(s: FatPtrContainer) -> &[u8] {
24     s.ptr
25 }
26
27 fn fat_ptr_to_struct(a: &[u8]) -> FatPtrContainer {
28     FatPtrContainer { ptr: a }
29 }
30
31 fn fat_ptr_store_to<'a>(a: &'a [u8], b: &mut &'a [u8]) {
32     *b = a;
33 }
34
35 fn fat_ptr_constant() -> &'static str {
36     "HELLO"
37 }
38
39 fn main() {
40     let a = Wrapper(4, [7,6,5]);
41
42     let p = fat_ptr_project(&a);
43     let p = fat_ptr_simple(p);
44     let p = fat_ptr_via_local(p);
45     let p = fat_ptr_from_struct(fat_ptr_to_struct(p));
46
47     let mut target : &[u8] = &[42];
48     fat_ptr_store_to(p, &mut target);
49     assert_eq!(target, &a.1);
50
51     assert_eq!(fat_ptr_constant(), "HELLO");
52 }