From 8bb54501280eba814b31628db1088ca4f03638e1 Mon Sep 17 00:00:00 2001 From: Ohad Ravid Date: Wed, 30 Oct 2019 16:53:41 +0100 Subject: [PATCH] Fix incorrect diagnostics for expected type in E0271 with an associated type --- src/librustc/traits/error_reporting.rs | 23 +++++++++++++++---- ...nding-to-type-defined-in-supertrait.stderr | 12 +++++----- .../associated-types/associated-types-eq-3.rs | 4 ++-- .../associated-types-eq-3.stderr | 12 +++++----- .../associated-types-eq-hr.stderr | 12 +++++----- .../associated-types-issue-20346.stderr | 6 ++--- ...ated-types-multiple-types-one-trait.stderr | 16 ++++++------- ...sociated-types-overridden-binding-2.stderr | 6 ++--- ...block-control-flow-static-semantics.stderr | 12 +++++----- src/test/ui/error-codes/E0271.stderr | 6 ++--- src/test/ui/issues/issue-24204.stderr | 6 ++--- 11 files changed, 64 insertions(+), 51 deletions(-) diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs index b55c0f29a70ec..7833d43438a91 100644 --- a/src/librustc/traits/error_reporting.rs +++ b/src/librustc/traits/error_reporting.rs @@ -226,13 +226,26 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { 0, &mut obligations ); + + debug!("report_projection_error obligation.cause={:?} obligation.param_env={:?}", + obligation.cause, obligation.param_env); + + debug!("report_projection_error normalized_ty={:?} data.ty={:?}", + normalized_ty, data.ty); + + let is_normalized_ty_expected = match &obligation.cause.code { + ObligationCauseCode::ItemObligation(_) | + ObligationCauseCode::BindingObligation(_, _) | + ObligationCauseCode::ObjectCastObligation(_) => false, + _ => true, + }; + if let Err(error) = self.at(&obligation.cause, obligation.param_env) - .eq(normalized_ty, data.ty) + .eq_exp(is_normalized_ty_expected, normalized_ty, data.ty) { - values = Some(infer::ValuePairs::Types(ExpectedFound { - expected: normalized_ty, - found: data.ty, - })); + values = Some(infer::ValuePairs::Types( + ExpectedFound::new(is_normalized_ty_expected, normalized_ty, data.ty))); + err_buf = error; err = &err_buf; } diff --git a/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.stderr b/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.stderr index 6a2135ca46445..800b762911a62 100644 --- a/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.stderr +++ b/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.stderr @@ -5,10 +5,10 @@ LL | fn blue_car>(c: C) { | -------- ---------- required by this bound in `blue_car` ... LL | fn b() { blue_car(ModelT); } - | ^^^^^^^^ expected struct `Black`, found struct `Blue` + | ^^^^^^^^ expected struct `Blue`, found struct `Black` | - = note: expected type `Black` - found type `Blue` + = note: expected type `Blue` + found type `Black` error[E0271]: type mismatch resolving `::Color == Black` --> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:32:10 @@ -17,10 +17,10 @@ LL | fn black_car>(c: C) { | --------- ----------- required by this bound in `black_car` ... LL | fn c() { black_car(ModelU); } - | ^^^^^^^^^ expected struct `Blue`, found struct `Black` + | ^^^^^^^^^ expected struct `Black`, found struct `Blue` | - = note: expected type `Blue` - found type `Black` + = note: expected type `Black` + found type `Blue` error: aborting due to 2 previous errors diff --git a/src/test/ui/associated-types/associated-types-eq-3.rs b/src/test/ui/associated-types/associated-types-eq-3.rs index 9366148b587d2..22e04a8f15ca8 100644 --- a/src/test/ui/associated-types/associated-types-eq-3.rs +++ b/src/test/ui/associated-types/associated-types-eq-3.rs @@ -37,8 +37,8 @@ pub fn main() { let a = 42; foo1(a); //~^ ERROR type mismatch resolving - //~| expected usize, found struct `Bar` + //~| expected struct `Bar`, found usize baz(&a); //~^ ERROR type mismatch resolving - //~| expected usize, found struct `Bar` + //~| expected struct `Bar`, found usize } diff --git a/src/test/ui/associated-types/associated-types-eq-3.stderr b/src/test/ui/associated-types/associated-types-eq-3.stderr index 83d89924944ab..0f2bc84aa1c59 100644 --- a/src/test/ui/associated-types/associated-types-eq-3.stderr +++ b/src/test/ui/associated-types/associated-types-eq-3.stderr @@ -16,19 +16,19 @@ LL | fn foo1>(x: I) { | ---- ----- required by this bound in `foo1` ... LL | foo1(a); - | ^^^^ expected usize, found struct `Bar` + | ^^^^ expected struct `Bar`, found usize | - = note: expected type `usize` - found type `Bar` + = note: expected type `Bar` + found type `usize` error[E0271]: type mismatch resolving `::A == Bar` --> $DIR/associated-types-eq-3.rs:41:9 | LL | baz(&a); - | ^^ expected usize, found struct `Bar` + | ^^ expected struct `Bar`, found usize | - = note: expected type `usize` - found type `Bar` + = note: expected type `Bar` + found type `usize` = note: required for the cast to the object type `dyn Foo` error: aborting due to 3 previous errors diff --git a/src/test/ui/associated-types/associated-types-eq-hr.stderr b/src/test/ui/associated-types/associated-types-eq-hr.stderr index 45b6cc9ba5f98..a8c239389e977 100644 --- a/src/test/ui/associated-types/associated-types-eq-hr.stderr +++ b/src/test/ui/associated-types/associated-types-eq-hr.stderr @@ -7,10 +7,10 @@ LL | where T : for<'x> TheTrait<&'x isize, A = &'x isize> | ------------- required by this bound in `foo` ... LL | foo::(); - | ^^^^^^^^^^^^^^^^^ expected usize, found isize + | ^^^^^^^^^^^^^^^^^ expected isize, found usize | - = note: expected type `&usize` - found type `&isize` + = note: expected type `&isize` + found type `&usize` error[E0271]: type mismatch resolving `for<'x> >::A == &'x usize` --> $DIR/associated-types-eq-hr.rs:86:5 @@ -21,10 +21,10 @@ LL | where T : for<'x> TheTrait<&'x isize, A = &'x usize> | ------------- required by this bound in `bar` ... LL | bar::(); - | ^^^^^^^^^^^^^^^^ expected isize, found usize + | ^^^^^^^^^^^^^^^^ expected usize, found isize | - = note: expected type `&isize` - found type `&usize` + = note: expected type `&usize` + found type `&isize` error[E0277]: the trait bound `for<'x, 'y> Tuple: TheTrait<(&'x isize, &'y isize)>` is not satisfied --> $DIR/associated-types-eq-hr.rs:91:17 diff --git a/src/test/ui/associated-types/associated-types-issue-20346.stderr b/src/test/ui/associated-types/associated-types-issue-20346.stderr index c8f8725afc45c..b763b82d540cc 100644 --- a/src/test/ui/associated-types/associated-types-issue-20346.stderr +++ b/src/test/ui/associated-types/associated-types-issue-20346.stderr @@ -5,10 +5,10 @@ LL | fn is_iterator_of>(_: &I) {} | -------------- ------ required by this bound in `is_iterator_of` ... LL | is_iterator_of::, _>(&adapter); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter, found enum `std::option::Option` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `std::option::Option`, found type parameter | - = note: expected type `T` - found type `std::option::Option` + = note: expected type `std::option::Option` + found type `T` = help: type parameters must be constrained to match other types = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters diff --git a/src/test/ui/associated-types/associated-types-multiple-types-one-trait.stderr b/src/test/ui/associated-types/associated-types-multiple-types-one-trait.stderr index c7de186c1d3e0..e3a2b5edf3f1b 100644 --- a/src/test/ui/associated-types/associated-types-multiple-types-one-trait.stderr +++ b/src/test/ui/associated-types/associated-types-multiple-types-one-trait.stderr @@ -2,28 +2,28 @@ error[E0271]: type mismatch resolving `::Y == i32` --> $DIR/associated-types-multiple-types-one-trait.rs:13:5 | LL | want_y(t); - | ^^^^^^ expected associated type, found i32 + | ^^^^^^ expected i32, found associated type ... LL | fn want_y>(t: &T) { } | ------ ----- required by this bound in `want_y` | - = note: expected type `::Y` - found type `i32` - = note: consider constraining the associated type `::Y` to `i32` or calling a method that returns `::Y` + = note: expected type `i32` + found type `::Y` + = note: consider constraining the associated type `::Y` to `i32` = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html error[E0271]: type mismatch resolving `::X == u32` --> $DIR/associated-types-multiple-types-one-trait.rs:18:5 | LL | want_x(t); - | ^^^^^^ expected associated type, found u32 + | ^^^^^^ expected u32, found associated type ... LL | fn want_x>(t: &T) { } | ------ ----- required by this bound in `want_x` | - = note: expected type `::X` - found type `u32` - = note: consider constraining the associated type `::X` to `u32` or calling a method that returns `::X` + = note: expected type `u32` + found type `::X` + = note: consider constraining the associated type `::X` to `u32` = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html error: aborting due to 2 previous errors diff --git a/src/test/ui/associated-types/associated-types-overridden-binding-2.stderr b/src/test/ui/associated-types/associated-types-overridden-binding-2.stderr index aff067c289107..02a6ac12dd927 100644 --- a/src/test/ui/associated-types/associated-types-overridden-binding-2.stderr +++ b/src/test/ui/associated-types/associated-types-overridden-binding-2.stderr @@ -2,10 +2,10 @@ error[E0271]: type mismatch resolving ` as std::iter::It --> $DIR/associated-types-overridden-binding-2.rs:6:43 | LL | let _: &dyn I32Iterator = &vec![42].into_iter(); - | ^^^^^^^^^^^^^^^^^^^^^ expected u32, found i32 + | ^^^^^^^^^^^^^^^^^^^^^ expected i32, found u32 | - = note: expected type `u32` - found type `i32` + = note: expected type `i32` + found type `u32` = note: required for the cast to the object type `dyn std::iter::Iterator` error: aborting due to previous error diff --git a/src/test/ui/async-await/async-block-control-flow-static-semantics.stderr b/src/test/ui/async-await/async-block-control-flow-static-semantics.stderr index a9b0e7ae7795d..6bef9dca265e7 100644 --- a/src/test/ui/async-await/async-block-control-flow-static-semantics.stderr +++ b/src/test/ui/async-await/async-block-control-flow-static-semantics.stderr @@ -33,10 +33,10 @@ error[E0271]: type mismatch resolving ` $DIR/async-block-control-flow-static-semantics.rs:18:39 | LL | let _: &dyn Future = █ - | ^^^^^^ expected u8, found () + | ^^^^^^ expected (), found u8 | - = note: expected type `u8` - found type `()` + = note: expected type `()` + found type `u8` = note: required for the cast to the object type `dyn std::future::Future` error[E0308]: mismatched types @@ -59,10 +59,10 @@ error[E0271]: type mismatch resolving ` $DIR/async-block-control-flow-static-semantics.rs:27:39 | LL | let _: &dyn Future = █ - | ^^^^^^ expected u8, found () + | ^^^^^^ expected (), found u8 | - = note: expected type `u8` - found type `()` + = note: expected type `()` + found type `u8` = note: required for the cast to the object type `dyn std::future::Future` error[E0308]: mismatched types diff --git a/src/test/ui/error-codes/E0271.stderr b/src/test/ui/error-codes/E0271.stderr index c56853f45a0b0..378e5e3630641 100644 --- a/src/test/ui/error-codes/E0271.stderr +++ b/src/test/ui/error-codes/E0271.stderr @@ -5,10 +5,10 @@ LL | fn foo(t: T) where T: Trait { | --- ------------------ required by this bound in `foo` ... LL | foo(3_i8); - | ^^^ expected reference, found u32 + | ^^^ expected u32, found reference | - = note: expected type `&'static str` - found type `u32` + = note: expected type `u32` + found type `&'static str` error: aborting due to previous error diff --git a/src/test/ui/issues/issue-24204.stderr b/src/test/ui/issues/issue-24204.stderr index 9658f6980be2f..9c53c1b86ceaf 100644 --- a/src/test/ui/issues/issue-24204.stderr +++ b/src/test/ui/issues/issue-24204.stderr @@ -5,10 +5,10 @@ LL | trait Trait: Sized { | ------------------ required by `Trait` ... LL | fn test>(b: i32) -> T where T::A: MultiDispatch { T::new(b) } - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected associated type, found type parameter + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter, found associated type | - = note: expected type `<::A as MultiDispatch>::O` - found type `T` + = note: expected type `T` + found type `<::A as MultiDispatch>::O` = note: you might be missing a type parameter or trait bound error: aborting due to previous error