protoc-gen-go: fix generation of proxy getters for distant types (#563)

Skip generating getters for fields that are not defined in the same file
as the publicly imported message.

This is the wrong way to fix this problem. It is, however, the expedient
one to hold us over until (soon, I hope) we completely redo public
imports as type aliases and make all of this moot.

The scenario:

  a.proto publicly imports b.proto
  b.proto has a message M
  M has a field F of type T

We generate forwarding methods for public imports.

  // T is defined in the same Go package as a.proto.
  func (m *M) GetF() T { ... }

Depending on what package T is defined in, we might need to qualify its
name (foo.T), and we might need to add an import for that package.
That's not what we used to do, however: Instead, we'd *only* generate
the GetF forwarder if T is defined in b.proto.

Commit 9d4962b4 made it so that we'd generate the forwarder if it T is
defined in the *same Go package* as b.proto, which seems safe and
reasonable. It turns out that something elsewhere in the generator is
getting confused by this, however. Rather than figuring out what--which
would also change a lot of generated code by adding forwarding methods
that didn't used to be there--this just reverts back to the behavior of
looking at files rather than packages.

All of this goes away completely once we start using type aliases,
because we don't need any forwarding methods at all at that point.
diff --git a/protoc-gen-go/generator/generator.go b/protoc-gen-go/generator/generator.go
index fb2dd16..be382f9 100644
--- a/protoc-gen-go/generator/generator.go
+++ b/protoc-gen-go/generator/generator.go
@@ -2293,8 +2293,14 @@
 		case descriptor.FieldDescriptorProto_TYPE_GROUP:
 			getter = false
 		case descriptor.FieldDescriptorProto_TYPE_MESSAGE, descriptor.FieldDescriptorProto_TYPE_ENUM:
-			// Only export getter if its return type is in this package.
-			getter = g.ObjectNamed(field.GetTypeName()).GoImportPath() == message.GoImportPath()
+			// Only export getter if its return type is in the same file.
+			//
+			// This should be the same package, not the same file.
+			// However, code elsewhere assumes that there's a 1-1 relationship
+			// between packages and files, so that's not safe.
+			//
+			// TODO: Tear out all of this complexity and just use type aliases.
+			getter = g.ObjectNamed(field.GetTypeName()).File() == message.File()
 			genType = true
 		default:
 			getter = true
diff --git a/protoc-gen-go/testdata/imp/imp2/imp2-enum.pb.go b/protoc-gen-go/testdata/imp/imp2/imp2-enum.pb.go
new file mode 100644
index 0000000..65f53bb
--- /dev/null
+++ b/protoc-gen-go/testdata/imp/imp2/imp2-enum.pb.go
@@ -0,0 +1,102 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// source: imp/imp2/imp2-enum.proto
+
+package imp2 // import "github.com/golang/protobuf/protoc-gen-go/testdata/imp/imp2"
+
+import proto "github.com/golang/protobuf/proto"
+import fmt "fmt"
+import math "math"
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ = proto.Marshal
+var _ = fmt.Errorf
+var _ = math.Inf
+
+// 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.ProtoPackageIsVersion2 // please upgrade the proto package
+
+type EnumPB_Type int32
+
+const (
+	EnumPB_TYPE_1 EnumPB_Type = 0
+)
+
+var EnumPB_Type_name = map[int32]string{
+	0: "TYPE_1",
+}
+var EnumPB_Type_value = map[string]int32{
+	"TYPE_1": 0,
+}
+
+func (x EnumPB_Type) Enum() *EnumPB_Type {
+	p := new(EnumPB_Type)
+	*p = x
+	return p
+}
+func (x EnumPB_Type) String() string {
+	return proto.EnumName(EnumPB_Type_name, int32(x))
+}
+func (x *EnumPB_Type) UnmarshalJSON(data []byte) error {
+	value, err := proto.UnmarshalJSONEnum(EnumPB_Type_value, data, "EnumPB_Type")
+	if err != nil {
+		return err
+	}
+	*x = EnumPB_Type(value)
+	return nil
+}
+func (EnumPB_Type) EnumDescriptor() ([]byte, []int) {
+	return fileDescriptor_imp2_enum_1cca8279ac3cbb94, []int{0, 0}
+}
+
+type EnumPB struct {
+	XXX_NoUnkeyedLiteral struct{} `json:"-"`
+	XXX_unrecognized     []byte   `json:"-"`
+	XXX_sizecache        int32    `json:"-"`
+}
+
+func (m *EnumPB) Reset()         { *m = EnumPB{} }
+func (m *EnumPB) String() string { return proto.CompactTextString(m) }
+func (*EnumPB) ProtoMessage()    {}
+func (*EnumPB) Descriptor() ([]byte, []int) {
+	return fileDescriptor_imp2_enum_1cca8279ac3cbb94, []int{0}
+}
+func (m *EnumPB) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_EnumPB.Unmarshal(m, b)
+}
+func (m *EnumPB) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_EnumPB.Marshal(b, m, deterministic)
+}
+func (dst *EnumPB) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_EnumPB.Merge(dst, src)
+}
+func (m *EnumPB) XXX_Size() int {
+	return xxx_messageInfo_EnumPB.Size(m)
+}
+func (m *EnumPB) XXX_DiscardUnknown() {
+	xxx_messageInfo_EnumPB.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_EnumPB proto.InternalMessageInfo
+
+func init() {
+	proto.RegisterType((*EnumPB)(nil), "imp.EnumPB")
+	proto.RegisterEnum("imp.EnumPB_Type", EnumPB_Type_name, EnumPB_Type_value)
+}
+
+func init() { proto.RegisterFile("imp/imp2/imp2-enum.proto", fileDescriptor_imp2_enum_1cca8279ac3cbb94) }
+
+var fileDescriptor_imp2_enum_1cca8279ac3cbb94 = []byte{
+	// 131 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0xc8, 0xcc, 0x2d, 0xd0,
+	0xcf, 0xcc, 0x2d, 0x30, 0x02, 0x13, 0xba, 0xa9, 0x79, 0xa5, 0xb9, 0x7a, 0x05, 0x45, 0xf9, 0x25,
+	0xf9, 0x42, 0xcc, 0x99, 0xb9, 0x05, 0x4a, 0x32, 0x5c, 0x6c, 0xae, 0x79, 0xa5, 0xb9, 0x01, 0x4e,
+	0x4a, 0x42, 0x5c, 0x2c, 0x21, 0x95, 0x05, 0xa9, 0x42, 0x5c, 0x5c, 0x6c, 0x21, 0x91, 0x01, 0xae,
+	0xf1, 0x86, 0x02, 0x0c, 0x4e, 0x36, 0x51, 0x56, 0xe9, 0x99, 0x25, 0x19, 0xa5, 0x49, 0x7a, 0xc9,
+	0xf9, 0xb9, 0xfa, 0xe9, 0xf9, 0x39, 0x89, 0x79, 0xe9, 0xfa, 0x60, 0xdd, 0x49, 0xa5, 0x69, 0x10,
+	0x46, 0xb2, 0x6e, 0x7a, 0x6a, 0x9e, 0x6e, 0x7a, 0xbe, 0x7e, 0x49, 0x6a, 0x71, 0x49, 0x4a, 0x62,
+	0x49, 0xa2, 0x3e, 0xcc, 0x42, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc6, 0x64, 0x9c, 0xa4, 0x7b,
+	0x00, 0x00, 0x00,
+}
diff --git a/protoc-gen-go/testdata/imp/imp2/imp2-enum.proto b/protoc-gen-go/testdata/imp/imp2/imp2-enum.proto
new file mode 100644
index 0000000..b31a4f7
--- /dev/null
+++ b/protoc-gen-go/testdata/imp/imp2/imp2-enum.proto
@@ -0,0 +1,42 @@
+// Go support for Protocol Buffers - Google's data interchange format
+//
+// Copyright 2011 The Go Authors.  All rights reserved.
+// https://github.com/golang/protobuf
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+//     * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+syntax = "proto2";
+
+package imp;
+
+option go_package = "github.com/golang/protobuf/protoc-gen-go/testdata/imp/imp2";
+
+message EnumPB {
+  enum Type {
+    TYPE_1 = 0;
+  }
+}
diff --git a/protoc-gen-go/testdata/imp/imp2/imp2.pb.go b/protoc-gen-go/testdata/imp/imp2/imp2.pb.go
index 048bc0a..ecdcf6d 100644
--- a/protoc-gen-go/testdata/imp/imp2/imp2.pb.go
+++ b/protoc-gen-go/testdata/imp/imp2/imp2.pb.go
@@ -51,21 +51,23 @@
 	return nil
 }
 func (PubliclyImportedEnum) EnumDescriptor() ([]byte, []int) {
-	return fileDescriptor_imp2_a81b5fa1a6dca420, []int{0}
+	return fileDescriptor_imp2_8c527bbde1f96503, []int{0}
 }
 
 type PubliclyImportedMessage struct {
-	Field                *int64   `protobuf:"varint,1,opt,name=field" json:"field,omitempty"`
-	XXX_NoUnkeyedLiteral struct{} `json:"-"`
-	XXX_unrecognized     []byte   `json:"-"`
-	XXX_sizecache        int32    `json:"-"`
+	Field *int64 `protobuf:"varint,1,opt,name=field" json:"field,omitempty"`
+	// Field using a type in the same Go package, but a different source file.
+	Field2               *EnumPB_Type `protobuf:"varint,2,opt,name=field2,enum=imp.EnumPB_Type" json:"field2,omitempty"`
+	XXX_NoUnkeyedLiteral struct{}     `json:"-"`
+	XXX_unrecognized     []byte       `json:"-"`
+	XXX_sizecache        int32        `json:"-"`
 }
 
 func (m *PubliclyImportedMessage) Reset()         { *m = PubliclyImportedMessage{} }
 func (m *PubliclyImportedMessage) String() string { return proto.CompactTextString(m) }
 func (*PubliclyImportedMessage) ProtoMessage()    {}
 func (*PubliclyImportedMessage) Descriptor() ([]byte, []int) {
-	return fileDescriptor_imp2_a81b5fa1a6dca420, []int{0}
+	return fileDescriptor_imp2_8c527bbde1f96503, []int{0}
 }
 func (m *PubliclyImportedMessage) XXX_Unmarshal(b []byte) error {
 	return xxx_messageInfo_PubliclyImportedMessage.Unmarshal(m, b)
@@ -92,6 +94,13 @@
 	return 0
 }
 
