diff --git a/crates/erg_compiler/ty/free.rs b/crates/erg_compiler/ty/free.rs index 475cd1b1..91d98527 100644 --- a/crates/erg_compiler/ty/free.rs +++ b/crates/erg_compiler/ty/free.rs @@ -170,6 +170,42 @@ impl Constraint { Self::Sandwiched { sub, sup } } + pub fn named_fmt(&self, f: &mut fmt::Formatter<'_>, name: &str, limit: usize) -> fmt::Result { + if limit == 0 { + return write!(f, "..."); + } + match self { + Self::Sandwiched { sub, sup } => match (sub == &Type::Never, sup == &Type::Obj) { + (true, true) => { + write!(f, "{name}: Type")?; + Ok(()) + } + (true, false) => { + write!(f, "{name} <: ")?; + sup.limited_fmt(f, limit - 1)?; + Ok(()) + } + (false, true) => { + write!(f, "{name} :> ")?; + sub.limited_fmt(f, limit - 1)?; + Ok(()) + } + (false, false) => { + write!(f, "{name} :> ")?; + sub.limited_fmt(f, limit - 1)?; + write!(f, ", {name} <: ")?; + sup.limited_fmt(f, limit - 1)?; + Ok(()) + } + }, + Self::TypeOf(t) => { + write!(f, "{name}: ")?; + t.limited_fmt(f, limit - 1) + } + Self::Uninited => write!(f, "Never"), + } + } + pub fn new_type_of(t: Type) -> Self { if t == Type::Type { Self::new_sandwiched(Type::Never, Type::Obj) diff --git a/crates/erg_compiler/ty/mod.rs b/crates/erg_compiler/ty/mod.rs index 426e8e07..61709443 100644 --- a/crates/erg_compiler/ty/mod.rs +++ b/crates/erg_compiler/ty/mod.rs @@ -805,8 +805,7 @@ impl LimitedDisplay for Type { if i != 0 { write!(f, ", ")?; } - write!(f, "{name}")?; - constr.limited_fmt(f, limit - 1)?; + constr.named_fmt(f, name, limit - 1)?; } write!(f, "|")?; quantified.limited_fmt(f, limit - 1)