Skip to content

Commit a589632

Browse files
committed
Add explicit syntax for coroutines instead of relying on closures having yield expressions
1 parent 29a56a3 commit a589632

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

Diff for: 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,
@@ -955,6 +956,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
955956
binder: &ClosureBinder,
956957
capture_clause: CaptureBy,
957958
closure_id: NodeId,
959+
closure_hir_id: hir::HirId,
958960
constness: Const,
959961
movability: Movability,
960962
decl: &FnDecl,
@@ -965,7 +967,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
965967
let (binder_clause, generic_params) = self.lower_closure_binder(binder);
966968

967969
let (body_id, closure_kind) = self.with_new_scopes(fn_decl_span, move |this| {
968-
let mut coroutine_kind = None;
970+
let mut coroutine_kind = if this
971+
.attrs
972+
.get(&closure_hir_id.local_id)
973+
.is_some_and(|attrs| attrs.iter().any(|attr| attr.has_name(sym::coroutine)))
974+
{
975+
Some(hir::CoroutineKind::Coroutine(Movability::Movable))
976+
} else {
977+
None
978+
};
969979
let body_id = this.lower_fn_body(decl, |this| {
970980
let e = this.lower_expr_mut(body);
971981
coroutine_kind = this.coroutine_kind;

Diff for: 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
// ==========================================================================

Diff for: 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)