cmd/protoc-gen-go: generate public imports

Generate forwarding declarations for all types, consts, and vars in
publicly imported files.

Change-Id: I2f1041fa91b0ae18b1e123935951618b8d594f84
Reviewed-on: https://go-review.googlesource.com/136175
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/cmd/protoc-gen-go/main.go b/cmd/protoc-gen-go/main.go
index 0c1b119..b477fe7 100644
--- a/cmd/protoc-gen-go/main.go
+++ b/cmd/protoc-gen-go/main.go
@@ -74,13 +74,11 @@
 	//
 	// TODO: Eventually make this consistent.
 	f.allEnums = append(f.allEnums, f.File.Enums...)
-	for _, message := range f.Messages {
-		walkMessage(message, func(message *protogen.Message) {
-			f.allMessages = append(f.allMessages, message)
-			f.allEnums = append(f.allEnums, message.Enums...)
-			f.allExtensions = append(f.allExtensions, message.Extensions...)
-		})
-	}
+	walkMessages(f.Messages, func(message *protogen.Message) {
+		f.allMessages = append(f.allMessages, message)
+		f.allEnums = append(f.allEnums, message.Enums...)
+		f.allExtensions = append(f.allExtensions, message.Extensions...)
+	})
 	f.allExtensions = append(f.allExtensions, f.File.Extensions...)
 
 	// Determine the name of the var holding the file descriptor:
@@ -123,6 +121,9 @@
 	}, "// please upgrade the proto package")
 	g.P()
 
+	for i, imps := 0, f.Desc.Imports(); i < imps.Len(); i++ {
+		genImport(gen, g, f, imps.Get(i))
+	}
 	for _, enum := range f.allEnums {
 		genEnum(gen, g, f, enum)
 	}
@@ -138,11 +139,49 @@
 	genFileDescriptor(gen, g, f)
 }
 
