cmd/protoc-gen-go: replicate v1 generator behavior for MessageSet extensions

Given:

  package foo
  extend proto2.bridge.MessageSet {
    optional Message message_set_extension = 100;
  }

Register the extension as a message set extension and give it the name
"foo.".

We really shouldn't do this in this case; the special-case treatment of
extensions to MessageSet is only for extensions nested in a parent
message. However, this is consistent with the behavior of the v1 generator.
Match that for now.

Change-Id: I919c409605a197904fd3227efc920192d484f431
Reviewed-on: https://go-review.googlesource.com/c/145957
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go
index 96b27fb..b5b6655 100644
--- a/cmd/protoc-gen-go/internal_gengo/main.go
+++ b/cmd/protoc-gen-go/internal_gengo/main.go
@@ -824,8 +824,8 @@
 	// differently in other languages:
 	// https://github.com/google/protobuf/blob/aff10976/src/google/protobuf/text_format.cc#L1560
 	name := extension.Desc.FullName()
-	if isExtensionMessageSetElement(gen, extension) {
-		name = name.Parent()
+	if n, ok := isExtensionMessageSetElement(extension); ok {
+		name = n
 	}
 
 	g.P("var ", extensionVar(f.File, extension), " = &", protogen.GoIdent{
@@ -846,10 +846,27 @@
 	g.P()
 }
 
-func isExtensionMessageSetElement(gen *protogen.Plugin, extension *protogen.Extension) bool {
-	return extension.ParentMessage != nil &&
-		extension.ExtendedType.Desc.Options().(*descpb.MessageOptions).GetMessageSetWireFormat() &&
-		extension.Desc.Name() == "message_set_extension"
+// isExtensionMessageSetELement returns the adjusted name of an extension
+// which extends proto2.bridge.MessageSet.
+func isExtensionMessageSetElement(extension *protogen.Extension) (name protoreflect.FullName, ok bool) {
+	opts := extension.ExtendedType.Desc.Options().(*descpb.MessageOptions)
+	if !opts.GetMessageSetWireFormat() || extension.Desc.Name() != "message_set_extension" {
+		return "", false
+	}
+	if extension.ParentMessage == nil {
+		// This case shouldn't be given special handling at all--we're
+		// only supposed to drop the ".message_set_extension" for
+		// extensions defined within a message (i.e., the extension
+		// takes the message's name).
+		//
+		// This matches the behavior of the v1 generator, however.
+		//
+		// TODO: See if we can drop this case.
+		name = extension.Desc.FullName()
+		name = name[:len(name)-len("message_set_extension")]
+		return name, true
+	}
+	return extension.Desc.FullName().Parent(), true
 }
 
 // extensionVar returns the var holding the ExtensionDesc for an extension.
@@ -928,7 +945,7 @@
 		GoImportPath: protoPackage,
 		GoName:       "RegisterExtension",
 	}, "(", extensionVar(f.File, extension), ")")
-	if isExtensionMessageSetElement(gen, extension) {
+	if name, ok := isExtensionMessageSetElement(extension); ok {
 		goType, pointer := fieldGoType(g, extension)
 		if pointer {
 			goType = "*" + goType
@@ -936,7 +953,7 @@
 		g.P(protogen.GoIdent{
 			GoImportPath: protoPackage,
 			GoName:       "RegisterMessageSetType",
-		}, "((", goType, ")(nil), ", extension.Desc.Number(), ",", strconv.Quote(string(extension.Desc.FullName().Parent())), ")")
+		}, "((", goType, ")(nil), ", extension.Desc.Number(), ",", strconv.Quote(string(name)), ")")
 	}
 }
 
diff --git a/cmd/protoc-gen-go/testdata/extensions/ext/ext.pb.go b/cmd/protoc-gen-go/testdata/extensions/ext/ext.pb.go
index e205a86..4170822 100644
--- a/cmd/protoc-gen-go/testdata/extensions/ext/ext.pb.go
+++ b/cmd/protoc-gen-go/testdata/extensions/ext/ext.pb.go
@@ -734,6 +734,15 @@
 	Filename:      "extensions/ext/ext.proto",
 }
 
