1
Fork 0

Merge Async and Gen into CoroutineKind

This commit is contained in:
Eric Holk 2023-11-30 14:54:39 -08:00
parent 3887b1645a
commit 48d5f1f0f2
No known key found for this signature in database
GPG key ID: 8EA6B43ED4CE0911
25 changed files with 442 additions and 238 deletions

View file

@ -1311,7 +1311,7 @@ pub struct Closure {
pub binder: ClosureBinder,
pub capture_clause: CaptureBy,
pub constness: Const,
pub asyncness: Async,
pub coro_kind: CoroutineKind,
pub movability: Movability,
pub fn_decl: P<FnDecl>,
pub body: P<Expr>,
@ -2406,28 +2406,38 @@ pub enum Unsafe {
No,
}
/// Describes what kind of coroutine markers, if any, a function has.
///
/// Coroutine markers are things that cause the function to generate a coroutine, such as `async`,
/// which makes the function return `impl Future`, or `gen`, which makes the function return `impl
/// Iterator`.
#[derive(Copy, Clone, Encodable, Decodable, Debug)]
pub enum Async {
Yes { span: Span, closure_id: NodeId, return_impl_trait_id: NodeId },
No,
pub enum CoroutineKind {
/// `async`, which evaluates to `impl Future`
Async { span: Span, closure_id: NodeId, return_impl_trait_id: NodeId },
/// `gen`, which evaluates to `impl Iterator`
Gen { span: Span, closure_id: NodeId, return_impl_trait_id: NodeId },
/// Neither `async` nor `gen`
None,
}
#[derive(Copy, Clone, Encodable, Decodable, Debug)]
pub enum Gen {
Yes { span: Span, closure_id: NodeId, return_impl_trait_id: NodeId },
No,
}
impl Async {
impl CoroutineKind {
pub fn is_async(self) -> bool {
matches!(self, Async::Yes { .. })
matches!(self, CoroutineKind::Async { .. })
}
pub fn is_gen(self) -> bool {
matches!(self, CoroutineKind::Gen { .. })
}
/// In this case this is an `async` return, the `NodeId` for the generated `impl Trait` item.
pub fn opt_return_id(self) -> Option<(NodeId, Span)> {
match self {
Async::Yes { return_impl_trait_id, span, .. } => Some((return_impl_trait_id, span)),
Async::No => None,
CoroutineKind::Async { return_impl_trait_id, span, .. }
| CoroutineKind::Gen { return_impl_trait_id, span, .. } => {
Some((return_impl_trait_id, span))
}
CoroutineKind::None => None,
}
}
}
@ -2831,25 +2841,22 @@ impl Extern {
pub struct FnHeader {
/// The `unsafe` keyword, if any
pub unsafety: Unsafe,
/// The `async` keyword, if any
pub asyncness: Async,
/// Whether this is `async`, `gen`, or nothing.
pub coro_kind: CoroutineKind,
/// The `const` keyword, if any
pub constness: Const,
/// The `extern` keyword and corresponding ABI string, if any
pub ext: Extern,
/// The `gen` keyword, if any
pub genness: Gen,
}
impl FnHeader {
/// Does this function header have any qualifiers or is it empty?
pub fn has_qualifiers(&self) -> bool {
let Self { unsafety, asyncness, constness, ext, genness } = self;
let Self { unsafety, coro_kind, constness, ext } = self;
matches!(unsafety, Unsafe::Yes(_))
|| asyncness.is_async()
|| !matches!(coro_kind, CoroutineKind::None)
|| matches!(constness, Const::Yes(_))
|| !matches!(ext, Extern::None)
|| matches!(genness, Gen::Yes { .. })
}
}
@ -2857,10 +2864,9 @@ impl Default for FnHeader {
fn default() -> FnHeader {
FnHeader {
unsafety: Unsafe::No,
asyncness: Async::No,
coro_kind: CoroutineKind::None,
constness: Const::No,
ext: Extern::None,
genness: Gen::No,
}
}
}
@ -3181,7 +3187,7 @@ mod size_asserts {
static_assert_size!(Block, 32);
static_assert_size!(Expr, 72);
static_assert_size!(ExprKind, 40);
static_assert_size!(Fn, 168);
static_assert_size!(Fn, 160);
static_assert_size!(ForeignItem, 96);
static_assert_size!(ForeignItemKind, 24);
static_assert_size!(GenericArg, 24);

View file

@ -121,12 +121,8 @@ pub trait MutVisitor: Sized {
noop_visit_fn_decl(d, self);
}
fn visit_asyncness(&mut self, a: &mut Async) {
noop_visit_asyncness(a, self);
}
fn visit_genness(&mut self, a: &mut Gen) {
noop_visit_genness(a, self);
fn visit_coro_kind(&mut self, a: &mut CoroutineKind) {
noop_visit_coro_kind(a, self);
}
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:
}
}
pub fn noop_visit_asyncness<T: MutVisitor>(asyncness: &mut Async, vis: &mut T) {
match asyncness {
Async::Yes { span: _, closure_id, return_impl_trait_id } => {
pub fn noop_visit_coro_kind<T: MutVisitor>(coro_kind: &mut CoroutineKind, vis: &mut T) {
match coro_kind {
CoroutineKind::Async { span: _, closure_id, return_impl_trait_id }
| CoroutineKind::Gen { span: _, closure_id, return_impl_trait_id } => {
vis.visit_id(closure_id);
vis.visit_id(return_impl_trait_id);
}
Async::No => {}
}
}
pub fn noop_visit_genness<T: MutVisitor>(genness: &mut Gen, vis: &mut T) {
match genness {
Gen::Yes { span: _, closure_id, return_impl_trait_id } => {
vis.visit_id(closure_id);
vis.visit_id(return_impl_trait_id);
}
Gen::No => {}
CoroutineKind::None => {}
}
}
@ -1184,10 +1171,9 @@ fn visit_const_item<T: MutVisitor>(
}
pub fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
let FnHeader { unsafety, asyncness, constness, ext: _, genness } = header;
let FnHeader { unsafety, coro_kind, constness, ext: _ } = header;
visit_constness(constness, vis);
vis.visit_asyncness(asyncness);
vis.visit_genness(genness);
vis.visit_coro_kind(coro_kind);
visit_unsafety(unsafety, vis);
}
@ -1421,7 +1407,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
binder,
capture_clause,
constness,
asyncness,
coro_kind,
movability: _,
fn_decl,
body,
@ -1430,7 +1416,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
}) => {
vis.visit_closure_binder(binder);
visit_constness(constness, vis);
vis.visit_asyncness(asyncness);
vis.visit_coro_kind(coro_kind);
vis.visit_capture_by(capture_clause);
vis.visit_fn_decl(fn_decl);
vis.visit_expr(body);

View file

@ -861,7 +861,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
ExprKind::Closure(box Closure {
binder,
capture_clause,
asyncness: _,
coro_kind: _,
constness: _,
movability: _,
fn_decl,