cmd/protoc-gen-go: additional conflict resolution for oneof field types

This is purely for consistent output with the previous protoc-gen-go.

Consider this message:

  message M {
    oneof union { string conflict = 1; }
    message Conflict {}
  }

The type for the wrapper of M.conflict will have the same name as the
embedded message type for M.Conflict.

The previous protoc-gen-go performs a disambiguation step where it adds
_s to the names of oneof field types until they have no conflicts with
nested messages or enums in the same message as the field.

There are a number of ways in which this can fail, of course. Preserve
the behavior for now.

Change-Id: I78a1c6588b577324e003b8bc337b75bb363432ba
Reviewed-on: https://go-review.googlesource.com/136357
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/cmd/protoc-gen-go/main.go b/cmd/protoc-gen-go/main.go
index 8c73c0c..2c94298 100644
--- a/cmd/protoc-gen-go/main.go
+++ b/cmd/protoc-gen-go/main.go
@@ -526,7 +526,7 @@
 		defaultValue := fieldDefaultValue(g, message, field)
 		g.P("func (m *", message.GoIdent, ") Get", field.GoName, "() ", goType, " {")
 		if field.OneofType != nil {
-			g.P("if x, ok := m.Get", field.OneofType.GoName, "().(*", message.GoIdent.GoName, "_", field.GoName, "); ok {")
+			g.P("if x, ok := m.Get", field.OneofType.GoName, "().(*", fieldOneofType(field), "); ok {")
 			g.P("return x.", field.GoName)
 			g.P("}")
 		} else {
diff --git a/cmd/protoc-gen-go/oneof.go b/cmd/protoc-gen-go/oneof.go
index 98880ec..2e788fe 100644
--- a/cmd/protoc-gen-go/oneof.go
+++ b/cmd/protoc-gen-go/oneof.go
@@ -339,8 +339,32 @@
 
 // fieldOneofType returns the wrapper type used to represent a field in a oneof.
 func fieldOneofType(field *protogen.Field) protogen.GoIdent {
-	return protogen.GoIdent{
+	ident := protogen.GoIdent{
 		GoImportPath: field.ParentMessage.GoIdent.GoImportPath,
 		GoName:       field.ParentMessage.GoIdent.GoName + "_" + field.GoName,
 	}
+	// Check for collisions with nested messages or enums.
+	//
+	// This conflict resolution is incomplete: Among other things, it
+	// does not consider collisions with other oneof field types.
+	//
+	// TODO: Consider dropping this entirely. Detecting conflicts and
+	// producing an error is almost certainly better than permuting
+	// field and type names in mostly unpredictable ways.
+Loop:
+	for {
+		for _, message := range field.ParentMessage.Messages {
+			if message.GoIdent == ident {
+				ident.GoName += "_"
+				continue Loop
+			}
+		}
+		for _, enum := range field.ParentMessage.Enums {
+			if enum.GoIdent == ident {
+				ident.GoName += "_"
+				continue Loop
+			}
+		}
+		return ident
+	}
 }
diff --git a/cmd/protoc-gen-go/testdata/fieldnames/fieldnames.pb.go b/cmd/protoc-gen-go/testdata/fieldnames/fieldnames.pb.go
index 570e348..6e6e84c 100644
--- a/cmd/protoc-gen-go/testdata/fieldnames/fieldnames.pb.go
+++ b/cmd/protoc-gen-go/testdata/fieldnames/fieldnames.pb.go
@@ -54,7 +54,12 @@
 	// Types that are valid to be assigned to OneofConflictB:
 	//	*Message_OneofNoConflict
 	//	*Message_OneofConflictB_
-	OneofConflictB       isMessage_OneofConflictB `protobuf_oneof:"oneof_conflict_b"`
+	OneofConflictB isMessage_OneofConflictB `protobuf_oneof:"oneof_conflict_b"`
+	// Oneof with a field name that conflicts with a nested message.
+	//
+	// Types that are valid to be assigned to OneofConflictC:
+	//	*Message_OneofMessageConflict_
+	OneofConflictC       isMessage_OneofConflictC `protobuf_oneof:"oneof_conflict_c"`
 	XXX_NoUnkeyedLiteral struct{}                 `json:"-"`
 	XXX_unrecognized     []byte                   `json:"-"`
 	XXX_sizecache        int32                    `json:"-"`
@@ -244,12 +249,37 @@
 	return ""
 }
 
+type isMessage_OneofConflictC interface {
+	isMessage_OneofConflictC()
+}
+
+type Message_OneofMessageConflict_ struct {
+	OneofMessageConflict string `protobuf:"bytes,60,opt,name=oneof_message_conflict,json=oneofMessageConflict,oneof"`
+}
+
+func (*Message_OneofMessageConflict_) isMessage_OneofConflictC() {}
+
+func (m *Message) GetOneofConflictC() isMessage_OneofConflictC {
+	if m != nil {
+		return m.OneofConflictC
+	}
+	return nil
+}
+
+func (m *Message) GetOneofMessageConflict() string {
+	if x, ok := m.GetOneofConflictC().(*Message_OneofMessageConflict_); ok {
+		return x.OneofMessageConflict
+	}
+	return ""
+}
+
 // XXX_OneofFuncs is for the internal use of the proto package.
 func (*Message) XXX_OneofFuncs() (func(msg proto.Message, b *proto.Buffer) error, func(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error), func(msg proto.Message) (n int), []interface{}) {
 	return _Message_OneofMarshaler, _Message_OneofUnmarshaler, _Message_OneofSizer, []interface{}{
 		(*Message_OneofConflictA)(nil),
 		(*Message_OneofNoConflict)(nil),
 		(*Message_OneofConflictB_)(nil),
+		(*Message_OneofMessageConflict_)(nil),
 	}
 }
 
@@ -276,6 +306,15 @@
 	default:
 		return fmt.Errorf("Message.OneofConflictB has unexpected type %T", x)
 	}
