From a7b03ad4eddb65abede497dbc408244f23c36256 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 6 May 2020 15:26:03 -0700 Subject: [PATCH] Fix E0284 to not use incorrect wording Fix #71584, fix #69683. --- .../traits/error_reporting/mod.rs | 20 ++++++++++-- ...associated-types-overridden-binding.stderr | 12 +++---- src/test/ui/issues/issue-12028.stderr | 6 ++-- src/test/ui/issues/issue-69455.rs | 2 +- src/test/ui/issues/issue-69455.stderr | 12 ++----- src/test/ui/issues/issue-69683.rs | 32 +++++++++++++++++++ src/test/ui/issues/issue-69683.stderr | 9 ++++++ src/test/ui/issues/issue-71584.rs | 5 +++ src/test/ui/issues/issue-71584.stderr | 9 ++++++ 9 files changed, 81 insertions(+), 26 deletions(-) create mode 100644 src/test/ui/issues/issue-69683.rs create mode 100644 src/test/ui/issues/issue-69683.stderr create mode 100644 src/test/ui/issues/issue-71584.rs create mode 100644 src/test/ui/issues/issue-71584.stderr diff --git a/src/librustc_trait_selection/traits/error_reporting/mod.rs b/src/librustc_trait_selection/traits/error_reporting/mod.rs index fa2af24c94534..866a5beeceb1f 100644 --- a/src/librustc_trait_selection/traits/error_reporting/mod.rs +++ b/src/librustc_trait_selection/traits/error_reporting/mod.rs @@ -1492,12 +1492,26 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { ty::Predicate::Projection(ref data) => { let trait_ref = data.to_poly_trait_ref(self.tcx); let self_ty = trait_ref.self_ty(); + let ty = data.skip_binder().ty; if predicate.references_error() { return; } - let mut err = self.need_type_info_err(body_id, span, self_ty, ErrorCode::E0284); - err.note(&format!("cannot satisfy `{}`", predicate)); - err + if self_ty.needs_infer() && ty.needs_infer() { + // We do this for the `foo.collect()?` case to produce a suggestion. + let mut err = self.need_type_info_err(body_id, span, self_ty, ErrorCode::E0284); + err.note(&format!("cannot satisfy `{}`", predicate)); + err + } else { + let mut err = struct_span_err!( + self.tcx.sess, + span, + E0284, + "type annotations needed: cannot satisfy `{}`", + predicate, + ); + err.span_label(span, &format!("cannot satisfy `{}`", predicate)); + err + } } _ => { diff --git a/src/test/ui/associated-types/associated-types-overridden-binding.stderr b/src/test/ui/associated-types/associated-types-overridden-binding.stderr index 3aed85645aef8..b8321ce5b2537 100644 --- a/src/test/ui/associated-types/associated-types-overridden-binding.stderr +++ b/src/test/ui/associated-types/associated-types-overridden-binding.stderr @@ -1,22 +1,18 @@ -error[E0284]: type annotations needed +error[E0284]: type annotations needed: cannot satisfy `::Item == i32` --> $DIR/associated-types-overridden-binding.rs:4:12 | LL | trait Foo: Iterator {} | ---------- required by this bound in `Foo` LL | trait Bar: Foo {} - | ^^^^^^^^^^^^^^^ cannot infer type for type parameter `Self` - | - = note: cannot satisfy `::Item == i32` + | ^^^^^^^^^^^^^^^ cannot satisfy `::Item == i32` -error[E0284]: type annotations needed +error[E0284]: type annotations needed: cannot satisfy `::Item == i32` --> $DIR/associated-types-overridden-binding.rs:7:21 | LL | trait I32Iterator = Iterator; | ---------- required by this bound in `I32Iterator` LL | trait U32Iterator = I32Iterator; - | ^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `Self` - | - = note: cannot satisfy `::Item == i32` + | ^^^^^^^^^^^^^^^^^^^^^^^ cannot satisfy `::Item == i32` error: aborting due to 2 previous errors diff --git a/src/test/ui/issues/issue-12028.stderr b/src/test/ui/issues/issue-12028.stderr index 434c5de2874e0..30cb7a1df8071 100644 --- a/src/test/ui/issues/issue-12028.stderr +++ b/src/test/ui/issues/issue-12028.stderr @@ -1,10 +1,8 @@ -error[E0284]: type annotations needed +error[E0284]: type annotations needed: cannot satisfy `<_ as StreamHasher>::S == ::S` --> $DIR/issue-12028.rs:27:14 | LL | self.input_stream(&mut stream); - | ^^^^^^^^^^^^ cannot infer type for type parameter `H` declared on the trait `StreamHash` - | - = note: cannot satisfy `<_ as StreamHasher>::S == ::S` + | ^^^^^^^^^^^^ cannot satisfy `<_ as StreamHasher>::S == ::S` error: aborting due to previous error diff --git a/src/test/ui/issues/issue-69455.rs b/src/test/ui/issues/issue-69455.rs index 017654554be16..f1935ae253463 100644 --- a/src/test/ui/issues/issue-69455.rs +++ b/src/test/ui/issues/issue-69455.rs @@ -26,5 +26,5 @@ impl Test for u64 { fn main() { let xs: Vec = vec![1, 2, 3]; - println!("{}", 23u64.test(xs.iter().sum())); //~ ERROR: type annotations needed [E0284] + println!("{}", 23u64.test(xs.iter().sum())); //~ ERROR: type annotations needed } diff --git a/src/test/ui/issues/issue-69455.stderr b/src/test/ui/issues/issue-69455.stderr index 4caa1aca9fd21..430bbcabf83e8 100644 --- a/src/test/ui/issues/issue-69455.stderr +++ b/src/test/ui/issues/issue-69455.stderr @@ -1,16 +1,8 @@ -error[E0284]: type annotations needed +error[E0284]: type annotations needed: cannot satisfy `>::Output == _` --> $DIR/issue-69455.rs:29:26 | -LL | type Output; - | ------------ `>::Output` defined here -... LL | println!("{}", 23u64.test(xs.iter().sum())); - | ------^^^^----------------- - | | | - | | cannot infer type for type `u64` - | this method call resolves to `>::Output` - | - = note: cannot satisfy `>::Output == _` + | ^^^^ cannot satisfy `>::Output == _` error: aborting due to previous error diff --git a/src/test/ui/issues/issue-69683.rs b/src/test/ui/issues/issue-69683.rs new file mode 100644 index 0000000000000..cc7f1fa0f5580 --- /dev/null +++ b/src/test/ui/issues/issue-69683.rs @@ -0,0 +1,32 @@ +pub trait Element { + type Array; +} + +impl Element<()> for T { + type Array = T; +} + +impl, S> Element<[S; 3]> for T { + type Array = [T::Array; 3]; +} + +trait Foo +where + u8: Element, +{ + fn foo(self, x: >::Array); +} + +impl Foo for u16 +where + u8: Element, +{ + fn foo(self, _: >::Array) {} +} + +fn main() { + let b: [u8; 3] = [0u8; 3]; + + 0u16.foo(b); //~ ERROR type annotations needed + //>::foo(0u16, b); +} diff --git a/src/test/ui/issues/issue-69683.stderr b/src/test/ui/issues/issue-69683.stderr new file mode 100644 index 0000000000000..776370331a4c9 --- /dev/null +++ b/src/test/ui/issues/issue-69683.stderr @@ -0,0 +1,9 @@ +error[E0284]: type annotations needed: cannot satisfy `>::Array == [u8; 3]` + --> $DIR/issue-69683.rs:30:10 + | +LL | 0u16.foo(b); + | ^^^ cannot satisfy `>::Array == [u8; 3]` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0284`. diff --git a/src/test/ui/issues/issue-71584.rs b/src/test/ui/issues/issue-71584.rs new file mode 100644 index 0000000000000..c96cd598f0ce0 --- /dev/null +++ b/src/test/ui/issues/issue-71584.rs @@ -0,0 +1,5 @@ +fn main() { + let n: u32 = 1; + let mut d: u64 = 2; + d = d % n.into(); //~ ERROR type annotations needed +} diff --git a/src/test/ui/issues/issue-71584.stderr b/src/test/ui/issues/issue-71584.stderr new file mode 100644 index 0000000000000..c162d338a93be --- /dev/null +++ b/src/test/ui/issues/issue-71584.stderr @@ -0,0 +1,9 @@ +error[E0284]: type annotations needed: cannot satisfy `>::Output == u64` + --> $DIR/issue-71584.rs:4:11 + | +LL | d = d % n.into(); + | ^ cannot satisfy `>::Output == u64` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0284`.