Skip to content

Commit 5abb286

Browse files
committed
Add explicit syntax for coroutines instead of relying on closures having yield expressions
1 parent 6c6b302 commit 5abb286

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
217217
binder,
218218
*capture_clause,
219219
e.id,
220+
hir_id,
220221
*constness,
221222
*movability,
222223
fn_decl,
@@ -958,6 +959,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
958959
binder: &ClosureBinder,
959960
capture_clause: CaptureBy,
960961
closure_id: NodeId,
962+
closure_hir_id: hir::HirId,
961963
constness: Const,
962964
movability: Movability,
963965
decl: &FnDecl,
@@ -968,7 +970,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
968970
let (binder_clause, generic_params) = self.lower_closure_binder(binder);
969971

970972
let (body_id, closure_kind) = self.with_new_scopes(fn_decl_span, move |this| {
971-
let mut coroutine_kind = None;
973+
let mut coroutine_kind = if this
974+
.attrs
975+
.get(&closure_hir_id.local_id)
976+
.is_some_and(|attrs| attrs.iter().any(|attr| attr.has_name(sym::coroutine)))
977+
{
978+
Some(hir::CoroutineKind::Coroutine(Movability::Movable))
979+
} else {
980+
None
981+
};
972982
let body_id = this.lower_fn_body(decl, |this| {
973983
let e = this.lower_expr_mut(body);
974984
coroutine_kind = this.coroutine_kind;

compiler/rustc_feature/src/builtin_attrs.rs

+6
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,12 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
534534
EncodeCrossCrate::Yes, experimental!(cfi_encoding)
535535
),
536536

537+
// `#[coroutine]` attribute to be applied to closures to make them coroutines instead
538+
gated!(
539+
coroutine, Normal, template!(Word), ErrorFollowing,
540+
EncodeCrossCrate::No, coroutines, experimental!(coroutines)
541+
),
542+
537543
// ==========================================================================
538544
// Internal attributes: Stability, deprecation, and unsafe:
539545
// ==========================================================================

tests/ui/coroutine/derived-drop-parent-expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub struct Client { pub nickname: String }
1010

1111
fn main() {
1212
let g = move || match drop(Client { ..Client::default() }) {
13-
_status => yield,
14-
};
13+
_status => yield,
14+
};
1515
assert_send(g);
1616
}

0 commit comments

Comments
 (0)