Skip to content

Commit d116f17

Browse files
committed
Merge Async and Gen into CoroutineKind
1 parent 84508c8 commit d116f17

File tree

25 files changed

+247
-239
lines changed

25 files changed

+247
-239
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ pub struct Closure {
13111311
pub binder: ClosureBinder,
13121312
pub capture_clause: CaptureBy,
13131313
pub constness: Const,
1314-
pub asyncness: Async,
1314+
pub coro_kind: CoroutineKind,
13151315
pub movability: Movability,
13161316
pub fn_decl: P<FnDecl>,
13171317
pub body: P<Expr>,
@@ -2394,28 +2394,38 @@ pub enum Unsafe {
23942394
No,
23952395
}
23962396

2397+
/// Describes what kind of coroutine markers, if any, a function has.
2398+
///
2399+
/// Coroutine markers are things that cause the function to generate a coroutine, such as `async`,
2400+
/// which makes the function return `impl Future`, or `gen`, which makes the function return `impl
2401+
/// Iterator`.
23972402
#[derive(Copy, Clone, Encodable, Decodable, Debug)]
2398-
pub enum Async {
2399-
Yes { span: Span, closure_id: NodeId, return_impl_trait_id: NodeId },
2400-
No,
2401-
}
2402-
2403-
#[derive(Copy, Clone, Encodable, Decodable, Debug)]
2404-
pub enum Gen {
2405-
Yes { span: Span, closure_id: NodeId, return_impl_trait_id: NodeId },
2406-
No,
2403+
pub enum CoroutineKind {
2404+
/// `async`, which evaluates to `impl Future`
2405+
Async { span: Span, closure_id: NodeId, return_impl_trait_id: NodeId },
2406+
/// `gen`, which evaluates to `impl Iterator`
2407+
Gen { span: Span, closure_id: NodeId, return_impl_trait_id: NodeId },
2408+
/// Neither `async` nor `gen`
2409+
None,
24072410
}
24082411

2409-
impl Async {
2412+
impl CoroutineKind {
24102413
pub fn is_async(self) -> bool {
2411-
matches!(self, Async::Yes { .. })
2414+
matches!(self, CoroutineKind::Async { .. })
2415+
}
2416+
2417+
pub fn is_gen(self) -> bool {
2418+
matches!(self, CoroutineKind::Gen { .. })
24122419
}
24132420

24142421
/// In this case this is an `async` return, the `NodeId` for the generated `impl Trait` item.
24152422
pub fn opt_return_id(self) -> Option<(NodeId, Span)> {
24162423
match self {
2417-
Async::Yes { return_impl_trait_id, span, .. } => Some((return_impl_trait_id, span)),
2418-
Async::No => None,
2424+
CoroutineKind::Async { return_impl_trait_id, span, .. }
2425+
| CoroutineKind::Gen { return_impl_trait_id, span, .. } => {
2426+
Some((return_impl_trait_id, span))
2427+
}
2428+
CoroutineKind::None => None,
24192429
}
24202430
}
24212431
}
@@ -2819,36 +2829,32 @@ impl Extern {
28192829
pub struct FnHeader {
28202830
/// The `unsafe` keyword, if any
28212831
pub unsafety: Unsafe,
2822-
/// The `async` keyword, if any
2823-
pub asyncness: Async,
2832+
/// Whether this is `async`, `gen`, or nothing.
2833+
pub coro_kind: CoroutineKind,
28242834
/// The `const` keyword, if any
28252835
pub constness: Const,
28262836
/// The `extern` keyword and corresponding ABI string, if any
28272837
pub ext: Extern,
2828-
/// The `gen` keyword, if any
2829-
pub genness: Gen,
28302838
}
28312839

28322840
impl FnHeader {
28332841
/// Does this function header have any qualifiers or is it empty?
28342842
pub fn has_qualifiers(&self) -> bool {
2835-
let Self { unsafety, asyncness, constness, ext, genness } = self;
2843+
let Self { unsafety, coro_kind, constness, ext } = self;
28362844
matches!(unsafety, Unsafe::Yes(_))
2837-
|| asyncness.is_async()
2845+
|| !matches!(coro_kind, CoroutineKind::None)
28382846
|| matches!(constness, Const::Yes(_))
28392847
|| !matches!(ext, Extern::None)
2840-
|| matches!(genness, Gen::Yes { .. })
28412848
}
28422849
}
28432850

28442851
impl Default for FnHeader {
28452852
fn default() -> FnHeader {
28462853
FnHeader {
28472854
unsafety: Unsafe::No,
2848-
asyncness: Async::No,
2855+
coro_kind: CoroutineKind::None,
28492856
constness: Const::No,
28502857
ext: Extern::None,
2851-
genness: Gen::No,
28522858
}
28532859
}
28542860
}
@@ -3169,7 +3175,7 @@ mod size_asserts {
31693175
static_assert_size!(Block, 32);
31703176
static_assert_size!(Expr, 72);
31713177
static_assert_size!(ExprKind, 40);
3172-
static_assert_size!(Fn, 168);
3178+
static_assert_size!(Fn, 160);
31733179
static_assert_size!(ForeignItem, 96);
31743180
static_assert_size!(ForeignItemKind, 24);
31753181
static_assert_size!(GenericArg, 24);

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,8 @@ pub trait MutVisitor: Sized {
121121
noop_visit_fn_decl(d, self);
122122
}
123123

124-
fn visit_asyncness(&mut self, a: &mut Async) {
125-
noop_visit_asyncness(a, self);
126-
}
127-
128-
fn visit_genness(&mut self, a: &mut Gen) {
129-
noop_visit_genness(a, self);
124+
fn visit_coro_kind(&mut self, a: &mut CoroutineKind) {
125+
noop_visit_coro_kind(a, self);
130126
}
131127

132128
fn visit_closure_binder(&mut self, b: &mut ClosureBinder) {
@@ -875,23 +871,14 @@ pub fn noop_visit_closure_binder<T: MutVisitor>(binder: &mut ClosureBinder, vis:
875871
}
876872
}
877873

878-
pub fn noop_visit_asyncness<T: MutVisitor>(asyncness: &mut Async, vis: &mut T) {
879-
match asyncness {
880-
Async::Yes { span: _, closure_id, return_impl_trait_id } => {
881-
vis.visit_id(closure_id);
882-
vis.visit_id(return_impl_trait_id);
883-
}
884-
Async::No => {}
885-
}
886-
}
887-
888-
pub fn noop_visit_genness<T: MutVisitor>(genness: &mut Gen, vis: &mut T) {
889-
match genness {
890-
Gen::Yes { span: _, closure_id, return_impl_trait_id } => {
874+
pub fn noop_visit_coro_kind<T: MutVisitor>(coro_kind: &mut CoroutineKind, vis: &mut T) {
875+
match coro_kind {
876+
CoroutineKind::Async { span: _, closure_id, return_impl_trait_id }
877+
| CoroutineKind::Gen { span: _, closure_id, return_impl_trait_id } => {
891878
vis.visit_id(closure_id);
892879
vis.visit_id(return_impl_trait_id);
893880
}
894-
Gen::No => {}
881+
CoroutineKind::None => {}
895882
}
896883
}
897884

@@ -1184,10 +1171,9 @@ fn visit_const_item<T: MutVisitor>(
11841171
}
11851172

11861173
pub fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
1187-
let FnHeader { unsafety, asyncness, constness, ext: _, genness } = header;
1174+
let FnHeader { unsafety, coro_kind, constness, ext: _ } = header;
11881175
visit_constness(constness, vis);
1189-
vis.visit_asyncness(asyncness);
1190-
vis.visit_genness(genness);
1176+
vis.visit_coro_kind(coro_kind);
11911177
visit_unsafety(unsafety, vis);
11921178
}
11931179

@@ -1421,7 +1407,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
14211407
binder,
14221408
capture_clause,
14231409
constness,
1424-
asyncness,
1410+
coro_kind,
14251411
movability: _,
14261412
fn_decl,
14271413
body,
@@ -1430,7 +1416,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
14301416
}) => {
14311417
vis.visit_closure_binder(binder);
14321418
visit_constness(constness, vis);
1433-
vis.visit_asyncness(asyncness);
1419+
vis.visit_coro_kind(coro_kind);
14341420
vis.visit_capture_by(capture_clause);
14351421
vis.visit_fn_decl(fn_decl);
14361422
vis.visit_expr(body);

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
861861
ExprKind::Closure(box Closure {
862862
binder,
863863
capture_clause,
864-
asyncness: _,
864+
coro_kind: _,
865865
constness: _,
866866
movability: _,
867867
fn_decl,

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
1414
use rustc_hir as hir;
1515
use rustc_hir::def::{DefKind, Res};
1616
use rustc_hir::definitions::DefPathData;
17+
use rustc_middle::span_bug;
1718
use rustc_session::errors::report_lit_error;
1819
use rustc_span::source_map::{respan, Spanned};
1920
use rustc_span::symbol::{sym, Ident, Symbol};
@@ -196,39 +197,39 @@ impl<'hir> LoweringContext<'_, 'hir> {
196197
binder,
197198
capture_clause,
198199
constness,
199-
asyncness,
200+
coro_kind,
200201
movability,
201202
fn_decl,
202203
body,
203204
fn_decl_span,
204205
fn_arg_span,
205-
}) => {
206-
if let Async::Yes { closure_id, .. } = asyncness {
207-
self.lower_expr_async_closure(
208-
binder,
209-
*capture_clause,
210-
e.id,
211-
hir_id,
212-
*closure_id,
213-
fn_decl,
214-
body,
215-
*fn_decl_span,
216-
*fn_arg_span,
217-
)
218-
} else {
219-
self.lower_expr_closure(
220-
binder,
221-
*capture_clause,
222-
e.id,
223-
*constness,
224-
*movability,
225-
fn_decl,
226-
body,
227-
*fn_decl_span,
228-
*fn_arg_span,
229-
)
206+
}) => match coro_kind {
207+
CoroutineKind::Async { closure_id, .. } => self.lower_expr_async_closure(
208+
binder,
209+
*capture_clause,
210+
e.id,
211+
hir_id,
212+
*closure_id,
213+
fn_decl,
214+
body,
215+
*fn_decl_span,
216+
*fn_arg_span,
217+
),
218+
CoroutineKind::Gen { .. } => {
219+
span_bug!(e.span, "generator closures are not allowed")
230220
}
231-
}
221+
CoroutineKind::None => self.lower_expr_closure(
222+
binder,
223+
*capture_clause,
224+
e.id,
225+
*constness,
226+
*movability,
227+
fn_decl,
228+
body,
229+
*fn_decl_span,
230+
*fn_arg_span,
231+
),
232+
},
232233
ExprKind::Block(blk, opt_label) => {
233234
let opt_label = self.lower_label(*opt_label);
234235
hir::ExprKind::Block(self.lower_block(blk, opt_label.is_some()), opt_label)
@@ -936,7 +937,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
936937

937938
let bound_generic_params = self.lower_lifetime_binder(closure_id, generic_params);
938939
// Lower outside new scope to preserve `is_in_loop_condition`.
939-
let fn_decl = self.lower_fn_decl(decl, closure_id, fn_decl_span, FnDeclKind::Closure, None);
940+
let fn_decl = self.lower_fn_decl(
941+
decl,
942+
closure_id,
943+
fn_decl_span,
944+
FnDeclKind::Closure,
945+
CoroutineKind::None,
946+
);
940947

941948
let c = self.arena.alloc(hir::Closure {
942949
def_id: self.local_def_id(closure_id),
@@ -1051,8 +1058,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
10511058
// We need to lower the declaration outside the new scope, because we
10521059
// have to conserve the state of being inside a loop condition for the
10531060
// closure argument types.
1054-
let fn_decl =
1055-
self.lower_fn_decl(&outer_decl, closure_id, fn_decl_span, FnDeclKind::Closure, None);
1061+
let fn_decl = self.lower_fn_decl(
1062+
&outer_decl,
1063+
closure_id,
1064+
fn_decl_span,
1065+
FnDeclKind::Closure,
1066+
CoroutineKind::None,
1067+
);
10561068

10571069
let c = self.arena.alloc(hir::Closure {
10581070
def_id: self.local_def_id(closure_id),

0 commit comments

Comments
 (0)