Skip to content

Commit 904e2af

Browse files
committed
Port #[coroutine] to the new attribute system
Related to #131229 (comment).
1 parent e1b9081 commit 904e2af

File tree

8 files changed

+42
-23
lines changed

8 files changed

+42
-23
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
9898
}
9999

100100
let expr_hir_id = self.lower_node_id(e.id);
101-
self.lower_attrs(expr_hir_id, &e.attrs, e.span);
101+
let attrs = self.lower_attrs(expr_hir_id, &e.attrs, e.span);
102102

103103
let kind = match &e.kind {
104104
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
@@ -232,10 +232,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
232232
*fn_arg_span,
233233
),
234234
None => self.lower_expr_closure(
235+
attrs,
235236
binder,
236237
*capture_clause,
237238
e.id,
238-
expr_hir_id,
239239
*constness,
240240
*movability,
241241
fn_decl,
@@ -1052,10 +1052,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
10521052

10531053
fn lower_expr_closure(
10541054
&mut self,
1055+
attrs: &[rustc_hir::Attribute],
10551056
binder: &ClosureBinder,
10561057
capture_clause: CaptureBy,
10571058
closure_id: NodeId,
1058-
closure_hir_id: hir::HirId,
10591059
constness: Const,
10601060
movability: Movability,
10611061
decl: &FnDecl,
@@ -1067,15 +1067,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
10671067
let (binder_clause, generic_params) = self.lower_closure_binder(binder);
10681068

10691069
let (body_id, closure_kind) = self.with_new_scopes(fn_decl_span, move |this| {
1070-
let mut coroutine_kind = if this
1071-
.attrs
1072-
.get(&closure_hir_id.local_id)
1073-
.is_some_and(|attrs| attrs.iter().any(|attr| attr.has_name(sym::coroutine)))
1074-
{
1075-
Some(hir::CoroutineKind::Coroutine(Movability::Movable))
1076-
} else {
1077-
None
1078-
};
1070+
1071+
let mut coroutine_kind = find_attr!(attrs, AttributeKind::Coroutine(_) => hir::CoroutineKind::Coroutine(Movability::Movable));
1072+
10791073
// FIXME(contracts): Support contracts on closures?
10801074
let body_id = this.lower_fn_body(decl, None, |this| {
10811075
this.coroutine_kind = coroutine_kind;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//! Attributes that can be found in function body.
2+
3+
use rustc_hir::attrs::AttributeKind;
4+
use rustc_span::{Symbol, sym};
5+
6+
use super::{NoArgsAttributeParser, OnDuplicate};
7+
use crate::context::Stage;
8+
9+
pub(crate) struct CoroutineParser;
10+
11+
impl<S: Stage> NoArgsAttributeParser<S> for CoroutineParser {
12+
const PATH: &[Symbol] = &[sym::coroutine];
13+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
14+
const CREATE: fn(rustc_span::Span) -> AttributeKind = |span| AttributeKind::Coroutine(span);
15+
}

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::parser::ArgParser;
2626
use crate::session_diagnostics::UnusedMultiple;
2727

2828
pub(crate) mod allow_unstable;
29+
pub(crate) mod body;
2930
pub(crate) mod cfg;
3031
pub(crate) mod cfg_old;
3132
pub(crate) mod codegen_attrs;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1616
use crate::attributes::allow_unstable::{
1717
AllowConstFnUnstableParser, AllowInternalUnstableParser, UnstableFeatureBoundParser,
1818
};
19+
use crate::attributes::body::CoroutineParser;
1920
use crate::attributes::codegen_attrs::{
2021
ColdParser, CoverageParser, ExportNameParser, NakedParser, NoMangleParser, OptimizeParser,
2122
TargetFeatureParser, TrackCallerParser, UsedParser,
@@ -184,6 +185,7 @@ attribute_parsers!(
184185
Single<WithoutArgs<ConstContinueParser>>,
185186
Single<WithoutArgs<ConstStabilityIndirectParser>>,
186187
Single<WithoutArgs<ConstTraitParser>>,
188+
Single<WithoutArgs<CoroutineParser>>,
187189
Single<WithoutArgs<DenyExplicitImplParser>>,
188190
Single<WithoutArgs<DoNotImplementViaObjectParser>>,
189191
Single<WithoutArgs<ExportStableParser>>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ pub enum AttributeKind {
297297
/// Represents `#[const_trait]`.
298298
ConstTrait(Span),
299299

300+
/// Represents `#[coroutine]`.
301+
Coroutine(Span),
302+
300303
/// Represents `#[coverage(..)]`.
301304
Coverage(Span, CoverageAttrKind),
302305

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ impl AttributeKind {
2828
ConstStability { .. } => Yes,
2929
ConstStabilityIndirect => No,
3030
ConstTrait(..) => No,
31+
Coroutine(..) => No,
3132
Coverage(..) => No,
3233
DenyExplicitImpl(..) => No,
3334
Deprecation { .. } => Yes,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
324324
&Attribute::Parsed(AttributeKind::Coverage(attr_span, _)) => {
325325
self.check_coverage(attr_span, span, target)
326326
}
327+
&Attribute::Parsed(AttributeKind::Coroutine(attr_span)) => {
328+
self.check_coroutine(attr_span, target)
329+
}
327330
Attribute::Unparsed(attr_item) => {
328331
style = Some(attr_item.style);
329332
match attr.path().as_slice() {
@@ -390,9 +393,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
390393
[sym::autodiff_forward, ..] | [sym::autodiff_reverse, ..] => {
391394
self.check_autodiff(hir_id, attr, span, target)
392395
}
393-
[sym::coroutine, ..] => {
394-
self.check_coroutine(attr, target);
395-
}
396396
[sym::linkage, ..] => self.check_linkage(attr, span, target),
397397
[
398398
// ok
@@ -2651,11 +2651,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
26512651
}
26522652
}
26532653

2654-
fn check_coroutine(&self, attr: &Attribute, target: Target) {
2654+
fn check_coroutine(&self, attr_span: Span, target: Target) {
26552655
match target {
26562656
Target::Closure => return,
26572657
_ => {
2658-
self.dcx().emit_err(errors::CoroutineOnNonClosure { span: attr.span() });
2658+
self.dcx().emit_err(errors::CoroutineOnNonClosure { span: attr_span });
26592659
}
26602660
}
26612661
}

tests/ui/attributes/malformed-attrs.stderr

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ error: malformed `patchable_function_entry` attribute input
5555
LL | #[patchable_function_entry]
5656
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[patchable_function_entry(prefix_nops = m, entry_nops = n)]`
5757

58-
error: malformed `coroutine` attribute input
59-
--> $DIR/malformed-attrs.rs:108:5
60-
|
61-
LL | #[coroutine = 63] || {}
62-
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[coroutine]`
63-
6458
error: malformed `must_not_suspend` attribute input
6559
--> $DIR/malformed-attrs.rs:129:1
6660
|
@@ -436,6 +430,15 @@ LL | #[proc_macro = 18]
436430
| | didn't expect any arguments here
437431
| help: must be of the form: `#[proc_macro]`
438432

433+
error[E0565]: malformed `coroutine` attribute input
434+
--> $DIR/malformed-attrs.rs:108:5
435+
|
436+
LL | #[coroutine = 63] || {}
437+
| ^^^^^^^^^^^^----^
438+
| | |
439+
| | didn't expect any arguments here
440+
| help: must be of the form: `#[coroutine]`
441+
439442
error[E0565]: malformed `proc_macro_attribute` attribute input
440443
--> $DIR/malformed-attrs.rs:113:1
441444
|

0 commit comments

Comments
 (0)