|
| 1 | +//! `await`-in-expression support via await-hoisting (A-normal form). |
| 2 | +//! |
| 3 | +//! The async lowering only handles `await` at a statement boundary (`let x = |
| 4 | +//! await f()`, `return await f()`, a bare `await f()` statement) or inside an |
| 5 | +//! `if`/`loop`/`match`/`with` body. A nested `await` — `f(x: await g())` — is |
| 6 | +//! otherwise rejected (`RS0411`). |
| 7 | +//! |
| 8 | +//! This pass lifts each nested await out into a preceding |
| 9 | +//! `let __rss_await_N = await <op>`, producing the linear awaits the backends |
| 10 | +//! already lower identically. It is sound and contained: the output uses only |
| 11 | +//! already-supported constructs, so the async lowering itself is untouched. |
| 12 | +//! |
| 13 | +//! Conservative boundaries (left as-is, so they stay rejected rather than risk |
| 14 | +//! wrong evaluation order): the RHS of short-circuit `&&`/`||`, `match`/`if` |
| 15 | +//! arms reached through an expression, and closure bodies. |
| 16 | +
|
| 17 | +use super::ast::{BinaryOp, Block, Callee, Expr, Item, LetKind, LetStmt, Program, Stmt}; |
| 18 | + |
| 19 | +/// Hoist nested awaits in every `async fn` body to statement boundaries. |
| 20 | +pub fn hoist_async_awaits(program: &mut Program) { |
| 21 | + for item in &mut program.items { |
| 22 | + if let Item::Function(function) = item |
| 23 | + && function.is_async |
| 24 | + { |
| 25 | + let mut counter = 0usize; |
| 26 | + hoist_block(&mut function.body, &mut counter); |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +fn hoist_block(block: &mut Block, counter: &mut usize) { |
| 32 | + let mut rewritten: Vec<Stmt> = Vec::with_capacity(block.statements.len()); |
| 33 | + for mut statement in std::mem::take(&mut block.statements) { |
| 34 | + let mut hoisted: Vec<Stmt> = Vec::new(); |
| 35 | + hoist_stmt(&mut statement, &mut hoisted, counter); |
| 36 | + rewritten.extend(hoisted); |
| 37 | + rewritten.push(statement); |
| 38 | + } |
| 39 | + block.statements = rewritten; |
| 40 | +} |
| 41 | + |
| 42 | +fn hoist_stmt(stmt: &mut Stmt, hoisted: &mut Vec<Stmt>, counter: &mut usize) { |
| 43 | + match stmt { |
| 44 | + Stmt::Let(let_stmt) => { |
| 45 | + if let Some(value) = &mut let_stmt.value { |
| 46 | + hoist_value(value, hoisted, counter); |
| 47 | + } |
| 48 | + } |
| 49 | + Stmt::Return(ret) => { |
| 50 | + if let Some(value) = &mut ret.value { |
| 51 | + hoist_value(value, hoisted, counter); |
| 52 | + } |
| 53 | + } |
| 54 | + Stmt::Expr(value) => hoist_value(value, hoisted, counter), |
| 55 | + Stmt::Assign(assign) => { |
| 56 | + // The target is an evaluated place: any await in a field/index base or |
| 57 | + // index expression is nested and must be hoisted. |
| 58 | + hoist_nested(&mut assign.target, hoisted, counter); |
| 59 | + hoist_value(&mut assign.value, hoisted, counter); |
| 60 | + } |
| 61 | + // Control-flow statements own their own blocks (boundaries). Recurse into |
| 62 | + // the nested blocks; leave the condition/scrutinee to the existing async |
| 63 | + // boundary lowering. |
| 64 | + Stmt::If(if_stmt) => { |
| 65 | + hoist_block(&mut if_stmt.then_body, counter); |
| 66 | + if let Some(else_body) = &mut if_stmt.else_body { |
| 67 | + hoist_block(else_body, counter); |
| 68 | + } |
| 69 | + } |
| 70 | + Stmt::Loop(loop_stmt) => hoist_block(&mut loop_stmt.body, counter), |
| 71 | + Stmt::For(for_stmt) => hoist_block(&mut for_stmt.body, counter), |
| 72 | + Stmt::With(with_stmt) => hoist_block(&mut with_stmt.body, counter), |
| 73 | + Stmt::TaskGroup(task_group) => hoist_block(&mut task_group.body, counter), |
| 74 | + Stmt::Match(match_stmt) => { |
| 75 | + for arm in &mut match_stmt.arms { |
| 76 | + hoist_block(&mut arm.body, counter); |
| 77 | + } |
| 78 | + } |
| 79 | + Stmt::Select(select) => { |
| 80 | + for arm in &mut select.arms { |
| 81 | + hoist_block(&mut arm.body, counter); |
| 82 | + } |
| 83 | + } |
| 84 | + Stmt::LetElse(let_else) => { |
| 85 | + hoist_value(&mut let_else.value, hoisted, counter); |
| 86 | + hoist_block(&mut let_else.else_body, counter); |
| 87 | + } |
| 88 | + Stmt::Break(_) |
| 89 | + | Stmt::Continue(_) |
| 90 | + | Stmt::MalformedWith(_) |
| 91 | + | Stmt::MalformedIf(_) |
| 92 | + | Stmt::MalformedLoop(_) |
| 93 | + | Stmt::MalformedFor(_) |
| 94 | + | Stmt::MalformedMatch(_) |
| 95 | + | Stmt::Unknown(_) => {} |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/// A root value position (`let`/`return`/`expr`/assign RHS): a top-level `await` |
| 100 | +/// (through transparent effect/`?` wrappers) is already linear, so only its |
| 101 | +/// operand is searched for nested awaits. |
| 102 | +fn hoist_value(expr: &mut Expr, hoisted: &mut Vec<Stmt>, counter: &mut usize) { |
| 103 | + match expr { |
| 104 | + Expr::Effect { value, .. } => hoist_value(value, hoisted, counter), |
| 105 | + Expr::Await { value, .. } => hoist_nested(value, hoisted, counter), |
| 106 | + Expr::Try { value, .. } if matches!(value.as_ref(), Expr::Await { .. }) => { |
| 107 | + if let Expr::Await { value: inner, .. } = value.as_mut() { |
| 108 | + hoist_nested(inner, hoisted, counter); |
| 109 | + } |
| 110 | + } |
| 111 | + _ => hoist_nested(expr, hoisted, counter), |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/// A nested (non-root) position: every `await` found here is lifted to a |
| 116 | +/// preceding `let __rss_await_N = await <op>` and replaced with the temp. |
| 117 | +fn hoist_nested(expr: &mut Expr, hoisted: &mut Vec<Stmt>, counter: &mut usize) { |
| 118 | + if is_await_unit(expr) { |
| 119 | + // Hoist awaits inside the awaited operand first (inner-to-outer order). |
| 120 | + if let Some(inner) = await_unit_operand_mut(expr) { |
| 121 | + hoist_nested(inner, hoisted, counter); |
| 122 | + } |
| 123 | + let span = expr.span().clone(); |
| 124 | + let name = format!("__rss_await_{counter}"); |
| 125 | + *counter += 1; |
| 126 | + let original = std::mem::replace(expr, Expr::Ident(name.clone(), span.clone())); |
| 127 | + hoisted.push(Stmt::Let(LetStmt { |
| 128 | + kind: LetKind::Managed, |
| 129 | + name, |
| 130 | + type_annotation: None, |
| 131 | + value: Some(original), |
| 132 | + is_async: false, |
| 133 | + is_mut: false, |
| 134 | + destructure: None, |
| 135 | + malformed: false, |
| 136 | + span, |
| 137 | + })); |
| 138 | + return; |
| 139 | + } |
| 140 | + match expr { |
| 141 | + Expr::Call { callee, args, .. } => { |
| 142 | + if let Callee::ReceiverCall { receiver, .. } = callee { |
| 143 | + hoist_nested(receiver, hoisted, counter); |
| 144 | + } |
| 145 | + for arg in args.iter_mut() { |
| 146 | + hoist_nested(&mut arg.value, hoisted, counter); |
| 147 | + } |
| 148 | + } |
| 149 | + // Short-circuit operators: the right operand is conditionally evaluated, |
| 150 | + // so hoisting it would change semantics — leave it (stays rejected). |
| 151 | + Expr::Binary { |
| 152 | + op: BinaryOp::LogicalAnd | BinaryOp::LogicalOr, |
| 153 | + left, |
| 154 | + .. |
| 155 | + } => hoist_nested(left, hoisted, counter), |
| 156 | + Expr::Binary { left, right, .. } => { |
| 157 | + hoist_nested(left, hoisted, counter); |
| 158 | + hoist_nested(right, hoisted, counter); |
| 159 | + } |
| 160 | + Expr::Field { base, .. } => hoist_nested(base, hoisted, counter), |
| 161 | + Expr::Index { base, index, .. } => { |
| 162 | + hoist_nested(base, hoisted, counter); |
| 163 | + hoist_nested(index, hoisted, counter); |
| 164 | + } |
| 165 | + Expr::Effect { value, .. } | Expr::Manage { value, .. } | Expr::Try { value, .. } => { |
| 166 | + hoist_nested(value, hoisted, counter); |
| 167 | + } |
| 168 | + Expr::ObjectLiteral { fields, .. } => { |
| 169 | + for field in fields { |
| 170 | + hoist_nested(&mut field.value, hoisted, counter); |
| 171 | + } |
| 172 | + } |
| 173 | + Expr::MapLiteral { entries, .. } => { |
| 174 | + for entry in entries { |
| 175 | + hoist_nested(&mut entry.key, hoisted, counter); |
| 176 | + hoist_nested(&mut entry.value, hoisted, counter); |
| 177 | + } |
| 178 | + } |
| 179 | + Expr::ArrayLiteral { items, .. } => { |
| 180 | + for item in items { |
| 181 | + hoist_nested(item, hoisted, counter); |
| 182 | + } |
| 183 | + } |
| 184 | + // Match-expression arms are statement-boundary scopes of their own; hoist |
| 185 | + // within each arm body. Leave the scrutinee and closures to existing rules. |
| 186 | + Expr::Match { arms, .. } => { |
| 187 | + for arm in arms { |
| 188 | + hoist_block(&mut arm.body, counter); |
| 189 | + } |
| 190 | + } |
| 191 | + Expr::Closure { .. } |
| 192 | + | Expr::Spawn { .. } |
| 193 | + | Expr::Await { .. } |
| 194 | + | Expr::Ident(_, _) |
| 195 | + | Expr::Number(_, _) |
| 196 | + | Expr::String(_, _) |
| 197 | + | Expr::MultilineString(_, _) |
| 198 | + | Expr::Unknown(_) => {} |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +/// `await op` or `await op?` (`Try` wrapping an `Await`) — the unit lifted whole. |
| 203 | +fn is_await_unit(expr: &Expr) -> bool { |
| 204 | + matches!(expr, Expr::Await { .. }) |
| 205 | + || matches!(expr, Expr::Try { value, .. } if matches!(value.as_ref(), Expr::Await { .. })) |
| 206 | +} |
| 207 | + |
| 208 | +/// The awaited operand inside an await-unit, for hoisting its own nested awaits. |
| 209 | +fn await_unit_operand_mut(expr: &mut Expr) -> Option<&mut Expr> { |
| 210 | + match expr { |
| 211 | + Expr::Await { value, .. } => Some(value), |
| 212 | + Expr::Try { value, .. } => match value.as_mut() { |
| 213 | + Expr::Await { value: inner, .. } => Some(inner), |
| 214 | + _ => None, |
| 215 | + }, |
| 216 | + _ => None, |
| 217 | + } |
| 218 | +} |
0 commit comments