From 547c4ead338065e0a019538f5164d76c7760615c Mon Sep 17 00:00:00 2001 From: Jamie Liu Date: Thu, 11 May 2023 09:55:05 -0700 Subject: [PATCH] Fix go_marshal array-of-structs bug. PiperOrigin-RevId: 531231832 --- tools/go_marshal/gomarshal/generator_interfaces.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/go_marshal/gomarshal/generator_interfaces.go b/tools/go_marshal/gomarshal/generator_interfaces.go index 2050c1cee..9917f41b0 100644 --- a/tools/go_marshal/gomarshal/generator_interfaces.go +++ b/tools/go_marshal/gomarshal/generator_interfaces.go @@ -77,6 +77,12 @@ func (g *interfaceGenerator) recordUsedImport(i string) { } func (g *interfaceGenerator) recordPotentiallyNonPackedField(fieldName string) { + // Some calls to g.unmarshalScalar() occur in emitted loops that use "idx" + // as a loop variable, passing "field[idx]" as the accessor. When + // g.unmarshalScalar() calls this function, we need to convert such cases + // to "field[0]" for g.areFieldsPackedExpression(), which is used in + // contexts where "idx" is not defined. + fieldName = strings.ReplaceAll(fieldName, "[idx]", "[0]") g.as[fieldName] = struct{}{} }