Skip to content

Commit 7301c6a

Browse files
Omit empty lists from ConstrainedSubsts
This is harder than it should be due to the way we write `Display` impls. `ConstrainedSubsts` is wrapped in `Canonical`, a container over a generic `T`, so the `Display` impl for `Canonical` cannot know that the contained type has a `display` method that bundles the interner. As a result, the interner is gone by the time we get to the `Display` impl for `ConstrainedSubsts`. I think a better solution is to implement a custom `DebugWith<Ctxt>` trait (see Jonathan Turner's `lark` for prior art).
1 parent a9b0371 commit 7301c6a

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

chalk-ir/src/debug.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
//! Debug impls for types.
22
3-
use std::fmt::{Debug, Display, Error, Formatter};
3+
use std::fmt::{self, Debug, Display, Error, Formatter};
44

55
use super::*;
66

7+
/// Wrapper to allow forwarding to `Display::fmt`, `Debug::fmt`, etc.
8+
pub struct Fmt<F>(pub F)
9+
where
10+
F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result;
11+
12+
impl<F> fmt::Display for Fmt<F>
13+
where
14+
F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result,
15+
{
16+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17+
(self.0)(f)
18+
}
19+
}
20+
721
impl<I: Interner> Debug for TraitId<I> {
822
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
923
I::debug_trait_id(*self, fmt).unwrap_or_else(|| write!(fmt, "TraitId({:?})", self.0))
@@ -959,14 +973,27 @@ impl<I: Interner> Debug for Constraint<I> {
959973
}
960974

961975
impl<I: Interner> Display for ConstrainedSubst<I> {
976+
#[rustfmt::skip]
962977
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
963978
let ConstrainedSubst { subst, constraints } = self;
964979

965-
write!(
966-
f,
967-
"substitution {}, lifetime constraints {:?}",
968-
subst, constraints,
969-
)
980+
let mut first = true;
981+
982+
let subst = format!("{}", Fmt(|f| Display::fmt(subst, f)));
983+
if subst != "[]" {
984+
write!(f, "substitution {}", subst)?;
985+
first = false;
986+
}
987+
988+
let constraints = format!("{}", Fmt(|f| Debug::fmt(constraints, f)));
989+
if constraints != "[]" {
990+
if !first { write!(f, ", ")?; }
991+
write!(f, "lifetime constraints {}", constraints)?;
992+
first = false;
993+
}
994+
995+
let _ = first;
996+
Ok(())
970997
}
971998
}
972999

0 commit comments

Comments
 (0)