Skip to content

Commit

Permalink
s/to_pat/to_diagnostic_pat/
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Oct 27, 2023
1 parent a4875ae commit feb769a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
12 changes: 9 additions & 3 deletions compiler/rustc_mir_build/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,13 +807,19 @@ impl<'tcx> Uncovered<'tcx> {
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),
}
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
32 changes: 19 additions & 13 deletions compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/thir/pattern/usefulness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit feb769a

Please sign in to comment.