Skip to content

Commit

Permalink
Omit empty lists from ConstrainedSubsts
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
ecstatic-morse committed Dec 24, 2021
1 parent a9b0371 commit 7301c6a
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions chalk-ir/src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
//! Debug impls for types.
use std::fmt::{Debug, Display, Error, Formatter};
use std::fmt::{self, Debug, Display, Error, Formatter};

use super::*;

/// Wrapper to allow forwarding to `Display::fmt`, `Debug::fmt`, etc.
pub struct Fmt<F>(pub F)
where
F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result;

impl<F> fmt::Display for Fmt<F>
where
F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(self.0)(f)
}
}

impl<I: Interner> Debug for TraitId<I> {
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
I::debug_trait_id(*self, fmt).unwrap_or_else(|| write!(fmt, "TraitId({:?})", self.0))
Expand Down Expand Up @@ -959,14 +973,27 @@ impl<I: Interner> Debug for Constraint<I> {
}

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

write!(
f,
"substitution {}, lifetime constraints {:?}",
subst, constraints,
)
let mut first = true;

let subst = format!("{}", Fmt(|f| Display::fmt(subst, f)));
if subst != "[]" {
write!(f, "substitution {}", subst)?;
first = false;
}

let constraints = format!("{}", Fmt(|f| Debug::fmt(constraints, f)));
if constraints != "[]" {
if !first { write!(f, ", ")?; }
write!(f, "lifetime constraints {}", constraints)?;
first = false;
}

let _ = first;
Ok(())
}
}

Expand Down

0 comments on commit 7301c6a

Please sign in to comment.