Skip to content

Commit 7b9105d

Browse files
committed
Trim output of E0277 in some cases
Remove default note for "trait is not implemented" in favor of the more colorful diff output from the previous commit. Removes duplicated output.
1 parent b7fc1a7 commit 7b9105d

16 files changed

+18
-34
lines changed

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use rustc_data_structures::fx::FxHashMap;
66
use rustc_data_structures::unord::UnordSet;
77
use rustc_errors::codes::*;
88
use rustc_errors::{
9-
Applicability, Diag, ErrorGuaranteed, MultiSpan, StashKey, StringPart, Suggestions, pluralize,
10-
struct_span_code_err,
9+
Applicability, Diag, ErrorGuaranteed, Level, MultiSpan, StashKey, StringPart, Suggestions,
10+
pluralize, struct_span_code_err,
1111
};
1212
use rustc_hir::def::Namespace;
1313
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
@@ -1833,6 +1833,22 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
18331833
return false;
18341834
}
18351835

1836+
if let [child, ..] = &err.children[..]
1837+
&& child.level == Level::Help
1838+
&& let Some(line) = child.messages.get(0)
1839+
&& let Some(line) = line.0.as_str()
1840+
&& line.starts_with("the trait")
1841+
&& line.contains("is not implemented for")
1842+
{
1843+
// HACK(estebank): we remove the pre-existing
1844+
// "the trait `X` is not implemented for" note, which only happens if there
1845+
// was a custom label. We do this because we want that note to always be the
1846+
// first, and making this logic run earlier will get tricky. For now, we
1847+
// instead keep the logic the same and modify the already constructed error
1848+
// to avoid the wording duplication.
1849+
err.children.remove(0);
1850+
}
1851+
18361852
let traits = self.cmp_traits(
18371853
obligation_trait_ref.def_id,
18381854
&obligation_trait_ref.args[1..],

tests/ui/indexing/index-help.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ error[E0277]: the type `[{integer}]` cannot be indexed by `i32`
44
LL | x[0i32];
55
| ^^^^ slice indices are of type `usize` or ranges of `usize`
66
|
7-
= help: the trait `SliceIndex<[{integer}]>` is not implemented for `i32`
87
= help: the trait `SliceIndex<[{integer}]>` is not implemented for `i32`
98
but it is implemented for `usize`
109
= help: for that trait implementation, expected `usize`, found `i32`

tests/ui/indexing/indexing-requires-a-uint.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ error[E0277]: the type `[{integer}]` cannot be indexed by `u8`
44
LL | [0][0u8];
55
| ^^^ slice indices are of type `usize` or ranges of `usize`
66
|
7-
= help: the trait `SliceIndex<[{integer}]>` is not implemented for `u8`
87
= help: the trait `SliceIndex<[{integer}]>` is not implemented for `u8`
98
but it is implemented for `usize`
109
= help: for that trait implementation, expected `usize`, found `u8`

tests/ui/integral-indexing.stderr

-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ error[E0277]: the type `[isize]` cannot be indexed by `u8`
44
LL | v[3u8];
55
| ^^^ slice indices are of type `usize` or ranges of `usize`
66
|
7-
= help: the trait `SliceIndex<[isize]>` is not implemented for `u8`
87
= help: the trait `SliceIndex<[isize]>` is not implemented for `u8`
98
but it is implemented for `usize`
109
= help: for that trait implementation, expected `usize`, found `u8`
@@ -16,7 +15,6 @@ error[E0277]: the type `[isize]` cannot be indexed by `i8`
1615
LL | v[3i8];
1716
| ^^^ slice indices are of type `usize` or ranges of `usize`
1817
|
19-
= help: the trait `SliceIndex<[isize]>` is not implemented for `i8`
2018
= help: the trait `SliceIndex<[isize]>` is not implemented for `i8`
2119
but it is implemented for `usize`
2220
= help: for that trait implementation, expected `usize`, found `i8`
@@ -28,7 +26,6 @@ error[E0277]: the type `[isize]` cannot be indexed by `u32`
2826
LL | v[3u32];
2927
| ^^^^ slice indices are of type `usize` or ranges of `usize`
3028
|
31-
= help: the trait `SliceIndex<[isize]>` is not implemented for `u32`
3229
= help: the trait `SliceIndex<[isize]>` is not implemented for `u32`
3330
but it is implemented for `usize`
3431
= help: for that trait implementation, expected `usize`, found `u32`
@@ -40,7 +37,6 @@ error[E0277]: the type `[isize]` cannot be indexed by `i32`
4037
LL | v[3i32];
4138
| ^^^^ slice indices are of type `usize` or ranges of `usize`
4239
|
43-
= help: the trait `SliceIndex<[isize]>` is not implemented for `i32`
4440
= help: the trait `SliceIndex<[isize]>` is not implemented for `i32`
4541
but it is implemented for `usize`
4642
= help: for that trait implementation, expected `usize`, found `i32`
@@ -52,7 +48,6 @@ error[E0277]: the type `[u8]` cannot be indexed by `u8`
5248
LL | s.as_bytes()[3u8];
5349
| ^^^ slice indices are of type `usize` or ranges of `usize`
5450
|
55-
= help: the trait `SliceIndex<[u8]>` is not implemented for `u8`
5651
= help: the trait `SliceIndex<[u8]>` is not implemented for `u8`
5752
but it is implemented for `usize`
5853
= help: for that trait implementation, expected `usize`, found `u8`
@@ -64,7 +59,6 @@ error[E0277]: the type `[u8]` cannot be indexed by `i8`
6459
LL | s.as_bytes()[3i8];
6560
| ^^^ slice indices are of type `usize` or ranges of `usize`
6661
|
67-
= help: the trait `SliceIndex<[u8]>` is not implemented for `i8`
6862
= help: the trait `SliceIndex<[u8]>` is not implemented for `i8`
6963
but it is implemented for `usize`
7064
= help: for that trait implementation, expected `usize`, found `i8`
@@ -76,7 +70,6 @@ error[E0277]: the type `[u8]` cannot be indexed by `u32`
7670
LL | s.as_bytes()[3u32];
7771
| ^^^^ slice indices are of type `usize` or ranges of `usize`
7872
|
79-
= help: the trait `SliceIndex<[u8]>` is not implemented for `u32`
8073
= help: the trait `SliceIndex<[u8]>` is not implemented for `u32`
8174
but it is implemented for `usize`
8275
= help: for that trait implementation, expected `usize`, found `u32`
@@ -88,7 +81,6 @@ error[E0277]: the type `[u8]` cannot be indexed by `i32`
8881
LL | s.as_bytes()[3i32];
8982
| ^^^^ slice indices are of type `usize` or ranges of `usize`
9083
|
91-
= help: the trait `SliceIndex<[u8]>` is not implemented for `i32`
9284
= help: the trait `SliceIndex<[u8]>` is not implemented for `i32`
9385
but it is implemented for `usize`
9486
= help: for that trait implementation, expected `usize`, found `i32`

tests/ui/issues/issue-34334.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ error[E0277]: a value of type `Vec<(u32, _, _)>` cannot be built from an iterato
1717
LL | let sr2: Vec<(u32, _, _)> = sr.iter().map(|(faction, th_sender, th_receiver)| {}).collect();
1818
| ^^^^^^^ value of type `Vec<(u32, _, _)>` cannot be built from `std::iter::Iterator<Item=()>`
1919
|
20-
= help: the trait `FromIterator<()>` is not implemented for `Vec<(u32, _, _)>`
2120
= help: the trait `FromIterator<()>` is not implemented for `Vec<(u32, _, _)>`
2221
but trait `FromIterator<(u32, _, _)>` is implemented for it
2322
= help: for that trait implementation, expected `(u32, _, _)`, found `()`

tests/ui/issues/issue-66923-show-error-for-correct-call.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ error[E0277]: a value of type `Vec<f64>` cannot be built from an iterator over e
44
LL | let x2: Vec<f64> = x1.into_iter().collect();
55
| ^^^^^^^ value of type `Vec<f64>` cannot be built from `std::iter::Iterator<Item=&f64>`
66
|
7-
= help: the trait `FromIterator<&f64>` is not implemented for `Vec<f64>`
87
= help: the trait `FromIterator<&_>` is not implemented for `Vec<f64>`
98
but trait `FromIterator<_>` is implemented for it
109
= help: for that trait implementation, expected `f64`, found `&f64`
@@ -26,7 +25,6 @@ LL | let x3 = x1.into_iter().collect::<Vec<f64>>();
2625
| |
2726
| required by a bound introduced by this call
2827
|
29-
= help: the trait `FromIterator<&f64>` is not implemented for `Vec<f64>`
3028
= help: the trait `FromIterator<&_>` is not implemented for `Vec<f64>`
3129
but trait `FromIterator<_>` is implemented for it
3230
= help: for that trait implementation, expected `f64`, found `&f64`

tests/ui/iterators/invalid-iterator-chain-fixable.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ LL | let i = i.map(|x| x.clone());
66
LL | i.collect()
77
| ^^^^^^^ value of type `Vec<X>` cannot be built from `std::iter::Iterator<Item=&X>`
88
|
9-
= help: the trait `FromIterator<&X>` is not implemented for `Vec<X>`
109
= help: the trait `FromIterator<&_>` is not implemented for `Vec<X>`
1110
but trait `FromIterator<_>` is implemented for it
1211
= help: for that trait implementation, expected `X`, found `&X`
@@ -124,7 +123,6 @@ error[E0277]: a value of type `Vec<i32>` cannot be built from an iterator over e
124123
LL | let g: Vec<i32> = f.collect();
125124
| ^^^^^^^ value of type `Vec<i32>` cannot be built from `std::iter::Iterator<Item=()>`
126125
|
127-
= help: the trait `FromIterator<()>` is not implemented for `Vec<i32>`
128126
= help: the trait `FromIterator<()>` is not implemented for `Vec<i32>`
129127
but trait `FromIterator<i32>` is implemented for it
130128
= help: for that trait implementation, expected `i32`, found `()`

tests/ui/iterators/invalid-iterator-chain.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ LL | let i = i.map(|x| x.clone());
66
LL | i.collect()
77
| ^^^^^^^ value of type `Vec<X>` cannot be built from `std::iter::Iterator<Item=&X>`
88
|
9-
= help: the trait `FromIterator<&X>` is not implemented for `Vec<X>`
109
= help: the trait `FromIterator<&_>` is not implemented for `Vec<X>`
1110
but trait `FromIterator<_>` is implemented for it
1211
= help: for that trait implementation, expected `X`, found `&X`
@@ -181,7 +180,6 @@ error[E0277]: a value of type `Vec<i32>` cannot be built from an iterator over e
181180
LL | let g: Vec<i32> = f.collect();
182181
| ^^^^^^^ value of type `Vec<i32>` cannot be built from `std::iter::Iterator<Item=()>`
183182
|
184-
= help: the trait `FromIterator<()>` is not implemented for `Vec<i32>`
185183
= help: the trait `FromIterator<()>` is not implemented for `Vec<i32>`
186184
but trait `FromIterator<i32>` is implemented for it
187185
= help: for that trait implementation, expected `i32`, found `()`

tests/ui/on-unimplemented/impl-substs.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ LL | Foo::<usize>::foo((1i32, 1i32, 1i32));
66
| |
77
| required by a bound introduced by this call
88
|
9-
= help: the trait `Foo<usize>` is not implemented for `(i32, i32, i32)`
109
= help: the trait `Foo<usize>` is not implemented for `(i32, i32, i32)`
1110
but trait `Foo<i32>` is implemented for it
1211
= help: for that trait implementation, expected `i32`, found `usize`

tests/ui/on-unimplemented/on-impl.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ LL | Index::<u32>::index(&[1, 2, 3] as &[i32], 2u32);
66
| |
77
| required by a bound introduced by this call
88
|
9-
= help: the trait `Index<u32>` is not implemented for `[i32]`
109
= help: the trait `Index<u32>` is not implemented for `[i32]`
1110
but trait `Index<usize>` is implemented for it
1211
= help: for that trait implementation, expected `usize`, found `u32`
@@ -17,7 +16,6 @@ error[E0277]: the trait bound `[i32]: Index<u32>` is not satisfied
1716
LL | Index::<u32>::index(&[1, 2, 3] as &[i32], 2u32);
1817
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ a usize is required to index into a slice
1918
|
20-
= help: the trait `Index<u32>` is not implemented for `[i32]`
2119
= help: the trait `Index<u32>` is not implemented for `[i32]`
2220
but trait `Index<usize>` is implemented for it
2321
= help: for that trait implementation, expected `usize`, found `u32`

tests/ui/on-unimplemented/slice-index.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ error[E0277]: the type `[i32]` cannot be indexed by `i32`
44
LL | x[1i32];
55
| ^^^^ slice indices are of type `usize` or ranges of `usize`
66
|
7-
= help: the trait `SliceIndex<[i32]>` is not implemented for `i32`
87
= help: the trait `SliceIndex<[i32]>` is not implemented for `i32`
98
but it is implemented for `usize`
109
= help: for that trait implementation, expected `usize`, found `i32`

tests/ui/str/str-idx.stderr

-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ error[E0277]: the type `str` cannot be indexed by `{integer}`
44
LL | let _: u8 = s[4];
55
| ^ string indices are ranges of `usize`
66
|
7-
= help: the trait `SliceIndex<str>` is not implemented for `{integer}`
87
= note: you can use `.chars().nth()` or `.bytes().nth()`
98
for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
109
= help: the trait `SliceIndex<str>` is not implemented for `{integer}`
@@ -20,7 +19,6 @@ LL | let _ = s.get(4);
2019
| |
2120
| required by a bound introduced by this call
2221
|
23-
= help: the trait `SliceIndex<str>` is not implemented for `{integer}`
2422
= note: you can use `.chars().nth()` or `.bytes().nth()`
2523
for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
2624
= help: the trait `SliceIndex<str>` is not implemented for `{integer}`
@@ -37,7 +35,6 @@ LL | let _ = s.get_unchecked(4);
3735
| |
3836
| required by a bound introduced by this call
3937
|
40-
= help: the trait `SliceIndex<str>` is not implemented for `{integer}`
4138
= note: you can use `.chars().nth()` or `.bytes().nth()`
4239
for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
4340
= help: the trait `SliceIndex<str>` is not implemented for `{integer}`

tests/ui/str/str-mut-idx.stderr

-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ error[E0277]: the type `str` cannot be indexed by `usize`
3030
LL | s[1usize] = bot();
3131
| ^^^^^^ string indices are ranges of `usize`
3232
|
33-
= help: the trait `SliceIndex<str>` is not implemented for `usize`
3433
= help: the trait `SliceIndex<str>` is not implemented for `usize`
3534
but trait `SliceIndex<[_]>` is implemented for it
3635
= help: for that trait implementation, expected `[_]`, found `str`
@@ -44,7 +43,6 @@ LL | s.get_mut(1);
4443
| |
4544
| required by a bound introduced by this call
4645
|
47-
= help: the trait `SliceIndex<str>` is not implemented for `{integer}`
4846
= note: you can use `.chars().nth()` or `.bytes().nth()`
4947
for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
5048
= help: the trait `SliceIndex<str>` is not implemented for `{integer}`
@@ -61,7 +59,6 @@ LL | s.get_unchecked_mut(1);
6159
| |
6260
| required by a bound introduced by this call
6361
|
64-
= help: the trait `SliceIndex<str>` is not implemented for `{integer}`
6562
= note: you can use `.chars().nth()` or `.bytes().nth()`
6663
for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
6764
= help: the trait `SliceIndex<str>` is not implemented for `{integer}`

tests/ui/suggestions/suggest-dereferencing-index.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ error[E0277]: the type `[{integer}]` cannot be indexed by `&usize`
44
LL | let one_item_please: i32 = [1, 2, 3][i];
55
| ^ slice indices are of type `usize` or ranges of `usize`
66
|
7-
= help: the trait `SliceIndex<[{integer}]>` is not implemented for `&usize`
87
= help: the trait `SliceIndex<[{integer}]>` is not implemented for `&_`
98
but it is implemented for `_`
109
= help: for that trait implementation, expected `usize`, found `&usize`

tests/ui/try-trait/bad-interconversion.stderr

-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ LL | fn result_to_control_flow() -> ControlFlow<String> {
6666
LL | ControlFlow::Continue(Err("hello")?)
6767
| ^ this `?` produces `Result<Infallible, &str>`, which is incompatible with `ControlFlow<String>`
6868
|
69-
= help: the trait `FromResidual<Result<Infallible, &str>>` is not implemented for `ControlFlow<String>`
7069
= help: the trait `FromResidual<Result<Infallible, &str>>` is not implemented for `ControlFlow<String>`
7170
but trait `FromResidual<ControlFlow<String, Infallible>>` is implemented for it
7271
= help: for that trait implementation, expected `ControlFlow<String, Infallible>`, found `Result<Infallible, &str>`
@@ -79,7 +78,6 @@ LL | fn option_to_control_flow() -> ControlFlow<u64> {
7978
LL | Some(3)?;
8079
| ^ this `?` produces `Option<Infallible>`, which is incompatible with `ControlFlow<u64>`
8180
|
82-
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `ControlFlow<u64>`
8381
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `ControlFlow<u64>`
8482
but trait `FromResidual<ControlFlow<u64, Infallible>>` is implemented for it
8583
= help: for that trait implementation, expected `ControlFlow<u64, Infallible>`, found `Option<Infallible>`
@@ -92,7 +90,6 @@ LL | fn control_flow_to_control_flow() -> ControlFlow<i64> {
9290
LL | ControlFlow::Break(4_u8)?;
9391
| ^ this `?` produces `ControlFlow<u8, Infallible>`, which is incompatible with `ControlFlow<i64>`
9492
|
95-
= help: the trait `FromResidual<ControlFlow<u8, Infallible>>` is not implemented for `ControlFlow<i64>`
9693
= note: unlike `Result`, there's no `From`-conversion performed for `ControlFlow`
9794
= help: the trait `FromResidual<ControlFlow<u8, _>>` is not implemented for `ControlFlow<i64>`
9895
but trait `FromResidual<ControlFlow<i64, _>>` is implemented for it

tests/ui/type-alias-impl-trait/self-referential-2.current.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ LL | fn bar() -> Bar {
66
LL | 42_i32
77
| ------ return type was inferred to be `i32` here
88
|
9-
= help: the trait `PartialEq<Foo>` is not implemented for `i32`
109
= help: the trait `PartialEq<Foo>` is not implemented for `i32`
1110
but trait `PartialEq<i32>` is implemented for it
1211

0 commit comments

Comments
 (0)