@@ -27,8 +27,8 @@ use rustc_middle::ty::print::{
27
27
with_forced_trimmed_paths,
28
28
} ;
29
29
use rustc_middle:: ty:: {
30
- self , TraitRef , Ty , TyCtxt , TypeFoldable , TypeFolder , TypeSuperFoldable , TypeVisitableExt ,
31
- Upcast ,
30
+ self , GenericArgKind , TraitRef , Ty , TyCtxt , TypeFoldable , TypeFolder , TypeSuperFoldable ,
31
+ TypeVisitableExt , Upcast ,
32
32
} ;
33
33
use rustc_middle:: { bug, span_bug} ;
34
34
use rustc_span:: { BytePos , DUMMY_SP , STDLIB_STABLE_CRATES , Span , Symbol , sym} ;
@@ -2316,7 +2316,19 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
2316
2316
cand
2317
2317
} )
2318
2318
. collect ( ) ;
2319
- impl_candidates. sort_by_key ( |cand| ( cand. similarity , cand. trait_ref . to_string ( ) ) ) ;
2319
+ impl_candidates. sort_by_key ( |cand| {
2320
+ // When suggesting array types, sort them by the length of the array, not lexicographically (#135098)
2321
+ let len = if let GenericArgKind :: Type ( ty) = cand. trait_ref . args [ 0 ] . kind ( )
2322
+ && let ty:: Array ( _, len) = ty. kind ( )
2323
+ {
2324
+ // Deprioritize suggestions for parameterized arrays.
2325
+ len. try_to_target_usize ( self . tcx ) . unwrap_or ( u64:: MAX )
2326
+ } else {
2327
+ 0
2328
+ } ;
2329
+
2330
+ ( cand. similarity , len, cand. trait_ref . to_string ( ) )
2331
+ } ) ;
2320
2332
let mut impl_candidates: Vec < _ > =
2321
2333
impl_candidates. into_iter ( ) . map ( |cand| cand. trait_ref ) . collect ( ) ;
2322
2334
impl_candidates. dedup ( ) ;
0 commit comments