Skip to content

Commit fb6278e

Browse files
committed
Reduce intermediate allocations while printing witnesses
1 parent 4ff9bed commit fb6278e

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

crates/hir-ty/src/diagnostics/expr.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! through the body using inference results: mismatched arg counts, missing
33
//! fields, etc.
44
5+
use std::fmt;
56
use std::sync::Arc;
67

78
use hir_def::{path::path, resolver::HasResolver, AdtId, AssocItemId, DefWithBodyId, HasModule};
@@ -378,32 +379,34 @@ fn missing_match_arms<'p>(
378379
witnesses: Vec<DeconstructedPat<'p>>,
379380
arms: &[MatchArm],
380381
) -> String {
382+
struct DisplayWitness<'a, 'p>(&'a DeconstructedPat<'p>, &'a MatchCheckCtx<'a, 'p>);
383+
impl<'a, 'p> fmt::Display for DisplayWitness<'a, 'p> {
384+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
385+
let DisplayWitness(witness, cx) = *self;
386+
let pat = witness.to_pat(cx);
387+
write!(f, "{}", pat.display(cx.db))
388+
}
389+
}
390+
381391
let non_empty_enum = match scrut_ty.as_adt() {
382392
Some((AdtId::EnumId(e), _)) => !cx.db.enum_data(e).variants.is_empty(),
383393
_ => false,
384394
};
385395
if arms.is_empty() && !non_empty_enum {
386396
format!("type `{}` is non-empty", scrut_ty.display(cx.db))
387397
} else {
398+
let pat_display = |witness| DisplayWitness(witness, cx);
388399
const LIMIT: usize = 3;
389400
match &*witnesses {
390-
[witness] => format!("`{}` not covered", witness.to_pat(&cx).display(cx.db)),
401+
[witness] => format!("`{}` not covered", pat_display(witness)),
391402
[head @ .., tail] if head.len() < LIMIT => {
392-
let head: Vec<_> = head.iter().map(|w| w.to_pat(cx)).collect();
393-
format!(
394-
"`{}` and `{}` not covered",
395-
head.iter().map(|p| p.display(cx.db)).join("`, `"),
396-
tail.to_pat(&cx).display(cx.db)
397-
)
403+
let head = head.iter().map(pat_display);
404+
format!("`{}` and `{}` not covered", head.format("`, `"), pat_display(tail))
398405
}
399406
_ => {
400407
let (head, tail) = witnesses.split_at(LIMIT);
401-
let head: Vec<_> = head.iter().map(|w| w.to_pat(cx)).collect();
402-
format!(
403-
"`{}` and {} more not covered",
404-
head.iter().map(|p| p.display(cx.db)).join("`, `"),
405-
tail.len()
406-
)
408+
let head = head.iter().map(pat_display);
409+
format!("`{}` and {} more not covered", head.format("`, `"), tail.len())
407410
}
408411
}
409412
}

crates/hir-ty/src/diagnostics/match_check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ impl<F> HirDisplay for WriteWith<F>
400400
where
401401
F: Fn(&mut HirFormatter) -> Result<(), HirDisplayError>,
402402
{
403-
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
403+
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
404404
(self.0)(f)
405405
}
406406
}

0 commit comments

Comments
 (0)