Skip to content

Commit e4487ad

Browse files
committed
Improve the WitnessPat: Debug impl
1 parent 3dfd0fd commit e4487ad

File tree

4 files changed

+89
-76
lines changed

4 files changed

+89
-76
lines changed

compiler/rustc_pattern_analysis/src/constructor.rs

+75
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,81 @@ impl<Cx: PatCx> Constructor<Cx> {
819819
}
820820
})
821821
}
822+
823+
pub(crate) fn fmt_fields(
824+
&self,
825+
f: &mut fmt::Formatter<'_>,
826+
ty: &Cx::Ty,
827+
mut fields: impl Iterator<Item = impl fmt::Debug>,
828+
) -> fmt::Result {
829+
let mut first = true;
830+
let mut start_or_continue = |s| {
831+
if first {
832+
first = false;
833+
""
834+
} else {
835+
s
836+
}
837+
};
838+
let mut start_or_comma = || start_or_continue(", ");
839+
840+
match self {
841+
Struct | Variant(_) | UnionField => {
842+
Cx::write_variant_name(f, self, ty)?;
843+
// Without `cx`, we can't know which field corresponds to which, so we can't
844+
// get the names of the fields. Instead we just display everything as a tuple
845+
// struct, which should be good enough.
846+
write!(f, "(")?;
847+
for p in fields {
848+
write!(f, "{}{:?}", start_or_comma(), p)?;
849+
}
850+
write!(f, ")")?;
851+
}
852+
// Note: given the expansion of `&str` patterns done in `expand_pattern`, we should
853+
// be careful to detect strings here. However a string literal pattern will never
854+
// be reported as a non-exhaustiveness witness, so we can ignore this issue.
855+
Ref => {
856+
write!(f, "&{:?}", &fields.next().unwrap())?;
857+
}
858+
Slice(slice) => {
859+
write!(f, "[")?;
860+
match slice.kind {
861+
SliceKind::FixedLen(_) => {
862+
for p in fields {
863+
write!(f, "{}{:?}", start_or_comma(), p)?;
864+
}
865+
}
866+
SliceKind::VarLen(prefix_len, _) => {
867+
for p in fields.by_ref().take(prefix_len) {
868+
write!(f, "{}{:?}", start_or_comma(), p)?;
869+
}
870+
write!(f, "{}..", start_or_comma())?;
871+
for p in fields {
872+
write!(f, "{}{:?}", start_or_comma(), p)?;
873+
}
874+
}
875+
}
876+
write!(f, "]")?;
877+
}
878+
Bool(b) => write!(f, "{b}")?,
879+
// Best-effort, will render signed ranges incorrectly
880+
IntRange(range) => write!(f, "{range:?}")?,
881+
F32Range(lo, hi, end) => write!(f, "{lo}{end}{hi}")?,
882+
F64Range(lo, hi, end) => write!(f, "{lo}{end}{hi}")?,
883+
Str(value) => write!(f, "{value:?}")?,
884+
Opaque(..) => write!(f, "<constant pattern>")?,
885+
Or => {
886+
for pat in fields {
887+
write!(f, "{}{:?}", start_or_continue(" | "), pat)?;
888+
}
889+
}
890+
Never => write!(f, "!")?,
891+
Wildcard | Missing | NonExhaustive | Hidden | PrivateUninhabited => {
892+
write!(f, "_ : {:?}", ty)?
893+
}
894+
}
895+
Ok(())
896+
}
822897
}
823898

824899
#[derive(Debug, Clone, Copy)]