+func (m *PubliclyImportedMessage) GetField2() EnumPB_Type {
+	if m != nil && m.Field2 != nil {
+		return *m.Field2
+	}
+	return EnumPB_TYPE_1
+}
+
 type PubliclyImportedMessage2 struct {
 	Field                *PubliclyImportedMessage `protobuf:"bytes,1,opt,name=field" json:"field,omitempty"`
 	XXX_NoUnkeyedLiteral struct{}                 `json:"-"`
@@ -103,7 +112,7 @@
 func (m *PubliclyImportedMessage2) String() string { return proto.CompactTextString(m) }
 func (*PubliclyImportedMessage2) ProtoMessage()    {}
 func (*PubliclyImportedMessage2) Descriptor() ([]byte, []int) {
-	return fileDescriptor_imp2_a81b5fa1a6dca420, []int{1}
+	return fileDescriptor_imp2_8c527bbde1f96503, []int{1}
 }
 func (m *PubliclyImportedMessage2) XXX_Unmarshal(b []byte) error {
 	return xxx_messageInfo_PubliclyImportedMessage2.Unmarshal(m, b)
@@ -136,21 +145,23 @@
 	proto.RegisterEnum("imp.PubliclyImportedEnum", PubliclyImportedEnum_name, PubliclyImportedEnum_value)
 }
 
-func init() { proto.RegisterFile("imp/imp2/imp2.proto", fileDescriptor_imp2_a81b5fa1a6dca420) }
+func init() { proto.RegisterFile("imp/imp2/imp2.proto", fileDescriptor_imp2_8c527bbde1f96503) }
 
-var fileDescriptor_imp2_a81b5fa1a6dca420 = []byte{
-	// 198 bytes of a gzipped FileDescriptorProto
+var fileDescriptor_imp2_8c527bbde1f96503 = []byte{
+	// 238 bytes of a gzipped FileDescriptorProto
 	0x1f, 0x8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xa4, 0xd2, 0x34, 0x08, 0x23, 0x59, 0x37, 0x3d, 0x35,
+	0x4f, 0x37, 0x3d, 0x5f, 0xbf, 0x24, 0xb5, 0xb8, 0x24, 0x25, 0xb1, 0x24, 0x51, 0x1f, 0x16, 0x4e,
+	0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xdf, 0xf8, 0xc4, 0xf4, 0x4c, 0x01, 0x00, 0x00,
 }
diff --git a/protoc-gen-go/testdata/imp/imp2/imp2.proto b/protoc-gen-go/testdata/imp/imp2/imp2.proto
index acf4088..6e709f5 100644
--- a/protoc-gen-go/testdata/imp/imp2/imp2.proto
+++ b/protoc-gen-go/testdata/imp/imp2/imp2.proto
@@ -35,8 +35,13 @@
 
 option go_package = "github.com/golang/protobuf/protoc-gen-go/testdata/imp/imp2";
 
+import "imp/imp2/imp2-enum.proto";
+
 message PubliclyImportedMessage {
   optional int64 field = 1;
+
+  // Field using a type in the same Go package, but a different source file.
+  optional imp.EnumPB.Type field2 = 2;
 }
 
 message PubliclyImportedMessage2 {