cmd/protoc-gen-go: register with the v2 registry for descriptor

By passing the global registries to fileinit.Builder, the Build
method can register the newly constructed descriptors on our behalf.
As a result, we can stop registering with the v1 registries as well.

We only do this for descriptor proto to remove another dependency on v1.
We deliberately keep the v1 registration logic for now to make it easy
to patch away this new behavior.

Change-Id: Ic6aa8ffba3d2d0abe08a61fc5e1c9ca7668e0988
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168000
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go
index 7a28ae6..70ecbc9 100644
--- a/cmd/protoc-gen-go/internal_gengo/main.go
+++ b/cmd/protoc-gen-go/internal_gengo/main.go
@@ -778,6 +778,11 @@
 // 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) {
+	// 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 {
diff --git a/cmd/protoc-gen-go/internal_gengo/reflect.go b/cmd/protoc-gen-go/internal_gengo/reflect.go
index 3e5300c..7f2d25f 100644
--- a/cmd/protoc-gen-go/internal_gengo/reflect.go
+++ b/cmd/protoc-gen-go/internal_gengo/reflect.go
@@ -24,10 +24,11 @@
 const minimumVersion = 0
 
 const (
-	reflectPackage      = protogen.GoImportPath("reflect")
-	protoimplPackage    = protogen.GoImportPath("github.com/golang/protobuf/v2/runtime/protoimpl")
-	protoreflectPackage = protogen.GoImportPath("github.com/golang/protobuf/v2/reflect/protoreflect")
-	prototypePackage    = protogen.GoImportPath("github.com/golang/protobuf/v2/internal/prototype")
+	reflectPackage       = protogen.GoImportPath("reflect")
+	protoimplPackage     = protogen.GoImportPath("github.com/golang/protobuf/v2/runtime/protoimpl")
+	protoreflectPackage  = protogen.GoImportPath("github.com/golang/protobuf/v2/reflect/protoreflect")
+	protoregistryPackage = protogen.GoImportPath("github.com/golang/protobuf/v2/reflect/protoregistry")
+	prototypePackage     = protogen.GoImportPath("github.com/golang/protobuf/v2/internal/prototype")
 )
 
 // TODO: Add support for proto options.
@@ -179,6 +180,11 @@
 	if len(f.allExtensions) > 0 {
 		g.P("ExtensionOutputTypes: extensionTypes,")
 	}
+	if isDescriptor(f.File) {
+		// TODO: Enable this for all protos.
+		g.P("FilesRegistry: ", protoregistryPackage.Ident("GlobalFiles"), ",")
+		g.P("TypesRegistry: ", protoregistryPackage.Ident("GlobalTypes"), ",")
+	}
 	g.P("}.Init()")
 
 	// Copy the local list of message types into the global array.
@@ -190,8 +196,6 @@
 		g.P("}")
 	}
 
-	// TODO: Add v2 registration and stop v1 registration in genInitFunction.
-
 	// The descriptor proto needs to register the option types with the
 	// prototype so that the package can properly handle those option types.
 	if isDescriptor(f.File) {