diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index 1afb703ca150..698fdfa3897b 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -1348,6 +1348,34 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { "...is found to be `{}` here", fcx.resolve_vars_with_obligations(expected), )); + err.note( + "`impl Trait` as a return type requires that all the returned values must have \ + the same type", + ); + let snippet = fcx + .tcx + .sess + .source_map() + .span_to_snippet(return_sp) + .unwrap_or_else(|_| "dyn Trait".to_string()); + let mut snippet_iter = snippet.split_whitespace(); + let has_impl = snippet_iter.next().map_or(false, |s| s == "impl"); + if has_impl { + err.help(&format!( + "you can instead return a trait object using `Box`", + &snippet[5..] + )); + } + let impl_trait_msg = "for information on `impl Trait`, see \ + "; + let trait_obj_msg = "for information on trait objects, see \ + "; + err.note(impl_trait_msg); + if has_impl { + err.note(trait_obj_msg); + } } err } diff --git a/src/test/ui/impl-trait/equality.stderr b/src/test/ui/impl-trait/equality.stderr index e53524e58d66..215b6d52918a 100644 --- a/src/test/ui/impl-trait/equality.stderr +++ b/src/test/ui/impl-trait/equality.stderr @@ -9,6 +9,11 @@ LL | return 1_i32; LL | } LL | 0_u32 | ^^^^^ expected `i32`, found `u32` + | + = note: `impl Trait` as a return type requires that all the returned values must have the same type + = help: you can instead return a trait object using `Box` + = note: for information on `impl Trait`, see + = note: for information on trait objects, see error[E0277]: cannot add `impl Foo` to `u32` --> $DIR/equality.rs:24:11 diff --git a/src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr b/src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr index 27b86007451d..9db5250e4d87 100644 --- a/src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr +++ b/src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr @@ -9,6 +9,11 @@ LL | return 0i32; LL | } LL | 1u32 | ^^^^ expected `i32`, found `u32` + | + = note: `impl Trait` as a return type requires that all the returned values must have the same type + = help: you can instead return a trait object using `Box` + = note: for information on `impl Trait`, see + = note: for information on trait objects, see error[E0308]: mismatched types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:13:16 @@ -21,6 +26,11 @@ LL | return 0i32; LL | } else { LL | return 1u32; | ^^^^ expected `i32`, found `u32` + | + = note: `impl Trait` as a return type requires that all the returned values must have the same type + = help: you can instead return a trait object using `Box` + = note: for information on `impl Trait`, see + = note: for information on trait objects, see error[E0308]: mismatched types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:22:9 @@ -33,6 +43,11 @@ LL | return 0i32; LL | } else { LL | 1u32 | ^^^^ expected `i32`, found `u32` + | + = note: `impl Trait` as a return type requires that all the returned values must have the same type + = help: you can instead return a trait object using `Box` + = note: for information on `impl Trait`, see + = note: for information on trait objects, see error[E0308]: `if` and `else` have incompatible types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:31:9 @@ -57,6 +72,11 @@ LL | 0 => return 0i32, | ---- ...is found to be `i32` here LL | _ => 1u32, | ^^^^ expected `i32`, found `u32` + | + = note: `impl Trait` as a return type requires that all the returned values must have the same type + = help: you can instead return a trait object using `Box` + = note: for information on `impl Trait`, see + = note: for information on trait objects, see error[E0308]: mismatched types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:45:5 @@ -71,6 +91,11 @@ LL | | 1 => 1u32, LL | | _ => 2u32, LL | | } | |_____^ expected `i32`, found `u32` + | + = note: `impl Trait` as a return type requires that all the returned values must have the same type + = help: you can instead return a trait object using `Box` + = note: for information on `impl Trait`, see + = note: for information on trait objects, see error[E0308]: mismatched types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:59:13 @@ -83,6 +108,11 @@ LL | return 0i32; ... LL | 1u32 | ^^^^ expected `i32`, found `u32` + | + = note: `impl Trait` as a return type requires that all the returned values must have the same type + = help: you can instead return a trait object using `Box` + = note: for information on `impl Trait`, see + = note: for information on trait objects, see error: aborting due to 7 previous errors