aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/rust/compile/issue-2905-2.rs
blob: 83c54ed92e5f62d798328f27e22e6c8e7527aa0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// { dg-options "-w" }
#![feature(intrinsics)]
#![feature(lang_items)]

#[lang = "sized"]
trait Sized {}

extern "rust-intrinsic" {
    fn transmute<T, U>(_: T) -> U;
    fn offset<T>(src: *const T, offset: isize) -> *const T;
}

pub mod core {
    pub mod marker {
        #[lang = "phantom_data"]
        pub struct PhantomData<T>;
    }

    pub mod slice {
        use core::marker::PhantomData;
        use core::option::Option;

        impl<T> core::iter::IntoIterator for &[T] {
            type Item = &T;
            type IntoIter = Weird<T>;

            fn into_iter(self) -> Weird<T> {
                self.iter()
            }
        }

        pub struct Weird<T> {
            ptr: *const T, // should be NonNull<T> but here it does not matter
            end: *const T,
            _marker: PhantomData<&T>,
        }

        impl<T> Weird<T> {
            pub(super) fn new(slice: &[T]) -> Self {
                let ptr = slice.as_ptr();
                // SAFETY: Similar to `IterMut::new`.
                unsafe {
                    // should be: ptr.add(slice.len())
                    let end = transmute::<*const T, usize>(ptr) + slice.len(); // TODO(Arthur): Missing `* size_of::<T>()`?
                    let end = transmute::<usize, *const T>(end);

                    Self {
                        ptr,
                        end,
                        _marker: PhantomData,
                    }
                }
            }

            fn is_empty(&self) -> bool {
                self.ptr == self.end
            }

            fn next_unchecked(&mut self) -> *const T {
                let old = self.ptr;

                self.ptr = unsafe { offset(self.ptr, 1) };

                old
            }
        }

        trait Foo {}

        impl<T> Foo for Weird<T> {}

        // impl<T> core::iter::Iterator for Iter<T> {
        //     type Item = &T;

        //     fn next(&mut self) -> Option<&T> {
        //         if self.is_empty() {
        //             Option::None
        //         } else {
        //             Option::Some(&*self.next_unchecked())
        //         }
        //     }
        // }

        union Repr<T> {
            pub(crate) rust: *const [T],
            rust_mut: *mut [T],
            pub(crate) raw: FatPtr<T>,
        }

        struct FatPtr<T> {
            data: *const T,
            pub(crate) len: usize,
        }

        impl<T> [T] {
            pub fn iter(&self) -> Weird<T> {
                Weird::new(self)
            }

            pub fn as_ptr(&self) -> *const T {
                self as *const [T] as *const T
            }

            pub fn len(&self) -> usize {
                unsafe { Repr { rust: self }.raw.len }
            }
        }
    }

    pub mod iter {
        use option::Option;

        pub trait IntoIterator {
            type Item;

            type IntoIter: Iterator<Item = Self::Item>;

            fn into_iter(self) -> Self::IntoIter;
        }

        pub trait Iterator {
            type Item;

            fn next(&mut self) -> Option<Self::Item>;
        }
    }

    pub mod option {
        pub enum Option<T> {
            Some(T),
            None,
        }
    }
}

fn main() {}