diff options
| author | bors <bors@rust-lang.org> | 2023-12-26 04:25:53 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-12-26 04:25:53 +0000 |
| commit | 1ab783112ab4e4807304dbd249b39771246013ef (patch) | |
| tree | 827d6051e0e3ffcaadd0d6c7369b71547319a525 /compiler/rustc_hir | |
| parent | 2271c26e4a8e062bb00d709d0ccb5846e0c341b9 (diff) | |
| parent | ba912855cc1aff5f2b403411c8d890d3978abf9a (diff) | |
| download | rust-1ab783112ab4e4807304dbd249b39771246013ef.tar.gz rust-1ab783112ab4e4807304dbd249b39771246013ef.zip | |
Auto merge of #119258 - compiler-errors:closure-kind, r=eholk
Make closures carry their own ClosureKind Right now, we use the "`movability`" field of `hir::Closure` to distinguish a closure and a coroutine. This is paired together with the `CoroutineKind`, which is located not in the `hir::Closure`, but the `hir::Body`. This is strange and redundant. This PR introduces `ClosureKind` with two variants -- `Closure` and `Coroutine`, which is put into `hir::Closure`. The `CoroutineKind` is thus removed from `hir::Body`, and `Option<Movability>` no longer needs to be a stand-in for "is this a closure or a coroutine". r? eholk
Diffstat (limited to 'compiler/rustc_hir')
| -rw-r--r-- | compiler/rustc_hir/src/hir.rs | 35 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/intravisit.rs | 2 |
2 files changed, 27 insertions, 10 deletions
diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 452f5d0b7ac..2b840860166 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -945,7 +945,18 @@ pub struct Closure<'hir> { pub fn_decl_span: Span, /// The span of the argument block `|...|` pub fn_arg_span: Option<Span>, - pub movability: Option<Movability>, + pub kind: ClosureKind, +} + +#[derive(Clone, PartialEq, Eq, Debug, Copy, Hash, HashStable_Generic, Encodable, Decodable)] +pub enum ClosureKind { + /// This is a plain closure expression. + Closure, + /// This is a coroutine expression -- i.e. a closure expression in which + /// we've found a `yield`. These can arise either from "plain" coroutine + /// usage (e.g. `let x = || { yield (); }`) or from a desugared expression + /// (e.g. `async` and `gen` blocks). + Coroutine(CoroutineKind), } /// A block of statements `{ .. }`, which may have a label (in this case the @@ -1335,17 +1346,12 @@ pub struct BodyId { pub struct Body<'hir> { pub params: &'hir [Param<'hir>], pub value: &'hir Expr<'hir>, - pub coroutine_kind: Option<CoroutineKind>, } impl<'hir> Body<'hir> { pub fn id(&self) -> BodyId { BodyId { hir_id: self.value.hir_id } } - - pub fn coroutine_kind(&self) -> Option<CoroutineKind> { - self.coroutine_kind - } } /// The type of source expression that caused this coroutine to be created. @@ -1355,7 +1361,18 @@ pub enum CoroutineKind { Desugared(CoroutineDesugaring, CoroutineSource), /// A coroutine literal created via a `yield` inside a closure. - Coroutine, + Coroutine(Movability), +} + +impl CoroutineKind { + pub fn movability(self) -> Movability { + match self { + CoroutineKind::Desugared(CoroutineDesugaring::Async, _) + | CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _) => Movability::Static, + CoroutineKind::Desugared(CoroutineDesugaring::Gen, _) => Movability::Movable, + CoroutineKind::Coroutine(mov) => mov, + } + } } impl fmt::Display for CoroutineKind { @@ -1365,7 +1382,7 @@ impl fmt::Display for CoroutineKind { d.fmt(f)?; k.fmt(f) } - CoroutineKind::Coroutine => f.write_str("coroutine"), + CoroutineKind::Coroutine(_) => f.write_str("coroutine"), } } } @@ -3661,7 +3678,7 @@ mod size_asserts { use super::*; // tidy-alphabetical-start static_assert_size!(Block<'_>, 48); - static_assert_size!(Body<'_>, 32); + static_assert_size!(Body<'_>, 24); static_assert_size!(Expr<'_>, 64); static_assert_size!(ExprKind<'_>, 48); static_assert_size!(FnDecl<'_>, 40); diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index 67e058a3219..e58e4c8fe0e 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -757,7 +757,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) capture_clause: _, fn_decl_span: _, fn_arg_span: _, - movability: _, + kind: _, constness: _, }) => { walk_list!(visitor, visit_generic_param, bound_generic_params); |
