diff options
| author | Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de> | 2024-04-11 12:48:32 +0000 |
|---|---|---|
| committer | Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de> | 2024-04-24 07:54:21 +0000 |
| commit | a589632dad58c2669b7984019efb59917a16ac67 (patch) | |
| tree | 8dfc3affad8580bf29e1f01a13a5972dd793e557 | |
| parent | 29a56a3b1c06a624c0c06728c0af756d09df6b1b (diff) | |
| download | rust-a589632dad58c2669b7984019efb59917a16ac67.tar.gz rust-a589632dad58c2669b7984019efb59917a16ac67.zip | |
Add explicit syntax for coroutines instead of relying on closures having `yield` expressions
| -rw-r--r-- | compiler/rustc_ast_lowering/src/expr.rs | 12 | ||||
| -rw-r--r-- | compiler/rustc_feature/src/builtin_attrs.rs | 6 | ||||
| -rw-r--r-- | tests/ui/coroutine/derived-drop-parent-expr.rs | 4 |
3 files changed, 19 insertions, 3 deletions
diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 2305cc07795..6c3f0343147 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -217,6 +217,7 @@ impl<'hir> LoweringContext<'_, 'hir> { binder, *capture_clause, e.id, + hir_id, *constness, *movability, fn_decl, @@ -955,6 +956,7 @@ impl<'hir> LoweringContext<'_, 'hir> { binder: &ClosureBinder, capture_clause: CaptureBy, closure_id: NodeId, + closure_hir_id: hir::HirId, constness: Const, movability: Movability, decl: &FnDecl, @@ -965,7 +967,15 @@ impl<'hir> LoweringContext<'_, 'hir> { let (binder_clause, generic_params) = self.lower_closure_binder(binder); let (body_id, closure_kind) = self.with_new_scopes(fn_decl_span, move |this| { - let mut coroutine_kind = None; + let mut coroutine_kind = if this + .attrs + .get(&closure_hir_id.local_id) + .is_some_and(|attrs| attrs.iter().any(|attr| attr.has_name(sym::coroutine))) + { + Some(hir::CoroutineKind::Coroutine(Movability::Movable)) + } else { + None + }; let body_id = this.lower_fn_body(decl, |this| { let e = this.lower_expr_mut(body); coroutine_kind = this.coroutine_kind; diff --git a/compiler/rustc_feature/src/builtin_attrs.rs b/compiler/rustc_feature/src/builtin_attrs.rs index 23b3f31b437..647cd8d06f2 100644 --- a/compiler/rustc_feature/src/builtin_attrs.rs +++ b/compiler/rustc_feature/src/builtin_attrs.rs @@ -534,6 +534,12 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[ EncodeCrossCrate::Yes, experimental!(cfi_encoding) ), + // `#[coroutine]` attribute to be applied to closures to make them coroutines instead + gated!( + coroutine, Normal, template!(Word), ErrorFollowing, + EncodeCrossCrate::No, coroutines, experimental!(coroutines) + ), + // ========================================================================== // Internal attributes: Stability, deprecation, and unsafe: // ========================================================================== diff --git a/tests/ui/coroutine/derived-drop-parent-expr.rs b/tests/ui/coroutine/derived-drop-parent-expr.rs index f70a732c90f..71f7506ab84 100644 --- a/tests/ui/coroutine/derived-drop-parent-expr.rs +++ b/tests/ui/coroutine/derived-drop-parent-expr.rs @@ -10,7 +10,7 @@ pub struct Client { pub nickname: String } fn main() { let g = move || match drop(Client { ..Client::default() }) { - _status => yield, - }; + _status => yield, + }; assert_send(g); } |
