Skip to content

Commit 3a6bc55

Browse files
committed
s/to_pat/to_diagnostic_pat/
1 parent 57cc6aa commit 3a6bc55

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

compiler/rustc_mir_build/src/errors.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -812,13 +812,19 @@ impl<'tcx> Uncovered<'tcx> {
812812
cx: &MatchCheckCtxt<'p, 'tcx>,
813813
witnesses: Vec<WitnessPat<'tcx>>,
814814
) -> Self {
815-
let witness_1 = witnesses.get(0).unwrap().to_pat(cx);
815+
let witness_1 = witnesses.get(0).unwrap().to_diagnostic_pat(cx);
816816
Self {
817817
span,
818818
count: witnesses.len(),
819819
// Substitute dummy values if witnesses is smaller than 3. These will never be read.
820-
witness_2: witnesses.get(1).map(|w| w.to_pat(cx)).unwrap_or_else(|| witness_1.clone()),
821-
witness_3: witnesses.get(2).map(|w| w.to_pat(cx)).unwrap_or_else(|| witness_1.clone()),
820+
witness_2: witnesses
821+
.get(1)
822+
.map(|w| w.to_diagnostic_pat(cx))
823+
.unwrap_or_else(|| witness_1.clone()),
824+
witness_3: witnesses
825+
.get(2)
826+
.map(|w| w.to_diagnostic_pat(cx))
827+
.unwrap_or_else(|| witness_1.clone()),
822828
witness_1,
823829
remainder: witnesses.len().saturating_sub(3),
824830
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ fn non_exhaustive_match<'p, 'tcx>(
760760
pattern = if witnesses.len() < 4 {
761761
witnesses
762762
.iter()
763-
.map(|witness| witness.to_pat(cx).to_string())
763+
.map(|witness| witness.to_diagnostic_pat(cx).to_string())
764764
.collect::<Vec<String>>()
765765
.join(" | ")
766766
} else {
@@ -915,13 +915,13 @@ pub(crate) fn joined_uncovered_patterns<'p, 'tcx>(
915915
witnesses: &[WitnessPat<'tcx>],
916916
) -> String {
917917
const LIMIT: usize = 3;
918-
let pat_to_str = |pat: &WitnessPat<'tcx>| pat.to_pat(cx).to_string();
918+
let pat_to_str = |pat: &WitnessPat<'tcx>| pat.to_diagnostic_pat(cx).to_string();
919919
match witnesses {
920920
[] => bug!(),
921-
[witness] => format!("`{}`", witness.to_pat(cx)),
921+
[witness] => format!("`{}`", witness.to_diagnostic_pat(cx)),
922922
[head @ .., tail] if head.len() < LIMIT => {
923923
let head: Vec<_> = head.iter().map(pat_to_str).collect();
924-
format!("`{}` and `{}`", head.join("`, `"), tail.to_pat(cx))
924+
format!("`{}` and `{}`", head.join("`, `"), tail.to_diagnostic_pat(cx))
925925
}
926926
_ => {
927927
let (head, tail) = witnesses.split_at(LIMIT);

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,13 @@ impl MaybeInfiniteInt {
142142
PatRangeBoundary::PosInfinity => PosInfinity,
143143
}
144144
}
145-
// This could change from finite to infinite if we got `usize::MAX+1` after range splitting.
146-
fn to_pat_range_bdy<'tcx>(self, ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> PatRangeBoundary<'tcx> {
145+
/// Used only for diagnostics.
146+
/// This could change from finite to infinite if we got `usize::MAX+1` after range splitting.
147+
fn to_diagnostic_pat_range_bdy<'tcx>(
148+
self,
149+
ty: Ty<'tcx>,
150+
tcx: TyCtxt<'tcx>,
151+
) -> PatRangeBoundary<'tcx> {
147152
match self {
148153
NegInfinity => PatRangeBoundary::NegInfinity,
149154
Finite(x) => {
@@ -346,25 +351,25 @@ impl IntRange {
346351
/// Whether the range denotes the values before `isize::MIN` or the values after
347352
/// `usize::MAX`/`isize::MAX`.
348353
pub(crate) fn is_beyond_boundaries<'tcx>(&self, ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> bool {
349-
// First check if we are usize/isize to avoid unnecessary `to_pat_range_bdy`.
354+
// First check if we are usize/isize to avoid unnecessary `to_diagnostic_pat_range_bdy`.
350355
ty.is_ptr_sized_integral() && !tcx.features().precise_pointer_size_matching && {
351-
let lo = self.lo.to_pat_range_bdy(ty, tcx);
352-
let hi = self.hi.to_pat_range_bdy(ty, tcx);
356+
let lo = self.lo.to_diagnostic_pat_range_bdy(ty, tcx);
357+
let hi = self.hi.to_diagnostic_pat_range_bdy(ty, tcx);
353358
matches!(lo, PatRangeBoundary::PosInfinity)
354359
|| matches!(hi, PatRangeBoundary::NegInfinity)
355360
}
356361
}
357362
/// Only used for displaying the range.
358-
fn to_pat<'tcx>(&self, ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Pat<'tcx> {
363+
fn to_diagnostic_pat<'tcx>(&self, ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Pat<'tcx> {
359364
let kind = if matches!((self.lo, self.hi), (NegInfinity, PosInfinity)) {
360365
PatKind::Wild
361366
} else if self.is_singleton() {
362-
let lo = self.lo.to_pat_range_bdy(ty, tcx);
367+
let lo = self.lo.to_diagnostic_pat_range_bdy(ty, tcx);
363368
let value = lo.as_finite().unwrap();
364369
PatKind::Constant { value }
365370
} else {
366-
let mut lo = self.lo.to_pat_range_bdy(ty, tcx);
367-
let mut hi = self.hi.to_pat_range_bdy(ty, tcx);
371+
let mut lo = self.lo.to_diagnostic_pat_range_bdy(ty, tcx);
372+
let mut hi = self.hi.to_diagnostic_pat_range_bdy(ty, tcx);
368373
let end = if hi.is_finite() {
369374
RangeEnd::Included
370375
} else {
@@ -421,7 +426,7 @@ impl IntRange {
421426
.filter_map(|pat| Some((pat.ctor().as_int_range()?, pat.span())))
422427
.filter(|(range, _)| self.suspicious_intersection(range))
423428
.map(|(range, span)| Overlap {
424-
range: self.intersection(&range).unwrap().to_pat(pcx.ty, pcx.cx.tcx),
429+
range: self.intersection(&range).unwrap().to_diagnostic_pat(pcx.ty, pcx.cx.tcx),
425430
span,
426431
})
427432
.collect();
@@ -1867,13 +1872,14 @@ impl<'tcx> WitnessPat<'tcx> {
18671872
self.ty
18681873
}
18691874

1870-
/// Convert back to a `thir::Pat` for diagnostic purposes.
1871-
pub(crate) fn to_pat(&self, cx: &MatchCheckCtxt<'_, 'tcx>) -> Pat<'tcx> {
1875+
/// Convert back to a `thir::Pat` for diagnostic purposes. This panics for patterns that don't
1876+
/// appear in diagnostics, like float ranges.
1877+
pub(crate) fn to_diagnostic_pat(&self, cx: &MatchCheckCtxt<'_, 'tcx>) -> Pat<'tcx> {
18721878
let is_wildcard = |pat: &Pat<'_>| matches!(pat.kind, PatKind::Wild);
1873-
let mut subpatterns = self.iter_fields().map(|p| Box::new(p.to_pat(cx)));
1879+
let mut subpatterns = self.iter_fields().map(|p| Box::new(p.to_diagnostic_pat(cx)));
18741880
let kind = match &self.ctor {
18751881
Bool(b) => PatKind::Constant { value: mir::Const::from_bool(cx.tcx, *b) },
1876-
IntRange(range) => return range.to_pat(self.ty, cx.tcx),
1882+
IntRange(range) => return range.to_diagnostic_pat(self.ty, cx.tcx),
18771883
Single | Variant(_) => match self.ty.kind() {
18781884
ty::Tuple(..) => PatKind::Leaf {
18791885
subpatterns: subpatterns

0 commit comments

Comments
 (0)