|
1 | | -#![allow(clippy::derived_hash_with_manual_eq)] |
2 | | - |
3 | 1 | use derive_where::derive_where; |
4 | 2 |
|
5 | 3 | #[cfg(feature = "nightly")] |
@@ -68,7 +66,7 @@ impl AliasTyKind { |
68 | 66 | /// Types written by the user start out as `hir::TyKind` and get |
69 | 67 | /// converted to this representation using `<dyn HirTyLowerer>::lower_ty`. |
70 | 68 | #[cfg_attr(feature = "nightly", rustc_diagnostic_item = "IrTyKind")] |
71 | | -#[derive_where(Clone, Copy, Hash, Eq; I: Interner)] |
| 69 | +#[derive_where(Clone, Copy, Hash, PartialEq, Eq; I: Interner)] |
72 | 70 | #[cfg_attr(feature = "nightly", derive(TyEncodable, TyDecodable, HashStable_NoContext))] |
73 | 71 | pub enum TyKind<I: Interner> { |
74 | 72 | /// The primitive boolean type. Written as `bool`. |
@@ -259,92 +257,6 @@ impl<I: Interner> TyKind<I> { |
259 | 257 | } |
260 | 258 | } |
261 | 259 |
|
262 | | -// This is manually implemented for `TyKind` because `std::mem::discriminant` |
263 | | -// returns an opaque value that is `PartialEq` but not `PartialOrd` |
264 | | -#[inline] |
265 | | -const fn tykind_discriminant<I: Interner>(value: &TyKind<I>) -> usize { |
266 | | - match value { |
267 | | - Bool => 0, |
268 | | - Char => 1, |
269 | | - Int(_) => 2, |
270 | | - Uint(_) => 3, |
271 | | - Float(_) => 4, |
272 | | - Adt(_, _) => 5, |
273 | | - Foreign(_) => 6, |
274 | | - Str => 7, |
275 | | - Array(_, _) => 8, |
276 | | - Slice(_) => 9, |
277 | | - RawPtr(_, _) => 10, |
278 | | - Ref(_, _, _) => 11, |
279 | | - FnDef(_, _) => 12, |
280 | | - FnPtr(_) => 13, |
281 | | - Dynamic(..) => 14, |
282 | | - Closure(_, _) => 15, |
283 | | - CoroutineClosure(_, _) => 16, |
284 | | - Coroutine(_, _) => 17, |
285 | | - CoroutineWitness(_, _) => 18, |
286 | | - Never => 19, |
287 | | - Tuple(_) => 20, |
288 | | - Pat(_, _) => 21, |
289 | | - Alias(_, _) => 22, |
290 | | - Param(_) => 23, |
291 | | - Bound(_, _) => 24, |
292 | | - Placeholder(_) => 25, |
293 | | - Infer(_) => 26, |
294 | | - Error(_) => 27, |
295 | | - } |
296 | | -} |
297 | | - |
298 | | -// FIXME(GrigorenkoPV): consider not implementing PartialEq manually |
299 | | -// This is manually implemented because a derive would require `I: PartialEq` |
300 | | -impl<I: Interner> PartialEq for TyKind<I> { |
301 | | - #[inline] |
302 | | - fn eq(&self, other: &TyKind<I>) -> bool { |
303 | | - // You might expect this `match` to be preceded with this: |
304 | | - // |
305 | | - // tykind_discriminant(self) == tykind_discriminant(other) && |
306 | | - // |
307 | | - // but the data patterns in practice are such that a comparison |
308 | | - // succeeds 99%+ of the time, and it's faster to omit it. |
309 | | - match (self, other) { |
310 | | - (Int(a_i), Int(b_i)) => a_i == b_i, |
311 | | - (Uint(a_u), Uint(b_u)) => a_u == b_u, |
312 | | - (Float(a_f), Float(b_f)) => a_f == b_f, |
313 | | - (Adt(a_d, a_s), Adt(b_d, b_s)) => a_d == b_d && a_s == b_s, |
314 | | - (Foreign(a_d), Foreign(b_d)) => a_d == b_d, |
315 | | - (Array(a_t, a_c), Array(b_t, b_c)) => a_t == b_t && a_c == b_c, |
316 | | - (Pat(a_t, a_c), Pat(b_t, b_c)) => a_t == b_t && a_c == b_c, |
317 | | - (Slice(a_t), Slice(b_t)) => a_t == b_t, |
318 | | - (RawPtr(a_t, a_m), RawPtr(b_t, b_m)) => a_t == b_t && a_m == b_m, |
319 | | - (Ref(a_r, a_t, a_m), Ref(b_r, b_t, b_m)) => a_r == b_r && a_t == b_t && a_m == b_m, |
320 | | - (FnDef(a_d, a_s), FnDef(b_d, b_s)) => a_d == b_d && a_s == b_s, |
321 | | - (FnPtr(a_s), FnPtr(b_s)) => a_s == b_s, |
322 | | - (Dynamic(a_p, a_r, a_repr), Dynamic(b_p, b_r, b_repr)) => { |
323 | | - a_p == b_p && a_r == b_r && a_repr == b_repr |
324 | | - } |
325 | | - (Closure(a_d, a_s), Closure(b_d, b_s)) => a_d == b_d && a_s == b_s, |
326 | | - (CoroutineClosure(a_d, a_s), CoroutineClosure(b_d, b_s)) => a_d == b_d && a_s == b_s, |
327 | | - (Coroutine(a_d, a_s), Coroutine(b_d, b_s)) => a_d == b_d && a_s == b_s, |
328 | | - (CoroutineWitness(a_d, a_s), CoroutineWitness(b_d, b_s)) => a_d == b_d && a_s == b_s, |
329 | | - (Tuple(a_t), Tuple(b_t)) => a_t == b_t, |
330 | | - (Alias(a_i, a_p), Alias(b_i, b_p)) => a_i == b_i && a_p == b_p, |
331 | | - (Param(a_p), Param(b_p)) => a_p == b_p, |
332 | | - (Bound(a_d, a_b), Bound(b_d, b_b)) => a_d == b_d && a_b == b_b, |
333 | | - (Placeholder(a_p), Placeholder(b_p)) => a_p == b_p, |
334 | | - (Infer(a_t), Infer(b_t)) => a_t == b_t, |
335 | | - (Error(a_e), Error(b_e)) => a_e == b_e, |
336 | | - (Bool, Bool) | (Char, Char) | (Str, Str) | (Never, Never) => true, |
337 | | - _ => { |
338 | | - debug_assert!( |
339 | | - tykind_discriminant(self) != tykind_discriminant(other), |
340 | | - "This branch must be unreachable, maybe the match is missing an arm? self = {self:?}, other = {other:?}" |
341 | | - ); |
342 | | - false |
343 | | - } |
344 | | - } |
345 | | - } |
346 | | -} |
347 | | - |
348 | 260 | // This is manually implemented because a derive would require `I: Debug` |
349 | 261 | impl<I: Interner> fmt::Debug for TyKind<I> { |
350 | 262 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
0 commit comments