diff --git a/src/transform/bound_array_accessors.cc b/src/transform/bound_array_accessors.cc index 5eecfec70c..d4b7bed5eb 100644 --- a/src/transform/bound_array_accessors.cc +++ b/src/transform/bound_array_accessors.cc @@ -69,7 +69,12 @@ ast::ArrayAccessorExpression* BoundArrayAccessors::Transform( if (size == 0) { if (is_arr) { - auto* arr_len = b.Call("arrayLength", ctx->Clone(expr->array())); + // Call Cloneable::Clone() instead of CloneContext::Clone() to ensure the + // AST node is duplicated. CloneContext::Clone() will ensure that repeated + // calls with the same pointer return the *same* cloned node - in this + // case we actually want two copies. + auto* arr = static_cast(expr->array()->Clone(ctx)); + auto* arr_len = b.Call("arrayLength", arr); auto* limit = b.Sub(arr_len, b.Expr(1u)); new_idx = b.Call("min", b.Construct(ctx->Clone(old_idx)), limit); } else {