Skip to content

Commit fc55129

Browse files
committed
Remove print::Pat entirely, replacing it with String
1 parent bfe88a3 commit fc55129

File tree

2 files changed

+35
-69
lines changed

2 files changed

+35
-69
lines changed

Diff for: compiler/rustc_pattern_analysis/src/rustc.rs

+28-42
Original file line numberDiff line numberDiff line change
@@ -774,17 +774,16 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
774774
}
775775
}
776776

777-
/// Convert to a [`print::Pat`] for diagnostic purposes.
778-
fn hoist_pat_range(&self, range: &IntRange, ty: RevealedTy<'tcx>) -> print::Pat<'tcx> {
779-
use print::{Pat, PatKind};
777+
/// Prints an [`IntRange`] to a string for diagnostic purposes.
778+
fn print_pat_range(&self, range: &IntRange, ty: RevealedTy<'tcx>) -> String {
780779
use MaybeInfiniteInt::*;
781780
let cx = self;
782-
let kind = if matches!((range.lo, range.hi), (NegInfinity, PosInfinity)) {
783-
PatKind::Print("_".to_string())
781+
if matches!((range.lo, range.hi), (NegInfinity, PosInfinity)) {
782+
"_".to_string()
784783
} else if range.is_singleton() {
785784
let lo = cx.hoist_pat_range_bdy(range.lo, ty);
786785
let value = lo.as_finite().unwrap();
787-
PatKind::Print(value.to_string())
786+
value.to_string()
788787
} else {
789788
// We convert to an inclusive range for diagnostics.
790789
let mut end = rustc_hir::RangeEnd::Included;
@@ -807,33 +806,24 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
807806
range.hi
808807
};
809808
let hi = cx.hoist_pat_range_bdy(hi, ty);
810-
PatKind::Print(PatRange { lo, hi, end, ty: ty.inner() }.to_string())
811-
};
812-
813-
Pat { ty: ty.inner(), kind }
809+
PatRange { lo, hi, end, ty: ty.inner() }.to_string()
810+
}
814811
}
815812

816813
/// Prints a [`WitnessPat`] to an owned string, for diagnostic purposes.
814+
///
815+
/// This panics for patterns that don't appear in diagnostics, like float ranges.
817816
pub fn print_witness_pat(&self, pat: &WitnessPat<'p, 'tcx>) -> String {
818-
// This works by converting the witness pattern to a `print::Pat`
819-
// and then printing that, but callers don't need to know that.
820-
self.hoist_witness_pat(pat).to_string()
821-
}
822-
823-
/// Convert to a [`print::Pat`] for diagnostic purposes. This panics for patterns that don't
824-
/// appear in diagnostics, like float ranges.
825-
fn hoist_witness_pat(&self, pat: &WitnessPat<'p, 'tcx>) -> print::Pat<'tcx> {
826-
use print::{FieldPat, Pat, PatKind};
827817
let cx = self;
828-
let hoist = |p| Box::new(cx.hoist_witness_pat(p));
829-
let kind = match pat.ctor() {
830-
Bool(b) => PatKind::Print(b.to_string()),
831-
Str(s) => PatKind::Print(s.to_string()),
832-
IntRange(range) => return self.hoist_pat_range(range, *pat.ty()),
818+
let print = |p| cx.print_witness_pat(p);
819+
match pat.ctor() {
820+
Bool(b) => b.to_string(),
821+
Str(s) => s.to_string(),
822+
IntRange(range) => return self.print_pat_range(range, *pat.ty()),
833823
Struct if pat.ty().is_box() => {
834824
// Outside of the `alloc` crate, the only way to create a struct pattern
835825
// of type `Box` is to use a `box` pattern via #[feature(box_patterns)].
836-
PatKind::Print(format!("box {}", hoist(&pat.fields[0])))
826+
format!("box {}", print(&pat.fields[0]))
837827
}
838828
Struct | Variant(_) | UnionField => {
839829
let enum_info = match *pat.ty().kind() {
@@ -848,9 +838,9 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
848838
let subpatterns = pat
849839
.iter_fields()
850840
.enumerate()
851-
.map(|(i, pat)| FieldPat {
841+
.map(|(i, pat)| print::FieldPat {
852842
field: FieldIdx::new(i),
853-
pattern: hoist(pat),
843+
pattern: print(pat),
854844
is_wildcard: would_print_as_wildcard(cx.tcx, pat),
855845
})
856846
.collect::<Vec<_>>();
@@ -864,12 +854,12 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
864854
&subpatterns,
865855
)
866856
.unwrap();
867-
PatKind::Print(s)
857+
s
868858
}
869859
Ref => {
870860
let mut s = String::new();
871-
print::write_ref_like(&mut s, pat.ty().inner(), &hoist(&pat.fields[0])).unwrap();
872-
PatKind::Print(s)
861+
print::write_ref_like(&mut s, pat.ty().inner(), &print(&pat.fields[0])).unwrap();
862+
s
873863
}
874864
Slice(slice) => {
875865
let (prefix_len, has_dot_dot) = match slice.kind {
@@ -897,27 +887,23 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
897887
}
898888
}
899889

900-
let prefix = prefix.iter().map(hoist).collect::<Vec<_>>();
901-
let suffix = suffix.iter().map(hoist).collect::<Vec<_>>();
890+
let prefix = prefix.iter().map(print).collect::<Vec<_>>();
891+
let suffix = suffix.iter().map(print).collect::<Vec<_>>();
902892

903893
let mut s = String::new();
904894
print::write_slice_like(&mut s, &prefix, has_dot_dot, &suffix).unwrap();
905-
PatKind::Print(s)
906-
}
907-
Never if self.tcx.features().never_patterns => PatKind::Print("!".to_string()),
908-
Never | Wildcard | NonExhaustive | Hidden | PrivateUninhabited => {
909-
PatKind::Print("_".to_string())
895+
s
910896
}
897+
Never if self.tcx.features().never_patterns => "!".to_string(),
898+
Never | Wildcard | NonExhaustive | Hidden | PrivateUninhabited => "_".to_string(),
911899
Missing { .. } => bug!(
912900
"trying to convert a `Missing` constructor into a `Pat`; this is probably a bug,
913901
`Missing` should have been processed in `apply_constructors`"
914902
),
915903
F16Range(..) | F32Range(..) | F64Range(..) | F128Range(..) | Opaque(..) | Or => {
916904
bug!("can't convert to pattern: {:?}", pat)
917905
}
918-
};
919-
920-
Pat { ty: pat.ty().inner(), kind }
906+
}
921907
}
922908
}
923909

