Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain where the closure return type was inferred #78235

Merged
merged 1 commit into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions compiler/rustc_typeck/src/check/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,28 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
if let (Some(sp), Some(fn_output)) = (fcx.ret_coercion_span.borrow().as_ref(), fn_output) {
self.add_impl_trait_explanation(&mut err, cause, fcx, expected, *sp, fn_output);
}

if let Some(sp) = fcx.ret_coercion_span.borrow().as_ref() {
// If the closure has an explicit return type annotation,
// 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.
if !err.span.primary_spans().iter().any(|span| span == sp) {
let hir = fcx.tcx.hir();
let body_owner = hir.body_owned_by(hir.enclosing_body_owner(fcx.body_id));
if fcx.tcx.is_closure(hir.body_owner_def_id(body_owner).to_def_id()) {
err.span_note(
*sp,
&format!(
"return type inferred to be `{}` here",
fcx.resolve_vars_if_possible(&expected)
),
);
}
}
}

err
}

Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/closures/closure-return-type-mismatch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
fn main() {
|| {
if false {
return "test";
}
let a = true;
a //~ ERROR mismatched types
};

|| -> bool {
if false {
return "hello" //~ ERROR mismatched types
};
let b = true;
b
};
}
21 changes: 21 additions & 0 deletions src/test/ui/closures/closure-return-type-mismatch.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0308]: mismatched types
--> $DIR/closure-return-type-mismatch.rs:7:9
|
LL | a
| ^ expected `&str`, found `bool`
|
note: return type inferred to be `&str` here
--> $DIR/closure-return-type-mismatch.rs:4:20
|
LL | return "test";
| ^^^^^^

error[E0308]: mismatched types
--> $DIR/closure-return-type-mismatch.rs:12:20
|
LL | return "hello"
| ^^^^^^^ expected `bool`, found `&str`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ LL | 5
|
= note: expected type `std::result::Result<{integer}, _>`
found type `{integer}`
note: return type inferred to be `std::result::Result<{integer}, _>` here
--> $DIR/type-mismatch-signature-deduction.rs:8:20
|
LL | return Ok(6);
| ^^^^^

error[E0271]: type mismatch resolving `<[generator@$DIR/type-mismatch-signature-deduction.rs:6:5: 14:6] as Generator>::Return == i32`
--> $DIR/type-mismatch-signature-deduction.rs:5:13
Expand Down