Debian Codegraph @

gcc/testsuite/rust/compile/issue-2905-2.rs

raw
1 // { dg-options "-w" }
2 #![feature(intrinsics)]
3 #![feature(lang_items)]
4
5 #[lang = "sized"]
6 trait Sized {}
7
8 extern "rust-intrinsic" {
9 fn transmute<T, U>(_: T) -> U;
10 fn offset<T>(src: *const T, offset: isize) -> *const T;
11 }
12
13 pub mod core {
14 pub mod marker {
15 #[lang = "phantom_data"]
16 pub struct PhantomData<T>;
17 }
18
19 pub mod slice {
20 use crate::core::marker::PhantomData;
21 use crate::core::option::Option;
22
23 impl<T> crate::core::iter::IntoIterator for &[T] {
24 type Item = &T;
25 type IntoIter = Weird<T>;
26
27 fn into_iter(self) -> Weird<T> {
28 self.iter()
29 }
30 }
31
32 pub struct Weird<T> {
33 ptr: *const T, // should be NonNull<T> but here it does not matter
34 end: *const T,
35 _marker: PhantomData<&T>,
36 }
37
38 impl<T> Weird<T> {
39 pub(super) fn new(slice: &[T]) -> Self {
40 let ptr = slice.as_ptr();
41 // SAFETY: Similar to `IterMut::new`.
42 unsafe {
43 // should be: ptr.add(slice.len())
44 let end = transmute::<*const T, usize>(ptr) + slice.len(); // TODO(Arthur): Missing `* size_of::<T>()`?
45 let end = transmute::<usize, *const T>(end);
46
47 Self {
48 ptr,
49 end,
50 _marker: PhantomData,
51 }
52 }
53 }
54
55 fn is_empty(&self) -> bool {
56 self.ptr == self.end
57 }
58
59 fn next_unchecked(&mut self) -> *const T {
60 let old = self.ptr;
61
62 self.ptr = unsafe { offset(self.ptr, 1) };
63
64 old
65 }
66 }
67
68 trait Foo {}
69
70 impl<T> Foo for Weird<T> {}
71
72 // impl<T> core::iter::Iterator for Iter<T> {
73 // type Item = &T;
74
75 // fn next(&mut self) -> Option<&T> {
76 // if self.is_empty() {
77 // Option::None
78 // } else {
79 // Option::Some(&*self.next_unchecked())
80 // }
81 // }
82 // }
83
84 union Repr<T> {
85 pub(crate) rust: *const [T],
86 rust_mut: *mut [T],
87 pub(crate) raw: FatPtr<T>,
88 }
89
90 struct FatPtr<T> {
91 data: *const T,
92 pub(crate) len: usize,
93 }
94
95 impl<T> [T] {
96 pub fn iter(&self) -> Weird<T> {
97 Weird::new(self)
98 }
99
100 pub fn as_ptr(&self) -> *const T {
101 self as *const [T] as *const T
102 }
103
104 pub fn len(&self) -> usize {
105 unsafe { Repr { rust: self }.raw.len }
106 }
107 }
108 }
109
110 pub mod iter {
111 use crate::core::option::Option;
112
113 pub trait IntoIterator {
114 type Item;
115
116 type IntoIter: Iterator<Item = Self::Item>;
117
118 fn into_iter(self) -> Self::IntoIter;
119 }
120
121 pub trait Iterator {
122 type Item;
123
124 fn next(&mut self) -> Option<Self::Item>;
125 }
126 }
127
128 pub mod option {
129 pub enum Option<T> {
130 Some(T),
131 None,
132 }
133 }
134 }
135
136 fn main() {}