Skip to content

Commit e195ce6

Browse files
committed
Fix some non-determinism in help messages for E0277 errors.
The diagnostic for this error prints `the following implementations were found` followed by the first N relevant impls, sorted. This commit makes the sort happen before slicing, so that the set of impls being printed is deterministic when the input is not.
1 parent 9be4c76 commit e195ce6

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

src/librustc/traits/error_reporting.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
471471
}
472472

473473
fn report_similar_impl_candidates(&self,
474-
mut impl_candidates: Vec<ty::TraitRef<'tcx>>,
474+
impl_candidates: Vec<ty::TraitRef<'tcx>>,
475475
err: &mut DiagnosticBuilder<'_>)
476476
{
477477
if impl_candidates.is_empty() {
@@ -497,14 +497,18 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
497497
});
498498

499499
// Sort impl candidates so that ordering is consistent for UI tests.
500-
let normalized_impl_candidates = &mut impl_candidates[0..end]
500+
let mut normalized_impl_candidates = impl_candidates
501501
.iter()
502502
.map(normalize)
503503
.collect::<Vec<String>>();
504+
505+
// Sort before taking the `..end` range,
506+
// because the ordering of `impl_candidates` may not be deterministic:
507+
// https://github.com/rust-lang/rust/pull/57475#issuecomment-455519507
504508
normalized_impl_candidates.sort();
505509

506510
err.help(&format!("the following implementations were found:{}{}",
507-
normalized_impl_candidates.join(""),
511+
normalized_impl_candidates[..end].join(""),
508512
if len > 5 {
509513
format!("\nand {} others", len - 4)
510514
} else {

src/test/ui/did_you_mean/issue-21659-show-relevant-trait-impls-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | f1.foo(1usize);
88
<Bar as Foo<i16>>
99
<Bar as Foo<i32>>
1010
<Bar as Foo<i8>>
11-
<Bar as Foo<u8>>
11+
<Bar as Foo<u16>>
1212
and 2 others
1313

1414
error: aborting due to previous error

src/test/ui/did_you_mean/issue-39802-show-5-trait-impls.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ LL | Foo::<i32>::bar(&true); //~ ERROR is not satisfied
4040
| ^^^^^^^^^^^^^^^ the trait `Foo<i32>` is not implemented for `bool`
4141
|
4242
= help: the following implementations were found:
43+
<bool as Foo<bool>>
44+
<bool as Foo<i8>>
4345
<bool as Foo<u16>>
4446
<bool as Foo<u32>>
45-
<bool as Foo<u64>>
46-
<bool as Foo<u8>>
4747
and 2 others
4848
note: required by `Foo::bar`
4949
--> $DIR/issue-39802-show-5-trait-impls.rs:2:5

src/test/ui/try-block/try-block-bad-type.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | Err("")?; //~ ERROR the trait bound `i32: std::convert::From<&str>`
55
| ^^^^^^^^ the trait `std::convert::From<&str>` is not implemented for `i32`
66
|
77
= help: the following implementations were found:
8+
<i32 as std::convert::From<bool>>
89
<i32 as std::convert::From<core::num::NonZeroI32>>
910
<i32 as std::convert::From<i16>>
1011
<i32 as std::convert::From<i8>>
11-
<i32 as std::convert::From<u8>>
1212
and 2 others
1313
= note: required by `std::convert::From::from`
1414

0 commit comments

Comments
 (0)