From 5e43ea96aad65fbaae5806ea6732ab59977ede1c Mon Sep 17 00:00:00 2001 From: Ryo Yoshida Date: Mon, 10 Oct 2022 19:32:24 +0900 Subject: [PATCH] fix: reorder dyn bounds on render --- crates/hir-ty/src/display.rs | 12 +++++++++- .../hir-ty/src/tests/display_source_code.rs | 22 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/crates/hir-ty/src/display.rs b/crates/hir-ty/src/display.rs index 7f0baf49dad..a6602747ef7 100644 --- a/crates/hir-ty/src/display.rs +++ b/crates/hir-ty/src/display.rs @@ -751,9 +751,19 @@ fn should_show( } TyKind::BoundVar(idx) => idx.hir_fmt(f)?, TyKind::Dyn(dyn_ty) => { + // Reorder bounds to satisfy `write_bounds_like_dyn_trait()`'s expectation. + // FIXME: `Iterator::partition_in_place()` or `Vec::drain_filter()` may make it + // more efficient when either of them hits stable. + let mut bounds: SmallVec<[_; 4]> = + dyn_ty.bounds.skip_binders().iter(Interner).cloned().collect(); + let (auto_traits, others): (SmallVec<[_; 4]>, _) = + bounds.drain(1..).partition(|b| b.skip_binders().trait_id().is_some()); + bounds.extend(others); + bounds.extend(auto_traits); + write_bounds_like_dyn_trait_with_prefix( "dyn", - dyn_ty.bounds.skip_binders().interned(), + &bounds, SizedByDefault::NotSized, f, )?; diff --git a/crates/hir-ty/src/tests/display_source_code.rs b/crates/hir-ty/src/tests/display_source_code.rs index 240942e488d..8a8ff08cfe8 100644 --- a/crates/hir-ty/src/tests/display_source_code.rs +++ b/crates/hir-ty/src/tests/display_source_code.rs @@ -55,6 +55,28 @@ fn main() { ); } +#[test] +fn render_dyn_ty_independent_of_order() { + check_types_source_code( + r#" +auto trait Send {} +trait A { + type Assoc; +} +trait B: A {} + +fn test( + _: &(dyn A + Send), + //^ &(dyn A + Send) + _: &(dyn Send + A), + //^ &(dyn A + Send) + _: &dyn B, + //^ &(dyn B) +) {} + "#, + ); +} + #[test] fn render_dyn_for_ty() { // FIXME