Skip to content

Commit 85dca06

Browse files
committed
fix: reorder dyn bounds on render
1 parent 3a69435 commit 85dca06

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

crates/hir-ty/src/display.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -751,9 +751,17 @@ impl HirDisplay for Ty {
751751
}
752752
TyKind::BoundVar(idx) => idx.hir_fmt(f)?,
753753
TyKind::Dyn(dyn_ty) => {
754+
// Reorder bounds to satisfy `write_bounds_like_dyn_trait()`'s expectation.
755+
let mut bounds: SmallVec<[_; 4]> =
756+
dyn_ty.bounds.skip_binders().iter(Interner).cloned().collect();
757+
let (auto_traits, others): (SmallVec<[_; 4]>, _) =
758+
bounds.drain(1..).partition(|b| b.skip_binders().trait_id().is_some());
759+
bounds.extend(others);
760+
bounds.extend(auto_traits);
761+
754762
write_bounds_like_dyn_trait_with_prefix(
755763
"dyn",
756-
dyn_ty.bounds.skip_binders().interned(),
764+
&bounds,
757765
SizedByDefault::NotSized,
758766
f,
759767
)?;

crates/hir-ty/src/tests/display_source_code.rs

+22
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ fn main() {
5555
);
5656
}
5757

58+
#[test]
59+
fn render_dyn_ty_independent_of_order() {
60+
check_types_source_code(
61+
r#"
62+
auto trait Send {}
63+
trait A {
64+
type Assoc;
65+
}
66+
trait B: A {}
67+
68+
fn test(
69+
_: &(dyn A<Assoc = ()> + Send),
70+
//^ &(dyn A<Assoc = ()> + Send)
71+
_: &(dyn Send + A<Assoc = ()>),
72+
//^ &(dyn A<Assoc = ()> + Send)
73+
_: &dyn B<Assoc = ()>,
74+
//^ &(dyn B<Assoc = ()>)
75+
) {}
76+
"#,
77+
);
78+
}
79+
5880
#[test]
5981
fn render_dyn_for_ty() {
6082
// FIXME

0 commit comments

Comments
 (0)