diff options
| author | bors <bors@rust-lang.org> | 2022-10-11 11:44:32 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-10-11 11:44:32 +0000 |
| commit | d08f1c3dff1827d401bafb82ab509a9d1e954008 (patch) | |
| tree | 12ed26b5f042877ea79cdd5f696036983973dad5 | |
| parent | 5d4995191326a3710206d152a710470e196e4466 (diff) | |
| parent | 5e43ea96aad65fbaae5806ea6732ab59977ede1c (diff) | |
| download | rust-d08f1c3dff1827d401bafb82ab509a9d1e954008.tar.gz rust-d08f1c3dff1827d401bafb82ab509a9d1e954008.zip | |
Auto merge of #13382 - lowr:fix/reorder-dyn-bounds-on-render, r=lowr
fix: reorder dyn bounds on render Fixes #13368 #13192 changed the order of dyn bounds, violating the [contract](https://github.com/rust-lang/rust-analyzer/blob/3a69435af7a1e6273744085cb251adb2b9c30a03/crates/hir-ty/src/display.rs#L896-L901) with `write_bounds_like_dyn_trait()` on render. The projection bounds are expected to come right after the trait bound they are accompanied with. Although the reordering procedure can be made a bit more efficient, I opted for relying only on the [invariants](https://github.com/rust-lang/rust-analyzer/blob/3a69435af7a1e6273744085cb251adb2b9c30a03/crates/hir-ty/src/lower.rs#L995-L998) currently documented in `lower_dyn_trait()`. It's not the hottest path and dyn bounds tend to be short so I believe it shouldn't hurt performance noticeably.
| -rw-r--r-- | crates/hir-ty/src/display.rs | 12 | ||||
| -rw-r--r-- | crates/hir-ty/src/tests/display_source_code.rs | 22 |
2 files changed, 33 insertions, 1 deletions
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 @@ impl HirDisplay for Ty { } 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 @@ -56,6 +56,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<Assoc = ()> + Send), + //^ &(dyn A<Assoc = ()> + Send) + _: &(dyn Send + A<Assoc = ()>), + //^ &(dyn A<Assoc = ()> + Send) + _: &dyn B<Assoc = ()>, + //^ &(dyn B<Assoc = ()>) +) {} + "#, + ); +} + +#[test] fn render_dyn_for_ty() { // FIXME check_types_source_code( |
