diff options
| author | Jana Dönszelmann <jonathan@donsz.nl> | 2025-09-13 02:40:44 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-13 02:40:44 +0200 |
| commit | 5dd5264d14c833e889202f61a044aade7ae92ab8 (patch) | |
| tree | 2b720e30aaf2cc7d9a6650dcf3ecb1a1cd37f8e1 /compiler | |
| parent | 147e97ae68b224f3980ad818b29738c0bc7e4d88 (diff) | |
| parent | 889be7860b0a0cbe82615787d4823bd57af40040 (diff) | |
| download | rust-5dd5264d14c833e889202f61a044aade7ae92ab8.tar.gz rust-5dd5264d14c833e889202f61a044aade7ae92ab8.zip | |
Rollup merge of #146403 - cyrgani:array-sugg-sorting, r=fee1-dead
sort array trait implementation suggestions correctly Fixes rust-lang/rust#135098. Previously tried in rust-lang/rust#137428.
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs index c82043f0222..149f5e638b1 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs @@ -27,8 +27,8 @@ use rustc_middle::ty::print::{ with_forced_trimmed_paths, }; use rustc_middle::ty::{ - self, TraitRef, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, - Upcast, + self, GenericArgKind, TraitRef, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, + TypeVisitableExt, Upcast, }; use rustc_middle::{bug, span_bug}; use rustc_span::{BytePos, DUMMY_SP, STDLIB_STABLE_CRATES, Span, Symbol, sym}; @@ -2316,7 +2316,19 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { cand }) .collect(); - impl_candidates.sort_by_key(|cand| (cand.similarity, cand.trait_ref.to_string())); + impl_candidates.sort_by_key(|cand| { + // When suggesting array types, sort them by the length of the array, not lexicographically (#135098) + let len = if let GenericArgKind::Type(ty) = cand.trait_ref.args[0].kind() + && let ty::Array(_, len) = ty.kind() + { + // Deprioritize suggestions for parameterized arrays. + len.try_to_target_usize(self.tcx).unwrap_or(u64::MAX) + } else { + 0 + }; + + (cand.similarity, len, cand.trait_ref.to_string()) + }); let mut impl_candidates: Vec<_> = impl_candidates.into_iter().map(|cand| cand.trait_ref).collect(); impl_candidates.dedup(); |