+var E_MessageSetExtension = &proto.ExtensionDesc{
+	ExtendedType:  (*base.MessageSetWireFormatMessage)(nil),
+	ExtensionType: (*MessageSetWireFormatExtension)(nil),
+	Field:         101,
+	Name:          "goproto.protoc.extension.ext.",
+	Tag:           "bytes,101,opt,name=message_set_extension",
+	Filename:      "extensions/ext/ext.proto",
+}
+
 func init() {
 	proto.RegisterEnum("goproto.protoc.extension.ext.Enum", Enum_name, Enum_value)
 	proto.RegisterType((*Message)(nil), "goproto.protoc.extension.ext.Message")
@@ -788,80 +797,82 @@
 	proto.RegisterExtension(E_Repeatedgroup)
 	proto.RegisterExtension(E_ExtendableField)
 	proto.RegisterExtension(E_ExtendableStringField)
+	proto.RegisterExtension(E_MessageSetExtension)
+	proto.RegisterMessageSetType((*MessageSetWireFormatExtension)(nil), 101, "goproto.protoc.extension.ext.")
 }
 
 func init() { proto.RegisterFile("extensions/ext/ext.proto", fileDescriptor_bf470ef4907b23cb) }
 
 var fileDescriptor_bf470ef4907b23cb = []byte{
-	// 1111 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x98, 0xcb, 0x6e, 0xdb, 0x46,
+	// 1120 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x98, 0xcb, 0x6e, 0xdb, 0x46,
 	0x14, 0x86, 0x3b, 0x92, 0x12, 0x2b, 0x53, 0xcb, 0x96, 0x18, 0x24, 0x15, 0x04, 0x17, 0x18, 0x08,
 	0x28, 0x32, 0x75, 0x1b, 0x09, 0x60, 0x05, 0x01, 0x65, 0x77, 0x46, 0xed, 0xc0, 0x0b, 0xa1, 0x28,
 	0x0d, 0xc3, 0x4e, 0xba, 0x60, 0x29, 0x73, 0xc4, 0xb0, 0x95, 0x48, 0x97, 0x97, 0x44, 0x5d, 0x34,
 	0x10, 0xfa, 0x04, 0x7d, 0x8f, 0xa2, 0xf7, 0x5b, 0x7a, 0xdf, 0xb6, 0x8b, 0x6e, 0xfa, 0x44, 0xc1,
-	0xcc, 0xf0, 0x32, 0xa4, 0x64, 0x59, 0xc3, 0x85, 0x04, 0x72, 0x48, 0x7d, 0xe7, 0xf0, 0x3f, 0x47,
-	0xfc, 0x0f, 0x09, 0xdb, 0x64, 0x1e, 0x12, 0x37, 0x70, 0x3c, 0x37, 0xe8, 0x93, 0x79, 0x48, 0x3f,
-	0xbd, 0x4b, 0xdf, 0x0b, 0x3d, 0x65, 0xcf, 0xf6, 0xd8, 0x06, 0xdf, 0x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8d, 0xe4, 0xb1, 0x0e, 0x02, 0xb8, 0x21, 0x60, 0x4f, 0x39, 0xb6, 0x28, 0xc1,
-	0x70, 0x20, 0x45, 0xfd, 0x08, 0x01, 0x5c, 0xcd, 0x4b, 0x30, 0x1c, 0x2c, 0x4b, 0x20, 0x49, 0xfd,
-	0x18, 0x01, 0xac, 0x14, 0x24, 0x28, 0x62, 0x23, 0x79, 0xec, 0x14, 0x01, 0x5c, 0x2b, 0x48, 0x30,
-	0x1c, 0x68, 0x0f, 0xa1, 0x22, 0x64, 0x3b, 0x71, 0xe6, 0xc4, 0x92, 0xd4, 0x76, 0x86, 0x00, 0xde,
-	0xd5, 0x5b, 0x59, 0xbe, 0x31, 0x44, 0x3b, 0x83, 0xd9, 0xa2, 0x51, 0x86, 0xec, 0x22, 0x80, 0xb7,
-	0xf4, 0xec, 0xb2, 0x8f, 0x62, 0x70, 0xae, 0x6c, 0x93, 0xa9, 0x67, 0x86, 0x52, 0x58, 0x0f, 0x01,
-	0x5c, 0x11, 0xca, 0x76, 0x44, 0x09, 0xab, 0x84, 0x90, 0x54, 0xf8, 0x12, 0x01, 0xdc, 0x5c, 0x12,
-	0x62, 0x38, 0x58, 0x21, 0x84, 0x24, 0xf9, 0x13, 0x04, 0xf0, 0xcd, 0xa2, 0x10, 0xc5, 0x9e, 0xb0,
-	0xbc, 0x68, 0x3c, 0x95, 0x33, 0x1c, 0x1f, 0x01, 0x0c, 0x84, 0x9e, 0x78, 0x97, 0x21, 0x0a, 0x1d,
-	0x2c, 0xef, 0x99, 0x01, 0xb3, 0x4c, 0xa1, 0x83, 0x19, 0x22, 0x5f, 0xb6, 0xf1, 0xa7, 0x21, 0x09,
-	0xa4, 0xa8, 0x21, 0x1b, 0x28, 0xb2, 0xb2, 0x1d, 0x50, 0x82, 0xf6, 0x4c, 0xd4, 0x76, 0x54, 0xc2,
-	0x74, 0x23, 0x66, 0x1d, 0xaf, 0x6d, 0x64, 0x1d, 0x42, 0x09, 0xe2, 0x15,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e, 0x23, 0xb5, 0x7f, 0x31, 0xb3, 0xf8, 0xfe,
+	0xc5, 0x5d, 0x9b, 0xb8, 0x77, 0x6d, 0xaf, 0x1f, 0x92, 0x20, 0xb4, 0xcc, 0x90, 0xbd, 0x0e, 0x13,
+	0xde, 0x8f, 0x3d, 0x0b, 0x00, 0x00, 0xff, 0xff, 0xe1, 0x33, 0x5c, 0xd7, 0x84, 0x13, 0x00, 0x00,
 }
diff --git a/cmd/protoc-gen-go/testdata/extensions/ext/ext.proto b/cmd/protoc-gen-go/testdata/extensions/ext/ext.proto
index 0bb263a..9997a84 100644
--- a/cmd/protoc-gen-go/testdata/extensions/ext/ext.proto
+++ b/cmd/protoc-gen-go/testdata/extensions/ext/ext.proto
@@ -101,3 +101,8 @@
     optional MessageSetWireFormatExtension message_set_extension = 100;
   }
 }
+
+// Message set extension, not nested in a message.
+extend goproto.protoc.extension.base.MessageSetWireFormatMessage {
+  optional MessageSetWireFormatExtension message_set_extension = 101;
+}