Debian Codegraph @

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

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