]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/list.rs
Auto merge of #93873 - Stovent:big-ints, r=m-ou-se
[rust.git] / compiler / rustc_middle / src / ty / list.rs
1 use crate::arena::Arena;
2 use rustc_serialize::{Encodable, Encoder};
3 use std::alloc::Layout;
4 use std::cmp::Ordering;
5 use std::fmt;
6 use std::hash::{Hash, Hasher};
7 use std::iter;
8 use std::mem;
9 use std::ops::Deref;
10 use std::ptr;
11 use std::slice;
12
13 /// `List<T>` is a bit like `&[T]`, but with some critical differences.
14 /// - IMPORTANT: Every `List<T>` is *required* to have unique contents. The
15 ///   type's correctness relies on this, *but it does not enforce it*.
16 ///   Therefore, any code that creates a `List<T>` must ensure uniqueness
17 ///   itself. In practice this is achieved by interning.
18 /// - The length is stored within the `List<T>`, so `&List<Ty>` is a thin
19 ///   pointer.
20 /// - Because of this, you cannot get a `List<T>` that is a sub-list of another
21 ///   `List<T>`. You can get a sub-slice `&[T]`, however.
22 /// - `List<T>` can be used with `CopyTaggedPtr`, which is useful within
23 ///   structs whose size must be minimized.
24 /// - Because of the uniqueness assumption, we can use the address of a
25 ///   `List<T>` for faster equality comparisons and hashing.
26 /// - `T` must be `Copy`. This lets `List<T>` be stored in a dropless arena and
27 ///   iterators return a `T` rather than a `&T`.
28 /// - `T` must not be zero-sized.
29 #[repr(C)]
30 pub struct List<T> {
31     len: usize,
32
33     /// Although this claims to be a zero-length array, in practice `len`
34     /// elements are actually present.
35     data: [T; 0],
36
37     opaque: OpaqueListContents,
38 }
39
40 extern "C" {
41     /// A dummy type used to force `List` to be unsized while not requiring
42     /// references to it be wide pointers.
43     type OpaqueListContents;
44 }
45
46 impl<T> List<T> {
47     /// Returns a reference to the (unique, static) empty list.
48     #[inline(always)]
49     pub fn empty<'a>() -> &'a List<T> {
50         #[repr(align(64))]
51         struct MaxAlign;
52
53         assert!(mem::align_of::<T>() <= mem::align_of::<MaxAlign>());
54
55         #[repr(C)]
56         struct InOrder<T, U>(T, U);
57
58         // The empty slice is static and contains a single `0` usize (for the
59         // length) that is 64-byte aligned, thus featuring the necessary
60         // trailing padding for elements with up to 64-byte alignment.
61         static EMPTY_SLICE: InOrder<usize, MaxAlign> = InOrder(0, MaxAlign);
62         unsafe { &*(&EMPTY_SLICE as *const _ as *const List<T>) }
63     }
64
65     pub fn len(&self) -> usize {
66         self.len
67     }
68
69     pub fn as_slice(&self) -> &[T] {
70         self
71     }
72 }
73
74 impl<T: Copy> List<T> {
75     /// Allocates a list from `arena` and copies the contents of `slice` into it.
76     ///
77     /// WARNING: the contents *must be unique*, such that no list with these
78     /// contents has been previously created. If not, operations such as `eq`
79     /// and `hash` might give incorrect results.
80     ///
81     /// Panics if `T` is `Drop`, or `T` is zero-sized, or the slice is empty
82     /// (because the empty list exists statically, and is available via
83     /// `empty()`).
84     #[inline]
85     pub(super) fn from_arena<'tcx>(arena: &'tcx Arena<'tcx>, slice: &[T]) -> &'tcx List<T> {
86         assert!(!mem::needs_drop::<T>());
87         assert!(mem::size_of::<T>() != 0);
88         assert!(!slice.is_empty());
89
90         let (layout, _offset) =
91             Layout::new::<usize>().extend(Layout::for_value::<[T]>(slice)).unwrap();
92         let mem = arena.dropless.alloc_raw(layout) as *mut List<T>;
93         unsafe {
94             // Write the length
95             ptr::addr_of_mut!((*mem).len).write(slice.len());
96
97             // Write the elements
98             ptr::addr_of_mut!((*mem).data)
99                 .cast::<T>()
100                 .copy_from_nonoverlapping(slice.as_ptr(), slice.len());
101
102             &*mem
103         }
104     }
105
106     // If this method didn't exist, we would use `slice.iter` due to
107     // deref coercion.
108     //
109     // This would be weird, as `self.into_iter` iterates over `T` directly.
110     #[inline(always)]
111     pub fn iter(&self) -> <&'_ List<T> as IntoIterator>::IntoIter {
112         self.into_iter()
113     }
114 }
115
116 impl<T: fmt::Debug> fmt::Debug for List<T> {
117     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118         (**self).fmt(f)
119     }
120 }
121
122 impl<S: Encoder, T: Encodable<S>> Encodable<S> for List<T> {
123     #[inline]
124     fn encode(&self, s: &mut S) {
125         (**self).encode(s);
126     }
127 }
128
129 impl<T: PartialEq> PartialEq for List<T> {
130     #[inline]
131     fn eq(&self, other: &List<T>) -> bool {
132         // Pointer equality implies list equality (due to the unique contents
133         // assumption).
134         ptr::eq(self, other)
135     }
136 }
137
138 impl<T: Eq> Eq for List<T> {}
139
140 impl<T> Ord for List<T>
141 where
142     T: Ord,
143 {
144     fn cmp(&self, other: &List<T>) -> Ordering {
145         // Pointer equality implies list equality (due to the unique contents
146         // assumption), but the contents must be compared otherwise.
147         if self == other { Ordering::Equal } else { <[T] as Ord>::cmp(&**self, &**other) }
148     }
149 }
150
151 impl<T> PartialOrd for List<T>
152 where
153     T: PartialOrd,
154 {
155     fn partial_cmp(&self, other: &List<T>) -> Option<Ordering> {
156         // Pointer equality implies list equality (due to the unique contents
157         // assumption), but the contents must be compared otherwise.
158         if self == other {
159             Some(Ordering::Equal)
160         } else {
161             <[T] as PartialOrd>::partial_cmp(&**self, &**other)
162         }
163     }
164 }
165
166 impl<T> Hash for List<T> {
167     #[inline]
168     fn hash<H: Hasher>(&self, s: &mut H) {
169         // Pointer hashing is sufficient (due to the unique contents
170         // assumption).
171         (self as *const List<T>).hash(s)
172     }
173 }
174
175 impl<T> Deref for List<T> {
176     type Target = [T];
177     #[inline(always)]
178     fn deref(&self) -> &[T] {
179         self.as_ref()
180     }
181 }
182
183 impl<T> AsRef<[T]> for List<T> {
184     #[inline(always)]
185     fn as_ref(&self) -> &[T] {
186         unsafe { slice::from_raw_parts(self.data.as_ptr(), self.len) }
187     }
188 }
189
190 impl<'a, T: Copy> IntoIterator for &'a List<T> {
191     type Item = T;
192     type IntoIter = iter::Copied<<&'a [T] as IntoIterator>::IntoIter>;
193     #[inline(always)]
194     fn into_iter(self) -> Self::IntoIter {
195         self[..].iter().copied()
196     }
197 }
198
199 unsafe impl<T: Sync> Sync for List<T> {}
200
201 unsafe impl<'a, T: 'a> rustc_data_structures::tagged_ptr::Pointer for &'a List<T> {
202     const BITS: usize = std::mem::align_of::<usize>().trailing_zeros() as usize;
203
204     #[inline]
205     fn into_usize(self) -> usize {
206         self as *const List<T> as usize
207     }
208
209     #[inline]
210     unsafe fn from_usize(ptr: usize) -> &'a List<T> {
211         &*(ptr as *const List<T>)
212     }
213
214     unsafe fn with_ref<R, F: FnOnce(&Self) -> R>(ptr: usize, f: F) -> R {
215         // `Self` is `&'a List<T>` which impls `Copy`, so this is fine.
216         let ptr = Self::from_usize(ptr);
217         f(&ptr)
218     }
219 }