Skip to content

Commit a7b03ad

Browse files
committed
Fix E0284 to not use incorrect wording
Fix #71584, fix #69683.
1 parent 339f574 commit a7b03ad

File tree

9 files changed

+81
-26
lines changed

9 files changed

+81
-26
lines changed

src/librustc_trait_selection/traits/error_reporting/mod.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -1492,12 +1492,26 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
14921492
ty::Predicate::Projection(ref data) => {
14931493
let trait_ref = data.to_poly_trait_ref(self.tcx);
14941494
let self_ty = trait_ref.self_ty();
1495+
let ty = data.skip_binder().ty;
14951496
if predicate.references_error() {
14961497
return;
14971498
}
1498-
let mut err = self.need_type_info_err(body_id, span, self_ty, ErrorCode::E0284);
1499-
err.note(&format!("cannot satisfy `{}`", predicate));
1500-
err
1499+
if self_ty.needs_infer() && ty.needs_infer() {
1500+
// We do this for the `foo.collect()?` case to produce a suggestion.
1501+
let mut err = self.need_type_info_err(body_id, span, self_ty, ErrorCode::E0284);
1502+
err.note(&format!("cannot satisfy `{}`", predicate));
1503+
err
1504+
} else {
1505+
let mut err = struct_span_err!(
1506+
self.tcx.sess,
1507+
span,
1508+
E0284,
1509+
"type annotations needed: cannot satisfy `{}`",
1510+
predicate,
1511+
);
1512+
err.span_label(span, &format!("cannot satisfy `{}`", predicate));
1513+
err
1514+
}
15011515
}
15021516

15031517
_ => {

src/test/ui/associated-types/associated-types-overridden-binding.stderr

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
error[E0284]: type annotations needed
1+
error[E0284]: type annotations needed: cannot satisfy `<Self as std::iter::Iterator>::Item == i32`
22
--> $DIR/associated-types-overridden-binding.rs:4:12
33
|
44
LL | trait Foo: Iterator<Item = i32> {}
55
| ---------- required by this bound in `Foo`
66
LL | trait Bar: Foo<Item = u32> {}
7-
| ^^^^^^^^^^^^^^^ cannot infer type for type parameter `Self`
8-
|
9-
= note: cannot satisfy `<Self as std::iter::Iterator>::Item == i32`
7+
| ^^^^^^^^^^^^^^^ cannot satisfy `<Self as std::iter::Iterator>::Item == i32`
108

11-
error[E0284]: type annotations needed
9+
error[E0284]: type annotations needed: cannot satisfy `<Self as std::iter::Iterator>::Item == i32`
1210
--> $DIR/associated-types-overridden-binding.rs:7:21
1311
|
1412
LL | trait I32Iterator = Iterator<Item = i32>;
1513
| ---------- required by this bound in `I32Iterator`
1614
LL | trait U32Iterator = I32Iterator<Item = u32>;
17-
| ^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `Self`
18-
|
19-
= note: cannot satisfy `<Self as std::iter::Iterator>::Item == i32`
15+
| ^^^^^^^^^^^^^^^^^^^^^^^ cannot satisfy `<Self as std::iter::Iterator>::Item == i32`
2016

2117
error: aborting due to 2 previous errors
2218

src/test/ui/issues/issue-12028.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
error[E0284]: type annotations needed
1+
error[E0284]: type annotations needed: cannot satisfy `<_ as StreamHasher>::S == <H as StreamHasher>::S`
22
--> $DIR/issue-12028.rs:27:14
33
|
44
LL | self.input_stream(&mut stream);
5-
| ^^^^^^^^^^^^ cannot infer type for type parameter `H` declared on the trait `StreamHash`
6-
|
7-
= note: cannot satisfy `<_ as StreamHasher>::S == <H as StreamHasher>::S`
5+
| ^^^^^^^^^^^^ cannot satisfy `<_ as StreamHasher>::S == <H as StreamHasher>::S`
86

97
error: aborting due to previous error
108

src/test/ui/issues/issue-69455.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ impl Test<u64> for u64 {
2626

2727
fn main() {
2828
let xs: Vec<u64> = vec![1, 2, 3];
29-
println!("{}", 23u64.test(xs.iter().sum())); //~ ERROR: type annotations needed [E0284]
29+
println!("{}", 23u64.test(xs.iter().sum())); //~ ERROR: type annotations needed
3030
}

src/test/ui/issues/issue-69455.stderr

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
error[E0284]: type annotations needed
1+
error[E0284]: type annotations needed: cannot satisfy `<u64 as Test<_>>::Output == _`
22
--> $DIR/issue-69455.rs:29:26
33
|
4-
LL | type Output;
5-
| ------------ `<Self as Test<Rhs>>::Output` defined here
6-
...
74
LL | println!("{}", 23u64.test(xs.iter().sum()));
8-
| ------^^^^-----------------
9-
| | |
10-
| | cannot infer type for type `u64`
11-
| this method call resolves to `<Self as Test<Rhs>>::Output`
12-
|
13-
= note: cannot satisfy `<u64 as Test<_>>::Output == _`
5+
| ^^^^ cannot satisfy `<u64 as Test<_>>::Output == _`
146

157
error: aborting due to previous error
168

src/test/ui/issues/issue-69683.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
pub trait Element<S> {
2+
type Array;
3+
}
4+
5+
impl<T> Element<()> for T {
6+
type Array = T;
7+
}
8+
9+
impl<T: Element<S>, S> Element<[S; 3]> for T {
10+
type Array = [T::Array; 3];
11+
}
12+
13+
trait Foo<I>
14+
where
15+
u8: Element<I>,
16+
{
17+
fn foo(self, x: <u8 as Element<I>>::Array);
18+
}
19+
20+
impl<I> Foo<I> for u16
21+
where
22+
u8: Element<I>,
23+
{
24+
fn foo(self, _: <u8 as Element<I>>::Array) {}
25+
}
26+
27+
fn main() {
28+
let b: [u8; 3] = [0u8; 3];
29+
30+
0u16.foo(b); //~ ERROR type annotations needed
31+
//<u16 as Foo<[(); 3]>>::foo(0u16, b);
32+
}

src/test/ui/issues/issue-69683.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0284]: type annotations needed: cannot satisfy `<u8 as Element<_>>::Array == [u8; 3]`
2+
--> $DIR/issue-69683.rs:30:10
3+
|
4+
LL | 0u16.foo(b);
5+
| ^^^ cannot satisfy `<u8 as Element<_>>::Array == [u8; 3]`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0284`.

src/test/ui/issues/issue-71584.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let n: u32 = 1;
3+
let mut d: u64 = 2;
4+
d = d % n.into(); //~ ERROR type annotations needed
5+
}

src/test/ui/issues/issue-71584.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0284]: type annotations needed: cannot satisfy `<u64 as std::ops::Rem<_>>::Output == u64`
2+
--> $DIR/issue-71584.rs:4:11
3+
|
4+
LL | d = d % n.into();
5+
| ^ cannot satisfy `<u64 as std::ops::Rem<_>>::Output == u64`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0284`.

0 commit comments

Comments
 (0)