Debian Codegraph @

gcc/testsuite/rust/execute/torture/issue-1232.rs

raw
1 // { dg-additional-options "-w" }
2 // { dg-output "slice_access=3\r*\n" }
3 #![feature(intrinsics)]
4
5 #[lang = "sized"]
6 pub trait Sized {}
7
8 extern "rust-intrinsic" {
9 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
10 fn offset<T>(dst: *const T, offset: isize) -> *const T;
11 }
12
13 extern "C" {
14 fn printf(s: *const i8, ...);
15 }
16
17 struct FatPtr<T> {
18 data: *const T,
19 len: usize,
20 }
21
22 pub union Repr<T> {
23 rust: *const [T],
24 rust_mut: *mut [T],
25 raw: FatPtr<T>,
26 }
27
28 pub enum Option<T> {
29 None,
30 Some(T),
31 }
32
33 #[lang = "Range"]
34 pub struct Range<Idx> {
35 pub start: Idx,
36 pub end: Idx,
37 }
38
39 #[lang = "const_slice_ptr"]
40 impl<T> *const [T] {
41 pub const fn len(self) -> usize {
42 unsafe { Repr { rust: self }.raw.len }
43 }
44
45 pub const fn as_ptr(self) -> *const T {
46 self as *const T
47 }
48 }
49
50 #[lang = "const_ptr"]
51 impl<T> *const T {
52 pub const unsafe fn offset(self, count: isize) -> *const T {
53 unsafe { offset(self, count) }
54 }
55
56 pub const unsafe fn add(self, count: usize) -> Self {
57 unsafe { self.offset(count as isize) }
58 }
59
60 pub const fn as_ptr(self) -> *const T {
61 self as *const T
62 }
63 }
64
65 const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
66 unsafe {
67 Repr {
68 raw: FatPtr { data, len },
69 }
70 .rust
71 }
72 }
73
74 #[lang = "index"]
75 trait Index<Idx> {
76 type Output;
77
78 fn index(&self, index: Idx) -> &Self::Output;
79 }
80
81 pub unsafe trait SliceIndex<T> {
82 type Output;
83
84 fn get(self, slice: &T) -> Option<&Self::Output>;
85
86 unsafe fn get_unchecked(self, slice: *const T) -> *const Self::Output;
87
88 fn index(self, slice: &T) -> &Self::Output;
89 }
90
91 unsafe impl<T> SliceIndex<[T]> for usize {
92 type Output = T;
93
94 fn get(self, slice: &[T]) -> Option<&T> {
95 unsafe { Option::Some(&*self.get_unchecked(slice)) }
96 }
97
98 unsafe fn get_unchecked(self, slice: *const [T]) -> *const T {
99 // SAFETY: the caller guarantees that `slice` is not dangling, so it
100 // cannot be longer than `isize::MAX`. They also guarantee that
101 // `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
102 // so the call to `add` is safe.
103 unsafe { slice.as_ptr().add(self) }
104 }
105
106 fn index(self, slice: &[T]) -> &T {
107 // N.B., use intrinsic indexing
108 // &(*slice)[self]
109 unsafe { &*self.get_unchecked(slice) }
110 }
111 }
112
113 unsafe impl<T> SliceIndex<[T]> for Range<usize> {
114 type Output = [T];
115
116 fn get(self, slice: &[T]) -> Option<&[T]> {
117 if self.start > self.end
118 /* || self.end > slice.len() */
119 {
120 Option::None
121 } else {
122 unsafe { Option::Some(&*self.get_unchecked(slice)) }
123 }
124 }
125
126 unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
127 unsafe {
128 let a: *const T = slice.as_ptr();
129 let b: *const T = a.add(self.start);
130 slice_from_raw_parts(b, self.end - self.start)
131 }
132 }
133
134 fn index(self, slice: &[T]) -> &[T] {
135 unsafe { &*self.get_unchecked(slice) }
136 }
137 }
138
139 impl<T, I> Index<I> for [T]
140 where
141 I: SliceIndex<[T]>,
142 {
143 type Output = I::Output;
144
145 fn index(&self, index: I) -> &I::Output {
146 index.index(self)
147 }
148 }
149
150 fn main() -> i32 {
151 let array = [1, 2, 3, 4, 5];
152 let slice = &array[1..3];
153 let slice_access = slice[1];
154
155 unsafe {
156 let a = "slice_access=%i\n";
157 let b = a as *const str;
158 let c = b as *const i8;
159
160 printf(c, slice_access);
161 }
162
163 0
164 }