+	// oneof_conflict_c
+	switch x := m.OneofConflictC.(type) {
+	case *Message_OneofMessageConflict_:
+		b.EncodeVarint(60<<3 | proto.WireBytes)
+		b.EncodeStringBytes(x.OneofMessageConflict)
+	case nil:
+	default:
+		return fmt.Errorf("Message.OneofConflictC has unexpected type %T", x)
+	}
 	return nil
 }
 
@@ -303,6 +342,13 @@
 		x, err := b.DecodeStringBytes()
 		m.OneofConflictB = &Message_OneofConflictB_{x}
 		return true, err
+	case 60: // oneof_conflict_c.oneof_message_conflict
+		if wire != proto.WireBytes {
+			return true, proto.ErrInternalBadWireType
+		}
+		x, err := b.DecodeStringBytes()
+		m.OneofConflictC = &Message_OneofMessageConflict_{x}
+		return true, err
 	default:
 		return false, nil
 	}
@@ -334,39 +380,83 @@
 	default:
 		panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
 	}
+	// oneof_conflict_c
+	switch x := m.OneofConflictC.(type) {
+	case *Message_OneofMessageConflict_:
+		n += 2 // tag and wire
+		n += proto.SizeVarint(uint64(len(x.OneofMessageConflict)))
+		n += len(x.OneofMessageConflict)
+	case nil:
+	default:
+		panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
+	}
 	return n
 }
 
+type Message_OneofMessageConflict struct {
+	XXX_NoUnkeyedLiteral struct{} `json:"-"`
+	XXX_unrecognized     []byte   `json:"-"`
+	XXX_sizecache        int32    `json:"-"`
+}
+
+func (m *Message_OneofMessageConflict) Reset()         { *m = Message_OneofMessageConflict{} }
+func (m *Message_OneofMessageConflict) String() string { return proto.CompactTextString(m) }
+func (*Message_OneofMessageConflict) ProtoMessage()    {}
+func (*Message_OneofMessageConflict) Descriptor() ([]byte, []int) {
+	return fileDescriptor_6bbe3f70febb9403, []int{0, 0}
+}
+
+func (m *Message_OneofMessageConflict) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_Message_OneofMessageConflict.Unmarshal(m, b)
+}
+func (m *Message_OneofMessageConflict) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_Message_OneofMessageConflict.Marshal(b, m, deterministic)
+}
+func (m *Message_OneofMessageConflict) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_Message_OneofMessageConflict.Merge(m, src)
+}
+func (m *Message_OneofMessageConflict) XXX_Size() int {
+	return xxx_messageInfo_Message_OneofMessageConflict.Size(m)
+}
+func (m *Message_OneofMessageConflict) XXX_DiscardUnknown() {
+	xxx_messageInfo_Message_OneofMessageConflict.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_Message_OneofMessageConflict proto.InternalMessageInfo
+
 func init() {
 	proto.RegisterType((*Message)(nil), "goproto.protoc.fieldnames.Message")
+	proto.RegisterType((*Message_OneofMessageConflict)(nil), "goproto.protoc.fieldnames.Message.OneofMessageConflict")
 }
 
 func init() { proto.RegisterFile("fieldnames/fieldnames.proto", fileDescriptor_6bbe3f70febb9403) }
 
 var fileDescriptor_6bbe3f70febb9403 = []byte{
-	// 382 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x92, 0x4b, 0x6f, 0xd3, 0x40,
-	0x10, 0xc7, 0x09, 0x44, 0x6a, 0x3c, 0x69, 0x79, 0xac, 0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xd5, 0xb7, 0x5f, 0x5f, 0x2d, 0x91, 0xcd,
+	0x71, 0x61, 0x29, 0xd7, 0xce, 0x2e, 0xc8, 0xdb, 0x25, 0x3f, 0xed, 0xd2, 0x14, 0x69, 0x9c, 0xcc,
+	0xb5, 0x45, 0x77, 0x6d, 0x69, 0x19, 0xb0, 0x0c, 0xa9, 0x0e, 0x7a, 0xef, 0xcf, 0xf1, 0x2f, 0x00,
+	0x00, 0xff, 0xff, 0xe0, 0x4d, 0xd2, 0x82, 0x34, 0x03, 0x00, 0x00,
 }
diff --git a/cmd/protoc-gen-go/testdata/fieldnames/fieldnames.proto b/cmd/protoc-gen-go/testdata/fieldnames/fieldnames.proto
index 9b4b917..4301804 100644
--- a/cmd/protoc-gen-go/testdata/fieldnames/fieldnames.proto
+++ b/cmd/protoc-gen-go/testdata/fieldnames/fieldnames.proto
@@ -46,4 +46,10 @@
     string oneof_no_conflict = 50;
     string OneofConflictB = 51;
   }
+
+  // Oneof with a field name that conflicts with a nested message.
+  oneof oneof_conflict_c {
+    string oneof_message_conflict = 60;
+  }
+  message OneofMessageConflict {}
 }