From f23696ddda6011f5422360f19ff994a4a0ca4412 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Wed, 7 Apr 2021 08:09:41 +0000 Subject: [PATCH] BoundArrayAccessors: Don't share AST nodes Each AST node must be unique. Having diamonds in the AST causes all sorts of exciting bugs in the resolver and later transforms. Bug: tint:469 Change-Id: I5dc43fef71a200632b3e8e8add77ec0537b01cd2 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/46870 Commit-Queue: Ben Clayton Reviewed-by: James Price --- src/transform/bound_array_accessors.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 {