Skip to content

Commit e84ccac

Browse files
oli-obkeholk
authored andcommitted
Make diagnostics know about more coro closures than async closures
1 parent 38ce680 commit e84ccac

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
@@ -1943,9 +1943,7 @@ impl CoroutineKind {
19431943
CoroutineKind::Coroutine(mov) => mov,
19441944
}
19451945
}
1946-
}
19471946

1948-
impl CoroutineKind {
19491947
pub fn is_fn_like(self) -> bool {
19501948
matches!(self, CoroutineKind::Desugared(_, CoroutineSource::Fn))
19511949
}

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
@@ -39,9 +39,7 @@ use super::{
3939
use crate::error_reporting::TypeErrCtxt;
4040
use crate::error_reporting::infer::TyCategory;
4141
use crate::error_reporting::traits::report_dyn_incompatibility;
42-
use crate::errors::{
43-
AsyncClosureNotFn, ClosureFnMutLabel, ClosureFnOnceLabel, ClosureKindMismatch,
44-
};
42+
use crate::errors::{ClosureFnMutLabel, ClosureFnOnceLabel, ClosureKindMismatch, CoroClosureNotFn};
4543
use crate::infer::{self, InferCtxt, InferCtxtExt as _};
4644
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
4745
use crate::traits::{
@@ -906,9 +904,18 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
906904
&& let ty::FnPtr(sig_tys, _) = by_ref_captures.kind()
907905
&& !sig_tys.skip_binder().output().is_unit()
908906
{
909-
let mut err = self.dcx().create_err(AsyncClosureNotFn {
907+
let coro_kind = match self
908+
.tcx
909+
.coroutine_kind(self.tcx.coroutine_for_closure(closure_def_id))
910+
.unwrap()
911+
{
912+
rustc_hir::CoroutineKind::Desugared(desugaring, _) => format!("{desugaring:#}"),
913+
coro => format!("{coro:#}"),
914+
};
915+
let mut err = self.dcx().create_err(CoroClosureNotFn {
910916
span: self.tcx.def_span(closure_def_id),
911917
kind: expected_kind.as_str(),
918+
coro_kind,
912919
});
913920
self.note_obligation_cause(&mut err, &obligation);
914921
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
@@ -170,11 +170,12 @@ pub struct ClosureFnMutLabel {
170170
}
171171

172172
#[derive(Diagnostic)]
173-
#[diag(trait_selection_async_closure_not_fn)]
174-
pub(crate) struct AsyncClosureNotFn {
173+
#[diag(trait_selection_coro_closure_not_fn)]
174+
pub(crate) struct CoroClosureNotFn {
175175
#[primary_span]
176176
pub span: Span,
177177
pub kind: &'static str,
178+
pub coro_kind: String,
178179
}
179180

180181
#[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)