Skip to content

Commit 8bf1329

Browse files
bors[bot]Veykril
andauthored
Merge #6148
6148: Fix trait object hir formatting behind pointer and references r=matklad a=Veykril Fixes #6064 Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents 9507a01 + 643bbf1 commit 8bf1329

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

crates/hir_ty/src/display.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,16 @@ impl HirDisplay for ApplicationTy {
221221
}
222222
TypeCtor::RawPtr(m) => {
223223
let t = self.parameters.as_single();
224-
write!(f, "*{}{}", m.as_keyword_for_ptr(), t.display(f.db))?;
224+
let ty_display = t.display(f.db);
225+
226+
write!(f, "*{}", m.as_keyword_for_ptr())?;
227+
if matches!(t, Ty::Dyn(predicates) if predicates.len() > 1) {
228+
write!(f, "(")?;
229+
write!(f, "{}", ty_display)?;
230+
write!(f, ")")?;
231+
} else {
232+
write!(f, "{}", ty_display)?;
233+
}
225234
}
226235
TypeCtor::Ref(m) => {
227236
let t = self.parameters.as_single();
@@ -230,7 +239,15 @@ impl HirDisplay for ApplicationTy {
230239
} else {
231240
t.display(f.db)
232241
};
233-
write!(f, "&{}{}", m.as_keyword_for_ref(), ty_display)?;
242+
243+
write!(f, "&{}", m.as_keyword_for_ref())?;
244+
if matches!(t, Ty::Dyn(predicates) if predicates.len() > 1) {
245+
write!(f, "(")?;
246+
write!(f, "{}", ty_display)?;
247+
write!(f, ")")?;
248+
} else {
249+
write!(f, "{}", ty_display)?;
250+
}
234251
}
235252
TypeCtor::Never => write!(f, "!")?,
236253
TypeCtor::Tuple { .. } => {
@@ -636,14 +653,14 @@ impl HirDisplay for GenericPredicate {
636653

637654
impl HirDisplay for Obligation {
638655
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
639-
Ok(match self {
640-
Obligation::Trait(tr) => write!(f, "Implements({})", tr.display(f.db))?,
656+
match self {
657+
Obligation::Trait(tr) => write!(f, "Implements({})", tr.display(f.db)),
641658
Obligation::Projection(proj) => write!(
642659
f,
643660
"Normalize({} => {})",
644661
proj.projection_ty.display(f.db),
645662
proj.ty.display(f.db)
646-
)?,
647-
})
663+
),
664+
}
648665
}
649666
}

crates/ide/src/inlay_hints.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,40 @@ mod collections {
10231023
type Item=T;
10241024
}
10251025
}
1026+
"#,
1027+
);
1028+
}
1029+
1030+
#[test]
1031+
fn multi_dyn_trait_bounds() {
1032+
check_with_config(
1033+
InlayHintsConfig {
1034+
type_hints: true,
1035+
parameter_hints: false,
1036+
chaining_hints: false,
1037+
max_length: None,
1038+
},
1039+
r#"
1040+
//- /main.rs crate:main
1041+
pub struct Vec<T> {}
1042+
1043+
impl<T> Vec<T> {
1044+
pub fn new() -> Self { Vec {} }
1045+
}
1046+
1047+
pub struct Box<T> {}
1048+
1049+
trait Display {}
1050+
trait Sync {}
1051+
1052+
fn main() {
1053+
let _v = Vec::<Box<&(dyn Display + Sync)>>::new();
1054+
//^^ Vec<Box<&(dyn Display + Sync)>>
1055+
let _v = Vec::<Box<*const (dyn Display + Sync)>>::new();
1056+
//^^ Vec<Box<*const (dyn Display + Sync)>>
1057+
let _v = Vec::<Box<dyn Display + Sync>>::new();
1058+
//^^ Vec<Box<dyn Display + Sync>>
1059+
}
10261060
"#,
10271061
);
10281062
}

0 commit comments

Comments
 (0)