Skip to content

Commit 42f4393

Browse files
committed
Iron out last rustc-specific details
1 parent cb622f3 commit 42f4393

File tree

5 files changed

+43
-18
lines changed

5 files changed

+43
-18
lines changed

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
429429
let arm = &self.thir.arms[arm];
430430
let got_error = self.with_lint_level(arm.lint_level, |this| {
431431
let Ok(pat) = this.lower_pattern(&cx, &arm.pattern) else { return true };
432-
let arm = MatchArm { pat, hir_id: this.lint_level, has_guard: arm.guard.is_some() };
432+
let arm =
433+
MatchArm { pat, arm_data: this.lint_level, has_guard: arm.guard.is_some() };
433434
tarms.push(arm);
434435
false
435436
});
@@ -552,7 +553,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
552553
) -> Result<(MatchCheckCtxt<'p, 'tcx>, UsefulnessReport<'p, 'tcx>), ErrorGuaranteed> {
553554
let cx = self.new_cx(refutability, None, scrut, pat.span);
554555
let pat = self.lower_pattern(&cx, pat)?;
555-
let arms = [MatchArm { pat, hir_id: self.lint_level, has_guard: false }];
556+
let arms = [MatchArm { pat, arm_data: self.lint_level, has_guard: false }];
556557
let report = analyze_match(&cx, &arms, pat.ty());
557558
Ok((cx, report))
558559
}
@@ -855,7 +856,7 @@ fn report_arm_reachability<'p, 'tcx>(
855856
for (arm, is_useful) in report.arm_usefulness.iter() {
856857
match is_useful {
857858
Usefulness::Redundant => {
858-
report_unreachable_pattern(*arm.pat.span(), arm.hir_id, catchall)
859+
report_unreachable_pattern(*arm.pat.span(), arm.arm_data, catchall)
859860
}
860861
Usefulness::Useful(redundant_spans) if redundant_spans.is_empty() => {}
861862
// The arm is reachable, but contains redundant subpatterns (from or-patterns).
@@ -864,7 +865,7 @@ fn report_arm_reachability<'p, 'tcx>(
864865
// Emit lints in the order in which they occur in the file.
865866
redundant_spans.sort_unstable();
866867
for span in redundant_spans {
867-
report_unreachable_pattern(span, arm.hir_id, None);
868+
report_unreachable_pattern(span, arm.arm_data, None);
868869
}
869870
}
870871
}

compiler/rustc_pattern_analysis/src/constructor.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ use smallvec::SmallVec;
156156

157157
use rustc_apfloat::ieee::{DoubleS, IeeeFloat, SingleS};
158158
use rustc_data_structures::fx::FxHashSet;
159-
use rustc_hir::RangeEnd;
160159
use rustc_index::IndexVec;
161160

162161
use self::Constructor::*;
@@ -173,6 +172,21 @@ enum Presence {
173172
Seen,
174173
}
175174

175+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
176+
pub enum RangeEnd {
177+
Included,
178+
Excluded,
179+
}
180+
181+
impl fmt::Display for RangeEnd {
182+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183+
f.write_str(match self {
184+
RangeEnd::Included => "..=",
185+
RangeEnd::Excluded => "..",
186+
})
187+
}
188+
}
189+
176190
/// A possibly infinite integer. Values are encoded such that the ordering on `u128` matches the
177191
/// natural order on the original type. For example, `-128i8` is encoded as `0` and `127i8` as
178192
/// `255`. See `signed_bias` for details.
@@ -220,7 +234,7 @@ impl MaybeInfiniteInt {
220234
match self {
221235
Finite(n) => match n.checked_sub(1) {
222236
Some(m) => Finite(m),
223-
None => bug!(),
237+
None => panic!("Called `MaybeInfiniteInt::minus_one` on 0"),
224238
},
225239
JustAfterMax => Finite(u128::MAX),
226240
x => x,
@@ -233,7 +247,7 @@ impl MaybeInfiniteInt {
233247
Some(m) => Finite(m),
234248
None => JustAfterMax,
235249
},
236-
JustAfterMax => bug!(),
250+
JustAfterMax => panic!("Called `MaybeInfiniteInt::plus_one` on u128::MAX+1"),
237251
x => x,
238252
}
239253
}
@@ -270,7 +284,7 @@ impl IntRange {
270284
}
271285
if lo >= hi {
272286
// This should have been caught earlier by E0030.
273-
bug!("malformed range pattern: {lo:?}..{hi:?}");
287+
panic!("malformed range pattern: {lo:?}..{hi:?}");
274288
}
275289
IntRange { lo, hi }
276290
}
@@ -431,7 +445,7 @@ impl Slice {
431445
let kind = match (array_len, kind) {
432446
// If the middle `..` has length 0, we effectively have a fixed-length pattern.
433447
(Some(len), VarLen(prefix, suffix)) if prefix + suffix == len => FixedLen(len),
434-
(Some(len), VarLen(prefix, suffix)) if prefix + suffix > len => bug!(
448+
(Some(len), VarLen(prefix, suffix)) if prefix + suffix > len => panic!(
435449
"Slice pattern of length {} longer than its array length {len}",
436450
prefix + suffix
437451
),

compiler/rustc_pattern_analysis/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ use crate::rustc::RustcCtxt;
3030
pub trait MatchCx: Sized + Clone + fmt::Debug {
3131
type Ty: Copy + Clone + fmt::Debug; // FIXME: remove Copy
3232
type Span: Clone + Default;
33+
/// The index of an enum variant.
3334
type VariantIdx: Clone + Idx;
35+
/// A string literal
3436
type StrLit: Clone + PartialEq + fmt::Debug;
37+
/// Extra data to store on a match arm.
38+
type ArmData: Copy + Clone + fmt::Debug;
3539

3640
fn is_opaque_ty(ty: Self::Ty) -> bool;
3741
fn is_exhaustive_patterns_feature_on(&self) -> bool;
@@ -60,8 +64,8 @@ pub trait MatchCx: Sized + Clone + fmt::Debug {
6064
pub struct MatchArm<'p, Cx: MatchCx> {
6165
/// The pattern must have been lowered through `check_match::MatchVisitor::lower_pattern`.
6266
pub pat: &'p DeconstructedPat<'p, Cx>,
63-
pub hir_id: HirId,
6467
pub has_guard: bool,
68+
pub arm_data: Cx::ArmData,
6569
}
6670

6771
impl<'p, Cx: MatchCx> Copy for MatchArm<'p, Cx> {}

compiler/rustc_pattern_analysis/src/lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
205205
// usage of the lint.
206206
for arm in arms {
207207
let (lint_level, lint_level_source) =
208-
cx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, arm.hir_id);
208+
cx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, arm.arm_data);
209209
if !matches!(lint_level, rustc_session::lint::Level::Allow) {
210210
let decorator = NonExhaustiveOmittedPatternLintOnArm {
211211
lint_span: lint_level_source.span(),

compiler/rustc_pattern_analysis/src/rustc.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::iter::once;
44
use rustc_arena::{DroplessArena, TypedArena};
55
use rustc_data_structures::captures::Captures;
66
use rustc_hir::def_id::DefId;
7-
use rustc_hir::{HirId, RangeEnd};
7+
use rustc_hir::HirId;
88
use rustc_index::Idx;
99
use rustc_index::IndexVec;
1010
use rustc_middle::middle::stability::EvalResult;
@@ -18,12 +18,13 @@ use rustc_target::abi::{FieldIdx, Integer, VariantIdx, FIRST_VARIANT};
1818
use smallvec::SmallVec;
1919

2020
use crate::constructor::{
21-
IntRange, MaybeInfiniteInt, OpaqueId, Slice, SliceKind, VariantVisibility,
21+
IntRange, MaybeInfiniteInt, OpaqueId, RangeEnd, Slice, SliceKind, VariantVisibility,
2222
};
2323
use crate::MatchCx;
2424

2525
use crate::constructor::Constructor::*;
2626

27+
// Re-export rustc-specific versions of all these types.
2728
pub type Constructor<'p, 'tcx> = crate::constructor::Constructor<RustcCtxt<'p, 'tcx>>;
2829
pub type ConstructorSet<'p, 'tcx> = crate::constructor::ConstructorSet<RustcCtxt<'p, 'tcx>>;
2930
pub type DeconstructedPat<'p, 'tcx> = crate::pat::DeconstructedPat<'p, RustcCtxt<'p, 'tcx>>;
@@ -520,12 +521,16 @@ impl<'p, 'tcx> RustcCtxt<'p, 'tcx> {
520521
}
521522
PatKind::Range(patrange) => {
522523
let PatRange { lo, hi, end, .. } = patrange.as_ref();
524+
let end = match end {
525+
rustc_hir::RangeEnd::Included => RangeEnd::Included,
526+
rustc_hir::RangeEnd::Excluded => RangeEnd::Excluded,
527+
};
523528
let ty = pat.ty;
524529
ctor = match ty.kind() {
525530
ty::Char | ty::Int(_) | ty::Uint(_) => {
526531
let lo = cx.lower_pat_range_bdy(*lo, ty);
527532
let hi = cx.lower_pat_range_bdy(*hi, ty);
528-
IntRange(IntRange::from_range(lo, hi, *end))
533+
IntRange(IntRange::from_range(lo, hi, end))
529534
}
530535
ty::Float(fty) => {
531536
use rustc_apfloat::Float;
@@ -536,13 +541,13 @@ impl<'p, 'tcx> RustcCtxt<'p, 'tcx> {
536541
use rustc_apfloat::ieee::Single;
537542
let lo = lo.map(Single::from_bits).unwrap_or(-Single::INFINITY);
538543
let hi = hi.map(Single::from_bits).unwrap_or(Single::INFINITY);
539-
F32Range(lo, hi, *end)
544+
F32Range(lo, hi, end)
540545
}
541546
ty::FloatTy::F64 => {
542547
use rustc_apfloat::ieee::Double;
543548
let lo = lo.map(Double::from_bits).unwrap_or(-Double::INFINITY);
544549
let hi = hi.map(Double::from_bits).unwrap_or(Double::INFINITY);
545-
F64Range(lo, hi, *end)
550+
F64Range(lo, hi, end)
546551
}
547552
}
548553
}
@@ -634,7 +639,7 @@ impl<'p, 'tcx> RustcCtxt<'p, 'tcx> {
634639
PatKind::Constant { value }
635640
} else {
636641
// We convert to an inclusive range for diagnostics.
637-
let mut end = RangeEnd::Included;
642+
let mut end = rustc_hir::RangeEnd::Included;
638643
let mut lo = cx.hoist_pat_range_bdy(range.lo, ty);
639644
if matches!(lo, PatRangeBoundary::PosInfinity) {
640645
// The only reason to get `PosInfinity` here is the special case where
@@ -648,7 +653,7 @@ impl<'p, 'tcx> RustcCtxt<'p, 'tcx> {
648653
}
649654
let hi = if matches!(range.hi, Finite(0)) {
650655
// The range encodes `..ty::MIN`, so we can't convert it to an inclusive range.
651-
end = RangeEnd::Excluded;
656+
end = rustc_hir::RangeEnd::Excluded;
652657
range.hi
653658
} else {
654659
range.hi.minus_one()
@@ -853,6 +858,7 @@ impl<'p, 'tcx> MatchCx for RustcCtxt<'p, 'tcx> {
853858
type Span = Span;
854859
type VariantIdx = VariantIdx;
855860
type StrLit = Const<'tcx>;
861+
type ArmData = HirId;
856862

857863
fn is_exhaustive_patterns_feature_on(&self) -> bool {
858864
self.tcx.features().exhaustive_patterns

0 commit comments

Comments
 (0)