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

Lines changed: 18 additions & 2 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 8 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 2 deletions
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`

0 commit comments

Comments
 (0)