Skip to content

Commit af95313

Browse files
Support async gen fn
1 parent 4a41f03 commit af95313

File tree

16 files changed

+115
-104
lines changed

16 files changed

+115
-104
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,10 +2415,12 @@ pub enum Unsafe {
24152415
/// Iterator`.
24162416
#[derive(Copy, Clone, Encodable, Decodable, Debug)]
24172417
pub enum CoroutineKind {
2418-
/// `async`, which evaluates to `impl Future`
2418+
/// `async`, which returns an `impl Future`
24192419
Async { span: Span, closure_id: NodeId, return_impl_trait_id: NodeId },
2420-
/// `gen`, which evaluates to `impl Iterator`
2420+
/// `gen`, which returns an `impl Iterator`
24212421
Gen { span: Span, closure_id: NodeId, return_impl_trait_id: NodeId },
2422+
/// `async gen`, which returns an `impl AsyncIterator`
2423+
AsyncGen { span: Span, closure_id: NodeId, return_impl_trait_id: NodeId },
24222424
}
24232425

24242426
impl CoroutineKind {
@@ -2435,7 +2437,10 @@ impl CoroutineKind {
24352437
pub fn return_id(self) -> (NodeId, Span) {
24362438
match self {
24372439
CoroutineKind::Async { return_impl_trait_id, span, .. }
2438-
| CoroutineKind::Gen { return_impl_trait_id, span, .. } => (return_impl_trait_id, span),
2440+
| CoroutineKind::Gen { return_impl_trait_id, span, .. }
2441+
| CoroutineKind::AsyncGen { return_impl_trait_id, span, .. } => {
2442+
(return_impl_trait_id, span)
2443+
}
24392444
}
24402445
}
24412446
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,8 @@ pub fn noop_visit_closure_binder<T: MutVisitor>(binder: &mut ClosureBinder, vis:
874874
pub fn noop_visit_coroutine_kind<T: MutVisitor>(coroutine_kind: &mut CoroutineKind, vis: &mut T) {
875875
match coroutine_kind {
876876
CoroutineKind::Async { span, closure_id, return_impl_trait_id }
877-
| CoroutineKind::Gen { span, closure_id, return_impl_trait_id } => {
877+
| CoroutineKind::Gen { span, closure_id, return_impl_trait_id }
878+
| CoroutineKind::AsyncGen { span, closure_id, return_impl_trait_id } => {
878879
vis.visit_span(span);
879880
vis.visit_id(closure_id);
880881
vis.visit_id(return_impl_trait_id);

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_ast::*;
1313
use rustc_data_structures::stack::ensure_sufficient_stack;
1414
use rustc_hir as hir;
1515
use rustc_hir::def::{DefKind, Res};
16+
use rustc_middle::span_bug;
1617
use rustc_session::errors::report_lit_error;
1718
use rustc_span::source_map::{respan, Spanned};
1819
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -202,15 +203,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
202203
fn_decl_span,
203204
fn_arg_span,
204205
}) => match coroutine_kind {
205-
Some(
206-
CoroutineKind::Async { closure_id, .. }
207-
| CoroutineKind::Gen { closure_id, .. },
208-
) => self.lower_expr_async_closure(
206+
Some(coroutine_kind) => self.lower_expr_coroutine_closure(
209207
binder,
210208
*capture_clause,
211209
e.id,
212210
hir_id,
213-
*closure_id,
211+
*coroutine_kind,
214212
fn_decl,
215213
body,
216214
*fn_decl_span,
@@ -1098,18 +1096,22 @@ impl<'hir> LoweringContext<'_, 'hir> {
10981096
(binder, params)
10991097
}
11001098

1101-
fn lower_expr_async_closure(
1099+
fn lower_expr_coroutine_closure(
11021100
&mut self,
11031101
binder: &ClosureBinder,
11041102
capture_clause: CaptureBy,
11051103
closure_id: NodeId,
11061104
closure_hir_id: hir::HirId,
1107-
inner_closure_id: NodeId,
1105+
coroutine_kind: CoroutineKind,
11081106
decl: &FnDecl,
11091107
body: &Expr,
11101108
fn_decl_span: Span,
11111109
fn_arg_span: Span,
11121110
) -> hir::ExprKind<'hir> {
1111+
let CoroutineKind::Async { closure_id: inner_closure_id, .. } = coroutine_kind else {
1112+
span_bug!(fn_decl_span, "`async gen` and `gen` closures are not supported, yet");
1113+
};
1114+
11131115
if let &ClosureBinder::For { span, .. } = binder {
11141116
self.tcx.sess.emit_err(NotSupportedForLifetimeBinderAsyncClosure { span });
11151117
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,11 +1035,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
10351035
let (Some(coroutine_kind), Some(body)) = (coroutine_kind, body) else {
10361036
return self.lower_fn_body_block(span, decl, body);
10371037
};
1038-
let closure_id = match coroutine_kind {
1039-
CoroutineKind::Async { closure_id, .. } | CoroutineKind::Gen { closure_id, .. } => {
1040-
closure_id
1041-
}
1042-
};
1038+
let (CoroutineKind::Async { closure_id, .. }
1039+
| CoroutineKind::Gen { closure_id, .. }
1040+
| CoroutineKind::AsyncGen { closure_id, .. }) = coroutine_kind;
10431041

10441042
self.lower_body(|this| {
10451043
let mut parameters: Vec<hir::Param<'_>> = Vec::new();
@@ -1224,6 +1222,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
12241222
hir::CoroutineSource::Fn,
12251223
mkbody,
12261224
),
1225+
CoroutineKind::AsyncGen { .. } => this.make_async_gen_expr(
1226+
CaptureBy::Value { move_kw: rustc_span::DUMMY_SP },
1227+
closure_id,
1228+
None,
1229+
body.span,
1230+
hir::CoroutineSource::Fn,
1231+
mkbody,
1232+
),
12271233
};
12281234

12291235
let hir_id = this.lower_node_id(closure_id);

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,7 +1904,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19041904

19051905
let opaque_ty_node_id = match coro {
19061906
CoroutineKind::Async { return_impl_trait_id, .. }
1907-
| CoroutineKind::Gen { return_impl_trait_id, .. } => return_impl_trait_id,
1907+
| CoroutineKind::Gen { return_impl_trait_id, .. }
1908+
| CoroutineKind::AsyncGen { return_impl_trait_id, .. } => return_impl_trait_id,
19081909
};
19091910

19101911
let captured_lifetimes: Vec<_> = self
@@ -1960,8 +1961,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19601961

19611962
// "<$assoc_ty_name = T>"
19621963
let (assoc_ty_name, trait_lang_item) = match coro {
1963-
CoroutineKind::Async { .. } => (hir::FN_OUTPUT_NAME, hir::LangItem::Future),
1964-
CoroutineKind::Gen { .. } => (hir::ITERATOR_ITEM_NAME, hir::LangItem::Iterator),
1964+
CoroutineKind::Async { .. } => (sym::Output, hir::LangItem::Future),
1965+
CoroutineKind::Gen { .. } => (sym::Item, hir::LangItem::Iterator),
1966+
CoroutineKind::AsyncGen { .. } => (sym::Item, hir::LangItem::AsyncIterator),
19651967
};
19661968

19671969
let future_args = self.arena.alloc(hir::GenericArgs {

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
389389
FnRetTy::Default(_) => self.arena.alloc(self.ty_tup(*span, &[])),
390390
};
391391
let args = smallvec![GenericArg::Type(self.arena.alloc(self.ty_tup(*inputs_span, inputs)))];
392-
let binding = self.assoc_ty_binding(hir::FN_OUTPUT_NAME, output_ty.span, output_ty);
392+
let binding = self.assoc_ty_binding(sym::Output, output_ty.span, output_ty);
393393
(
394394
GenericArgsCtor {
395395
args,

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,10 @@ impl<'a> State<'a> {
14981498
ast::CoroutineKind::Async { .. } => {
14991499
self.word_nbsp("async");
15001500
}
1501+
ast::CoroutineKind::AsyncGen { .. } => {
1502+
self.word_nbsp("async");
1503+
self.word_nbsp("gen");
1504+
}
15011505
}
15021506
}
15031507

compiler/rustc_hir/src/hir.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,11 +2255,6 @@ pub enum ImplItemKind<'hir> {
22552255
Type(&'hir Ty<'hir>),
22562256
}
22572257

2258-
/// The name of the associated type for `Fn` return types.
2259-
pub const FN_OUTPUT_NAME: Symbol = sym::Output;
2260-
/// The name of the associated type for `Iterator` item types.
2261-
pub const ITERATOR_ITEM_NAME: Symbol = sym::Item;
2262-
22632258
/// Bind a type to an associated type (i.e., `A = Foo`).
22642259
///
22652260
/// Bindings like `A: Debug` are represented as a special type `A =

compiler/rustc_parse/messages.ftl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ parse_async_block_in_2015 = `async` blocks are only allowed in Rust 2018 or late
2323
parse_async_fn_in_2015 = `async fn` is not permitted in Rust 2015
2424
.label = to use `async fn`, switch to Rust 2018 or later
2525
26-
parse_async_gen_fn = `async gen` functions are not supported
27-
2826
parse_async_move_block_in_2015 = `async move` blocks are only allowed in Rust 2018 or later
2927
3028
parse_async_move_order_incorrect = the order of `move` and `async` is incorrect

compiler/rustc_parse/src/errors.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -562,13 +562,6 @@ pub(crate) struct GenFn {
562562
pub span: Span,
563563
}
564564

565-
#[derive(Diagnostic)]
566-
#[diag(parse_async_gen_fn)]
567-
pub(crate) struct AsyncGenFn {
568-
#[primary_span]
569-
pub span: Span,
570-
}
571-
572565
#[derive(Diagnostic)]
573566
#[diag(parse_comma_after_base_struct)]
574567
#[note]

0 commit comments

Comments
 (0)