cmd/protoc-gen-go: fix init order for v1 registration
The v1 registration leaks the message types out to the proto package.
When doing that, it must ensure that the reflection data structures
for those types are properly initialized first. We achieve that by
doing v1 registration at the end of the reflection init function.
Change-Id: If6df18df59d05bad50ff39c2eff6beb19e7466cc
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168348
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go
index 70ecbc9..fc9cb45 100644
--- a/cmd/protoc-gen-go/internal_gengo/main.go
+++ b/cmd/protoc-gen-go/internal_gengo/main.go
@@ -132,7 +132,6 @@
}
genExtensions(gen, g, f)
- genInitFunction(gen, g, f)
genFileDescriptor(gen, g, f)
genReflectFileDescriptor(gen, g, f)
@@ -775,15 +774,14 @@
return f.GoImportPath.Ident(name)
}
-// genInitFunction generates an init function that registers the types in the
-// generated file with the proto package.
-func genInitFunction(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo) {
+// genRegistrationV1 generates the init function body that registers the
+// types in the generated file with the v1 proto package.
+func genRegistrationV1(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo) {
// TODO: Remove this function when we always register with v2.
if isDescriptor(f.File) {
return
}
- g.P("func init() {")
g.P(protoPackage.Ident("RegisterFile"), "(", strconv.Quote(f.Desc.Path()), ", ", f.descriptorGzipVar, ")")
for _, enum := range f.allEnums {
name := enum.GoIdent.GoName
@@ -818,8 +816,6 @@
for _, extension := range f.allExtensions {
g.P(protoPackage.Ident("RegisterExtension"), "(", extensionVar(f.File, extension), ")")
}
- g.P("}")
- g.P()
}
// deprecationComment returns a standard deprecation comment if deprecated is true.