-// walkMessage calls f on message and all of its descendants.
-func walkMessage(message *protogen.Message, f func(*protogen.Message)) {
-	f(message)
-	for _, m := range message.Messages {
-		walkMessage(m, f)
+// walkMessages calls f on each message and all of its descendants.
+func walkMessages(messages []*protogen.Message, f func(*protogen.Message)) {
+	for _, m := range messages {
+		f(m)
+		walkMessages(m.Messages, f)
+	}
+}
+
+func genImport(gen *protogen.Plugin, g *protogen.GeneratedFile, f *File, imp protoreflect.FileImport) {
+	if !imp.IsPublic {
+		return
+	}
+	impFile, ok := gen.FileByName(imp.Path())
+	if !ok {
+		return
+	}
+	if impFile.GoImportPath == f.GoImportPath {
+		// Don't generate aliases for types in the same Go package.
+		return
+	}
+	var enums []*protogen.Enum
+	enums = append(enums, impFile.Enums...)
+	walkMessages(impFile.Messages, func(message *protogen.Message) {
+		enums = append(enums, message.Enums...)
+		g.P("// ", message.GoIdent.GoName, " from public import ", imp.Path())
+		g.P("type ", message.GoIdent.GoName, " = ", message.GoIdent)
+		for _, oneof := range message.Oneofs {
+			for _, field := range oneof.Fields {
+				typ := fieldOneofType(field)
+				g.P("type ", typ.GoName, " = ", typ)
+			}
+		}
+		g.P()
+	})
+	for _, enum := range enums {
+		g.P("// ", enum.GoIdent.GoName, " from public import ", imp.Path())
+		g.P("type ", enum.GoIdent.GoName, " = ", enum.GoIdent)
+		g.P("var ", enum.GoIdent.GoName, "_name = ", enum.GoIdent, "_name")
+		g.P("var ", enum.GoIdent.GoName, "_value = ", enum.GoIdent, "_value")
+		g.P()
+		for _, value := range enum.Values {
+			g.P("const ", value.GoIdent.GoName, " = ", enum.GoIdent.GoName, "(", value.GoIdent, ")")
+		}
 	}
 }
 
diff --git a/cmd/protoc-gen-go/testdata/import_public/a.pb.go b/cmd/protoc-gen-go/testdata/import_public/a.pb.go
new file mode 100644
index 0000000..01c95cb
--- /dev/null
+++ b/cmd/protoc-gen-go/testdata/import_public/a.pb.go
@@ -0,0 +1,134 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// source: import_public/a.proto
+
+package import_public
+
+import (
+	fmt "fmt"
+	proto "github.com/golang/protobuf/proto"
+	sub "google.golang.org/proto/cmd/protoc-gen-go/testdata/import_public/sub"
+	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
+
+// M from public import import_public/sub/a.proto
+type M = sub.M
+type M_OneofInt32 = sub.M_OneofInt32
+type M_OneofInt64 = sub.M_OneofInt64
+
+// M_Submessage from public import import_public/sub/a.proto
+type M_Submessage = sub.M_Submessage
+type M_Submessage_SubmessageOneofInt32 = sub.M_Submessage_SubmessageOneofInt32
+type M_Submessage_SubmessageOneofInt64 = sub.M_Submessage_SubmessageOneofInt64
+
+// E from public import import_public/sub/a.proto
+type E = sub.E
+
+var E_name = sub.E_name
+var E_value = sub.E_value
+
+const E_ZERO = E(sub.E_ZERO)
+
+// M_Subenum from public import import_public/sub/a.proto
+type M_Subenum = sub.M_Subenum
+
+var M_Subenum_name = sub.M_Subenum_name
+var M_Subenum_value = sub.M_Subenum_value
+
+const M_M_ZERO = M_Subenum(sub.M_M_ZERO)
+
+// M_Submessage_Submessage_Subenum from public import import_public/sub/a.proto
+type M_Submessage_Submessage_Subenum = sub.M_Submessage_Submessage_Subenum
+
+var M_Submessage_Submessage_Subenum_name = sub.M_Submessage_Submessage_Subenum_name
+var M_Submessage_Submessage_Subenum_value = sub.M_Submessage_Submessage_Subenum_value
+
+const M_Submessage_M_SUBMESSAGE_ZERO = M_Submessage_Submessage_Subenum(sub.M_Submessage_M_SUBMESSAGE_ZERO)
+
+type Public struct {
+	M                    *sub.M   `protobuf:"bytes,1,opt,name=m,proto3" json:"m,omitempty"`
+	E                    sub.E    `protobuf:"varint,2,opt,name=e,proto3,enum=goproto.protoc.import_public.sub.E" json:"e,omitempty"`
+	Local                *Local   `protobuf:"bytes,3,opt,name=local,proto3" json:"local,omitempty"`
+	XXX_NoUnkeyedLiteral struct{} `json:"-"`
+	XXX_unrecognized     []byte   `json:"-"`
+	XXX_sizecache        int32    `json:"-"`
+}
+
+func (m *Public) Reset()         { *m = Public{} }
+func (m *Public) String() string { return proto.CompactTextString(m) }
+func (*Public) ProtoMessage()    {}
+func (*Public) Descriptor() ([]byte, []int) {
+	return fileDescriptor_73b7577c95fa6b70, []int{0}
+}
+
+func (m *Public) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_Public.Unmarshal(m, b)
+}
+func (m *Public) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_Public.Marshal(b, m, deterministic)
+}
+func (m *Public) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_Public.Merge(m, src)
+}
+func (m *Public) XXX_Size() int {
+	return xxx_messageInfo_Public.Size(m)
+}
+func (m *Public) XXX_DiscardUnknown() {
+	xxx_messageInfo_Public.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_Public proto.InternalMessageInfo
+
+func (m *Public) GetM() *sub.M {
+	if m != nil {
+		return m.M
+	}
+	return nil
+}
+
+func (m *Public) GetE() sub.E {
+	if m != nil {
+		return m.E
+	}
+	return sub.E_ZERO
+}
+
+func (m *Public) GetLocal() *Local {
+	if m != nil {
+		return m.Local
+	}
+	return nil
+}
+
+func init() {
+	proto.RegisterType((*Public)(nil), "goproto.protoc.import_public.Public")
+}
+
+func init() { proto.RegisterFile("import_public/a.proto", fileDescriptor_73b7577c95fa6b70) }
+
+var fileDescriptor_73b7577c95fa6b70 = []byte{
+	// 201 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, 0x14, 0xe5, 0x90, 0x9e, 0x9f, 0x9f, 0x9e, 0x93, 0xaa, 0x97, 0x9e, 0x9f, 0x93, 0x98,
+	0x97, 0xae, 0x97, 0x5f, 0x94, 0xae, 0x0f, 0xd6, 0xa6, 0x9f, 0x9c, 0x9b, 0x02, 0x61, 0x25, 0xeb,
+	0xa6, 0xa7, 0xe6, 0xe9, 0xa6, 0xe7, 0xeb, 0x97, 0xa4, 0x16, 0x97, 0xa4, 0x24, 0x96, 0x24, 0xea,
+	0xa3, 0x18, 0x18, 0xc0, 0x10, 0xc0, 0x98, 0xc4, 0x06, 0x56, 0x69, 0x0c, 0x08, 0x00, 0x00, 0xff,
+	0xff, 0x12, 0x1b, 0x7e, 0x64, 0x63, 0x01, 0x00, 0x00,
+}
diff --git a/cmd/protoc-gen-go/testdata/import_public/a.proto b/cmd/protoc-gen-go/testdata/import_public/a.proto
new file mode 100644
index 0000000..6517d94
--- /dev/null
+++ b/cmd/protoc-gen-go/testdata/import_public/a.proto
@@ -0,0 +1,18 @@
+// 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 = "proto3";
+
+package goproto.protoc.import_public;
+
+option go_package = "google.golang.org/proto/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.
+
+message Public {
+  goproto.protoc.import_public.sub.M m = 1;
+  goproto.protoc.import_public.sub.E e = 2;
+  Local local = 3;
+}
diff --git a/cmd/protoc-gen-go/testdata/import_public/b.pb.go b/cmd/protoc-gen-go/testdata/import_public/b.pb.go
new file mode 100644
index 0000000..713fbc2
--- /dev/null
+++ b/cmd/protoc-gen-go/testdata/import_public/b.pb.go
@@ -0,0 +1,90 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// source: import_public/b.proto
+
+package import_public
+
+import (
+	fmt "fmt"
+	proto "github.com/golang/protobuf/proto"
+	sub "google.golang.org/proto/cmd/protoc-gen-go/testdata/import_public/sub"
+	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 Local struct {
+	M                    *sub.M   `protobuf:"bytes,1,opt,name=m,proto3" json:"m,omitempty"`
+	E                    sub.E    `protobuf:"varint,2,opt,name=e,proto3,enum=goproto.protoc.import_public.sub.E" json:"e,omitempty"`
+	XXX_NoUnkeyedLiteral struct{} `json:"-"`
+	XXX_unrecognized     []byte   `json:"-"`
+	XXX_sizecache        int32    `json:"-"`
+}
+
+func (m *Local) Reset()         { *m = Local{} }
+func (m *Local) String() string { return proto.CompactTextString(m) }
+func (*Local) ProtoMessage()    {}
+func (*Local) Descriptor() ([]byte, []int) {
+	return fileDescriptor_84995586b3d09710, []int{0}
+}
+
+func (m *Local) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_Local.Unmarshal(m, b)
+}
+func (m *Local) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_Local.Marshal(b, m, deterministic)
+}
+func (m *Local) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_Local.Merge(m, src)
+}
+func (m *Local) XXX_Size() int {
+	return xxx_messageInfo_Local.Size(m)
+}
+func (m *Local) XXX_DiscardUnknown() {
+	xxx_messageInfo_Local.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_Local proto.InternalMessageInfo
+
+func (m *Local) GetM() *sub.M {
+	if m != nil {
+		return m.M
+	}
+	return nil
+}
+
+func (m *Local) GetE() sub.E {
+	if m != nil {
+		return m.E
+	}
+	return sub.E_ZERO
+}
+
+func init() {
+	proto.RegisterType((*Local)(nil), "goproto.protoc.import_public.Local")
+}
+
+func init() { proto.RegisterFile("import_public/b.proto", fileDescriptor_84995586b3d09710) }
+
+var fileDescriptor_84995586b3d09710 = []byte{
+	// 174 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x8c, 0x31, 0x0b, 0xc2, 0x30,
+	0x10, 0x46, 0x39, 0x41, 0x87, 0x0a, 0x0e, 0x05, 0xa1, 0x8a, 0x43, 0xd1, 0xa5, 0x4b, 0xef, 0x50,
+	0xff, 0x80, 0x14, 0xdc, 0x74, 0xe9, 0xe8, 0x22, 0x49, 0x1a, 0x8e, 0x42, 0xd2, 0x2b, 0x6d, 0xfa,
+	0xff, 0xc5, 0x76, 0xea, 0x22, 0x6e, 0xdf, 0x07, 0xef, 0xbd, 0x68, 0x5b, 0xfb, 0x56, 0xba, 0xf0,
+	0x6e, 0x07, 0xed, 0x6a, 0x43, 0x1a, 0xdb, 0x4e, 0x82, 0xc4, 0x07, 0x96, 0x71, 0x4c, 0xd7, 0xe0,
+	0x8c, 0xda, 0xef, 0xe6, 0x52, 0x3f, 0x68, 0x52, 0x13, 0x79, 0xf4, 0xd1, 0xf2, 0x21, 0x46, 0xb9,
+	0xf8, 0x1c, 0x81, 0x4f, 0x20, 0x85, 0x6c, 0x7d, 0x39, 0xe1, 0xaf, 0x1a, 0xf6, 0x83, 0xc6, 0x67,
+	0x09, 0xfe, 0xab, 0xd8, 0x64, 0x91, 0x42, 0xb6, 0xf9, 0x47, 0xb9, 0x97, 0x60, 0x8b, 0xe2, 0x75,
+	0x63, 0x11, 0x76, 0x16, 0x59, 0x9c, 0x6a, 0x18, 0xa5, 0x63, 0x1a, 0x15, 0x32, 0xbe, 0x9a, 0x96,
+	0xc9, 0xd9, 0x36, 0x39, 0x0b, 0x05, 0xdb, 0x87, 0x4a, 0x05, 0x45, 0xb3, 0x98, 0x5e, 0x8d, 0xd4,
+	0xf5, 0x13, 0x00, 0x00, 0xff, 0xff, 0x2c, 0x8b, 0x86, 0xd2, 0x0b, 0x01, 0x00, 0x00,
+}
diff --git a/cmd/protoc-gen-go/testdata/import_public/b.proto b/cmd/protoc-gen-go/testdata/import_public/b.proto
new file mode 100644
index 0000000..d8f6705
--- /dev/null
+++ b/cmd/protoc-gen-go/testdata/import_public/b.proto
@@ -0,0 +1,16 @@
+// 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 = "proto3";
+
+package goproto.protoc.import_public;
+
+option go_package = "google.golang.org/proto/cmd/protoc-gen-go/testdata/import_public";
+
+import "import_public/sub/a.proto";
+
+message Local {
+  goproto.protoc.import_public.sub.M m = 1;
+  goproto.protoc.import_public.sub.E e = 2;
+}
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
new file mode 100644
index 0000000..f1d1d78
--- /dev/null
+++ b/cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go
@@ -0,0 +1,402 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// source: import_public/sub/a.proto
+
+package sub
+
+import (
+	fmt "fmt"
+	proto "github.com/golang/protobuf/proto"
+	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 E int32
+
+const (
+	E_ZERO E = 0
+)
+
+var E_name = map[int32]string{
+	0: "ZERO",
+}
+
+var E_value = map[string]int32{
+	"ZERO": 0,
+}
+
+func (x E) String() string {
+	return proto.EnumName(E_name, int32(x))
+}
+
+func (E) EnumDescriptor() ([]byte, []int) {
+	return fileDescriptor_382f7805394b5c4e, []int{0}
+}
+
+type M_Subenum int32
+
+const (
+	M_M_ZERO M_Subenum = 0
+)
+
+var M_Subenum_name = map[int32]string{
+	0: "M_ZERO",
+}
+
+var M_Subenum_value = map[string]int32{
+	"M_ZERO": 0,
+}
+
+func (x M_Subenum) String() string {
+	return proto.EnumName(M_Subenum_name, int32(x))
+}
+
+func (M_Subenum) EnumDescriptor() ([]byte, []int) {
+	return fileDescriptor_382f7805394b5c4e, []int{0, 0}
+}
+
+type M_Submessage_Submessage_Subenum int32
+
+const (
+	M_Submessage_M_SUBMESSAGE_ZERO M_Submessage_Submessage_Subenum = 0
+)
+
+var M_Submessage_Submessage_Subenum_name = map[int32]string{
+	0: "M_SUBMESSAGE_ZERO",
+}
+
+var M_Submessage_Submessage_Subenum_value = map[string]int32{
+	"M_SUBMESSAGE_ZERO": 0,
+}
+
+func (x M_Submessage_Submessage_Subenum) String() string {
+	return proto.EnumName(M_Submessage_Submessage_Subenum_name, int32(x))
+}
+
+func (M_Submessage_Submessage_Subenum) EnumDescriptor() ([]byte, []int) {
+	return fileDescriptor_382f7805394b5c4e, []int{0, 0, 0}
+}
+
+type M struct {
+	// Field using a type in the same Go package, but a different source file.
+	M2 *M2 `protobuf:"bytes,1,opt,name=m2,proto3" json:"m2,omitempty"`
+	// Types that are valid to be assigned to OneofField:
+	//	*M_OneofInt32
+	//	*M_OneofInt64
+	OneofField           isM_OneofField `protobuf_oneof:"oneof_field"`
+	XXX_NoUnkeyedLiteral struct{}       `json:"-"`
+	XXX_unrecognized     []byte         `json:"-"`
+	XXX_sizecache        int32          `json:"-"`
+}
+
+func (m *M) Reset()         { *m = M{} }
+func (m *M) String() string { return proto.CompactTextString(m) }
+func (*M) ProtoMessage()    {}
+func (*M) Descriptor() ([]byte, []int) {
+	return fileDescriptor_382f7805394b5c4e, []int{0}
+}
+
+func (m *M) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_M.Unmarshal(m, b)
+}
+func (m *M) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_M.Marshal(b, m, deterministic)
+}
+func (m *M) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_M.Merge(m, src)
+}
+func (m *M) XXX_Size() int {
+	return xxx_messageInfo_M.Size(m)
+}
+func (m *M) XXX_DiscardUnknown() {
+	xxx_messageInfo_M.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_M proto.InternalMessageInfo
+
+func (m *M) GetM2() *M2 {
+	if m != nil {
+		return m.M2
+	}
+	return nil
+}
+
+type isM_OneofField interface {
+	isM_OneofField()
+}
+
+type M_OneofInt32 struct {
+	OneofInt32 int32 `protobuf:"varint,2,opt,name=oneof_int32,json=oneofInt32,proto3,oneof"`
+}
+
+type M_OneofInt64 struct {
+	OneofInt64 int64 `protobuf:"varint,3,opt,name=oneof_int64,json=oneofInt64,proto3,oneof"`
+}
+
+func (*M_OneofInt32) isM_OneofField() {}
+
+func (*M_OneofInt64) isM_OneofField() {}
+
+func (m *M) GetOneofField() isM_OneofField {
+	if m != nil {
+		return m.OneofField
+	}
+	return nil
+}
+
+func (m *M) GetOneofInt32() int32 {
+	if x, ok := m.GetOneofField().(*M_OneofInt32); ok {
+		return x.OneofInt32
+	}
+	return 0
+}
+
+func (m *M) GetOneofInt64() int64 {
+	if x, ok := m.GetOneofField().(*M_OneofInt64); ok {
+		return x.OneofInt64
+	}
+	return 0
+}
+
+// XXX_OneofFuncs is for the internal use of the proto package.
+func (*M) 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 _M_OneofMarshaler, _M_OneofUnmarshaler, _M_OneofSizer, []interface{}{
+		(*M_OneofInt32)(nil),
+		(*M_OneofInt64)(nil),
+	}
+}
+
+func _M_OneofMarshaler(msg proto.Message, b *proto.Buffer) error {
+	m := msg.(*M)
+	// oneof_field
+	switch x := m.OneofField.(type) {
+	case *M_OneofInt32:
+		b.EncodeVarint(2<<3 | proto.WireVarint)
+		b.EncodeVarint(uint64(x.OneofInt32))
+	case *M_OneofInt64:
+		b.EncodeVarint(3<<3 | proto.WireVarint)
+		b.EncodeVarint(uint64(x.OneofInt64))
+	case nil:
+	default:
+		return fmt.Errorf("M.OneofField has unexpected type %T", x)
+	}
+	return nil
+}
+
+func _M_OneofUnmarshaler(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error) {
+	m := msg.(*M)
+	switch tag {
+	case 2: // oneof_field.oneof_int32
+		if wire != proto.WireVarint {
+			return true, proto.ErrInternalBadWireType
+		}
+		x, err := b.DecodeVarint()
+		m.OneofField = &M_OneofInt32{int32(x)}
+		return true, err
+	case 3: // oneof_field.oneof_int64
+		if wire != proto.WireVarint {
+			return true, proto.ErrInternalBadWireType
+		}
+		x, err := b.DecodeVarint()
+		m.OneofField = &M_OneofInt64{int64(x)}
+		return true, err
+	default:
+		return false, nil
+	}
+}
+
+func _M_OneofSizer(msg proto.Message) (n int) {
+	m := msg.(*M)
+	// oneof_field
+	switch x := m.OneofField.(type) {
+	case *M_OneofInt32:
+		n += 1 // tag and wire
+		n += proto.SizeVarint(uint64(x.OneofInt32))
+	case *M_OneofInt64:
+		n += 1 // tag and wire
+		n += proto.SizeVarint(uint64(x.OneofInt64))
+	case nil:
+	default:
+		panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
+	}
+	return n
+}
+
+type M_Submessage struct {
+	// Types that are valid to be assigned to SubmessageOneofField:
+	//	*M_Submessage_SubmessageOneofInt32
+	//	*M_Submessage_SubmessageOneofInt64
+	SubmessageOneofField isM_Submessage_SubmessageOneofField `protobuf_oneof:"submessage_oneof_field"`
+	XXX_NoUnkeyedLiteral struct{}                            `json:"-"`
+	XXX_unrecognized     []byte                              `json:"-"`
+	XXX_sizecache        int32                               `json:"-"`
+}
+
+func (m *M_Submessage) Reset()         { *m = M_Submessage{} }
+func (m *M_Submessage) String() string { return proto.CompactTextString(m) }
+func (*M_Submessage) ProtoMessage()    {}
+func (*M_Submessage) Descriptor() ([]byte, []int) {
+	return fileDescriptor_382f7805394b5c4e, []int{0, 0}
+}
+
+func (m *M_Submessage) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_M_Submessage.Unmarshal(m, b)
+}
+func (m *M_Submessage) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_M_Submessage.Marshal(b, m, deterministic)
+}
+func (m *M_Submessage) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_M_Submessage.Merge(m, src)
+}
+func (m *M_Submessage) XXX_Size() int {
+	return xxx_messageInfo_M_Submessage.Size(m)
+}
+func (m *M_Submessage) XXX_DiscardUnknown() {
+	xxx_messageInfo_M_Submessage.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_M_Submessage proto.InternalMessageInfo
+
+type isM_Submessage_SubmessageOneofField interface {
+	isM_Submessage_SubmessageOneofField()
+}
+
+type M_Submessage_SubmessageOneofInt32 struct {
+	SubmessageOneofInt32 int32 `protobuf:"varint,1,opt,name=submessage_oneof_int32,json=submessageOneofInt32,proto3,oneof"`
+}
+
+type M_Submessage_SubmessageOneofInt64 struct {
+	SubmessageOneofInt64 int64 `protobuf:"varint,2,opt,name=submessage_oneof_int64,json=submessageOneofInt64,proto3,oneof"`
+}
+
+func (*M_Submessage_SubmessageOneofInt32) isM_Submessage_SubmessageOneofField() {}
+
+func (*M_Submessage_SubmessageOneofInt64) isM_Submessage_SubmessageOneofField() {}
+
+func (m *M_Submessage) GetSubmessageOneofField() isM_Submessage_SubmessageOneofField {
+	if m != nil {
+		return m.SubmessageOneofField
+	}
+	return nil
+}
+
+func (m *M_Submessage) GetSubmessageOneofInt32() int32 {
+	if x, ok := m.GetSubmessageOneofField().(*M_Submessage_SubmessageOneofInt32); ok {
+		return x.SubmessageOneofInt32
+	}
+	return 0
+}
+
+func (m *M_Submessage) GetSubmessageOneofInt64() int64 {
+	if x, ok := m.GetSubmessageOneofField().(*M_Submessage_SubmessageOneofInt64); ok {
+		return x.SubmessageOneofInt64
+	}
+	return 0
+}
+
+// XXX_OneofFuncs is for the internal use of the proto package.
+func (*M_Submessage) 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 _M_Submessage_OneofMarshaler, _M_Submessage_OneofUnmarshaler, _M_Submessage_OneofSizer, []interface{}{
+		(*M_Submessage_SubmessageOneofInt32)(nil),
+		(*M_Submessage_SubmessageOneofInt64)(nil),
+	}
+}
+
+func _M_Submessage_OneofMarshaler(msg proto.Message, b *proto.Buffer) error {
+	m := msg.(*M_Submessage)
+	// submessage_oneof_field
+	switch x := m.SubmessageOneofField.(type) {
+	case *M_Submessage_SubmessageOneofInt32:
+		b.EncodeVarint(1<<3 | proto.WireVarint)
+		b.EncodeVarint(uint64(x.SubmessageOneofInt32))
+	case *M_Submessage_SubmessageOneofInt64:
+		b.EncodeVarint(2<<3 | proto.WireVarint)
+		b.EncodeVarint(uint64(x.SubmessageOneofInt64))
+	case nil:
+	default:
+		return fmt.Errorf("M_Submessage.SubmessageOneofField has unexpected type %T", x)
+	}
+	return nil
+}
+
+func _M_Submessage_OneofUnmarshaler(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error) {
+	m := msg.(*M_Submessage)
+	switch tag {
+	case 1: // submessage_oneof_field.submessage_oneof_int32
+		if wire != proto.WireVarint {
+			return true, proto.ErrInternalBadWireType
+		}
+		x, err := b.DecodeVarint()
+		m.SubmessageOneofField = &M_Submessage_SubmessageOneofInt32{int32(x)}
+		return true, err
+	case 2: // submessage_oneof_field.submessage_oneof_int64
+		if wire != proto.WireVarint {
+			return true, proto.ErrInternalBadWireType
+		}
+		x, err := b.DecodeVarint()
+		m.SubmessageOneofField = &M_Submessage_SubmessageOneofInt64{int64(x)}
+		return true, err
+	default:
+		return false, nil
+	}
+}
+
+func _M_Submessage_OneofSizer(msg proto.Message) (n int) {
+	m := msg.(*M_Submessage)
+	// submessage_oneof_field
+	switch x := m.SubmessageOneofField.(type) {
+	case *M_Submessage_SubmessageOneofInt32:
+		n += 1 // tag and wire
+		n += proto.SizeVarint(uint64(x.SubmessageOneofInt32))
+	case *M_Submessage_SubmessageOneofInt64:
+		n += 1 // tag and wire
+		n += proto.SizeVarint(uint64(x.SubmessageOneofInt64))
+	case nil:
+	default:
+		panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
+	}
+	return n
+}
+
+func init() {
+	proto.RegisterType((*M)(nil), "goproto.protoc.import_public.sub.M")
+	proto.RegisterType((*M_Submessage)(nil), "goproto.protoc.import_public.sub.M.Submessage")
+	proto.RegisterEnum("goproto.protoc.import_public.sub.E", E_name, E_value)
+	proto.RegisterEnum("goproto.protoc.import_public.sub.M_Subenum", M_Subenum_name, M_Subenum_value)
+	proto.RegisterEnum("goproto.protoc.import_public.sub.M_Submessage_Submessage_Subenum", M_Submessage_Submessage_Subenum_name, M_Submessage_Submessage_Subenum_value)
+}
+
+func init() { proto.RegisterFile("import_public/sub/a.proto", fileDescriptor_382f7805394b5c4e) }
+
+var fileDescriptor_382f7805394b5c4e = []byte{
+	// 313 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x91, 0x4d, 0x4f, 0xc2, 0x30,
+	0x1c, 0xc6, 0xe9, 0x50, 0x34, 0x7f, 0x42, 0xa2, 0x8d, 0x98, 0xc9, 0x69, 0x12, 0x0f, 0x44, 0x43,
+	0x97, 0x8c, 0x65, 0x77, 0x89, 0xf3, 0xe5, 0xb0, 0x90, 0xb0, 0x78, 0xe1, 0xb2, 0xac, 0xa3, 0x34,
+	0x4b, 0xd6, 0x95, 0xd0, 0xf6, 0x13, 0xfa, 0x71, 0xfc, 0x12, 0xc6, 0x02, 0x02, 0x11, 0xe3, 0xed,
+	0xff, 0xf2, 0x7b, 0x9e, 0xa7, 0x2f, 0x70, 0x53, 0x8a, 0xa5, 0x5c, 0xe9, 0x6c, 0x69, 0x68, 0x55,
+	0x16, 0xbe, 0x32, 0xd4, 0xcf, 0xc9, 0x72, 0x25, 0xb5, 0xc4, 0x1e, 0x97, 0xb6, 0x58, 0xb7, 0x05,
+	0x39, 0x20, 0x89, 0x32, 0xb4, 0x77, 0x44, 0x4c, 0xd7, 0x74, 0xff, 0xd3, 0x01, 0x94, 0xe0, 0x10,
+	0x1c, 0x11, 0xb8, 0xc8, 0x43, 0x83, 0x76, 0x70, 0x47, 0xfe, 0xf3, 0x23, 0x49, 0x30, 0x75, 0x44,
+	0x80, 0x6f, 0xa1, 0x2d, 0x6b, 0x26, 0x17, 0x59, 0x59, 0xeb, 0x51, 0xe0, 0x3a, 0x1e, 0x1a, 0x9c,
+	0xbe, 0x36, 0xa6, 0x60, 0x87, 0x6f, 0xdf, 0xb3, 0x03, 0x24, 0x0a, 0xdd, 0xa6, 0x87, 0x06, 0xcd,
+	0x7d, 0x24, 0x0a, 0x7b, 0x1f, 0x08, 0x20, 0x35, 0x54, 0x30, 0xa5, 0x72, 0xce, 0x70, 0x04, 0xd7,
+	0xea, 0xa7, 0xcb, 0xf6, 0xfd, 0xd1, 0xc6, 0xff, 0x6a, 0xb7, 0x9f, 0xec, 0x92, 0xfe, 0xd0, 0x45,
+	0xa1, 0x3d, 0x57, 0xf3, 0xb8, 0x2e, 0x0a, 0xfb, 0x0f, 0x80, 0x77, 0xe9, 0x59, 0x6a, 0x28, 0xab,
+	0x8d, 0xc0, 0x5d, 0xb8, 0x4c, 0xb2, 0xf4, 0x7d, 0x9c, 0xc4, 0x69, 0xfa, 0xf8, 0x12, 0x67, 0xb3,
+	0x78, 0x3a, 0xb9, 0x68, 0x8c, 0xdd, 0x23, 0x21, 0x8b, 0x92, 0x55, 0xf3, 0x7e, 0x17, 0xce, 0xb6,
+	0x5a, 0x80, 0x56, 0xb2, 0x15, 0x74, 0xb6, 0xf7, 0xb7, 0xd4, 0x7d, 0x07, 0x50, 0x8c, 0xcf, 0xe1,
+	0x64, 0xb3, 0x7d, 0x9e, 0x3d, 0x71, 0x29, 0x79, 0xc5, 0x08, 0x97, 0x55, 0x5e, 0x73, 0x22, 0x57,
+	0xdc, 0xb7, 0xaf, 0xee, 0x17, 0x62, 0xbe, 0xae, 0x8a, 0x21, 0x67, 0xf5, 0x90, 0x4b, 0x5f, 0x33,
+	0xa5, 0xe7, 0xb9, 0xce, 0xfd, 0x5f, 0x9f, 0x49, 0x5b, 0x96, 0x1c, 0x7d, 0x05, 0x00, 0x00, 0xff,
+	0xff, 0xd5, 0x26, 0xfe, 0xc5, 0x25, 0x02, 0x00, 0x00,
+}
diff --git a/cmd/protoc-gen-go/testdata/import_public/sub/a.proto b/cmd/protoc-gen-go/testdata/import_public/sub/a.proto
new file mode 100644
index 0000000..b77090d
--- /dev/null
+++ b/cmd/protoc-gen-go/testdata/import_public/sub/a.proto
@@ -0,0 +1,40 @@
+// 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 = "proto3";
+
+package goproto.protoc.import_public.sub;
+
+option go_package = "google.golang.org/proto/cmd/protoc-gen-go/testdata/import_public/sub";
+
+import "import_public/sub/b.proto";
+
+message M {
+  // Field using a type in the same Go package, but a different source file.
+  M2 m2 = 1;
+
+  oneof oneof_field {
+    int32 oneof_int32 = 2;
+    int64 oneof_int64 = 3;
+  }
+
+  message Submessage {
+    enum Submessage_Subenum {
+      M_SUBMESSAGE_ZERO = 0;
+    }
+
+    oneof submessage_oneof_field {
+      int32 submessage_oneof_int32 = 1;
+      int64 submessage_oneof_int64 = 2;
+    }
+  }
+
+  enum Subenum {
+    M_ZERO = 0;
+  }
+}
+
+enum E {
+  ZERO = 0;
+}
diff --git a/cmd/protoc-gen-go/testdata/import_public/sub/b.pb.go b/cmd/protoc-gen-go/testdata/import_public/sub/b.pb.go
new file mode 100644
index 0000000..9c294dd
--- /dev/null
+++ b/cmd/protoc-gen-go/testdata/import_public/sub/b.pb.go
@@ -0,0 +1,70 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// source: import_public/sub/b.proto
+
+package sub
+
+import (
+	fmt "fmt"
+	proto "github.com/golang/protobuf/proto"
+	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 M2 struct {
+	XXX_NoUnkeyedLiteral struct{} `json:"-"`
+	XXX_unrecognized     []byte   `json:"-"`
+	XXX_sizecache        int32    `json:"-"`
+}
+
+func (m *M2) Reset()         { *m = M2{} }
+func (m *M2) String() string { return proto.CompactTextString(m) }
+func (*M2) ProtoMessage()    {}
+func (*M2) Descriptor() ([]byte, []int) {
+	return fileDescriptor_fc66afda3d7c2232, []int{0}
+}
+
+func (m *M2) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_M2.Unmarshal(m, b)
+}
+func (m *M2) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_M2.Marshal(b, m, deterministic)
+}
+func (m *M2) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_M2.Merge(m, src)
+}
+func (m *M2) XXX_Size() int {
+	return xxx_messageInfo_M2.Size(m)
+}
+func (m *M2) XXX_DiscardUnknown() {
+	xxx_messageInfo_M2.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_M2 proto.InternalMessageInfo
+
+func init() {
+	proto.RegisterType((*M2)(nil), "goproto.protoc.import_public.sub.M2")
+}
+
+func init() { proto.RegisterFile("import_public/sub/b.proto", fileDescriptor_fc66afda3d7c2232) }
+
+var fileDescriptor_fc66afda3d7c2232 = []byte{
+	// 128 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0xcc, 0xcc, 0x2d, 0xc8,
+	0x2f, 0x2a, 0x89, 0x2f, 0x28, 0x4d, 0xca, 0xc9, 0x4c, 0xd6, 0x2f, 0x2e, 0x4d, 0xd2, 0x4f, 0xd2,
+	0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x52, 0x48, 0xcf, 0x07, 0x33, 0x20, 0xdc, 0x64, 0x3d, 0x14,
+	0x95, 0x7a, 0xc5, 0xa5, 0x49, 0x4a, 0x2c, 0x5c, 0x4c, 0xbe, 0x46, 0x4e, 0x6e, 0x51, 0x2e, 0xe9,
+	0xf9, 0xf9, 0xe9, 0x39, 0xa9, 0x7a, 0xe9, 0xf9, 0x39, 0x89, 0x79, 0xe9, 0x7a, 0xf9, 0x45, 0xe9,
+	0xfa, 0x60, 0x3d, 0xfa, 0xc9, 0xb9, 0x29, 0x10, 0x56, 0xb2, 0x6e, 0x7a, 0x6a, 0x9e, 0x6e, 0x7a,
+	0xbe, 0x7e, 0x49, 0x6a, 0x71, 0x49, 0x4a, 0x62, 0x49, 0xa2, 0x3e, 0x86, 0xbd, 0x49, 0x6c, 0x60,
+	0x95, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xca, 0x5a, 0x18, 0x57, 0x93, 0x00, 0x00, 0x00,
+}
diff --git a/cmd/protoc-gen-go/testdata/import_public/sub/b.proto b/cmd/protoc-gen-go/testdata/import_public/sub/b.proto
new file mode 100644
index 0000000..9f69ab9
--- /dev/null
+++ b/cmd/protoc-gen-go/testdata/import_public/sub/b.proto
@@ -0,0 +1,12 @@
+// 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 = "proto3";
+
+package goproto.protoc.import_public.sub;
+
+option go_package = "google.golang.org/proto/cmd/protoc-gen-go/testdata/import_public/sub";
+
+message M2 {
+}