Skip to content

Commit 2a3779b

Browse files
committed
Add explicit syntax for coroutines instead of relying on closures having yield expressions
1 parent 7bdae13 commit 2a3779b

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
@@ -216,6 +216,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
216216
binder,
217217
*capture_clause,
218218
e.id,
219+
hir_id,
219220
*constness,
220221
*movability,
221222
fn_decl,
@@ -957,6 +958,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
957958
binder: &ClosureBinder,
958959
capture_clause: CaptureBy,
959960
closure_id: NodeId,
961+
closure_hir_id: hir::HirId,
960962
constness: Const,
961963
movability: Movability,
962964
decl: &FnDecl,
@@ -967,7 +969,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
967969
let (binder_clause, generic_params) = self.lower_closure_binder(binder);
968970

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