Skip to content

Commit 2767f6c

Browse files
oli-obkeholk
authored andcommitted
Make diagnostics know about more coro closures than async closures
1 parent 47410c6 commit 2767f6c

File tree

5 files changed

+17
-11
lines changed

5 files changed

+17
-11
lines changed

compiler/rustc_hir/src/hir.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,9 +2061,7 @@ impl CoroutineKind {
20612061
CoroutineKind::Coroutine(mov) => mov,
20622062
}
20632063
}
2064-
}
20652064

2066-
impl CoroutineKind {
20672065
pub fn is_fn_like(self) -> bool {
20682066
matches!(self, CoroutineKind::Desugared(_, CoroutineSource::Fn))
20692067
}

compiler/rustc_trait_selection/messages.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ trait_selection_adjust_signature_remove_borrow = consider adjusting the signatur
7272
7373
trait_selection_ascribe_user_type_prove_predicate = ...so that the where clause holds
7474
75-
trait_selection_async_closure_not_fn = async closure does not implement `{$kind}` because it captures state from its environment
76-
7775
trait_selection_await_both_futures = consider `await`ing on both `Future`s
7876
trait_selection_await_future = consider `await`ing on the `Future`
7977
trait_selection_await_note = calling an async function returns a future
@@ -123,6 +121,8 @@ trait_selection_closure_kind_requirement = the requirement to implement `{$trait
123121
124122
trait_selection_compare_impl_item_obligation = ...so that the definition in impl matches the definition from the trait
125123
trait_selection_consider_specifying_length = consider specifying the actual array length
124+
trait_selection_coro_closure_not_fn = {$coro_kind}closure does not implement `{$kind}` because it captures state from its environment
125+
126126
trait_selection_data_flows = ...but data{$label_var1_exists ->
127127
[true] {" "}from `{$label_var1}`
128128
*[false] {""}

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ use super::{
4242
use crate::error_reporting::TypeErrCtxt;
4343
use crate::error_reporting::infer::TyCategory;
4444
use crate::error_reporting::traits::report_dyn_incompatibility;
45-
use crate::errors::{
46-
AsyncClosureNotFn, ClosureFnMutLabel, ClosureFnOnceLabel, ClosureKindMismatch,
47-
};
45+
use crate::errors::{ClosureFnMutLabel, ClosureFnOnceLabel, ClosureKindMismatch, CoroClosureNotFn};
4846
use crate::infer::{self, InferCtxt, InferCtxtExt as _};
4947
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
5048
use crate::traits::{
@@ -886,9 +884,18 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
886884
// is unimplemented is because async closures don't implement `Fn`/`FnMut`
887885
// if they have captures.
888886
if has_self_borrows && expected_kind != ty::ClosureKind::FnOnce {
889-
let mut err = self.dcx().create_err(AsyncClosureNotFn {
887+
let coro_kind = match self
888+
.tcx
889+
.coroutine_kind(self.tcx.coroutine_for_closure(closure_def_id))
890+
.unwrap()
891+
{
892+
rustc_hir::CoroutineKind::Desugared(desugaring, _) => format!("{desugaring:#}"),
893+
coro => format!("{coro:#}"),
894+
};
895+
let mut err = self.dcx().create_err(CoroClosureNotFn {
890896
span: self.tcx.def_span(closure_def_id),
891897
kind: expected_kind.as_str(),
898+
coro_kind,
892899
});
893900
self.note_obligation_cause(&mut err, &obligation);
894901
return Some(err.emit());

compiler/rustc_trait_selection/src/errors.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,12 @@ pub struct ClosureFnMutLabel {
201201
}
202202

203203
#[derive(Diagnostic)]
204-
#[diag(trait_selection_async_closure_not_fn)]
205-
pub(crate) struct AsyncClosureNotFn {
204+
#[diag(trait_selection_coro_closure_not_fn)]
205+
pub(crate) struct CoroClosureNotFn {
206206
#[primary_span]
207207
pub span: Span,
208208
pub kind: &'static str,
209+
pub coro_kind: String,
209210
}
210211

211212
#[derive(Diagnostic)]

tests/ui/iterators/generator_returned_from_fn.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | | }
1010
LL | | } }
1111
| |_____^ returns a reference to data owned by the current function
1212

13-
error: async closure does not implement `Fn` because it captures state from its environment
13+
error: `gen` closure does not implement `Fn` because it captures state from its environment
1414
--> $DIR/generator_returned_from_fn.rs:35:13
1515
|
1616
LL | iter! { move || {

0 commit comments

Comments
 (0)