about summary refs log tree commit diff
path: root/compiler/rustc_pattern_analysis
diff options
context:
space:
mode:
authorNadrieril <nadrieril+git@gmail.com>2023-12-11 20:45:34 +0100
committerNadrieril <nadrieril+git@gmail.com>2023-12-15 16:58:37 +0100
commit42f43938248d1272c0b3edc85385bde322a9c7fc (patch)
treeb16d0e497638f4a60a5fb56751bf658f18e38a4a /compiler/rustc_pattern_analysis
parentcb622f3994a23d09bb24ef1add67a1569009d558 (diff)
downloadrust-42f43938248d1272c0b3edc85385bde322a9c7fc.tar.gz
rust-42f43938248d1272c0b3edc85385bde322a9c7fc.zip
Iron out last rustc-specific details
Diffstat (limited to 'compiler/rustc_pattern_analysis')
-rw-r--r--compiler/rustc_pattern_analysis/src/constructor.rs24
-rw-r--r--compiler/rustc_pattern_analysis/src/lib.rs6
-rw-r--r--compiler/rustc_pattern_analysis/src/lints.rs2
-rw-r--r--compiler/rustc_pattern_analysis/src/rustc.rs20
4 files changed, 38 insertions, 14 deletions
diff --git a/compiler/rustc_pattern_analysis/src/constructor.rs b/compiler/rustc_pattern_analysis/src/constructor.rs
index 2082ecda44c..d7e216ddbc5 100644
--- a/compiler/rustc_pattern_analysis/src/constructor.rs
+++ b/compiler/rustc_pattern_analysis/src/constructor.rs
@@ -156,7 +156,6 @@ use smallvec::SmallVec;
 
 use rustc_apfloat::ieee::{DoubleS, IeeeFloat, SingleS};
 use rustc_data_structures::fx::FxHashSet;
-use rustc_hir::RangeEnd;
 use rustc_index::IndexVec;
 
 use self::Constructor::*;
@@ -173,6 +172,21 @@ enum Presence {
     Seen,
 }
 
+#[derive(Debug, Copy, Clone, PartialEq, Eq)]
+pub enum RangeEnd {
+    Included,
+    Excluded,
+}
+
+impl fmt::Display for RangeEnd {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.write_str(match self {
+            RangeEnd::Included => "..=",
+            RangeEnd::Excluded => "..",
+        })
+    }
+}
+
 /// A possibly infinite integer. Values are encoded such that the ordering on `u128` matches the
 /// natural order on the original type. For example, `-128i8` is encoded as `0` and `127i8` as
 /// `255`. See `signed_bias` for details.
@@ -220,7 +234,7 @@ impl MaybeInfiniteInt {
         match self {
             Finite(n) => match n.checked_sub(1) {
                 Some(m) => Finite(m),
-                None => bug!(),
+                None => panic!("Called `MaybeInfiniteInt::minus_one` on 0"),
             },
             JustAfterMax => Finite(u128::MAX),
             x => x,
@@ -233,7 +247,7 @@ impl MaybeInfiniteInt {
                 Some(m) => Finite(m),
                 None => JustAfterMax,
             },
-            JustAfterMax => bug!(),
+            JustAfterMax => panic!("Called `MaybeInfiniteInt::plus_one` on u128::MAX+1"),
             x => x,
         }
     }
@@ -270,7 +284,7 @@ impl IntRange {
         }
         if lo >= hi {
             // This should have been caught earlier by E0030.
-            bug!("malformed range pattern: {lo:?}..{hi:?}");
+            panic!("malformed range pattern: {lo:?}..{hi:?}");
         }
         IntRange { lo, hi }
     }
