diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index a9607b2e01..45a77b83a2 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -155,9 +155,20 @@ bool GeneratorImpl::EmitArrayAccessor(ast::ArrayAccessorExpression* expr) { } bool GeneratorImpl::EmitMemberAccessor(ast::MemberAccessorExpression* expr) { + bool paren_lhs = + !expr->structure() + ->IsAnyOf(); + if (paren_lhs) { + out_ << "("; + } if (!EmitExpression(expr->structure())) { return false; } + if (paren_lhs) { + out_ << ")"; + } out_ << "."; diff --git a/src/writer/wgsl/generator_impl_member_accessor_test.cc b/src/writer/wgsl/generator_impl_member_accessor_test.cc index 1a2801164a..03fccc6265 100644 --- a/src/writer/wgsl/generator_impl_member_accessor_test.cc +++ b/src/writer/wgsl/generator_impl_member_accessor_test.cc @@ -34,6 +34,20 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_MemberAccessor) { EXPECT_EQ(gen.result(), "str.mem"); } +TEST_F(WgslGeneratorImplTest, EmitExpression_MemberAccessor_OfDref) { + auto* s = Structure("Data", {Member("mem", ty.f32())}); + Global("str", s, ast::StorageClass::kPrivate); + + auto* p = Const("p", nullptr, AddressOf("str")); + auto* expr = MemberAccessor(Deref("p"), "mem"); + WrapInFunction(p, expr); + + GeneratorImpl& gen = Build(); + + ASSERT_TRUE(gen.EmitExpression(expr)) << gen.error(); + EXPECT_EQ(gen.result(), "(*(p)).mem"); +} + } // namespace } // namespace wgsl } // namespace writer