Skip to content

Commit db05b0f

Browse files
committed
Encapsulate the printing of WitnessPat
This hides the fact that we print `WitnessPat` by converting it to `thir::Pat` and then printing that.
1 parent e1fc4a9 commit db05b0f

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ fn report_non_exhaustive_match<'p, 'tcx>(
10771077
let suggested_arm = if suggest_the_witnesses {
10781078
let pattern = witnesses
10791079
.iter()
1080-
.map(|witness| cx.hoist_witness_pat(witness).to_string())
1080+
.map(|witness| cx.print_witness_pat(witness))
10811081
.collect::<Vec<String>>()
10821082
.join(" | ");
10831083
if witnesses.iter().all(|p| p.is_never_pattern()) && cx.tcx.features().never_patterns {
@@ -1195,13 +1195,13 @@ fn joined_uncovered_patterns<'p, 'tcx>(
11951195
witnesses: &[WitnessPat<'p, 'tcx>],
11961196
) -> String {
11971197
const LIMIT: usize = 3;
1198-
let pat_to_str = |pat: &WitnessPat<'p, 'tcx>| cx.hoist_witness_pat(pat).to_string();
1198+
let pat_to_str = |pat: &WitnessPat<'p, 'tcx>| cx.print_witness_pat(pat);
11991199
match witnesses {
12001200
[] => bug!(),
1201-
[witness] => format!("`{}`", cx.hoist_witness_pat(witness)),
1201+
[witness] => format!("`{}`", cx.print_witness_pat(witness)),
12021202
[head @ .., tail] if head.len() < LIMIT => {
12031203
let head: Vec<_> = head.iter().map(pat_to_str).collect();
1204-
format!("`{}` and `{}`", head.join("`, `"), cx.hoist_witness_pat(tail))
1204+
format!("`{}` and `{}`", head.join("`, `"), cx.print_witness_pat(tail))
12051205
}
12061206
_ => {
12071207
let (head, tail) = witnesses.split_at(LIMIT);

compiler/rustc_pattern_analysis/src/errors.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,13 @@ impl Uncovered {
2626
where
2727
'tcx: 'p,
2828
{
29-
let witness_1 = cx.hoist_witness_pat(witnesses.get(0).unwrap()).to_string();
29+
let witness_1 = cx.print_witness_pat(witnesses.get(0).unwrap());
3030
Self {
3131
span,
3232
count: witnesses.len(),
3333
// Substitute dummy values if witnesses is smaller than 3. These will never be read.
34-
witness_2: witnesses
35-
.get(1)
36-
.map(|w| cx.hoist_witness_pat(w).to_string())
37-
.unwrap_or_default(),
38-
witness_3: witnesses
39-
.get(2)
40-
.map(|w| cx.hoist_witness_pat(w).to_string())
41-
.unwrap_or_default(),
34+
witness_2: witnesses.get(1).map(|w| cx.print_witness_pat(w)).unwrap_or_default(),
35+
witness_3: witnesses.get(2).map(|w| cx.print_witness_pat(w)).unwrap_or_default(),
4236
witness_1,
4337
remainder: witnesses.len().saturating_sub(3),
4438
}

compiler/rustc_pattern_analysis/src/rustc.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
744744
/// Note: it is possible to get `isize/usize::MAX+1` here, as explained in the doc for
745745
/// [`IntRange::split`]. This cannot be represented as a `Const`, so we represent it with
746746
/// `PosInfinity`.
747-
pub(crate) fn hoist_pat_range_bdy(
747+
fn hoist_pat_range_bdy(
748748
&self,
749749
miint: MaybeInfiniteInt,
750750
ty: RevealedTy<'tcx>,
@@ -775,7 +775,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
775775
}
776776

777777
/// Convert back to a `thir::Pat` for diagnostic purposes.
778-
pub(crate) fn hoist_pat_range(&self, range: &IntRange, ty: RevealedTy<'tcx>) -> Pat<'tcx> {
778+
fn hoist_pat_range(&self, range: &IntRange, ty: RevealedTy<'tcx>) -> Pat<'tcx> {
779779
use MaybeInfiniteInt::*;
780780
let cx = self;
781781
let kind = if matches!((range.lo, range.hi), (NegInfinity, PosInfinity)) {
@@ -811,9 +811,17 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
811811

812812
Pat { ty: ty.inner(), span: DUMMY_SP, kind }
813813
}
814+
815+
/// Prints a [`WitnessPat`] to an owned string, for diagnostic purposes.
816+
pub fn print_witness_pat(&self, pat: &WitnessPat<'p, 'tcx>) -> String {
817+
// This works by converting the witness pattern back to a `thir::Pat`
818+
// and then printing that, but callers don't need to know that.
819+
self.hoist_witness_pat(pat).to_string()
820+
}
821+
814822
/// Convert back to a `thir::Pat` for diagnostic purposes. This panics for patterns that don't
815823
/// appear in diagnostics, like float ranges.
816-
pub fn hoist_witness_pat(&self, pat: &WitnessPat<'p, 'tcx>) -> Pat<'tcx> {
824+
fn hoist_witness_pat(&self, pat: &WitnessPat<'p, 'tcx>) -> Pat<'tcx> {
817825
let cx = self;
818826
let is_wildcard = |pat: &Pat<'_>| matches!(pat.kind, PatKind::Wild);
819827
let mut subpatterns = pat.iter_fields().map(|p| Box::new(cx.hoist_witness_pat(p)));

0 commit comments

Comments
 (0)