internal/cmd/generate-types: move variable declaration

Move the declaration of the slice variable
as close to its actual use as possible.

Change-Id: Ibe26460b5ca81edbed1073b32faf1c5f5d2c922a
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/263719
Trust: Joe Tsai <joetsai@digital-static.net>
Trust: Joe Tsai <joetsai@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/cmd/generate-types/impl.go b/internal/cmd/generate-types/impl.go
index 96996f2..2113629 100644
--- a/internal/cmd/generate-types/impl.go
+++ b/internal/cmd/generate-types/impl.go
@@ -422,7 +422,6 @@
 
 // consume{{.Name}}SliceValidateUTF8 wire decodes a []{{.GoType}} pointer as a repeated {{.Name}}.
 func consume{{.Name}}SliceValidateUTF8(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, _ unmarshalOptions) (out unmarshalOutput, err error) {
-	sp := p.{{.GoType.PointerMethod}}Slice()
 	if wtyp != {{.WireType.Expr}} {
 		return out, errUnknown
 	}
@@ -433,6 +432,7 @@
 	if !utf8.Valid{{if eq .Name "String"}}String{{end}}(v) {
 		return out, errInvalidUTF8{}
 	}
+	sp := p.{{.GoType.PointerMethod}}Slice()
 	*sp = append(*sp, {{.ToGoType}})
 	out.n = n
 	return out, nil
diff --git a/internal/impl/codec_gen.go b/internal/impl/codec_gen.go
index 98aaf2d..bb14c13 100644
--- a/internal/impl/codec_gen.go
+++ b/internal/impl/codec_gen.go
@@ -5174,7 +5174,6 @@
 
 // consumeStringSliceValidateUTF8 wire decodes a []string pointer as a repeated String.
 func consumeStringSliceValidateUTF8(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, _ unmarshalOptions) (out unmarshalOutput, err error) {
-	sp := p.StringSlice()
 	if wtyp != protowire.BytesType {
 		return out, errUnknown
 	}
@@ -5185,6 +5184,7 @@
 	if !utf8.ValidString(v) {
 		return out, errInvalidUTF8{}
 	}
+	sp := p.StringSlice()
 	*sp = append(*sp, v)
 	out.n = n
 	return out, nil
@@ -5514,7 +5514,6 @@
 
 // consumeBytesSliceValidateUTF8 wire decodes a [][]byte pointer as a repeated Bytes.
 func consumeBytesSliceValidateUTF8(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, _ unmarshalOptions) (out unmarshalOutput, err error) {
-	sp := p.BytesSlice()
 	if wtyp != protowire.BytesType {
 		return out, errUnknown
 	}
@@ -5525,6 +5524,7 @@
 	if !utf8.Valid(v) {
 		return out, errInvalidUTF8{}
 	}
+	sp := p.BytesSlice()
 	*sp = append(*sp, append(emptyBuf[:], v...))
 	out.n = n
 	return out, nil