diff options
| author | Nadrieril <nadrieril+git@gmail.com> | 2024-02-07 18:59:40 +0100 |
|---|---|---|
| committer | Nadrieril <nadrieril+git@gmail.com> | 2024-02-07 23:16:47 +0100 |
| commit | be29cd173a87a7d4a87ec6db62f26c3b7666d14a (patch) | |
| tree | 6a0b72fddb90a43d50a8c76d125d17b2057dc195 /compiler/rustc_pattern_analysis/src/usefulness.rs | |
| parent | 8465c82b64501a70d3f9ae967987473efb4f151c (diff) | |
| download | rust-be29cd173a87a7d4a87ec6db62f26c3b7666d14a.tar.gz rust-be29cd173a87a7d4a87ec6db62f26c3b7666d14a.zip | |
Use a unique id instead of by-address indexing
Diffstat (limited to 'compiler/rustc_pattern_analysis/src/usefulness.rs')
| -rw-r--r-- | compiler/rustc_pattern_analysis/src/usefulness.rs | 44 |
1 files changed, 10 insertions, 34 deletions
diff --git a/compiler/rustc_pattern_analysis/src/usefulness.rs b/compiler/rustc_pattern_analysis/src/usefulness.rs index bcc734dc81c..0cbf6203ada 100644 --- a/compiler/rustc_pattern_analysis/src/usefulness.rs +++ b/compiler/rustc_pattern_analysis/src/usefulness.rs @@ -713,10 +713,9 @@ use rustc_hash::FxHashSet; use rustc_index::bit_set::BitSet; use smallvec::{smallvec, SmallVec}; use std::fmt; -use std::ops::Deref; use crate::constructor::{Constructor, ConstructorSet, IntRange}; -use crate::pat::{DeconstructedPat, PatOrWild, WitnessPat}; +use crate::pat::{DeconstructedPat, PatId, PatOrWild, WitnessPat}; use crate::{Captures, MatchArm, TypeCx}; use self::ValidityConstraint::*; @@ -728,36 +727,13 @@ pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R { f() } -/// Wrapper type for by-address hashing. Comparison and hashing of the wrapped pointer type will be -/// based on the address of its contents, rather than their value. -struct ByAddress<T>(T); - -impl<T: Deref> ByAddress<T> { - fn addr(&self) -> *const T::Target { - (&*self.0) as *const _ - } -} -/// Raw pointer hashing and comparison. -impl<T: Deref> std::hash::Hash for ByAddress<T> { - fn hash<H: std::hash::Hasher>(&self, state: &mut H) { - self.addr().hash(state) - } -} -impl<T: Deref> PartialEq for ByAddress<T> { - fn eq(&self, other: &Self) -> bool { - std::ptr::eq(self.addr(), other.addr()) - } -} -impl<T: Deref> Eq for ByAddress<T> {} - /// Context that provides information for usefulness checking. -struct UsefulnessCtxt<'a, 'p, Cx: TypeCx> { +struct UsefulnessCtxt<'a, Cx: TypeCx> { /// The context for type information. tycx: &'a Cx, /// Collect the patterns found useful during usefulness checking. This is used to lint - /// unreachable (sub)patterns. We distinguish patterns by their address to avoid needing to - /// inspect the contents. They'll all be distinct anyway since they carry a `Span`. - useful_subpatterns: FxHashSet<ByAddress<&'p DeconstructedPat<Cx>>>, + /// unreachable (sub)patterns. + useful_subpatterns: FxHashSet<PatId>, } /// Context that provides information local to a place under investigation. @@ -1398,7 +1374,7 @@ impl<Cx: TypeCx> WitnessMatrix<Cx> { /// We can however get false negatives because exhaustiveness does not explore all cases. See the /// section on relevancy at the top of the file. fn collect_overlapping_range_endpoints<'p, Cx: TypeCx>( - mcx: &mut UsefulnessCtxt<'_, 'p, Cx>, + mcx: &mut UsefulnessCtxt<'_, Cx>, overlap_range: IntRange, matrix: &Matrix<'p, Cx>, specialized_matrix: &Matrix<'p, Cx>, @@ -1471,7 +1447,7 @@ fn collect_overlapping_range_endpoints<'p, Cx: TypeCx>( /// This is all explained at the top of the file. #[instrument(level = "debug", skip(mcx), ret)] fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>( - mcx: &mut UsefulnessCtxt<'a, 'p, Cx>, + mcx: &mut UsefulnessCtxt<'a, Cx>, matrix: &mut Matrix<'p, Cx>, ) -> Result<WitnessMatrix<Cx>, Cx::Error> { debug_assert!(matrix.rows().all(|r| r.len() == matrix.column_count())); @@ -1598,7 +1574,7 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>( for row in matrix.rows() { if row.useful { if let PatOrWild::Pat(pat) = row.head() { - mcx.useful_subpatterns.insert(ByAddress(pat)); + mcx.useful_subpatterns.insert(pat.uid); } } } @@ -1620,14 +1596,14 @@ pub enum Usefulness<'p, Cx: TypeCx> { /// Report whether this pattern was found useful, and its subpatterns that were not useful if any. fn collect_pattern_usefulness<'p, Cx: TypeCx>( - useful_subpatterns: &FxHashSet<ByAddress<&'p DeconstructedPat<Cx>>>, + useful_subpatterns: &FxHashSet<PatId>, pat: &'p DeconstructedPat<Cx>, ) -> Usefulness<'p, Cx> { fn pat_is_useful<'p, Cx: TypeCx>( - useful_subpatterns: &FxHashSet<ByAddress<&'p DeconstructedPat<Cx>>>, + useful_subpatterns: &FxHashSet<PatId>, pat: &'p DeconstructedPat<Cx>, ) -> bool { - if useful_subpatterns.contains(&ByAddress(pat)) { + if useful_subpatterns.contains(&pat.uid) { true } else if pat.is_or_pat() && pat.iter_fields().any(|f| pat_is_useful(useful_subpatterns, f)) { |
