Skip to content

Commit 0946b64

Browse files
author
Joshua Nelson
authored
Rollup merge of rust-lang#84244 - ABouttefeux:closure-return-conflict-suggest, r=estebank
fix incomplete diagnostic notes when closure returns conflicting for genric type fixes rust-lang#84128 Correctly report the span on for conflicting return type in closures
2 parents 1d83b1b + af90cac commit 0946b64

File tree

4 files changed

+60
-22
lines changed

4 files changed

+60
-22
lines changed

compiler/rustc_typeck/src/check/coercion.rs

-22
Original file line numberDiff line numberDiff line change
@@ -1492,28 +1492,6 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
14921492
if let (Some(sp), Some(fn_output)) = (fcx.ret_coercion_span.get(), fn_output) {
14931493
self.add_impl_trait_explanation(&mut err, cause, fcx, expected, sp, fn_output);
14941494
}
1495-
1496-
if let Some(sp) = fcx.ret_coercion_span.get() {
1497-
// If the closure has an explicit return type annotation,
1498-
// then a type error may occur at the first return expression we
1499-
// see in the closure (if it conflicts with the declared
1500-
// return type). Skip adding a note in this case, since it
1501-
// would be incorrect.
1502-
if !err.span.primary_spans().iter().any(|&span| span == sp) {
1503-
let hir = fcx.tcx.hir();
1504-
let body_owner = hir.body_owned_by(hir.enclosing_body_owner(fcx.body_id));
1505-
if fcx.tcx.is_closure(hir.body_owner_def_id(body_owner).to_def_id()) {
1506-
err.span_note(
1507-
sp,
1508-
&format!(
1509-
"return type inferred to be `{}` here",
1510-
fcx.resolve_vars_if_possible(expected)
1511-
),
1512-
);
1513-
}
1514-
}
1515-
}
1516-
15171495
err
15181496
}
15191497

compiler/rustc_typeck/src/check/demand.rs

+29
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3737
self.suggest_missing_parentheses(err, expr);
3838
self.note_need_for_fn_pointer(err, expected, expr_ty);
3939
self.note_internal_mutation_in_method(err, expr, expected, expr_ty);
40+
self.report_closure_infered_return_type(err, expected)
4041
}
4142

4243
// Requires that the two types unify, and prints an error message if
@@ -1061,4 +1062,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10611062
_ => false,
10621063
}
10631064
}
1065+
1066+
// Report the type inferred by the return statement.
1067+
fn report_closure_infered_return_type(
1068+
&self,
1069+
err: &mut DiagnosticBuilder<'_>,
1070+
expected: Ty<'tcx>,
1071+
) {
1072+
if let Some(sp) = self.ret_coercion_span.get() {
1073+
// If the closure has an explicit return type annotation,
1074+
// then a type error may occur at the first return expression we
1075+
// see in the closure (if it conflicts with the declared
1076+
// return type). Skip adding a note in this case, since it
1077+
// would be incorrect.
1078+
if !err.span.primary_spans().iter().any(|&span| span == sp) {
1079+
let hir = self.tcx.hir();
1080+
let body_owner = hir.body_owned_by(hir.enclosing_body_owner(self.body_id));
1081+
if self.tcx.is_closure(hir.body_owner_def_id(body_owner).to_def_id()) {
1082+
err.span_note(
1083+
sp,
1084+
&format!(
1085+
"return type inferred to be `{}` here",
1086+
self.resolve_vars_if_possible(expected)
1087+
),
1088+
);
1089+
}
1090+
}
1091+
}
1092+
}
10641093
}

src/test/ui/closures/issue-84128.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// test for issue 84128
2+
// missing suggestion for similar ADT type with diffetent generic paramenter
3+
// on closure ReturnNoExpression
4+
5+
struct Foo<T>(T);
6+
7+
fn main() {
8+
|| {
9+
if false {
10+
return Foo(0);
11+
}
12+
13+
Foo(())
14+
//~^ ERROR mismatched types [E0308]
15+
};
16+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-84128.rs:13:13
3+
|
4+
LL | Foo(())
5+
| ^^ expected integer, found `()`
6+
|
7+
note: return type inferred to be `{integer}` here
8+
--> $DIR/issue-84128.rs:10:20
9+
|
10+
LL | return Foo(0);
11+
| ^^^^^^
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)