From feb769a5c9e3954a4dd0a83d321b2996a1ac5667 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 21 Oct 2023 20:16:10 +0200 Subject: [PATCH] s/to_pat/to_diagnostic_pat/ --- compiler/rustc_mir_build/src/errors.rs | 12 +++++-- .../src/thir/pattern/check_match.rs | 8 ++--- .../src/thir/pattern/deconstruct_pat.rs | 32 +++++++++++-------- .../src/thir/pattern/usefulness.rs | 2 +- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index 730670a8369cc..5bfce3ab51008 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -807,13 +807,19 @@ impl<'tcx> Uncovered<'tcx> { cx: &MatchCheckCtxt<'p, 'tcx>, witnesses: Vec>, ) -> Self { - let witness_1 = witnesses.get(0).unwrap().to_pat(cx); + let witness_1 = witnesses.get(0).unwrap().to_diagnostic_pat(cx); Self { span, count: witnesses.len(), // Substitute dummy values if witnesses is smaller than 3. These will never be read. - witness_2: witnesses.get(1).map(|w| w.to_pat(cx)).unwrap_or_else(|| witness_1.clone()), - witness_3: witnesses.get(2).map(|w| w.to_pat(cx)).unwrap_or_else(|| witness_1.clone()), + witness_2: witnesses + .get(1) + .map(|w| w.to_diagnostic_pat(cx)) + .unwrap_or_else(|| witness_1.clone()), + witness_3: witnesses + .get(2) + .map(|w| w.to_diagnostic_pat(cx)) + .unwrap_or_else(|| witness_1.clone()), witness_1, remainder: witnesses.len().saturating_sub(3), } diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index f26f80105ee05..933653e708e94 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -760,7 +760,7 @@ fn non_exhaustive_match<'p, 'tcx>( pattern = if witnesses.len() < 4 { witnesses .iter() - .map(|witness| witness.to_pat(cx).to_string()) + .map(|witness| witness.to_diagnostic_pat(cx).to_string()) .collect::>() .join(" | ") } else { @@ -915,13 +915,13 @@ pub(crate) fn joined_uncovered_patterns<'p, 'tcx>( witnesses: &[WitnessPat<'tcx>], ) -> String { const LIMIT: usize = 3; - let pat_to_str = |pat: &WitnessPat<'tcx>| pat.to_pat(cx).to_string(); + let pat_to_str = |pat: &WitnessPat<'tcx>| pat.to_diagnostic_pat(cx).to_string(); match witnesses { [] => bug!(), - [witness] => format!("`{}`", witness.to_pat(cx)), + [witness] => format!("`{}`", witness.to_diagnostic_pat(cx)), [head @ .., tail] if head.len() < LIMIT => { let head: Vec<_> = head.iter().map(pat_to_str).collect(); - format!("`{}` and `{}`", head.join("`, `"), tail.to_pat(cx)) + format!("`{}` and `{}`", head.join("`, `"), tail.to_diagnostic_pat(cx)) } _ => { let (head, tail) = witnesses.split_at(LIMIT); diff --git a/compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs index 5e9179896f791..8d9998d17ed5b 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs @@ -140,8 +140,13 @@ impl MaybeInfiniteInt { PatRangeBoundary::PosInfinity => PosInfinity, } } - // This could change from finite to infinite if we got `usize::MAX+1` after range splitting. - fn to_pat_range_bdy<'tcx>(self, ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> PatRangeBoundary<'tcx> { + /// Used only for diagnostics. + /// This could change from finite to infinite if we got `usize::MAX+1` after range splitting. + fn to_diagnostic_pat_range_bdy<'tcx>( + self, + ty: Ty<'tcx>, + tcx: TyCtxt<'tcx>, + ) -> PatRangeBoundary<'tcx> { match self { NegInfinity => PatRangeBoundary::NegInfinity, Finite(x) => { @@ -326,25 +331,25 @@ impl IntRange { /// Whether the range denotes the values before `isize::MIN` or the values after /// `usize::MAX`/`isize::MAX`. pub(crate) fn is_beyond_boundaries<'tcx>(&self, ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> bool { - // First check if we are usize/isize to avoid unnecessary `to_pat_range_bdy`. + // First check if we are usize/isize to avoid unnecessary `to_diagnostic_pat_range_bdy`. ty.is_ptr_sized_integral() && !tcx.features().precise_pointer_size_matching && { - let lo = self.lo.to_pat_range_bdy(ty, tcx); - let hi = self.hi.to_pat_range_bdy(ty, tcx); + let lo = self.lo.to_diagnostic_pat_range_bdy(ty, tcx); + let hi = self.hi.to_diagnostic_pat_range_bdy(ty, tcx); matches!(lo, PatRangeBoundary::PosInfinity) || matches!(hi, PatRangeBoundary::NegInfinity) } } /// Only used for displaying the range. - pub(super) fn to_pat<'tcx>(&self, ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Pat<'tcx> { + pub(super) fn to_diagnostic_pat<'tcx>(&self, ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Pat<'tcx> { let kind = if matches!((self.lo, self.hi), (NegInfinity, PosInfinity)) { PatKind::Wild } else if self.is_singleton() { - let lo = self.lo.to_pat_range_bdy(ty, tcx); + let lo = self.lo.to_diagnostic_pat_range_bdy(ty, tcx); let value = lo.as_finite().unwrap(); PatKind::Constant { value } } else { - let mut lo = self.lo.to_pat_range_bdy(ty, tcx); - let mut hi = self.hi.to_pat_range_bdy(ty, tcx); + let mut lo = self.lo.to_diagnostic_pat_range_bdy(ty, tcx); + let mut hi = self.hi.to_diagnostic_pat_range_bdy(ty, tcx); let end = if hi.is_finite() { RangeEnd::Included } else { @@ -1803,13 +1808,14 @@ impl<'tcx> WitnessPat<'tcx> { self.ty } - /// Convert back to a `thir::Pat` for diagnostic purposes. - pub(crate) fn to_pat(&self, cx: &MatchCheckCtxt<'_, 'tcx>) -> Pat<'tcx> { + /// Convert back to a `thir::Pat` for diagnostic purposes. This panics for patterns that don't + /// appear in diagnostics, like float ranges. + pub(crate) fn to_diagnostic_pat(&self, cx: &MatchCheckCtxt<'_, 'tcx>) -> Pat<'tcx> { let is_wildcard = |pat: &Pat<'_>| matches!(pat.kind, PatKind::Wild); - let mut subpatterns = self.iter_fields().map(|p| Box::new(p.to_pat(cx))); + let mut subpatterns = self.iter_fields().map(|p| Box::new(p.to_diagnostic_pat(cx))); let kind = match &self.ctor { Bool(b) => PatKind::Constant { value: mir::Const::from_bool(cx.tcx, *b) }, - IntRange(range) => return range.to_pat(self.ty, cx.tcx), + IntRange(range) => return range.to_diagnostic_pat(self.ty, cx.tcx), Single | Variant(_) => match self.ty.kind() { ty::Tuple(..) => PatKind::Leaf { subpatterns: subpatterns diff --git a/compiler/rustc_mir_build/src/thir/pattern/usefulness.rs b/compiler/rustc_mir_build/src/thir/pattern/usefulness.rs index 3a210f2587ece..c0920a3ef5a5b 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/usefulness.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/usefulness.rs @@ -1014,7 +1014,7 @@ fn lint_overlapping_range_endpoints<'p, 'tcx>( if IntRange::is_integral(ty) { let emit_lint = |overlap: &IntRange, this_span: Span, overlapped_spans: &[Span]| { - let overlap_as_pat = overlap.to_pat(ty, cx.tcx); + let overlap_as_pat = overlap.to_diagnostic_pat(ty, cx.tcx); let overlaps: Vec<_> = overlapped_spans .iter() .copied()