From c62a8ea9df90a14c8c48ccbaffa959ed6ca2a97e Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 4 Aug 2022 02:55:40 +0000 Subject: [PATCH 1/2] Don't point out return span on every E0308 --- compiler/rustc_typeck/src/check/coercion.rs | 23 ++++++++++++++++++- compiler/rustc_typeck/src/check/demand.rs | 22 ------------------ src/test/ui/closures/issue-84128.stderr | 5 ---- .../dont-point-return-on-E0308.rs | 18 +++++++++++++++ .../dont-point-return-on-E0308.stderr | 19 +++++++++++++++ 5 files changed, 59 insertions(+), 28 deletions(-) create mode 100644 src/test/ui/mismatched_types/dont-point-return-on-E0308.rs create mode 100644 src/test/ui/mismatched_types/dont-point-return-on-E0308.stderr diff --git a/compiler/rustc_typeck/src/check/coercion.rs b/compiler/rustc_typeck/src/check/coercion.rs index 2ed5f569b4f3e..b75a2f3edd95a 100644 --- a/compiler/rustc_typeck/src/check/coercion.rs +++ b/compiler/rustc_typeck/src/check/coercion.rs @@ -1648,9 +1648,30 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { ); } - if let (Some(sp), Some(fn_output)) = (fcx.ret_coercion_span.get(), fn_output) { + let ret_coercion_span = fcx.ret_coercion_span.get(); + + if let Some(sp) = ret_coercion_span + // If the closure has an explicit return type annotation, or if + // the closure's return type has been inferred from outside + // requirements (such as an Fn* trait bound), then a type error + // may occur at the first return expression we see in the closure + // (if it conflicts with the declared return type). Skip adding a + // note in this case, since it would be incorrect. + && !fcx.return_type_pre_known + { + err.span_note( + sp, + &format!( + "return type inferred to be `{}` here", + fcx.resolve_vars_if_possible(expected) + ), + ); + } + + if let (Some(sp), Some(fn_output)) = (ret_coercion_span, fn_output) { self.add_impl_trait_explanation(&mut err, cause, fcx, expected, sp, fn_output); } + err } diff --git a/compiler/rustc_typeck/src/check/demand.rs b/compiler/rustc_typeck/src/check/demand.rs index 4de48dc5ba107..0595b9a73bed5 100644 --- a/compiler/rustc_typeck/src/check/demand.rs +++ b/compiler/rustc_typeck/src/check/demand.rs @@ -45,7 +45,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.note_type_is_not_clone(err, expected, expr_ty, expr); self.note_need_for_fn_pointer(err, expected, expr_ty); self.note_internal_mutation_in_method(err, expr, expected, expr_ty); - self.report_closure_inferred_return_type(err, expected); } // Requires that the two types unify, and prints an error message if @@ -1418,25 +1417,4 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { _ => false, } } - - // Report the type inferred by the return statement. - fn report_closure_inferred_return_type(&self, err: &mut Diagnostic, expected: Ty<'tcx>) { - if let Some(sp) = self.ret_coercion_span.get() - // If the closure has an explicit return type annotation, or if - // the closure's return type has been inferred from outside - // requirements (such as an Fn* trait bound), then a type error - // may occur at the first return expression we see in the closure - // (if it conflicts with the declared return type). Skip adding a - // note in this case, since it would be incorrect. - && !self.return_type_pre_known - { - err.span_note( - sp, - &format!( - "return type inferred to be `{}` here", - self.resolve_vars_if_possible(expected) - ), - ); - } - } } diff --git a/src/test/ui/closures/issue-84128.stderr b/src/test/ui/closures/issue-84128.stderr index 09c44d261af0c..59607afec8f8d 100644 --- a/src/test/ui/closures/issue-84128.stderr +++ b/src/test/ui/closures/issue-84128.stderr @@ -6,11 +6,6 @@ LL | Foo(()) | | | arguments to this struct are incorrect | -note: return type inferred to be `{integer}` here - --> $DIR/issue-84128.rs:10:20 - | -LL | return Foo(0); - | ^^^^^^ note: tuple struct defined here --> $DIR/issue-84128.rs:5:8 | diff --git a/src/test/ui/mismatched_types/dont-point-return-on-E0308.rs b/src/test/ui/mismatched_types/dont-point-return-on-E0308.rs new file mode 100644 index 0000000000000..f2ba610e2d1f6 --- /dev/null +++ b/src/test/ui/mismatched_types/dont-point-return-on-E0308.rs @@ -0,0 +1,18 @@ +// edition:2021 + +async fn f(_: &()) {} +//~^ NOTE function defined here +//~| NOTE +// Second note is the span of the underlined argument, I think... + +fn main() { + (|| async { + Err::<(), ()>(())?; + f(()); + //~^ ERROR mismatched types + //~| NOTE arguments to this function are incorrect + //~| NOTE expected `&()`, found `()` + //~| HELP consider borrowing here + Ok::<(), ()>(()) + })(); +} diff --git a/src/test/ui/mismatched_types/dont-point-return-on-E0308.stderr b/src/test/ui/mismatched_types/dont-point-return-on-E0308.stderr new file mode 100644 index 0000000000000..e79ab537b4c0f --- /dev/null +++ b/src/test/ui/mismatched_types/dont-point-return-on-E0308.stderr @@ -0,0 +1,19 @@ +error[E0308]: mismatched types + --> $DIR/dont-point-return-on-E0308.rs:10:11 + | +LL | f(()); + | - ^^ + | | | + | | expected `&()`, found `()` + | | help: consider borrowing here: `&()` + | arguments to this function are incorrect + | +note: function defined here + --> $DIR/dont-point-return-on-E0308.rs:3:10 + | +LL | async fn f(_: &()) {} + | ^ ------ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. From 1f463ac40735529abd7343bb8a12a1888bb623df Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 4 Aug 2022 03:05:57 +0000 Subject: [PATCH 2/2] Resolve vars before emitting coerce error --- compiler/rustc_typeck/src/check/coercion.rs | 3 +- src/test/ui/expr/if/if-branch-types.stderr | 5 ++ .../ui/expr/if/if-else-type-mismatch.stderr | 10 +++ .../type-mismatch-signature-deduction.stderr | 6 ++ src/test/ui/impl-trait/equality.stderr | 7 +- ...trait-in-return-position-impl-trait.stderr | 4 +- ...type-err-cause-on-impl-trait-return.stderr | 67 +++++++++++++++++-- .../dont-point-return-on-E0308.stderr | 2 +- .../ui/mismatched_types/issue-84976.stderr | 5 ++ src/test/ui/reify-intrinsic.stderr | 3 + .../type-alias-impl-trait/issue-74280.stderr | 2 +- 11 files changed, 102 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_typeck/src/check/coercion.rs b/compiler/rustc_typeck/src/check/coercion.rs index b75a2f3edd95a..b659eab5ca4c0 100644 --- a/compiler/rustc_typeck/src/check/coercion.rs +++ b/compiler/rustc_typeck/src/check/coercion.rs @@ -1478,6 +1478,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { // type) (self.final_ty.unwrap_or(self.expected_ty), expression_ty) }; + let (expected, found) = fcx.resolve_vars_if_possible((expected, found)); let mut err; let mut unsized_return = false; @@ -1663,7 +1664,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { sp, &format!( "return type inferred to be `{}` here", - fcx.resolve_vars_if_possible(expected) + expected ), ); } diff --git a/src/test/ui/expr/if/if-branch-types.stderr b/src/test/ui/expr/if/if-branch-types.stderr index 14f02163a8320..d2bba88211ea8 100644 --- a/src/test/ui/expr/if/if-branch-types.stderr +++ b/src/test/ui/expr/if/if-branch-types.stderr @@ -5,6 +5,11 @@ LL | let x = if true { 10i32 } else { 10u32 }; | ----- ^^^^^ expected `i32`, found `u32` | | | expected because of this + | +help: change the type of the numeric literal from `u32` to `i32` + | +LL | let x = if true { 10i32 } else { 10i32 }; + | ~~~ error: aborting due to previous error diff --git a/src/test/ui/expr/if/if-else-type-mismatch.stderr b/src/test/ui/expr/if/if-else-type-mismatch.stderr index 9fa190d6c9df3..f1fffdb1e7ef8 100644 --- a/src/test/ui/expr/if/if-else-type-mismatch.stderr +++ b/src/test/ui/expr/if/if-else-type-mismatch.stderr @@ -10,6 +10,11 @@ LL | | 2u32 | | ^^^^ expected `i32`, found `u32` LL | | }; | |_____- `if` and `else` have incompatible types + | +help: change the type of the numeric literal from `u32` to `i32` + | +LL | 2i32 + | ~~~ error[E0308]: `if` and `else` have incompatible types --> $DIR/if-else-type-mismatch.rs:8:38 @@ -18,6 +23,11 @@ LL | let _ = if true { 42i32 } else { 42u32 }; | ----- ^^^^^ expected `i32`, found `u32` | | | expected because of this + | +help: change the type of the numeric literal from `u32` to `i32` + | +LL | let _ = if true { 42i32 } else { 42i32 }; + | ~~~ error[E0308]: `if` and `else` have incompatible types --> $DIR/if-else-type-mismatch.rs:13:9 diff --git a/src/test/ui/generator/type-mismatch-signature-deduction.stderr b/src/test/ui/generator/type-mismatch-signature-deduction.stderr index 7938fc8097cd2..b98da1ed8be6e 100644 --- a/src/test/ui/generator/type-mismatch-signature-deduction.stderr +++ b/src/test/ui/generator/type-mismatch-signature-deduction.stderr @@ -11,6 +11,12 @@ note: return type inferred to be `Result<{integer}, _>` here | LL | return Ok(6); | ^^^^^ +help: try wrapping the expression in a variant of `Result` + | +LL | Ok(5) + | +++ + +LL | Err(5) + | ++++ + error[E0271]: type mismatch resolving `<[generator@$DIR/type-mismatch-signature-deduction.rs:7:5: 7:7] as Generator>::Return == i32` --> $DIR/type-mismatch-signature-deduction.rs:5:13 diff --git a/src/test/ui/impl-trait/equality.stderr b/src/test/ui/impl-trait/equality.stderr index f14b447b07730..d4a3495515cf1 100644 --- a/src/test/ui/impl-trait/equality.stderr +++ b/src/test/ui/impl-trait/equality.stderr @@ -12,10 +12,15 @@ error[E0308]: mismatched types --> $DIR/equality.rs:15:5 | LL | fn two(x: bool) -> impl Foo { - | -------- expected `_` because of return type + | -------- expected `i32` because of return type ... LL | 0_u32 | ^^^^^ expected `i32`, found `u32` + | +help: change the type of the numeric literal from `u32` to `i32` + | +LL | 0_i32 + | ~~~ error[E0277]: cannot add `impl Foo` to `u32` --> $DIR/equality.rs:24:11 diff --git a/src/test/ui/impl-trait/object-unsafe-trait-in-return-position-impl-trait.stderr b/src/test/ui/impl-trait/object-unsafe-trait-in-return-position-impl-trait.stderr index 5ca01a593761c..d6f5a1ac25b64 100644 --- a/src/test/ui/impl-trait/object-unsafe-trait-in-return-position-impl-trait.stderr +++ b/src/test/ui/impl-trait/object-unsafe-trait-in-return-position-impl-trait.stderr @@ -2,7 +2,7 @@ error[E0308]: mismatched types --> $DIR/object-unsafe-trait-in-return-position-impl-trait.rs:36:5 | LL | fn can() -> impl NotObjectSafe { - | ------------------ expected `_` because of return type + | ------------------ expected `A` because of return type ... LL | B | ^ expected struct `A`, found struct `B` @@ -11,7 +11,7 @@ error[E0308]: mismatched types --> $DIR/object-unsafe-trait-in-return-position-impl-trait.rs:43:5 | LL | fn cat() -> impl ObjectSafe { - | --------------- expected `_` because of return type + | --------------- expected `A` because of return type ... LL | B | ^ expected struct `A`, found struct `B` diff --git a/src/test/ui/impl-trait/point-to-type-err-cause-on-impl-trait-return.stderr b/src/test/ui/impl-trait/point-to-type-err-cause-on-impl-trait-return.stderr index 10510c1754eda..11c1072f02ccc 100644 --- a/src/test/ui/impl-trait/point-to-type-err-cause-on-impl-trait-return.stderr +++ b/src/test/ui/impl-trait/point-to-type-err-cause-on-impl-trait-return.stderr @@ -2,28 +2,43 @@ error[E0308]: mismatched types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:5:5 | LL | fn foo() -> impl std::fmt::Display { - | ---------------------- expected `_` because of return type + | ---------------------- expected `i32` because of return type ... LL | 1u32 | ^^^^ expected `i32`, found `u32` + | +help: change the type of the numeric literal from `u32` to `i32` + | +LL | 1i32 + | ~~~ error[E0308]: mismatched types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:12:16 | LL | fn bar() -> impl std::fmt::Display { - | ---------------------- expected `_` because of return type + | ---------------------- expected `i32` because of return type ... LL | return 1u32; | ^^^^ expected `i32`, found `u32` + | +help: change the type of the numeric literal from `u32` to `i32` + | +LL | return 1i32; + | ~~~ error[E0308]: mismatched types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:20:9 | LL | fn baz() -> impl std::fmt::Display { - | ---------------------- expected `_` because of return type + | ---------------------- expected `i32` because of return type ... LL | 1u32 | ^^^^ expected `i32`, found `u32` + | +help: you can convert a `u32` to an `i32` and panic if the converted value doesn't fit + | +LL | }.try_into().unwrap() + | ++++++++++++++++++++ error[E0308]: `if` and `else` have incompatible types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:28:9 @@ -36,36 +51,56 @@ LL | | 1u32 | | ^^^^ expected `i32`, found `u32` LL | | } | |_____- `if` and `else` have incompatible types + | +help: change the type of the numeric literal from `u32` to `i32` + | +LL | 1i32 + | ~~~ error[E0308]: mismatched types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:35:14 | LL | fn bat() -> impl std::fmt::Display { - | ---------------------- expected `_` because of return type + | ---------------------- expected `i32` because of return type ... LL | _ => 1u32, | ^^^^ expected `i32`, found `u32` + | +help: you can convert a `u32` to an `i32` and panic if the converted value doesn't fit + | +LL | }.try_into().unwrap() + | ++++++++++++++++++++ error[E0308]: mismatched types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:40:5 | LL | fn can() -> impl std::fmt::Display { - | ---------------------- expected `_` because of return type + | ---------------------- expected `i32` because of return type LL | / match 13 { LL | | 0 => return 0i32, LL | | 1 => 1u32, LL | | _ => 2u32, LL | | } | |_____^ expected `i32`, found `u32` + | +help: you can convert a `u32` to an `i32` and panic if the converted value doesn't fit + | +LL | }.try_into().unwrap() + | ++++++++++++++++++++ error[E0308]: mismatched types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:53:13 | LL | fn cat() -> impl std::fmt::Display { - | ---------------------- expected `_` because of return type + | ---------------------- expected `i32` because of return type ... LL | 1u32 | ^^^^ expected `i32`, found `u32` + | +help: you can convert a `u32` to an `i32` and panic if the converted value doesn't fit + | +LL | }.try_into().unwrap() + | ++++++++++++++++++++ error[E0308]: `match` arms have incompatible types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:61:14 @@ -78,6 +113,11 @@ LL | | 1 => 1u32, LL | | _ => 2u32, LL | | } | |_____- `match` arms have incompatible types + | +help: change the type of the numeric literal from `u32` to `i32` + | +LL | 1 => 1i32, + | ~~~ error[E0308]: `if` and `else` have incompatible types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:97:9 @@ -90,6 +130,11 @@ LL | | 1u32 | | ^^^^ expected `i32`, found `u32` LL | | } | |_____- `if` and `else` have incompatible types + | +help: change the type of the numeric literal from `u32` to `i32` + | +LL | 1i32 + | ~~~ error[E0746]: return type cannot have an unboxed trait object --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:66:13 @@ -125,6 +170,11 @@ LL | | 1 => 1u32, LL | | _ => 2u32, LL | | } | |_____- `match` arms have incompatible types + | +help: change the type of the numeric literal from `u32` to `i32` + | +LL | 1 => 1i32, + | ~~~ error[E0746]: return type cannot have an unboxed trait object --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:77:13 @@ -164,6 +214,11 @@ LL | | 1u32 | | ^^^^ expected `i32`, found `u32` LL | | } | |_____- `if` and `else` have incompatible types + | +help: change the type of the numeric literal from `u32` to `i32` + | +LL | 1i32 + | ~~~ error[E0746]: return type cannot have an unboxed trait object --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:85:13 diff --git a/src/test/ui/mismatched_types/dont-point-return-on-E0308.stderr b/src/test/ui/mismatched_types/dont-point-return-on-E0308.stderr index e79ab537b4c0f..13942682d289c 100644 --- a/src/test/ui/mismatched_types/dont-point-return-on-E0308.stderr +++ b/src/test/ui/mismatched_types/dont-point-return-on-E0308.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/dont-point-return-on-E0308.rs:10:11 + --> $DIR/dont-point-return-on-E0308.rs:11:11 | LL | f(()); | - ^^ diff --git a/src/test/ui/mismatched_types/issue-84976.stderr b/src/test/ui/mismatched_types/issue-84976.stderr index f8f2b1f0f5720..9157566e3a79b 100644 --- a/src/test/ui/mismatched_types/issue-84976.stderr +++ b/src/test/ui/mismatched_types/issue-84976.stderr @@ -3,6 +3,11 @@ error[E0308]: mismatched types | LL | length = { foo(&length) }; | ^^^^^^^^^^^^ expected `u32`, found `i32` + | +help: you can convert an `i32` to a `u32` and panic if the converted value doesn't fit + | +LL | length = { foo(&length).try_into().unwrap() }; + | ++++++++++++++++++++ error[E0308]: mismatched types --> $DIR/issue-84976.rs:17:14 diff --git a/src/test/ui/reify-intrinsic.stderr b/src/test/ui/reify-intrinsic.stderr index 360557fb5201d..f78f1d822bf60 100644 --- a/src/test/ui/reify-intrinsic.stderr +++ b/src/test/ui/reify-intrinsic.stderr @@ -23,6 +23,9 @@ LL | std::intrinsics::unlikely, | = note: expected fn item `extern "rust-intrinsic" fn(_) -> _ {likely}` found fn item `extern "rust-intrinsic" fn(_) -> _ {unlikely}` + = note: different `fn` items always have unique types, even if their signatures are the same + = help: change the expected type to be function pointer `extern "rust-intrinsic" fn(bool) -> bool` + = help: if the expected type is due to type inference, cast the expected `fn` to a function pointer: `likely as extern "rust-intrinsic" fn(bool) -> bool` error: aborting due to 3 previous errors diff --git a/src/test/ui/type-alias-impl-trait/issue-74280.stderr b/src/test/ui/type-alias-impl-trait/issue-74280.stderr index 5ed29e0ac94ff..66886db6eb946 100644 --- a/src/test/ui/type-alias-impl-trait/issue-74280.stderr +++ b/src/test/ui/type-alias-impl-trait/issue-74280.stderr @@ -2,7 +2,7 @@ error[E0308]: mismatched types --> $DIR/issue-74280.rs:9:5 | LL | fn test() -> Test { - | ---- expected `_` because of return type + | ---- expected `()` because of return type LL | let y = || -> Test { () }; LL | 7 | ^ expected `()`, found integer