cmd/protoc-gen-go: add additional import public test case

New test case demonstrating an import public bug:

  a.proto publicly imports b.proto and c.proto
  b.proto publicly imports c.proto
  a.pb.go includes two copies of symbols in c.pb.go

The problem is that the new implementation of public imports, which
parses the generated code for the import to determine what symbols
are present, doesn't distinguish between symbols defined in a file
and symbols imported from a public import of some other file. This
can then lead to duplicate definitions in cases like the above.

Change-Id: Ia86e9f188d7bae8d9d4afbd2b5db9b64071425c3
Reviewed-on: https://go-review.googlesource.com/c/156347
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/cmd/protoc-gen-go/testdata/import_public/a.pb.go b/cmd/protoc-gen-go/testdata/import_public/a.pb.go
index 738dc5e..70151ad 100644
--- a/cmd/protoc-gen-go/testdata/import_public/a.pb.go
+++ b/cmd/protoc-gen-go/testdata/import_public/a.pb.go
@@ -6,6 +6,7 @@
 import (
 	proto "github.com/golang/protobuf/proto"
 	sub "github.com/golang/protobuf/v2/cmd/protoc-gen-go/testdata/import_public/sub"
+	sub2 "github.com/golang/protobuf/v2/cmd/protoc-gen-go/testdata/import_public/sub2"
 	protoreflect "github.com/golang/protobuf/v2/reflect/protoreflect"
 	prototype "github.com/golang/protobuf/v2/reflect/prototype"
 	protoimpl "github.com/golang/protobuf/v2/runtime/protoimpl"
@@ -19,6 +20,7 @@
 
 // Symbols defined in public import of import_public/sub/a.proto
 
+type Sub2Message = sub.Sub2Message
 type E = sub.E
 
 const E_ZERO = sub.E_ZERO
@@ -55,6 +57,10 @@
 
 var E_ExtensionField = sub.E_ExtensionField
 
+// Symbols defined in public import of import_public/sub2/a.proto
+
+type Sub2Message = sub2.Sub2Message
+
 type Public struct {
 	M                    *sub.M   `protobuf:"bytes,1,opt,name=m" json:"m,omitempty"`
 	E                    *sub.E   `protobuf:"varint,2,opt,name=e,enum=goproto.protoc.import_public.sub.E" json:"e,omitempty"`
@@ -134,20 +140,21 @@
 }
 
 var fileDescriptor_73b7577c95fa6b70 = []byte{
-	// 202 bytes of a gzipped FileDescriptorProto
+	// 210 bytes of a gzipped FileDescriptorProto
 	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0xcd, 0xcc, 0x2d, 0xc8,
 	0x2f, 0x2a, 0x89, 0x2f, 0x28, 0x4d, 0xca, 0xc9, 0x4c, 0xd6, 0x4f, 0xd4, 0x2b, 0x28, 0xca, 0x2f,
 	0xc9, 0x17, 0x92, 0x49, 0xcf, 0x07, 0x33, 0x20, 0xdc, 0x64, 0x3d, 0x14, 0x55, 0x52, 0x92, 0xa8,
-	0x9a, 0x8a, 0x4b, 0x93, 0x60, 0x1a, 0xa5, 0xd0, 0xcc, 0x4b, 0x82, 0x08, 0x2b, 0xad, 0x64, 0xe4,
-	0x62, 0x0b, 0x00, 0x0b, 0x09, 0x19, 0x72, 0x31, 0xe6, 0x4a, 0x30, 0x2a, 0x30, 0x6a, 0x70, 0x1b,
-	0x29, 0xeb, 0xe1, 0xb3, 0x46, 0xaf, 0xb8, 0x34, 0x49, 0xcf, 0x37, 0x88, 0x31, 0x17, 0xa4, 0x25,
-	0x55, 0x82, 0x49, 0x81, 0x51, 0x83, 0x8f, 0x18, 0x2d, 0xae, 0x41, 0x8c, 0xa9, 0x42, 0x96, 0x5c,
-	0xac, 0x39, 0xf9, 0xc9, 0x89, 0x39, 0x12, 0xcc, 0xc4, 0xd8, 0xe4, 0x03, 0x52, 0x1a, 0x04, 0xd1,
-	0xe1, 0xe4, 0x11, 0xe5, 0x96, 0x9e, 0x59, 0x92, 0x51, 0x9a, 0xa4, 0x97, 0x9c, 0x9f, 0xab, 0x9f,
-	0x9e, 0x9f, 0x93, 0x98, 0x97, 0xae, 0x0f, 0xd6, 0x96, 0x54, 0x9a, 0xa6, 0x5f, 0x66, 0xa4, 0x9f,
-	0x9c, 0x9b, 0x02, 0xe1, 0x27, 0xeb, 0xa6, 0xa7, 0xe6, 0xe9, 0xa6, 0xe7, 0xeb, 0x97, 0xa4, 0x16,
-	0x97, 0xa4, 0x24, 0x96, 0x24, 0xea, 0xa3, 0x18, 0x1b, 0xc0, 0x10, 0xc0, 0x08, 0x08, 0x00, 0x00,
-	0xff, 0xff, 0xfa, 0x3e, 0xda, 0xad, 0x61, 0x01, 0x00, 0x00,
+	0x9a, 0x8a, 0x4b, 0x93, 0x60, 0x1a, 0xa5, 0xa4, 0x30, 0xa4, 0x8c, 0xe0, 0x72, 0x68, 0x76, 0x25,
+	0x41, 0x84, 0x95, 0x56, 0x32, 0x72, 0xb1, 0x05, 0x80, 0x85, 0x84, 0x0c, 0xb9, 0x18, 0x73, 0x25,
+	0x18, 0x15, 0x18, 0x35, 0xb8, 0x8d, 0x94, 0xf5, 0xf0, 0x39, 0x41, 0xaf, 0xb8, 0x34, 0x49, 0xcf,
+	0x37, 0x88, 0x31, 0x17, 0xa4, 0x25, 0x55, 0x82, 0x49, 0x81, 0x51, 0x83, 0x8f, 0x18, 0x2d, 0xae,
+	0x41, 0x8c, 0xa9, 0x42, 0x96, 0x5c, 0xac, 0x39, 0xf9, 0xc9, 0x89, 0x39, 0x12, 0xcc, 0xc4, 0xd8,
+	0xe4, 0x03, 0x52, 0x1a, 0x04, 0xd1, 0xe1, 0xe4, 0x11, 0xe5, 0x96, 0x9e, 0x59, 0x92, 0x51, 0x9a,
+	0xa4, 0x97, 0x9c, 0x9f, 0xab, 0x9f, 0x9e, 0x9f, 0x93, 0x98, 0x97, 0xae, 0x0f, 0xd6, 0x96, 0x54,
+	0x9a, 0xa6, 0x5f, 0x66, 0xa4, 0x9f, 0x9c, 0x9b, 0x02, 0xe1, 0x27, 0xeb, 0xa6, 0xa7, 0xe6, 0xe9,
+	0xa6, 0xe7, 0xeb, 0x97, 0xa4, 0x16, 0x97, 0xa4, 0x24, 0x96, 0x24, 0xea, 0xa3, 0x18, 0x1b, 0xc0,
+	0x10, 0xc0, 0x18, 0xc0, 0x04, 0x08, 0x00, 0x00, 0xff, 0xff, 0xb3, 0x41, 0x0a, 0x11, 0x7f, 0x01,
+	0x00, 0x00,
 }
 
 func init() {
@@ -172,6 +179,7 @@
 	Package: "goproto.protoc.import_public",
 	Imports: []protoreflect.FileImport{
 		{FileDescriptor: prototype.PlaceholderFile("import_public/sub/a.proto", "goproto.protoc.import_public.sub"), IsPublic: true},
+		{FileDescriptor: prototype.PlaceholderFile("import_public/sub2/a.proto", "goproto.protoc.import_public.sub2"), IsPublic: true},
 		{FileDescriptor: prototype.PlaceholderFile("import_public/b.proto", "goproto.protoc.import_public"), IsPublic: true},
 	},
 }
diff --git a/cmd/protoc-gen-go/testdata/import_public/a.proto b/cmd/protoc-gen-go/testdata/import_public/a.proto
index e33b758..091dcac 100644
--- a/cmd/protoc-gen-go/testdata/import_public/a.proto
+++ b/cmd/protoc-gen-go/testdata/import_public/a.proto
@@ -8,8 +8,9 @@
 
 option go_package = "github.com/golang/protobuf/v2/cmd/protoc-gen-go/testdata/import_public";
 
-import public "import_public/sub/a.proto"; // Different Go package.
-import public "import_public/b.proto";     // Same Go package.
+import public "import_public/sub/a.proto";  // Different Go package.
+import public "import_public/sub2/a.proto"; // Different Go package.
+import public "import_public/b.proto";      // Same Go package.
 
 message Public {
   optional goproto.protoc.import_public.sub.M m = 1;
diff --git a/cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go b/cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go
index faefc5e..3e83c85 100644
--- a/cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go
+++ b/cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go
@@ -5,6 +5,7 @@
 
 import (
 	proto "github.com/golang/protobuf/proto"
+	sub2 "github.com/golang/protobuf/v2/cmd/protoc-gen-go/testdata/import_public/sub2"
 	protoreflect "github.com/golang/protobuf/v2/reflect/protoreflect"
 	prototype "github.com/golang/protobuf/v2/reflect/prototype"
 	protoimpl "github.com/golang/protobuf/v2/runtime/protoimpl"
@@ -17,6 +18,10 @@
 // proto package needs to be updated.
 const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
 
+// Symbols defined in public import of import_public/sub2/a.proto
+
+type Sub2Message = sub2.Sub2Message
+
 type E int32
 
 const (
@@ -427,33 +432,33 @@
 }
 
 var fileDescriptor_382f7805394b5c4e = []byte{
-	// 402 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x92, 0xcb, 0x6e, 0xd3, 0x40,
-	0x14, 0x86, 0x7b, 0xe2, 0x5e, 0xc2, 0x29, 0x85, 0x76, 0x44, 0xd0, 0xd0, 0x95, 0x09, 0x2c, 0xac,
-	0xa2, 0x7a, 0x24, 0x63, 0x79, 0x91, 0x1d, 0x91, 0xc2, 0x4d, 0xb5, 0x2a, 0xd9, 0x62, 0xd3, 0x8d,
-	0xe5, 0xb1, 0xc7, 0xc6, 0x52, 0x3c, 0x13, 0x75, 0x66, 0x10, 0xcb, 0x3c, 0x15, 0x2f, 0xc0, 0x8b,
-	0xa1, 0xd8, 0x6d, 0x93, 0x88, 0x20, 0xba, 0xf3, 0xf9, 0xfd, 0x7d, 0xff, 0xb1, 0x3c, 0x83, 0xaf,
-	0x9a, 0x76, 0xa1, 0x6e, 0x4d, 0xb6, 0xb0, 0x7c, 0xde, 0x14, 0x4c, 0x5b, 0xce, 0x72, 0x7f, 0x71,
-	0xab, 0x8c, 0x22, 0x6e, 0xad, 0xba, 0x87, 0x7e, 0x2c, 0xfc, 0x2d, 0xd2, 0xd7, 0x96, 0x9f, 0xef,
-	0x90, 0x79, 0x4f, 0x8f, 0x7f, 0x39, 0x08, 0x31, 0x09, 0x71, 0xd0, 0x06, 0x14, 0x5c, 0xf0, 0x8e,
-	0x83, 0xb7, 0xfe, 0xff, 0xfa, 0xfc, 0x38, 0x48, 0x06, 0x6d, 0x40, 0x46, 0x08, 0x9a, 0xee, 0xbb,
-	0xe0, 0x3d, 0x99, 0x1c, 0x95, 0xa2, 0xca, 0xed, 0xdc, 0x24, 0xa0, 0x57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,
+	0x28, 0x29, 0x6b, 0xbe, 0x28, 0x26, 0x23, 0x3c, 0x5a, 0xb3, 0x88, 0x87, 0xd1, 0x2d, 0x70, 0x3e,
+	0x1c, 0x16, 0xa7, 0xab, 0xd5, 0x6a, 0x35, 0x98, 0x9d, 0xac, 0xbf, 0x44, 0xe7, 0x3f, 0x3f, 0x41,
+	0x98, 0x93, 0x21, 0xee, 0xf7, 0xbe, 0xe9, 0x25, 0x3e, 0xe7, 0xbf, 0x34, 0x17, 0xaa, 0x96, 0xa2,
+	0x77, 0x90, 0x37, 0x8f, 0x38, 0xa4, 0x5d, 0xb4, 0xe7, 0x8b, 0x9f, 0xdd, 0xb1, 0x1f, 0x5b, 0x74,
+	0x76, 0x79, 0xfd, 0xb5, 0xaa, 0xf5, 0x0f, 0xc3, 0xbc, 0x5c, 0x36, 0xb4, 0x92, 0x8b, 0x4c, 0x54,
+	0xb4, 0x4b, 0x61, 0xa6, 0xa4, 0x3f, 0x7d, 0x9a, 0x37, 0x45, 0x3f, 0xe7, 0x17, 0x15, 0x17, 0x17,
+	0x95, 0xa4, 0x9a, 0x2b, 0x5d, 0x64, 0x3a, 0xa3, 0x0f, 0x7e, 0xb6, 0x6f, 0xf0, 0x37, 0x00, 0x00,
+	0xff, 0xff, 0x19, 0x27, 0xa9, 0x12, 0xda, 0x02, 0x00, 0x00,
 }
 
 func init() {
@@ -480,6 +485,7 @@
 	Package: "goproto.protoc.import_public.sub",
 	Imports: []protoreflect.FileImport{
 		{FileDescriptor: prototype.PlaceholderFile("import_public/sub/b.proto", "goproto.protoc.import_public.sub")},
+		{FileDescriptor: prototype.PlaceholderFile("import_public/sub2/a.proto", "goproto.protoc.import_public.sub2"), IsPublic: true},
 	},
 }
 var xxx_A_ProtoFile_EnumTypes = [3]protoreflect.EnumType{
diff --git a/cmd/protoc-gen-go/testdata/import_public/sub/a.proto b/cmd/protoc-gen-go/testdata/import_public/sub/a.proto
index a833b11..83c407e 100644
--- a/cmd/protoc-gen-go/testdata/import_public/sub/a.proto
+++ b/cmd/protoc-gen-go/testdata/import_public/sub/a.proto
@@ -9,6 +9,7 @@
 option go_package = "github.com/golang/protobuf/v2/cmd/protoc-gen-go/testdata/import_public/sub";
 
 import "import_public/sub/b.proto";
+import public "import_public/sub2/a.proto";
 
 message M {
   // Field using a type in the same Go package, but a different source file.
diff --git a/cmd/protoc-gen-go/testdata/import_public/sub2/a.pb.go b/cmd/protoc-gen-go/testdata/import_public/sub2/a.pb.go
new file mode 100644
index 0000000..9546058
--- /dev/null
+++ b/cmd/protoc-gen-go/testdata/import_public/sub2/a.pb.go
@@ -0,0 +1,116 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// source: import_public/sub2/a.proto
+
+package sub2
+
+import (
+	proto "github.com/golang/protobuf/proto"
+	protoreflect "github.com/golang/protobuf/v2/reflect/protoreflect"
+	prototype "github.com/golang/protobuf/v2/reflect/prototype"
+	protoimpl "github.com/golang/protobuf/v2/runtime/protoimpl"
+)
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the proto package it is being compiled against.
+// A compilation error at this line likely means your copy of the
+// proto package needs to be updated.
+const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
+
+type Sub2Message struct {
+	XXX_NoUnkeyedLiteral struct{} `json:"-"`
+	XXX_unrecognized     []byte   `json:"-"`
+	XXX_sizecache        int32    `json:"-"`
+}
+
+type xxx_Sub2Message struct{ m *Sub2Message }
+
+func (m *Sub2Message) ProtoReflect() protoreflect.Message {
+	return xxx_Sub2Message{m}
+}
+func (m xxx_Sub2Message) Type() protoreflect.MessageType {
+	return xxx_A_ProtoFile_MessageTypes[0].Type
+}
+func (m xxx_Sub2Message) KnownFields() protoreflect.KnownFields {
+	return xxx_A_ProtoFile_MessageTypes[0].KnownFieldsOf(m.m)
+}
+func (m xxx_Sub2Message) UnknownFields() protoreflect.UnknownFields {
+	return xxx_A_ProtoFile_MessageTypes[0].UnknownFieldsOf(m.m)
+}
+func (m xxx_Sub2Message) Interface() protoreflect.ProtoMessage {
+	return m.m
+}
+
+func (m *Sub2Message) Reset()         { *m = Sub2Message{} }
+func (m *Sub2Message) String() string { return proto.CompactTextString(m) }
+func (*Sub2Message) ProtoMessage()    {}
+func (*Sub2Message) Descriptor() ([]byte, []int) {
+	return fileDescriptor_7ac53d99328778ac, []int{0}
+}
+
+func (m *Sub2Message) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_Sub2Message.Unmarshal(m, b)
+}
+func (m *Sub2Message) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_Sub2Message.Marshal(b, m, deterministic)
+}
+func (m *Sub2Message) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_Sub2Message.Merge(m, src)
+}
+func (m *Sub2Message) XXX_Size() int {
+	return xxx_messageInfo_Sub2Message.Size(m)
+}
+func (m *Sub2Message) XXX_DiscardUnknown() {
+	xxx_messageInfo_Sub2Message.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_Sub2Message proto.InternalMessageInfo
+
+func init() {
+	proto.RegisterFile("import_public/sub2/a.proto", fileDescriptor_7ac53d99328778ac)
+	proto.RegisterType((*Sub2Message)(nil), "goproto.protoc.import_public.sub2.Sub2Message")
+}
+
+var fileDescriptor_7ac53d99328778ac = []byte{
+	// 137 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x64, 0x8e, 0x31, 0x0b, 0xc2, 0x40,
+	0x0c, 0x46, 0xf7, 0x8a, 0x8b, 0xa3, 0x9b, 0xee, 0xbd, 0xc0, 0xfd, 0x04, 0x57, 0xe9, 0xe4, 0xe6,
+	0x22, 0x49, 0x7a, 0xc6, 0x83, 0x5e, 0x73, 0xf4, 0x12, 0x7f, 0xbf, 0xd8, 0x4e, 0xe2, 0xf6, 0x3d,
+	0x78, 0x1f, 0xbc, 0xee, 0x98, 0x4b, 0xd5, 0xc5, 0x1e, 0xd5, 0x69, 0xca, 0x0c, 0xcd, 0x29, 0x02,
+	0x86, 0xba, 0xa8, 0xe9, 0xe1, 0x24, 0xba, 0x8e, 0x0d, 0x39, 0xfc, 0xa8, 0xe1, 0xab, 0x9e, 0xf7,
+	0xdd, 0xee, 0xe6, 0x14, 0x87, 0xd4, 0x1a, 0x4a, 0xba, 0x0c, 0xf7, 0xab, 0x64, 0x7b, 0x39, 0x05,
+	0xd6, 0x02, 0xa2, 0x13, 0xce, 0x02, 0xeb, 0x9b, 0xfc, 0x09, 0xef, 0x08, 0x5c, 0xc6, 0x8d, 0xb9,
+	0x97, 0x34, 0xf7, 0xa2, 0x60, 0xa9, 0xd9, 0x88, 0x86, 0xf0, 0x1f, 0xf2, 0x09, 0x00, 0x00, 0xff,
+	0xff, 0xb4, 0xbf, 0x14, 0x77, 0x9d, 0x00, 0x00, 0x00,
+}
+
+func init() {
+	xxx_A_ProtoFile_FileDesc.Messages = xxx_A_ProtoFile_MessageDescs[0:1]
+	var err error
+	A_ProtoFile, err = prototype.NewFile(&xxx_A_ProtoFile_FileDesc)
+	if err != nil {
+		panic(err)
+	}
+}
+
+const _ = protoimpl.EnforceVersion(protoimpl.Version - 0)
+
+var A_ProtoFile protoreflect.FileDescriptor
+
+var xxx_A_ProtoFile_FileDesc = prototype.File{
+	Syntax:  protoreflect.Proto2,
+	Path:    "import_public/sub2/a.proto",
+	Package: "goproto.protoc.import_public.sub2",
+}
+var xxx_A_ProtoFile_MessageTypes = [1]protoimpl.MessageType{
+	{Type: prototype.GoMessage(
+		xxx_A_ProtoFile_MessageDescs[0].Reference(),
+		func(protoreflect.MessageType) protoreflect.ProtoMessage {
+			return new(Sub2Message)
+		},
+	)},
+}
+var xxx_A_ProtoFile_MessageDescs = [1]prototype.Message{
+	{
+		Name: "Sub2Message",
+	},
+}
diff --git a/cmd/protoc-gen-go/testdata/import_public/sub2/a.proto b/cmd/protoc-gen-go/testdata/import_public/sub2/a.proto
new file mode 100644
index 0000000..e63240d
--- /dev/null
+++ b/cmd/protoc-gen-go/testdata/import_public/sub2/a.proto
@@ -0,0 +1,11 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+syntax = "proto2";
+
+package goproto.protoc.import_public.sub2;
+
+option go_package = "github.com/golang/protobuf/v2/cmd/protoc-gen-go/testdata/import_public/sub2";
+
+message Sub2Message {}