Avoid misleading closure return type note

This commit is contained in:
yukang
2026-04-25 16:27:05 +08:00
parent 0a4ee3f74b
commit 5e00484c38
3 changed files with 26 additions and 0 deletions
@@ -2009,6 +2009,9 @@ fn report_return_mismatched_types<'infcx>(
// note in this case, since it would be incorrect.
&& let Some(fn_sig) = fcx.body_fn_sig()
&& fn_sig.output().is_ty_var()
&& fcx.ret_coercion.as_ref().is_some_and(|ret_coercion| {
fcx.resolve_vars_if_possible(ret_coercion.borrow().expected_ty()) == expected
})
{
err.span_note(sp, format!("return type inferred to be `{expected}` here"));
}
@@ -0,0 +1,14 @@
struct SmolStr;
const _: fn() = || {
match Some(()) {
Some(()) => (),
None => return,
};
let _: String = {
SmolStr
//~^ ERROR mismatched types
};
};
fn main() {}
@@ -0,0 +1,9 @@
error[E0308]: mismatched types
--> $DIR/closure-return-block-note-issue-155670.rs:9:9
|
LL | SmolStr
| ^^^^^^^ expected `String`, found `SmolStr`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.