1
Fork 0

When encountering unexpected closure return type, point at return type/expression

```
error[E0271]: expected `{closure@fallback-closure-wrap.rs:18:40}` to be a closure that returns `()`, but it returns `!`
  --> $DIR/fallback-closure-wrap.rs:19:9
   |
LL |     let error = Closure::wrap(Box::new(move || {
   |                                        -------
LL |         panic!("Can't connect to server.");
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `!`
   |
   = note: expected unit type `()`
                   found type `!`
   = note: required for the cast from `Box<{closure@$DIR/fallback-closure-wrap.rs:18:40: 18:47}>` to `Box<dyn FnMut()>`
```

```
error[E0271]: expected `{closure@dont-ice-for-type-mismatch-in-closure-in-async.rs:6:10}` to be a closure that returns `bool`, but it returns `Option<()>`
  --> $DIR/dont-ice-for-type-mismatch-in-closure-in-async.rs:6:16
   |
LL |     call(|| -> Option<()> {
   |     ---- ------^^^^^^^^^^
   |     |          |
   |     |          expected `bool`, found `Option<()>`
   |     required by a bound introduced by this call
   |
   = note: expected type `bool`
              found enum `Option<()>`
note: required by a bound in `call`
  --> $DIR/dont-ice-for-type-mismatch-in-closure-in-async.rs:3:25
   |
LL | fn call(_: impl Fn() -> bool) {}
   |                         ^^^^ required by this bound in `call`
```

```
error[E0271]: expected `{closure@f670.rs:28:13}` to be a closure that returns `Result<(), _>`, but it returns `!`
    --> f670.rs:28:20
     |
28   |     let c = |e| -> ! {
     |             -------^
     |                    |
     |                    expected `Result<(), _>`, found `!`
...
32   |     f().or_else(c);
     |         ------- required by a bound introduced by this call
-Ztrack-diagnostics: created at compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs:1433:28
     |
     = note: expected enum `Result<(), _>`
                found type `!`
note: required by a bound in `Result::<T, E>::or_else`
    --> /home/gh-estebank/rust/library/core/src/result.rs:1406:39
     |
1406 |     pub fn or_else<F, O: FnOnce(E) -> Result<T, F>>(self, op: O) -> Result<T, F> {
     |                                       ^^^^^^^^^^^^ required by this bound in `Result::<T, E>::or_else`
```
This commit is contained in:
Esteban Küber 2024-10-25 21:27:13 +00:00
parent a6434ef9c0
commit d3a148fe07
11 changed files with 114 additions and 65 deletions

View file

@ -642,6 +642,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
);
let hir = tcx.hir();
infcx.err_ctxt().note_type_err(
cause.span,
&mut diag,
&cause,
hir.get_if_local(impl_m.def_id)
@ -1061,6 +1062,7 @@ fn report_trait_method_mismatch<'tcx>(
cause.span = impl_err_span;
infcx.err_ctxt().note_type_err(
cause.span,
&mut diag,
&cause,
trait_err_span.map(|sp| (sp, Cow::from("type in trait"), false)),
@ -1853,6 +1855,7 @@ fn compare_const_predicate_entailment<'tcx>(
});
infcx.err_ctxt().note_type_err(
cause.span,
&mut diag,
&cause,
trait_c_span.map(|span| (span, Cow::from("type in trait"), false)),

View file

@ -640,6 +640,7 @@ pub fn check_function_signature<'tcx>(
let failure_code = cause.as_failure_code_diag(err, cause.span, vec![]);
let mut diag = tcx.dcx().create_err(failure_code);
err_ctxt.note_type_err(
cause.span,
&mut diag,
&cause,
None,