gccrs: Add execution test cases
[official-gcc.git] / gcc / testsuite / rust / execute / torture / str-layout1.rs
blob80bdc2a9c9f44f52232b582032688bf03ca91408
1 // { dg-additional-options "-w" }
2 // { dg-output "t1sz=5 t2sz=10" }
3 mod mem {
4     extern "rust-intrinsic" {
5         #[rustc_const_stable(feature = "const_transmute", since = "1.46.0")]
6         fn transmute<T, U>(_: T) -> U;
7     }
10 extern "C" {
11     fn printf(s: *const i8, ...);
14 struct FatPtr<T> {
15     data: *const T,
16     len: usize,
19 pub union Repr<T> {
20     rust: *const [T],
21     rust_mut: *mut [T],
22     raw: FatPtr<T>,
25 impl<T> [T] {
26     pub const fn len(&self) -> usize {
27         unsafe { Repr { rust: self }.raw.len }
28     }
31 impl str {
32     pub const fn len(&self) -> usize {
33         self.as_bytes().len()
34     }
36     pub const fn as_bytes(&self) -> &[u8] {
37         unsafe { mem::transmute(self) }
38     }
41 fn main() -> i32 {
42     let t1: &str = "TEST1";
43     let t2: &str = &"TEST_12345";
45     let t1sz = t1.len();
46     let t2sz = t2.len();
48     unsafe {
49         let a = "t1sz=%i t2sz=%i\n";
50         let b = a as *const str;
51         let c = b as *const i8;
53         printf(c, t1sz as i32, t2sz as i32);
54     }
56     0