diff --git a/src/transform/inline_pointer_lets_test.cc b/src/transform/inline_pointer_lets_test.cc index 6f53e6262d..51c1d89dbd 100644 --- a/src/transform/inline_pointer_lets_test.cc +++ b/src/transform/inline_pointer_lets_test.cc @@ -70,7 +70,7 @@ fn f() { auto* expect = R"( fn f() { var m : mat4x4; - let f : f32 = *(&(*(&(*(&(m))[2]))[1])); + let f : f32 = *(&((*(&((*(&(m)))[2])))[1])); } )"; @@ -220,7 +220,7 @@ fn f() { let j : i32 = 1; let p_save = i; let q_save = j; - *(&(*(&(arr[p_save]))[q_save])) = 12; + *(&((*(&(arr[p_save])))[q_save])) = 12; } )"; diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 81e06e52aa..6d0a523257 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -165,9 +165,20 @@ bool GeneratorImpl::EmitConstructedType(const sem::Type* ty) { } bool GeneratorImpl::EmitArrayAccessor(ast::ArrayAccessorExpression* expr) { + bool paren_lhs = + !expr->array() + ->IsAnyOf(); + if (paren_lhs) { + out_ << "("; + } if (!EmitExpression(expr->array())) { return false; } + if (paren_lhs) { + out_ << ")"; + } out_ << "["; if (!EmitExpression(expr->idx_expr())) { diff --git a/src/writer/msl/generator_impl_array_accessor_test.cc b/src/writer/msl/generator_impl_array_accessor_test.cc index 0c3c67bbeb..f5c2f13bfc 100644 --- a/src/writer/msl/generator_impl_array_accessor_test.cc +++ b/src/writer/msl/generator_impl_array_accessor_test.cc @@ -32,6 +32,19 @@ TEST_F(MslGeneratorImplTest, ArrayAccessor) { EXPECT_EQ(gen.result(), "ary[5]"); } +TEST_F(MslGeneratorImplTest, ArrayAccessor_OfDref) { + Global("ary", ty.array(), ast::StorageClass::kPrivate); + + auto* p = Const("p", nullptr, AddressOf("ary")); + auto* expr = IndexAccessor(Deref("p"), 5); + WrapInFunction(p, expr); + + GeneratorImpl& gen = Build(); + + ASSERT_TRUE(gen.EmitExpression(expr)) << gen.error(); + EXPECT_EQ(gen.result(), "(*(p))[5]"); +} + } // namespace } // namespace msl } // namespace writer diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index 17b83598d6..9ed6527fc9 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -141,9 +141,20 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) { } bool GeneratorImpl::EmitArrayAccessor(ast::ArrayAccessorExpression* expr) { + bool paren_lhs = + !expr->array() + ->IsAnyOf(); + if (paren_lhs) { + out_ << "("; + } if (!EmitExpression(expr->array())) { return false; } + if (paren_lhs) { + out_ << ")"; + } out_ << "["; if (!EmitExpression(expr->idx_expr())) { diff --git a/src/writer/wgsl/generator_impl_array_accessor_test.cc b/src/writer/wgsl/generator_impl_array_accessor_test.cc index 77137ae130..db18d16b83 100644 --- a/src/writer/wgsl/generator_impl_array_accessor_test.cc +++ b/src/writer/wgsl/generator_impl_array_accessor_test.cc @@ -32,6 +32,19 @@ TEST_F(WgslGeneratorImplTest, ArrayAccessor) { EXPECT_EQ(gen.result(), "ary[5]"); } +TEST_F(WgslGeneratorImplTest, ArrayAccessor_OfDref) { + Global("ary", ty.array(), ast::StorageClass::kPrivate); + + auto* p = Const("p", nullptr, AddressOf("ary")); + auto* expr = IndexAccessor(Deref("p"), 5); + WrapInFunction(p, expr); + + GeneratorImpl& gen = Build(); + + ASSERT_TRUE(gen.EmitExpression(expr)) << gen.error(); + EXPECT_EQ(gen.result(), "(*(p))[5]"); +} + } // namespace } // namespace wgsl } // namespace writer