diff --git a/src/transform/promote_side_effects_to_decl.cc b/src/transform/promote_side_effects_to_decl.cc index 6274187347..520e8458a4 100644 --- a/src/transform/promote_side_effects_to_decl.cc +++ b/src/transform/promote_side_effects_to_decl.cc @@ -242,9 +242,7 @@ class PromoteSideEffectsToDecl::State { body_stmts.emplace_back(b.If(not_cond, break_body)); } // Next emit the for-loop body - for (auto* body_stmt : for_loop->body->statements) { - body_stmts.emplace_back(ctx.Clone(body_stmt)); - } + body_stmts.emplace_back(ctx.Clone(for_loop->body)); // Finally create the continuing block if there was one. const ast::BlockStatement* continuing = nullptr; diff --git a/src/transform/promote_side_effects_to_decl_test.cc b/src/transform/promote_side_effects_to_decl_test.cc index 9c7a7fe9ad..c520aa14f1 100644 --- a/src/transform/promote_side_effects_to_decl_test.cc +++ b/src/transform/promote_side_effects_to_decl_test.cc @@ -184,7 +184,9 @@ fn f() { if (!((f == tint_symbol[0]))) { break; } - var marker = 1; + { + var marker = 1; + } continuing { f = (f + 1.0); @@ -218,7 +220,9 @@ fn f() { if (!((f < 10.0))) { break; } - var marker = 1; + { + var marker = 1; + } continuing { let tint_symbol = array(1.0); @@ -257,7 +261,9 @@ fn f() { if (!((f < tint_symbol_1[0]))) { break; } - var marker = 1; + { + var marker = 1; + } continuing { let tint_symbol_2 = array(2.0); @@ -678,7 +684,9 @@ fn f() { if (!((var_for_index[i] < 3))) { break; } - break; + { + break; + } } } )"; @@ -712,9 +720,54 @@ fn f() { if (!((var_for_index[i].x < 3.0))) { break; } + { + break; + } + } +} +)"; + + DataMap data; + data.Add(/* type_ctor_to_let */ false, + /* dynamic_index_to_var */ true); + auto got = Run(src, data); + + EXPECT_EQ(expect, str(got)); +} + +TEST_F(PromoteSideEffectsToDeclTest, + DynamicIndexToVar_MatrixIndexInForLoopCondWithNestedIndex) { + auto* src = R"( +fn f() { + var i : i32; + let p = mat2x2(1.0, 2.0, 3.0, 4.0); + for(; p[i].x < 3.0; ) { + if (p[i].x < 1.0) { + var marker = 1; + } break; } } +)"; + + auto* expect = R"( +fn f() { + var i : i32; + let p = mat2x2(1.0, 2.0, 3.0, 4.0); + loop { + var var_for_index = p; + if (!((var_for_index[i].x < 3.0))) { + break; + } + { + var var_for_index_1 = p; + if ((var_for_index_1[i].x < 1.0)) { + var marker = 1; + } + break; + } + } +} )"; DataMap data; diff --git a/test/statements/for/condition/array_ctor.wgsl.expected.glsl b/test/statements/for/condition/array_ctor.wgsl.expected.glsl index 42330bb79f..c02c4fb416 100644 --- a/test/statements/for/condition/array_ctor.wgsl.expected.glsl +++ b/test/statements/for/condition/array_ctor.wgsl.expected.glsl @@ -12,6 +12,8 @@ void f() { if (!((i < tint_symbol[0]))) { break; } + { + } } } diff --git a/test/statements/for/condition/array_ctor.wgsl.expected.hlsl b/test/statements/for/condition/array_ctor.wgsl.expected.hlsl index 409e7c4ce5..141cd27664 100644 --- a/test/statements/for/condition/array_ctor.wgsl.expected.hlsl +++ b/test/statements/for/condition/array_ctor.wgsl.expected.hlsl @@ -10,5 +10,7 @@ void f() { if (!((i < tint_symbol[0]))) { break; } + { + } } } diff --git a/test/statements/for/condition/array_ctor.wgsl.expected.msl b/test/statements/for/condition/array_ctor.wgsl.expected.msl index 2ec1a687f3..5aefd59235 100644 --- a/test/statements/for/condition/array_ctor.wgsl.expected.msl +++ b/test/statements/for/condition/array_ctor.wgsl.expected.msl @@ -12,6 +12,8 @@ void f() { if (!((i < tint_symbol.arr[0]))) { break; } + { + } } } diff --git a/test/statements/for/condition/struct_ctor.wgsl.expected.glsl b/test/statements/for/condition/struct_ctor.wgsl.expected.glsl index 853168f8e5..ad8c2cbf8d 100644 --- a/test/statements/for/condition/struct_ctor.wgsl.expected.glsl +++ b/test/statements/for/condition/struct_ctor.wgsl.expected.glsl @@ -16,6 +16,8 @@ void f() { if (!((i < tint_symbol.i))) { break; } + { + } } } diff --git a/test/statements/for/condition/struct_ctor.wgsl.expected.hlsl b/test/statements/for/condition/struct_ctor.wgsl.expected.hlsl index c9d9ff0a4d..d80f79a7b4 100644 --- a/test/statements/for/condition/struct_ctor.wgsl.expected.hlsl +++ b/test/statements/for/condition/struct_ctor.wgsl.expected.hlsl @@ -14,5 +14,7 @@ void f() { if (!((i < tint_symbol.i))) { break; } + { + } } } diff --git a/test/statements/for/condition/struct_ctor.wgsl.expected.msl b/test/statements/for/condition/struct_ctor.wgsl.expected.msl index a370f250d7..a89dfc28ad 100644 --- a/test/statements/for/condition/struct_ctor.wgsl.expected.msl +++ b/test/statements/for/condition/struct_ctor.wgsl.expected.msl @@ -12,6 +12,8 @@ void f() { if (!((i < tint_symbol.i))) { break; } + { + } } } diff --git a/test/statements/for/continuing/array_ctor.wgsl.expected.glsl b/test/statements/for/continuing/array_ctor.wgsl.expected.glsl index d40e419bd5..76006fdc63 100644 --- a/test/statements/for/continuing/array_ctor.wgsl.expected.glsl +++ b/test/statements/for/continuing/array_ctor.wgsl.expected.glsl @@ -11,6 +11,8 @@ void f() { if (!(false)) { break; } + { + } { int tint_symbol[1] = int[1](1); i = (i + tint_symbol[0]); diff --git a/test/statements/for/continuing/array_ctor.wgsl.expected.hlsl b/test/statements/for/continuing/array_ctor.wgsl.expected.hlsl index e24b6341a0..7348a72604 100644 --- a/test/statements/for/continuing/array_ctor.wgsl.expected.hlsl +++ b/test/statements/for/continuing/array_ctor.wgsl.expected.hlsl @@ -9,6 +9,8 @@ void f() { if (!(false)) { break; } + { + } { const int tint_symbol[1] = {1}; i = (i + tint_symbol[0]); diff --git a/test/statements/for/continuing/array_ctor.wgsl.expected.msl b/test/statements/for/continuing/array_ctor.wgsl.expected.msl index 5339dfaaf6..ac6ffc25ab 100644 --- a/test/statements/for/continuing/array_ctor.wgsl.expected.msl +++ b/test/statements/for/continuing/array_ctor.wgsl.expected.msl @@ -11,6 +11,8 @@ void f() { if (!(false)) { break; } + { + } { tint_array_wrapper const tint_symbol = {.arr={1}}; i = as_type((as_type(i) + as_type(tint_symbol.arr[0]))); diff --git a/test/statements/for/continuing/struct_ctor.wgsl.expected.glsl b/test/statements/for/continuing/struct_ctor.wgsl.expected.glsl index 014a37a9e7..ca9a56c1a8 100644 --- a/test/statements/for/continuing/struct_ctor.wgsl.expected.glsl +++ b/test/statements/for/continuing/struct_ctor.wgsl.expected.glsl @@ -16,6 +16,8 @@ void f() { if (!(false)) { break; } + { + } { S tint_symbol = S(1); i = (i + tint_symbol.i); diff --git a/test/statements/for/continuing/struct_ctor.wgsl.expected.hlsl b/test/statements/for/continuing/struct_ctor.wgsl.expected.hlsl index 671f26f7d0..4dd0cf2879 100644 --- a/test/statements/for/continuing/struct_ctor.wgsl.expected.hlsl +++ b/test/statements/for/continuing/struct_ctor.wgsl.expected.hlsl @@ -14,6 +14,8 @@ void f() { if (!(false)) { break; } + { + } { const S tint_symbol = {1}; i = (i + tint_symbol.i); diff --git a/test/statements/for/continuing/struct_ctor.wgsl.expected.msl b/test/statements/for/continuing/struct_ctor.wgsl.expected.msl index a022488354..08603f7ca4 100644 --- a/test/statements/for/continuing/struct_ctor.wgsl.expected.msl +++ b/test/statements/for/continuing/struct_ctor.wgsl.expected.msl @@ -12,6 +12,8 @@ void f() { if (!(false)) { break; } + { + } { S const tint_symbol = {.i=1}; i = as_type((as_type(i) + as_type(tint_symbol.i)));