fix: TLA 游走器补 export 声明顶层 await 判定

This commit is contained in:
2026-10-13 18:09:56 +00:00
parent 9e4cfed7f8
commit 9eb30ff1f2
4 changed files with 78 additions and 23 deletions
+17 -1
View File
@@ -421,7 +421,23 @@ fn stmt_has_top_level_await(stmt: &Statement) -> bool {
.any(|d| d.init.as_ref().is_some_and(|i| expr_has_top_level_await(i))),
// 函数/类声明:体不进入(函数体是独立异步语义边界)。
Statement::FunctionDeclaration(_) | Statement::ClassDeclaration(_) => false,
// import/export 与无子表达式的语句(break/continue/debugger/empty)不含顶层 await。
// `export const/let/var x = await ...` 是顶层 await;
// 函数/类声明形态不进入(体是独立异步语义边界,与裸声明臂同口径)。
Statement::ExportNamedDeclaration(exp) => {
exp.declaration.as_ref().is_some_and(|decl| match decl {
Declaration::VariableDeclaration(vd) => vd
.declarations
.iter()
.any(|d| d.init.as_ref().is_some_and(|i| expr_has_top_level_await(i))),
_ => false,
})
}
// `export default await ...` 是顶层 await;函数/类声明形态不进入(同口径)。
Statement::ExportDefaultDeclaration(exp) => {
exp.declaration.as_expression().is_some_and(expr_has_top_level_await)
}
// import、`export *`、`export { x }`(无声明)与无子表达式的语句
// (break/continue/debugger/empty)不含顶层 await。
_ => false,
}
}
@@ -546,3 +546,42 @@ fn sync_dep_no_gate() {
assert_eq!(native_calls_to(&ir, "__moduleWaitAll"), 0, "同步依赖不得发聚合 gate");
assert_eq!(ir.insts.iter().filter(|i| i.op == OpCode::AWAIT).count(), 0, "主身不得含 AWAIT");
}
/// 面 25:`export const x = await ...`(ExportNamedDeclaration 带变量声明)是
/// 顶层 await,模块标记异步、体 await 编译为 AWAIT。
#[test]
fn export_named_var_decl_await_makes_module_async() {
let (ir, _) = emit_module("export const x = await Promise.resolve(1);", "./entry.js", &[])
.expect("export const x = await 模块应编译成功");
assert!(ir.is_async, "export const x = await ... 应标记模块异步求值");
assert_eq!(
ir.insts.iter().filter(|i| i.op == OpCode::AWAIT).count(),
1,
"顶层 await 应编译为一条 AWAIT"
);
}
/// 面 26:`export default await ...`(ExportDefaultDeclaration 表达式形态)是
/// 顶层 await,模块标记异步、体 await 编译为 AWAIT。
#[test]
fn export_default_await_makes_module_async() {
let (ir, _) = emit_module("export default await Promise.resolve(1);", "./entry.js", &[])
.expect("export default await 模块应编译成功");
assert!(ir.is_async, "export default await ... 应标记模块异步求值");
assert_eq!(
ir.insts.iter().filter(|i| i.op == OpCode::AWAIT).count(),
1,
"顶层 await 应编译为一条 AWAIT"
);
}
/// 面 27:`export const f = async () => {}`(导出声明初始化器为异步函数表达式)
/// 不使模块异步——函数体是独立异步语义边界,不进入游走。
#[test]
fn export_named_async_fn_expr_not_tla() {
let (ir, _) =
emit_module("export const f = async () => { await Promise.resolve(1); };", "./entry.js", &[])
.expect("导出异步函数表达式模块应编译成功");
assert!(!ir.is_async, "异步函数表达式体不得使模块标记异步");
assert_eq!(ir.insts.iter().filter(|i| i.op == OpCode::AWAIT).count(), 0, "主身不得含 AWAIT");
}