Skip to content

Commit c62a8ea

Browse files
Don't point out return span on every E0308
1 parent 1b57946 commit c62a8ea

File tree

5 files changed

+59
-28
lines changed

5 files changed

+59
-28
lines changed

compiler/rustc_typeck/src/check/coercion.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -1648,9 +1648,30 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
16481648
);
16491649
}
16501650

1651-
if let (Some(sp), Some(fn_output)) = (fcx.ret_coercion_span.get(), fn_output) {
1651+
let ret_coercion_span = fcx.ret_coercion_span.get();
1652+
1653+
if let Some(sp) = ret_coercion_span
1654+
// If the closure has an explicit return type annotation, or if
1655+
// the closure's return type has been inferred from outside
1656+
// requirements (such as an Fn* trait bound), then a type error
1657+
// may occur at the first return expression we see in the closure
1658+
// (if it conflicts with the declared return type). Skip adding a
1659+
// note in this case, since it would be incorrect.
1660+
&& !fcx.return_type_pre_known
1661+
{
1662+
err.span_note(
1663+
sp,
1664+
&format!(
1665+
"return type inferred to be `{}` here",
1666+
fcx.resolve_vars_if_possible(expected)
1667+
),
1668+
);
1669+
}
1670+
1671+
if let (Some(sp), Some(fn_output)) = (ret_coercion_span, fn_output) {
16521672
self.add_impl_trait_explanation(&mut err, cause, fcx, expected, sp, fn_output);
16531673
}
1674+
16541675
err
16551676
}
16561677

compiler/rustc_typeck/src/check/demand.rs

-22
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4545
self.note_type_is_not_clone(err, expected, expr_ty, expr);
4646
self.note_need_for_fn_pointer(err, expected, expr_ty);
4747
self.note_internal_mutation_in_method(err, expr, expected, expr_ty);
48-
self.report_closure_inferred_return_type(err, expected);
4948
}
5049

5150
// Requires that the two types unify, and prints an error message if
@@ -1418,25 +1417,4 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14181417
_ => false,
14191418
}
14201419
}
1421-
1422-
// Report the type inferred by the return statement.
1423-
fn report_closure_inferred_return_type(&self, err: &mut Diagnostic, expected: Ty<'tcx>) {
1424-
if let Some(sp) = self.ret_coercion_span.get()
1425-
// If the closure has an explicit return type annotation, or if
1426-
// the closure's return type has been inferred from outside
1427-
// requirements (such as an Fn* trait bound), then a type error
1428-
// may occur at the first return expression we see in the closure
1429-
// (if it conflicts with the declared return type). Skip adding a
1430-
// note in this case, since it would be incorrect.
1431-
&& !self.return_type_pre_known
1432-
{
1433-
err.span_note(
1434-
sp,
1435-
&format!(
1436-
"return type inferred to be `{}` here",
1437-
self.resolve_vars_if_possible(expected)
1438-
),
1439-
);
1440-
}
1441-
}
14421420
}

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

-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ LL | Foo(())
66
| |
77
| arguments to this struct are incorrect
88
|
9-
note: return type inferred to be `{integer}` here
10-
--> $DIR/issue-84128.rs:10:20
11-
|
12-
LL | return Foo(0);
13-
| ^^^^^^
149
note: tuple struct defined here
1510
--> $DIR/issue-84128.rs:5:8
1611
|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// edition:2021
2+
3+
async fn f(_: &()) {}
4+
//~^ NOTE function defined here
5+
//~| NOTE
6+
// Second note is the span of the underlined argument, I think...
7+
8+
fn main() {
9+
(|| async {
10+
Err::<(), ()>(())?;
11+
f(());
12+
//~^ ERROR mismatched types
13+
//~| NOTE arguments to this function are incorrect
14+
//~| NOTE expected `&()`, found `()`
15+
//~| HELP consider borrowing here
16+
Ok::<(), ()>(())
17+
})();
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/dont-point-return-on-E0308.rs:10:11
3+
|
4+
LL | f(());
5+
| - ^^
6+
| | |
7+
| | expected `&()`, found `()`
8+
| | help: consider borrowing here: `&()`
9+
| arguments to this function are incorrect
10+
|
11+
note: function defined here
12+
--> $DIR/dont-point-return-on-E0308.rs:3:10
13+
|
14+
LL | async fn f(_: &()) {}
15+
| ^ ------
16+
17+
error: aborting due to previous error
18+
19+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)