compiler/rustc_pattern_analysis/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ pub trait PatCx: Sized + fmt::Debug {
120120
/// `DeconstructedPat`. Only invoqued when `pat.ctor()` is `Struct | Variant(_) | UnionField`.
121121
fn write_variant_name(
122122
f: &mut fmt::Formatter<'_>,
123-
pat: &crate::pat::DeconstructedPat<Self>,
123+
ctor: &crate::constructor::Constructor<Self>,
124+
ty: &Self::Ty,
124125
) -> fmt::Result;
125126

126127
/// Raise a bug.

compiler/rustc_pattern_analysis/src/pat.rs

+8-72
Original file line numberDiff line numberDiff line change
@@ -138,81 +138,11 @@ impl<Cx: PatCx> DeconstructedPat<Cx> {
138138
/// This is best effort and not good enough for a `Display` impl.
139139
impl<Cx: PatCx> fmt::Debug for DeconstructedPat<Cx> {
140140
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141-
let pat = self;
142-
let mut first = true;
143-
let mut start_or_continue = |s| {
144-
if first {
145-
first = false;
146-
""
147-
} else {
148-
s
149-
}
150-
};
151-
let mut start_or_comma = || start_or_continue(", ");
152-
153141
let mut fields: Vec<_> = (0..self.arity).map(|_| PatOrWild::Wild).collect();
154142
for ipat in self.iter_fields() {
155143
fields[ipat.idx] = PatOrWild::Pat(&ipat.pat);
156144
}
157-
158-
match pat.ctor() {
159-
Struct | Variant(_) | UnionField => {
160-
Cx::write_variant_name(f, pat)?;
161-
// Without `cx`, we can't know which field corresponds to which, so we can't
162-
// get the names of the fields. Instead we just display everything as a tuple
163-
// struct, which should be good enough.
164-
write!(f, "(")?;
165-
for p in fields {
166-
write!(f, "{}", start_or_comma())?;
167-
write!(f, "{p:?}")?;
168-
}
169-
write!(f, ")")
170-
}
171-
// Note: given the expansion of `&str` patterns done in `expand_pattern`, we should
172-
// be careful to detect strings here. However a string literal pattern will never
173-
// be reported as a non-exhaustiveness witness, so we can ignore this issue.
174-
Ref => {
175-
write!(f, "&{:?}", &fields[0])
176-
}
177-
Slice(slice) => {
178-
write!(f, "[")?;
179-
match slice.kind {
180-
SliceKind::FixedLen(_) => {
181-
for p in fields {
182-
write!(f, "{}{:?}", start_or_comma(), p)?;
183-
}
184-
}
185-
SliceKind::VarLen(prefix_len, _) => {
186-
for p in &fields[..prefix_len] {
187-
write!(f, "{}{:?}", start_or_comma(), p)?;
188-
}
189-
write!(f, "{}", start_or_comma())?;
190-
write!(f, "..")?;
191-
for p in &fields[prefix_len..] {
192-
write!(f, "{}{:?}", start_or_comma(), p)?;
193-
}
194-
}
195-
}
196-
write!(f, "]")
197-
}
198-
Bool(b) => write!(f, "{b}"),
199-
// Best-effort, will render signed ranges incorrectly
200-
IntRange(range) => write!(f, "{range:?}"),
201-
F32Range(lo, hi, end) => write!(f, "{lo}{end}{hi}"),
202-
F64Range(lo, hi, end) => write!(f, "{lo}{end}{hi}"),
203-
Str(value) => write!(f, "{value:?}"),
204-
Opaque(..) => write!(f, "<constant pattern>"),
205-
Or => {
206-
for pat in fields {
207-
write!(f, "{}{:?}", start_or_continue(" | "), pat)?;
208-
}
209-
Ok(())
210-
}
211-
Never => write!(f, "!"),
212-
Wildcard | Missing | NonExhaustive | Hidden | PrivateUninhabited => {
213-
write!(f, "_ : {:?}", pat.ty())
214-
}
215-
}
145+
self.ctor().fmt_fields(f, self.ty(), fields.into_iter())
216146
}
217147
}
218148

@@ -295,7 +225,6 @@ impl<'p, Cx: PatCx> fmt::Debug for PatOrWild<'p, Cx> {
295225

296226
/// Same idea as `DeconstructedPat`, except this is a fictitious pattern built up for diagnostics
297227
/// purposes. As such they don't use interning and can be cloned.
298-
#[derive(Debug)]
299228
pub struct WitnessPat<Cx: PatCx> {
300229
ctor: Constructor<Cx>,
301230
pub(crate) fields: Vec<WitnessPat<Cx>>,
@@ -353,3 +282,10 @@ impl<Cx: PatCx> WitnessPat<Cx> {
353282
self.fields.iter()
354283
}
355284
}
285+
286+
/// This is best effort and not good enough for a `Display` impl.
287+
impl<Cx: PatCx> fmt::Debug for WitnessPat<Cx> {
288+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
289+
self.ctor().fmt_fields(f, self.ty(), self.fields.iter())
290+
}
291+
}

compiler/rustc_pattern_analysis/src/rustc.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -874,13 +874,14 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
874874

875875
fn write_variant_name(
876876
f: &mut fmt::Formatter<'_>,
877-
pat: &crate::pat::DeconstructedPat<Self>,
877+
ctor: &crate::constructor::Constructor<Self>,
878+
ty: &Self::Ty,
878879
) -> fmt::Result {
879-
if let ty::Adt(adt, _) = pat.ty().kind() {
880+
if let ty::Adt(adt, _) = ty.kind() {
880881
if adt.is_box() {
881882
write!(f, "Box")?
882883
} else {
883-
let variant = adt.variant(Self::variant_index_for_adt(pat.ctor(), *adt));
884+
let variant = adt.variant(Self::variant_index_for_adt(ctor, *adt));
884885
write!(f, "{}", variant.name)?;
885886
}
886887
}

0 commit comments

Comments
 (0)