@@ -993,7 +979,7 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
993979
overlaps_on: IntRange,
994980
overlaps_with: &[&crate::pat::DeconstructedPat<Self>],
995981
) {
996-
let overlap_as_pat = self.hoist_pat_range(&overlaps_on, *pat.ty());
982+
let overlap_as_pat = self.print_pat_range(&overlaps_on, *pat.ty());
997983
let overlaps: Vec<_> = overlaps_with
998984
.iter()
999985
.map(|pat| pat.data().span)
@@ -1033,7 +1019,7 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
10331019
suggested_range.end = rustc_hir::RangeEnd::Included;
10341020
suggested_range.to_string()
10351021
};
1036-
let gap_as_pat = self.hoist_pat_range(&gap, *pat.ty());
1022+
let gap_as_pat = self.print_pat_range(&gap, *pat.ty());
10371023
if gapped_with.is_empty() {
10381024
// If `gapped_with` is empty, `gap == T::MAX`.
10391025
self.tcx.emit_node_span_lint(

Diff for: compiler/rustc_pattern_analysis/src/rustc/print.rs

+7-27
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,12 @@ use rustc_span::sym;
1717
use rustc_target::abi::{FieldIdx, VariantIdx};
1818

1919
#[derive(Clone, Debug)]
20-
pub(crate) struct FieldPat<'tcx> {
20+
pub(crate) struct FieldPat {
2121
pub(crate) field: FieldIdx,
22-
pub(crate) pattern: Box<Pat<'tcx>>,
22+
pub(crate) pattern: String,
2323
pub(crate) is_wildcard: bool,
2424
}
2525

26-
#[derive(Clone, Debug)]
27-
pub(crate) struct Pat<'tcx> {
28-
#[allow(dead_code)]
29-
pub(crate) ty: Ty<'tcx>,
30-
pub(crate) kind: PatKind,
31-
}
32-
33-
#[derive(Clone, Debug)]
34-
pub(crate) enum PatKind {
35-
Print(String),
36-
}
37-
38-
impl<'tcx> fmt::Display for Pat<'tcx> {
39-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40-
match self.kind {
41-
PatKind::Print(ref string) => write!(f, "{string}"),
42-
}
43-
}
44-
}
45-
4626
/// Returns a closure that will return `""` when called the first time,
4727
/// and then return `", "` when called any subsequent times.
4828
/// Useful for printing comma-separated lists.
@@ -69,7 +49,7 @@ pub(crate) fn write_struct_like<'tcx>(
6949
tcx: TyCtxt<'_>,
7050
ty: Ty<'tcx>,
7151
enum_info: &EnumInfo<'tcx>,
72-
subpatterns: &[FieldPat<'tcx>],
52+
subpatterns: &[FieldPat],
7353
) -> fmt::Result {
7454
let variant_and_name = match *enum_info {
7555
EnumInfo::Enum { adt_def, variant_index } => {
@@ -148,7 +128,7 @@ pub(crate) fn write_struct_like<'tcx>(
148128
pub(crate) fn write_ref_like<'tcx>(
149129
f: &mut impl fmt::Write,
150130
ty: Ty<'tcx>,
151-
subpattern: &Pat<'tcx>,
131+
subpattern: &str,
152132
) -> fmt::Result {
153133
match ty.kind() {
154134
ty::Ref(_, _, mutbl) => {
@@ -159,11 +139,11 @@ pub(crate) fn write_ref_like<'tcx>(
159139
write!(f, "{subpattern}")
160140
}
161141

162-
pub(crate) fn write_slice_like<'tcx>(
142+
pub(crate) fn write_slice_like(
163143
f: &mut impl fmt::Write,
164-
prefix: &[Box<Pat<'tcx>>],
144+
prefix: &[String],
165145
has_dot_dot: bool,
166-
suffix: &[Box<Pat<'tcx>>],
146+
suffix: &[String],
167147
) -> fmt::Result {
168148
let mut start_or_comma = start_or_comma();
169149
write!(f, "[")?;

0 commit comments

Comments
 (0)