fix: for-const 循环 update 段豁免 const 写检查(per-iteration 绑定合法)

This commit is contained in:
2026-08-15 22:29:09 +08:00
parent 3a40024e99
commit bca463438d
6 changed files with 79 additions and 29 deletions
+5 -1
View File
@@ -467,8 +467,12 @@ impl Emitter {
&self, name: &str, update: &oxide_parser::UpdateExpression, ctx: &mut CompileCtx,
) -> Result<u32, String> {
// 自增/自减先解析赋值引用:TDZ 绑定抛 ReferenceError;const 抛 TypeError(编译期拦截)。
// 循环 update 段的 let/const 循环变量是 per-iteration 可变绑定(CreateMutableBinding),
// 规范允许写,故豁免 const 检查(register_update_names 覆盖全部循环头声明名)。
self.emit_identifier_tdz_guard(name, ctx)?;
self.emit_const_write_guard(name, ctx)?;
if !ctx.register_update_names.iter().any(|n| n == name) {
self.emit_const_write_guard(name, ctx)?;
}
let uv_idx = ctx.current_upvalue_captures.iter().position(|u| u.name == name);
let captured_cell = ctx.captured_bindings.get(name).copied();
// 循环 update 段:被捕获绑定走寄存器 INC/DEC(C 风格 for 每迭代 fresh,