]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/dst-raw.rs
Rollup merge of #51722 - Aaronepower:master, r=Mark-Simulacrum
[rust.git] / src / test / run-pass / dst-raw.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Test DST raw pointers
12
13
14 #![feature(unsized_tuple_coercion)]
15
16 trait Trait {
17     fn foo(&self) -> isize;
18 }
19
20 struct A {
21     f: isize
22 }
23 impl Trait for A {
24     fn foo(&self) -> isize {
25         self.f
26     }
27 }
28
29 struct Foo<T: ?Sized> {
30     f: T
31 }
32
33 pub fn main() {
34     // raw trait object
35     let x = A { f: 42 };
36     let z: *const Trait = &x;
37     let r = unsafe {
38         (&*z).foo()
39     };
40     assert_eq!(r, 42);
41
42     // raw DST struct
43     let p = Foo {f: A { f: 42 }};
44     let o: *const Foo<Trait> = &p;
45     let r = unsafe {
46         (&*o).f.foo()
47     };
48     assert_eq!(r, 42);
49
50     // raw DST tuple
51     let p = (A { f: 42 },);
52     let o: *const (Trait,) = &p;
53     let r = unsafe {
54         (&*o).0.foo()
55     };
56     assert_eq!(r, 42);
57
58     // raw slice
59     let a: *const [_] = &[1, 2, 3];
60     unsafe {
61         let b = (*a)[2];
62         assert_eq!(b, 3);
63         let len = (*a).len();
64         assert_eq!(len, 3);
65     }
66
67     // raw slice with explicit cast
68     let a = &[1, 2, 3] as *const [i32];
69     unsafe {
70         let b = (*a)[2];
71         assert_eq!(b, 3);
72         let len = (*a).len();
73         assert_eq!(len, 3);
74     }
75
76     // raw DST struct with slice
77     let c: *const Foo<[_]> = &Foo {f: [1, 2, 3]};
78     unsafe {
79         let b = (&*c).f[0];
80         assert_eq!(b, 1);
81         let len = (&*c).f.len();
82         assert_eq!(len, 3);
83     }
84
85     // raw DST tuple with slice
86     let c: *const ([_],) = &([1, 2, 3],);
87     unsafe {
88         let b = (&*c).0[0];
89         assert_eq!(b, 1);
90         let len = (&*c).0.len();
91         assert_eq!(len, 3);
92     }
93
94     // all of the above with *mut
95     let mut x = A { f: 42 };
96     let z: *mut Trait = &mut x;
97     let r = unsafe {
98         (&*z).foo()
99     };
100     assert_eq!(r, 42);
101
102     let mut p = Foo {f: A { f: 42 }};
103     let o: *mut Foo<Trait> = &mut p;
104     let r = unsafe {
105         (&*o).f.foo()
106     };
107     assert_eq!(r, 42);
108
109     let mut p = (A { f: 42 },);
110     let o: *mut (Trait,) = &mut p;
111     let r = unsafe {
112         (&*o).0.foo()
113     };
114     assert_eq!(r, 42);
115
116     let a: *mut [_] = &mut [1, 2, 3];
117     unsafe {
118         let b = (*a)[2];
119         assert_eq!(b, 3);
120         let len = (*a).len();
121         assert_eq!(len, 3);
122     }
123
124     let a = &mut [1, 2, 3] as *mut [i32];
125     unsafe {
126         let b = (*a)[2];
127         assert_eq!(b, 3);
128         let len = (*a).len();
129         assert_eq!(len, 3);
130     }
131
132     let c: *mut Foo<[_]> = &mut Foo {f: [1, 2, 3]};
133     unsafe {
134         let b = (&*c).f[0];
135         assert_eq!(b, 1);
136         let len = (&*c).f.len();
137         assert_eq!(len, 3);
138     }
139
140     let c: *mut ([_],) = &mut ([1, 2, 3],);
141     unsafe {
142         let b = (&*c).0[0];
143         assert_eq!(b, 1);
144         let len = (&*c).0.len();
145         assert_eq!(len, 3);
146     }
147 }