s/to_pat/to_diagnostic_pat/

This commit is contained in:
Nadrieril 2023-10-21 20:16:10 +02:00
parent a4875ae1e2
commit feb769a5c9
4 changed files with 33 additions and 21 deletions

View File

@ -807,13 +807,19 @@ pub fn new<'p>(
cx: &MatchCheckCtxt<'p, 'tcx>,
witnesses: Vec<WitnessPat<'tcx>>,
) -> 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),
}

View File

@ -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::<Vec<String>>()
.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);

View File

@ -140,8 +140,13 @@ fn from_pat_range_bdy<'tcx>(
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 @@ fn split(
/// 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 @@ pub(super) fn ty(&self) -> Ty<'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

View File

@ -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()