Skip to content

Commit 889be78

Browse files
committed
sort array trait implementation suggestions correctly
1 parent 7ad23f4 commit 889be78

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use rustc_middle::ty::print::{
2727
with_forced_trimmed_paths,
2828
};
2929
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,
3232
};
3333
use rustc_middle::{bug, span_bug};
3434
use rustc_span::{BytePos, DUMMY_SP, STDLIB_STABLE_CRATES, Span, Symbol, sym};
@@ -2316,7 +2316,19 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
23162316
cand
23172317
})
23182318
.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+
});
23202332
let mut impl_candidates: Vec<_> =
23212333
impl_candidates.into_iter().map(|cand| cand.trait_ref).collect();
23222334
impl_candidates.dedup();

tests/ui/consts/missing-larger-array-impl.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ LL | <[X; 35] as Default>::default();
88
&[T]
99
&mut [T]
1010
[T; 0]
11-
[T; 10]
12-
[T; 11]
13-
[T; 12]
14-
[T; 13]
15-
[T; 14]
11+
[T; 1]
12+
[T; 2]
13+
[T; 3]
14+
[T; 4]
15+
[T; 5]
1616
and 27 others
1717

1818
error: aborting due to 1 previous error

tests/ui/suggestions/issue-71394-no-from-impl.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ LL | let _: &[i8] = data.into();
55
| ^^^^ the trait `From<&[u8]>` is not implemented for `&[i8]`
66
|
77
= help: the following other types implement trait `From<T>`:
8-
`[T; 10]` implements `From<(T, T, T, T, T, T, T, T, T, T)>`
9-
`[T; 11]` implements `From<(T, T, T, T, T, T, T, T, T, T, T)>`
10-
`[T; 12]` implements `From<(T, T, T, T, T, T, T, T, T, T, T, T)>`
118
`[T; 1]` implements `From<(T,)>`
129
`[T; 2]` implements `From<(T, T)>`
1310
`[T; 3]` implements `From<(T, T, T)>`
1411
`[T; 4]` implements `From<(T, T, T, T)>`
1512
`[T; 5]` implements `From<(T, T, T, T, T)>`
13+
`[T; 6]` implements `From<(T, T, T, T, T, T)>`
14+
`[T; 7]` implements `From<(T, T, T, T, T, T, T)>`
15+
`[T; 8]` implements `From<(T, T, T, T, T, T, T, T)>`
1616
and 6 others
1717
= note: required for `&[u8]` to implement `Into<&[i8]>`
1818

0 commit comments

Comments
 (0)