@@ -431,7 +445,7 @@ impl Slice {
         let kind = match (array_len, kind) {
             // If the middle `..` has length 0, we effectively have a fixed-length pattern.
             (Some(len), VarLen(prefix, suffix)) if prefix + suffix == len => FixedLen(len),
-            (Some(len), VarLen(prefix, suffix)) if prefix + suffix > len => bug!(
+            (Some(len), VarLen(prefix, suffix)) if prefix + suffix > len => panic!(
                 "Slice pattern of length {} longer than its array length {len}",
                 prefix + suffix
             ),
diff --git a/compiler/rustc_pattern_analysis/src/lib.rs b/compiler/rustc_pattern_analysis/src/lib.rs
index 192f35c6abf..d0eff55f5a1 100644
--- a/compiler/rustc_pattern_analysis/src/lib.rs
+++ b/compiler/rustc_pattern_analysis/src/lib.rs
@@ -30,8 +30,12 @@ use crate::rustc::RustcCtxt;
 pub trait MatchCx: Sized + Clone + fmt::Debug {
     type Ty: Copy + Clone + fmt::Debug; // FIXME: remove Copy
     type Span: Clone + Default;
+    /// The index of an enum variant.
     type VariantIdx: Clone + Idx;
+    /// A string literal
     type StrLit: Clone + PartialEq + fmt::Debug;
+    /// Extra data to store on a match arm.
+    type ArmData: Copy + Clone + fmt::Debug;
 
     fn is_opaque_ty(ty: Self::Ty) -> bool;
     fn is_exhaustive_patterns_feature_on(&self) -> bool;
@@ -60,8 +64,8 @@ pub trait MatchCx: Sized + Clone + fmt::Debug {
 pub struct MatchArm<'p, Cx: MatchCx> {
     /// The pattern must have been lowered through `check_match::MatchVisitor::lower_pattern`.
     pub pat: &'p DeconstructedPat<'p, Cx>,
-    pub hir_id: HirId,
     pub has_guard: bool,
+    pub arm_data: Cx::ArmData,
 }
 
 impl<'p, Cx: MatchCx> Copy for MatchArm<'p, Cx> {}
diff --git a/compiler/rustc_pattern_analysis/src/lints.rs b/compiler/rustc_pattern_analysis/src/lints.rs
index 89eb439c3f0..be12c8dbd7d 100644
--- a/compiler/rustc_pattern_analysis/src/lints.rs
+++ b/compiler/rustc_pattern_analysis/src/lints.rs
@@ -205,7 +205,7 @@ pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
         // usage of the lint.
         for arm in arms {
             let (lint_level, lint_level_source) =
-                cx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, arm.hir_id);
+                cx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, arm.arm_data);
             if !matches!(lint_level, rustc_session::lint::Level::Allow) {
                 let decorator = NonExhaustiveOmittedPatternLintOnArm {
                     lint_span: lint_level_source.span(),
diff --git a/compiler/rustc_pattern_analysis/src/rustc.rs b/compiler/rustc_pattern_analysis/src/rustc.rs
index 26c13332269..4b13c11d29e 100644
--- a/compiler/rustc_pattern_analysis/src/rustc.rs
+++ b/compiler/rustc_pattern_analysis/src/rustc.rs
@@ -4,7 +4,7 @@ use std::iter::once;
 use rustc_arena::{DroplessArena, TypedArena};
 use rustc_data_structures::captures::Captures;
 use rustc_hir::def_id::DefId;
-use rustc_hir::{HirId, RangeEnd};
+use rustc_hir::HirId;
 use rustc_index::Idx;
 use rustc_index::IndexVec;
 use rustc_middle::middle::stability::EvalResult;
@@ -18,12 +18,13 @@ use rustc_target::abi::{FieldIdx, Integer, VariantIdx, FIRST_VARIANT};
 use smallvec::SmallVec;
 
 use crate::constructor::{
-    IntRange, MaybeInfiniteInt, OpaqueId, Slice, SliceKind, VariantVisibility,
+    IntRange, MaybeInfiniteInt, OpaqueId, RangeEnd, Slice, SliceKind, VariantVisibility,
 };
 use crate::MatchCx;
 
 use crate::constructor::Constructor::*;
 
+// Re-export rustc-specific versions of all these types.
 pub type Constructor<'p, 'tcx> = crate::constructor::Constructor<RustcCtxt<'p, 'tcx>>;
 pub type ConstructorSet<'p, 'tcx> = crate::constructor::ConstructorSet<RustcCtxt<'p, 'tcx>>;
 pub type DeconstructedPat<'p, 'tcx> = crate::pat::DeconstructedPat<'p, RustcCtxt<'p, 'tcx>>;
@@ -520,12 +521,16 @@ impl<'p, 'tcx> RustcCtxt<'p, 'tcx> {
             }
             PatKind::Range(patrange) => {
                 let PatRange { lo, hi, end, .. } = patrange.as_ref();
+                let end = match end {
+                    rustc_hir::RangeEnd::Included => RangeEnd::Included,
+                    rustc_hir::RangeEnd::Excluded => RangeEnd::Excluded,
+                };
                 let ty = pat.ty;
                 ctor = match ty.kind() {
                     ty::Char | ty::Int(_) | ty::Uint(_) => {
                         let lo = cx.lower_pat_range_bdy(*lo, ty);
                         let hi = cx.lower_pat_range_bdy(*hi, ty);
-                        IntRange(IntRange::from_range(lo, hi, *end))
+                        IntRange(IntRange::from_range(lo, hi, end))
                     }
                     ty::Float(fty) => {
                         use rustc_apfloat::Float;
@@ -536,13 +541,13 @@ impl<'p, 'tcx> RustcCtxt<'p, 'tcx> {
                                 use rustc_apfloat::ieee::Single;
                                 let lo = lo.map(Single::from_bits).unwrap_or(-Single::INFINITY);
                                 let hi = hi.map(Single::from_bits).unwrap_or(Single::INFINITY);
-                                F32Range(lo, hi, *end)
+                                F32Range(lo, hi, end)
                             }
                             ty::FloatTy::F64 => {
                                 use rustc_apfloat::ieee::Double;
                                 let lo = lo.map(Double::from_bits).unwrap_or(-Double::INFINITY);
                                 let hi = hi.map(Double::from_bits).unwrap_or(Double::INFINITY);
-                                F64Range(lo, hi, *end)
+                                F64Range(lo, hi, end)
                             }
                         }
                     }
@@ -634,7 +639,7 @@ impl<'p, 'tcx> RustcCtxt<'p, 'tcx> {
             PatKind::Constant { value }
         } else {
             // We convert to an inclusive range for diagnostics.
-            let mut end = RangeEnd::Included;
+            let mut end = rustc_hir::RangeEnd::Included;
             let mut lo = cx.hoist_pat_range_bdy(range.lo, ty);
             if matches!(lo, PatRangeBoundary::PosInfinity) {
                 // The only reason to get `PosInfinity` here is the special case where
@@ -648,7 +653,7 @@ impl<'p, 'tcx> RustcCtxt<'p, 'tcx> {
             }
             let hi = if matches!(range.hi, Finite(0)) {
                 // The range encodes `..ty::MIN`, so we can't convert it to an inclusive range.
-                end = RangeEnd::Excluded;
+                end = rustc_hir::RangeEnd::Excluded;
                 range.hi
             } else {
                 range.hi.minus_one()
@@ -853,6 +858,7 @@ impl<'p, 'tcx> MatchCx for RustcCtxt<'p, 'tcx> {
     type Span = Span;
     type VariantIdx = VariantIdx;
     type StrLit = Const<'tcx>;
+    type ArmData = HirId;
 
     fn is_exhaustive_patterns_feature_on(&self) -> bool {
         self.tcx.features().exhaustive_patterns