From 643bbf15a2a76e7241156249261bf0d060deecd4 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 6 Oct 2020 14:40:27 +0200 Subject: Fix trait object hir formatting behind pointer and references --- crates/hir_ty/src/display.rs | 29 +++++++++++++++++++++++------ crates/ide/src/inlay_hints.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index f389c5a4b..d2e151f25 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -221,7 +221,16 @@ impl HirDisplay for ApplicationTy { } TypeCtor::RawPtr(m) => { let t = self.parameters.as_single(); - write!(f, "*{}{}", m.as_keyword_for_ptr(), t.display(f.db))?; + let ty_display = t.display(f.db); + + write!(f, "*{}", m.as_keyword_for_ptr())?; + if matches!(t, Ty::Dyn(predicates) if predicates.len() > 1) { + write!(f, "(")?; + write!(f, "{}", ty_display)?; + write!(f, ")")?; + } else { + write!(f, "{}", ty_display)?; + } } TypeCtor::Ref(m) => { let t = self.parameters.as_single(); @@ -230,7 +239,15 @@ impl HirDisplay for ApplicationTy { } else { t.display(f.db) }; - write!(f, "&{}{}", m.as_keyword_for_ref(), ty_display)?; + + write!(f, "&{}", m.as_keyword_for_ref())?; + if matches!(t, Ty::Dyn(predicates) if predicates.len() > 1) { + write!(f, "(")?; + write!(f, "{}", ty_display)?; + write!(f, ")")?; + } else { + write!(f, "{}", ty_display)?; + } } TypeCtor::Never => write!(f, "!")?, TypeCtor::Tuple { .. } => { @@ -636,14 +653,14 @@ impl HirDisplay for GenericPredicate { impl HirDisplay for Obligation { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { - Ok(match self { - Obligation::Trait(tr) => write!(f, "Implements({})", tr.display(f.db))?, + match self { + Obligation::Trait(tr) => write!(f, "Implements({})", tr.display(f.db)), Obligation::Projection(proj) => write!( f, "Normalize({} => {})", proj.projection_ty.display(f.db), proj.ty.display(f.db) - )?, - }) + ), + } } } diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 1d7e8de56..3a4dc6a84 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -1023,6 +1023,40 @@ mod collections { type Item=T; } } +"#, + ); + } + + #[test] + fn multi_dyn_trait_bounds() { + check_with_config( + InlayHintsConfig { + type_hints: true, + parameter_hints: false, + chaining_hints: false, + max_length: None, + }, + r#" +//- /main.rs crate:main +pub struct Vec {} + +impl Vec { + pub fn new() -> Self { Vec {} } +} + +pub struct Box {} + +trait Display {} +trait Sync {} + +fn main() { + let _v = Vec::>::new(); + //^^ Vec> + let _v = Vec::>::new(); + //^^ Vec> + let _v = Vec::>::new(); + //^^ Vec> +} "#, ); } -- cgit v1.2.3