cmd/protoc-gen-go: rely on protoimpl for basic helpers

The EnumName, UnmarshalJSONEnum, and CompressGZIP helpers currently live
in v1 protoapi, which would cause all generated messages to depend on v1.
In an effort to break the dependency of v2 on v1, we move these helper
functions to v2 (and re-written to take advantage of protobuf reflection).

These helpers are unfortunate, but we cannot eliminate the functionality
that they implement since they are exposed in the publicly generated API.

Since EnumName does not rely on the enum maps, it removes another dependency
on those variables. Eventually, we can get to the point where these variables
(though declared) are not linked into the binary if the user does not use them.

Also, we rely on the v1 proto package for registration instead of v1 protoapi.
This may re-introduce a cyclic dependency on descriptor proto again in the
future, but the better approach is to just start registering with v2.

Change-Id: Id755585a7a1df14e4a6a2dfa650df221a3c153fb
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/167921
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/impl/export.go b/internal/impl/export.go
index a38390d..6e54939 100644
--- a/internal/impl/export.go
+++ b/internal/impl/export.go
@@ -5,6 +5,8 @@
 package impl
 
 import (
+	"strconv"
+
 	ptype "github.com/golang/protobuf/v2/internal/prototype"
 	pref "github.com/golang/protobuf/v2/reflect/protoreflect"
 )
@@ -35,6 +37,16 @@
 	return legacyWrapper.EnumTypeOf(e)
 }
 
+// EnumStringOf returns the enum value as a string, either as the name if
+// the number is resolvable, or the number formatted as a string.
+func (Export) EnumStringOf(ed pref.EnumDescriptor, n pref.EnumNumber) string {
+	ev := ed.Values().ByNumber(n)
+	if ev != nil {
+		return string(ev.Name())
+	}
+	return strconv.Itoa(int(n))
+}
+
 // MessageOf returns the protoreflect.Message interface over m.
 // If m already implements proto.Message, then it directly calls the
 // ProtoReflect method, otherwise it wraps the v1 message to implement
diff --git a/internal/impl/legact_export_test.go b/internal/impl/legact_export_test.go
new file mode 100644
index 0000000..e1d2c08
--- /dev/null
+++ b/internal/impl/legact_export_test.go
@@ -0,0 +1,41 @@
+// Copyright 2019 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.
+
+package impl
+
+import (
+	"bytes"
+	"compress/gzip"
+	"io/ioutil"
+	"math"
+	"strings"
+	"testing"
+)
+
+func TestCompressGZIP(t *testing.T) {
+	tests := []string{
+		"",
+		"a",
+		"ab",
+		"abc",
+		strings.Repeat("a", math.MaxUint16-1),
+		strings.Repeat("b", math.MaxUint16),
+		strings.Repeat("c", math.MaxUint16+1),
+		strings.Repeat("abcdefghijklmnopqrstuvwxyz", math.MaxUint16-13),
+	}
+	for _, want := range tests {
+		rb := bytes.NewReader(Export{}.CompressGZIP([]byte(want)))
+		zr, err := gzip.NewReader(rb)
+		if err != nil {
+			t.Errorf("unexpected gzip.NewReader error: %v", err)
+		}
+		b, err := ioutil.ReadAll(zr)
+		if err != nil {
+			t.Errorf("unexpected ioutil.ReadAll error: %v", err)
+		}
+		if got := string(b); got != want {
+			t.Errorf("output mismatch: got %q, want %q", got, want)
+		}
+	}
+}
diff --git a/internal/impl/legacy_export.go b/internal/impl/legacy_export.go
new file mode 100644
index 0000000..8038333
--- /dev/null
+++ b/internal/impl/legacy_export.go
@@ -0,0 +1,76 @@
+// Copyright 2019 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.
+
+package impl
+
+import (
+	"encoding/binary"
+	"encoding/json"
+	"hash/crc32"
+	"math"
+
+	"github.com/golang/protobuf/v2/internal/errors"
+	pref "github.com/golang/protobuf/v2/reflect/protoreflect"
+)
+
+// These functions exist to support exported APIs in generated protobufs.
+// While these are deprecated, they cannot be removed for compatibility reasons.
+
+// UnmarshalJSONEnum unmarshals an enum from a JSON-encoded input.
+// The input can either be a string representing the enum value by name,
+// or a number representing the enum number itself.
+func (Export) UnmarshalJSONEnum(ed pref.EnumDescriptor, b []byte) (pref.EnumNumber, error) {
+	if b[0] == '"' {
+		var name pref.Name
+		if err := json.Unmarshal(b, &name); err != nil {
+			return 0, errors.New("invalid input for enum %v: %s", ed.FullName(), b)
+		}
+		ev := ed.Values().ByName(name)
+		if ev != nil {
+			return 0, errors.New("invalid value for enum %v: %s", ed.FullName(), name)
+		}
+		return ev.Number(), nil
+	} else {
+		var num pref.EnumNumber
+		if err := json.Unmarshal(b, &num); err != nil {
+			return 0, errors.New("invalid input for enum %v: %s", ed.FullName(), b)
+		}
+		return num, nil
+	}
+}
+
+// CompressGZIP compresses the input as a GZIP-encoded file.
+// The current implementation does no compression.
+func (Export) CompressGZIP(in []byte) (out []byte) {
+	// RFC 1952, section 2.3.1.
+	var gzipHeader = [10]byte{0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff}
+
+	// RFC 1951, section 3.2.4.
+	var blockHeader [5]byte
+	const maxBlockSize = math.MaxUint16
+	numBlocks := 1 + len(in)/maxBlockSize
+
+	// RFC 1952, section 2.3.1.
+	var gzipFooter [8]byte
+	binary.LittleEndian.PutUint32(gzipFooter[0:4], crc32.ChecksumIEEE(in))
+	binary.LittleEndian.PutUint32(gzipFooter[4:8], uint32(len(in)))
+
+	// Encode the input without compression using raw DEFLATE blocks.
+	out = make([]byte, 0, len(gzipHeader)+len(blockHeader)*numBlocks+len(in)+len(gzipFooter))
+	out = append(out, gzipHeader[:]...)
+	for blockHeader[0] == 0 {
+		blockSize := maxBlockSize
+		if blockSize > len(in) {
+			blockHeader[0] = 0x01 // final bit per RFC 1951, section 3.2.3.
+			blockSize = len(in)
+		}
+		binary.LittleEndian.PutUint16(blockHeader[1:3], uint16(blockSize)^0x0000)
+		binary.LittleEndian.PutUint16(blockHeader[3:5], uint16(blockSize)^0xffff)
+		out = append(out, blockHeader[:]...)
+		out = append(out, in[:blockSize]...)
+		in = in[blockSize:]
+	}
+	out = append(out, gzipFooter[:]...)
+	return out
+}
diff --git a/internal/testprotos/conformance/conformance.pb.go b/internal/testprotos/conformance/conformance.pb.go
index f6b6483..dab5b06 100644
--- a/internal/testprotos/conformance/conformance.pb.go
+++ b/internal/testprotos/conformance/conformance.pb.go
@@ -5,18 +5,11 @@
 
 import (
 	proto "github.com/golang/protobuf/proto"
-	protoapi "github.com/golang/protobuf/protoapi"
 	protoreflect "github.com/golang/protobuf/v2/reflect/protoreflect"
 	protoimpl "github.com/golang/protobuf/v2/runtime/protoimpl"
 	reflect "reflect"
 )
 
-// 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 WireFormat int32
 
 const (
@@ -34,6 +27,7 @@
 	return protoreflect.EnumNumber(e)
 }
 
+// Deprecated: Use WireFormat.Type.Values instead.
 var WireFormat_name = map[int32]string{
 	0: "UNSPECIFIED",
 	1: "PROTOBUF",
@@ -42,6 +36,7 @@
 	4: "TEXT_FORMAT",
 }
 
+// Deprecated: Use WireFormat.Type.Values instead.
 var WireFormat_value = map[string]int32{
 	"UNSPECIFIED": 0,
 	"PROTOBUF":    1,
@@ -51,9 +46,10 @@
 }
 
 func (x WireFormat) String() string {
-	return proto.EnumName(WireFormat_name, int32(x))
+	return protoimpl.X.EnumStringOf(x.Type(), protoreflect.EnumNumber(x))
 }
 
+// Deprecated: Use WireFormat.Type instead.
 func (WireFormat) EnumDescriptor() ([]byte, []int) {
 	return xxx_File_conformance_conformance_proto_rawdesc_gzipped, []int{0}
 }
@@ -84,6 +80,7 @@
 	return protoreflect.EnumNumber(e)
 }
 
+// Deprecated: Use TestCategory.Type.Values instead.
 var TestCategory_name = map[int32]string{
 	0: "UNSPECIFIED_TEST",
 	1: "BINARY_TEST",
@@ -93,6 +90,7 @@
 	5: "TEXT_FORMAT_TEST",
 }
 
+// Deprecated: Use TestCategory.Type.Values instead.
 var TestCategory_value = map[string]int32{
 	"UNSPECIFIED_TEST":                 0,
 	"BINARY_TEST":                      1,
@@ -103,9 +101,10 @@
 }
 
 func (x TestCategory) String() string {
-	return proto.EnumName(TestCategory_name, int32(x))
+	return protoimpl.X.EnumStringOf(x.Type(), protoreflect.EnumNumber(x))
 }
 
+// Deprecated: Use TestCategory.Type instead.
 func (TestCategory) EnumDescriptor() ([]byte, []int) {
 	return xxx_File_conformance_conformance_proto_rawdesc_gzipped, []int{1}
 }
@@ -126,6 +125,8 @@
 func (m *FailureSet) Reset()         { *m = FailureSet{} }
 func (m *FailureSet) String() string { return proto.CompactTextString(m) }
 func (*FailureSet) ProtoMessage()    {}
+
+// Deprecated: Use FailureSet.ProtoReflect.Type instead.
 func (*FailureSet) Descriptor() ([]byte, []int) {
 	return xxx_File_conformance_conformance_proto_rawdesc_gzipped, []int{0}
 }
@@ -199,6 +200,8 @@
 func (m *ConformanceRequest) Reset()         { *m = ConformanceRequest{} }
 func (m *ConformanceRequest) String() string { return proto.CompactTextString(m) }
 func (*ConformanceRequest) ProtoMessage()    {}
+
+// Deprecated: Use ConformanceRequest.ProtoReflect.Type instead.
 func (*ConformanceRequest) Descriptor() ([]byte, []int) {
 	return xxx_File_conformance_conformance_proto_rawdesc_gzipped, []int{1}
 }
@@ -367,6 +370,8 @@
 func (m *ConformanceResponse) Reset()         { *m = ConformanceResponse{} }
 func (m *ConformanceResponse) String() string { return proto.CompactTextString(m) }
 func (*ConformanceResponse) ProtoMessage()    {}
+
+// Deprecated: Use ConformanceResponse.ProtoReflect.Type instead.
 func (*ConformanceResponse) Descriptor() ([]byte, []int) {
 	return xxx_File_conformance_conformance_proto_rawdesc_gzipped, []int{2}
 }
@@ -533,6 +538,8 @@
 func (m *JspbEncodingConfig) Reset()         { *m = JspbEncodingConfig{} }
 func (m *JspbEncodingConfig) String() string { return proto.CompactTextString(m) }
 func (*JspbEncodingConfig) ProtoMessage()    {}
+
+// Deprecated: Use JspbEncodingConfig.ProtoReflect.Type instead.
 func (*JspbEncodingConfig) Descriptor() ([]byte, []int) {
 	return xxx_File_conformance_conformance_proto_rawdesc_gzipped, []int{3}
 }
@@ -658,7 +665,7 @@
 	0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
 }
 
-var xxx_File_conformance_conformance_proto_rawdesc_gzipped = protoapi.CompressGZIP(xxx_File_conformance_conformance_proto_rawdesc)
+var xxx_File_conformance_conformance_proto_rawdesc_gzipped = protoimpl.X.CompressGZIP(xxx_File_conformance_conformance_proto_rawdesc)
 
 const _ = protoimpl.EnforceVersion(protoimpl.Version - 0)
 
diff --git a/internal/testprotos/legacy/legacy.pb.go b/internal/testprotos/legacy/legacy.pb.go
index 5703700..670a8b4 100644
--- a/internal/testprotos/legacy/legacy.pb.go
+++ b/internal/testprotos/legacy/legacy.pb.go
@@ -5,7 +5,6 @@
 
 import (
 	proto "github.com/golang/protobuf/proto"
-	protoapi "github.com/golang/protobuf/protoapi"
 	proto2_v0_0 "github.com/golang/protobuf/v2/internal/testprotos/legacy/proto2.v0.0.0-20160225-2fc053c5"
 	proto2_v0_01 "github.com/golang/protobuf/v2/internal/testprotos/legacy/proto2.v0.0.0-20160519-a4ab9ec5"
 	proto2_v1_0 "github.com/golang/protobuf/v2/internal/testprotos/legacy/proto2.v1.0.0-20180125-92554152"
@@ -23,12 +22,6 @@
 	reflect "reflect"
 )
 
-// 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 Legacy struct {
 	F1                   *proto2_v0_0.Message  `protobuf:"bytes,1,opt,name=f1,proto3" json:"f1,omitempty"`
 	F2                   *proto3_v0_0.Message  `protobuf:"bytes,2,opt,name=f2,proto3" json:"f2,omitempty"`
@@ -53,6 +46,8 @@
 func (m *Legacy) Reset()         { *m = Legacy{} }
 func (m *Legacy) String() string { return proto.CompactTextString(m) }
 func (*Legacy) ProtoMessage()    {}
+
+// Deprecated: Use Legacy.ProtoReflect.Type instead.
 func (*Legacy) Descriptor() ([]byte, []int) {
 	return xxx_File_legacy_legacy_proto_rawdesc_gzipped, []int{0}
 }
@@ -260,7 +255,7 @@
 	0x33,
 }
 
-var xxx_File_legacy_legacy_proto_rawdesc_gzipped = protoapi.CompressGZIP(xxx_File_legacy_legacy_proto_rawdesc)
+var xxx_File_legacy_legacy_proto_rawdesc_gzipped = protoimpl.X.CompressGZIP(xxx_File_legacy_legacy_proto_rawdesc)
 
 const _ = protoimpl.EnforceVersion(protoimpl.Version - 0)
 
diff --git a/internal/testprotos/test/ext.pb.go b/internal/testprotos/test/ext.pb.go
index a1fce7e..f52447d 100644
--- a/internal/testprotos/test/ext.pb.go
+++ b/internal/testprotos/test/ext.pb.go
@@ -5,17 +5,10 @@
 
 import (
 	proto "github.com/golang/protobuf/proto"
-	protoapi "github.com/golang/protobuf/protoapi"
 	protoreflect "github.com/golang/protobuf/v2/reflect/protoreflect"
 	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
-
 var xxx_File_test_ext_proto_extDescs = []proto.ExtensionDesc{
 	{
 		ExtendedType:  (*TestAllExtensions)(nil),
@@ -53,7 +46,7 @@
 	0x74, 0x65, 0x73, 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2f, 0x74, 0x65, 0x73, 0x74,
 }
 
-var xxx_File_test_ext_proto_rawdesc_gzipped = protoapi.CompressGZIP(xxx_File_test_ext_proto_rawdesc)
+var xxx_File_test_ext_proto_rawdesc_gzipped = protoimpl.X.CompressGZIP(xxx_File_test_ext_proto_rawdesc)
 
 const _ = protoimpl.EnforceVersion(protoimpl.Version - 0)
 
diff --git a/internal/testprotos/test/test.pb.go b/internal/testprotos/test/test.pb.go
index 0e6f681..e6b9c88 100644
--- a/internal/testprotos/test/test.pb.go
+++ b/internal/testprotos/test/test.pb.go
@@ -5,18 +5,11 @@
 
 import (
 	proto "github.com/golang/protobuf/proto"
-	protoapi "github.com/golang/protobuf/protoapi"
 	protoreflect "github.com/golang/protobuf/v2/reflect/protoreflect"
 	protoimpl "github.com/golang/protobuf/v2/runtime/protoimpl"
 	reflect "reflect"
 )
 
-// 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 ForeignEnum int32
 
 const (
@@ -32,12 +25,14 @@
 	return protoreflect.EnumNumber(e)
 }
 
+// Deprecated: Use ForeignEnum.Type.Values instead.
 var ForeignEnum_name = map[int32]string{
 	4: "FOREIGN_FOO",
 	5: "FOREIGN_BAR",
 	6: "FOREIGN_BAZ",
 }
 
+// Deprecated: Use ForeignEnum.Type.Values instead.
 var ForeignEnum_value = map[string]int32{
 	"FOREIGN_FOO": 4,
 	"FOREIGN_BAR": 5,
@@ -45,24 +40,24 @@
 }
 
 func (x ForeignEnum) Enum() *ForeignEnum {
-	p := new(ForeignEnum)
-	*p = x
-	return p
+	return &x
 }
 
 func (x ForeignEnum) String() string {
-	return proto.EnumName(ForeignEnum_name, int32(x))
+	return protoimpl.X.EnumStringOf(x.Type(), protoreflect.EnumNumber(x))
 }
 
-func (x *ForeignEnum) UnmarshalJSON(data []byte) error {
-	value, err := proto.UnmarshalJSONEnum(ForeignEnum_value, data, "ForeignEnum")
+// Deprecated: Do not use.
+func (x *ForeignEnum) UnmarshalJSON(b []byte) error {
+	num, err := protoimpl.X.UnmarshalJSONEnum(x.Type(), b)
 	if err != nil {
 		return err
 	}
-	*x = ForeignEnum(value)
+	*x = ForeignEnum(num)
 	return nil
 }
 
+// Deprecated: Use ForeignEnum.Type instead.
 func (ForeignEnum) EnumDescriptor() ([]byte, []int) {
 	return xxx_File_test_test_proto_rawdesc_gzipped, []int{0}
 }
@@ -80,33 +75,35 @@
 	return protoreflect.EnumNumber(e)
 }
 
+// Deprecated: Use TestReservedEnumFields.Type.Values instead.
 var TestReservedEnumFields_name = map[int32]string{
 	0: "RESERVED_ENUM",
 }
 
+// Deprecated: Use TestReservedEnumFields.Type.Values instead.
 var TestReservedEnumFields_value = map[string]int32{
 	"RESERVED_ENUM": 0,
 }
 
 func (x TestReservedEnumFields) Enum() *TestReservedEnumFields {
-	p := new(TestReservedEnumFields)
-	*p = x
-	return p
+	return &x
 }
 
 func (x TestReservedEnumFields) String() string {
-	return proto.EnumName(TestReservedEnumFields_name, int32(x))
+	return protoimpl.X.EnumStringOf(x.Type(), protoreflect.EnumNumber(x))
 }
 
-func (x *TestReservedEnumFields) UnmarshalJSON(data []byte) error {
-	value, err := proto.UnmarshalJSONEnum(TestReservedEnumFields_value, data, "TestReservedEnumFields")
+// Deprecated: Do not use.
+func (x *TestReservedEnumFields) UnmarshalJSON(b []byte) error {
+	num, err := protoimpl.X.UnmarshalJSONEnum(x.Type(), b)
 	if err != nil {
 		return err
 	}
-	*x = TestReservedEnumFields(value)
+	*x = TestReservedEnumFields(num)
 	return nil
 }
 
+// Deprecated: Use TestReservedEnumFields.Type instead.
 func (TestReservedEnumFields) EnumDescriptor() ([]byte, []int) {
 	return xxx_File_test_test_proto_rawdesc_gzipped, []int{1}
 }
@@ -127,6 +124,7 @@
 	return protoreflect.EnumNumber(e)
 }
 
+// Deprecated: Use TestAllTypes_NestedEnum.Type.Values instead.
 var TestAllTypes_NestedEnum_name = map[int32]string{
 	0:  "FOO",
 	1:  "BAR",
@@ -134,6 +132,7 @@
 	-1: "NEG",
 }
 
+// Deprecated: Use TestAllTypes_NestedEnum.Type.Values instead.
 var TestAllTypes_NestedEnum_value = map[string]int32{
 	"FOO": 0,
 	"BAR": 1,
@@ -142,24 +141,24 @@
 }
 
 func (x TestAllTypes_NestedEnum) Enum() *TestAllTypes_NestedEnum {
-	p := new(TestAllTypes_NestedEnum)
-	*p = x
-	return p
+	return &x
 }
 
 func (x TestAllTypes_NestedEnum) String() string {
-	return proto.EnumName(TestAllTypes_NestedEnum_name, int32(x))
+	return protoimpl.X.EnumStringOf(x.Type(), protoreflect.EnumNumber(x))
 }
 
-func (x *TestAllTypes_NestedEnum) UnmarshalJSON(data []byte) error {
-	value, err := proto.UnmarshalJSONEnum(TestAllTypes_NestedEnum_value, data, "TestAllTypes_NestedEnum")
+// Deprecated: Do not use.
+func (x *TestAllTypes_NestedEnum) UnmarshalJSON(b []byte) error {
+	num, err := protoimpl.X.UnmarshalJSONEnum(x.Type(), b)
 	if err != nil {
 		return err
 	}
-	*x = TestAllTypes_NestedEnum(value)
+	*x = TestAllTypes_NestedEnum(num)
 	return nil
 }
 
+// Deprecated: Use TestAllTypes_NestedEnum.Type instead.
 func (TestAllTypes_NestedEnum) EnumDescriptor() ([]byte, []int) {
 	return xxx_File_test_test_proto_rawdesc_gzipped, []int{0, 0}
 }
@@ -176,33 +175,35 @@
 	return protoreflect.EnumNumber(e)
 }
 
+// Deprecated: Use TestDeprecatedMessage_DeprecatedEnum.Type.Values instead.
 var TestDeprecatedMessage_DeprecatedEnum_name = map[int32]string{
 	0: "DEPRECATED",
 }
 
+// Deprecated: Use TestDeprecatedMessage_DeprecatedEnum.Type.Values instead.
 var TestDeprecatedMessage_DeprecatedEnum_value = map[string]int32{
 	"DEPRECATED": 0,
 }
 
 func (x TestDeprecatedMessage_DeprecatedEnum) Enum() *TestDeprecatedMessage_DeprecatedEnum {
-	p := new(TestDeprecatedMessage_DeprecatedEnum)
-	*p = x
-	return p
+	return &x
 }
 
 func (x TestDeprecatedMessage_DeprecatedEnum) String() string {
-	return proto.EnumName(TestDeprecatedMessage_DeprecatedEnum_name, int32(x))
+	return protoimpl.X.EnumStringOf(x.Type(), protoreflect.EnumNumber(x))
 }
 
-func (x *TestDeprecatedMessage_DeprecatedEnum) UnmarshalJSON(data []byte) error {
-	value, err := proto.UnmarshalJSONEnum(TestDeprecatedMessage_DeprecatedEnum_value, data, "TestDeprecatedMessage_DeprecatedEnum")
+// Deprecated: Do not use.
+func (x *TestDeprecatedMessage_DeprecatedEnum) UnmarshalJSON(b []byte) error {
+	num, err := protoimpl.X.UnmarshalJSONEnum(x.Type(), b)
 	if err != nil {
 		return err
 	}
-	*x = TestDeprecatedMessage_DeprecatedEnum(value)
+	*x = TestDeprecatedMessage_DeprecatedEnum(num)
 	return nil
 }
 
+// Deprecated: Use TestDeprecatedMessage_DeprecatedEnum.Type instead.
 func (TestDeprecatedMessage_DeprecatedEnum) EnumDescriptor() ([]byte, []int) {
 	return xxx_File_test_test_proto_rawdesc_gzipped, []int{1, 0}
 }
@@ -309,6 +310,8 @@
 func (m *TestAllTypes) Reset()         { *m = TestAllTypes{} }
 func (m *TestAllTypes) String() string { return proto.CompactTextString(m) }
 func (*TestAllTypes) ProtoMessage()    {}
+
+// Deprecated: Use TestAllTypes.ProtoReflect.Type instead.
 func (*TestAllTypes) Descriptor() ([]byte, []int) {
 	return xxx_File_test_test_proto_rawdesc_gzipped, []int{0}
 }
@@ -1057,6 +1060,8 @@
 func (m *TestDeprecatedMessage) Reset()         { *m = TestDeprecatedMessage{} }
 func (m *TestDeprecatedMessage) String() string { return proto.CompactTextString(m) }
 func (*TestDeprecatedMessage) ProtoMessage()    {}
+
+// Deprecated: Use TestDeprecatedMessage.ProtoReflect.Type instead.
 func (*TestDeprecatedMessage) Descriptor() ([]byte, []int) {
 	return xxx_File_test_test_proto_rawdesc_gzipped, []int{1}
 }
@@ -1133,6 +1138,8 @@
 func (m *ForeignMessage) Reset()         { *m = ForeignMessage{} }
 func (m *ForeignMessage) String() string { return proto.CompactTextString(m) }
 func (*ForeignMessage) ProtoMessage()    {}
+
+// Deprecated: Use ForeignMessage.ProtoReflect.Type instead.
 func (*ForeignMessage) Descriptor() ([]byte, []int) {
 	return xxx_File_test_test_proto_rawdesc_gzipped, []int{2}
 }
@@ -1181,6 +1188,8 @@
 func (m *TestReservedFields) Reset()         { *m = TestReservedFields{} }
 func (m *TestReservedFields) String() string { return proto.CompactTextString(m) }
 func (*TestReservedFields) ProtoMessage()    {}
+
+// Deprecated: Use TestReservedFields.ProtoReflect.Type instead.
 func (*TestReservedFields) Descriptor() ([]byte, []int) {
 	return xxx_File_test_test_proto_rawdesc_gzipped, []int{3}
 }
@@ -1216,6 +1225,8 @@
 func (m *TestAllExtensions) Reset()         { *m = TestAllExtensions{} }
 func (m *TestAllExtensions) String() string { return proto.CompactTextString(m) }
 func (*TestAllExtensions) ProtoMessage()    {}
+
+// Deprecated: Use TestAllExtensions.ProtoReflect.Type instead.
 func (*TestAllExtensions) Descriptor() ([]byte, []int) {
 	return xxx_File_test_test_proto_rawdesc_gzipped, []int{4}
 }
@@ -1224,6 +1235,7 @@
 	{Start: 1, End: 536870911},
 }
 
+// Deprecated: Use TestAllExtensions.ProtoReflect.Type.ExtensionRanges instead.
 func (*TestAllExtensions) ExtensionRangeArray() []proto.ExtensionRange {
 	return extRange_TestAllExtensions
 }
@@ -1259,6 +1271,8 @@
 func (m *OptionalGroupExtension) Reset()         { *m = OptionalGroupExtension{} }
 func (m *OptionalGroupExtension) String() string { return proto.CompactTextString(m) }
 func (*OptionalGroupExtension) ProtoMessage()    {}
+
+// Deprecated: Use OptionalGroupExtension.ProtoReflect.Type instead.
 func (*OptionalGroupExtension) Descriptor() ([]byte, []int) {
 	return xxx_File_test_test_proto_rawdesc_gzipped, []int{5}
 }
@@ -1301,6 +1315,8 @@
 func (m *RepeatedGroupExtension) Reset()         { *m = RepeatedGroupExtension{} }
 func (m *RepeatedGroupExtension) String() string { return proto.CompactTextString(m) }
 func (*RepeatedGroupExtension) ProtoMessage()    {}
+
+// Deprecated: Use RepeatedGroupExtension.ProtoReflect.Type instead.
 func (*RepeatedGroupExtension) Descriptor() ([]byte, []int) {
 	return xxx_File_test_test_proto_rawdesc_gzipped, []int{6}
 }
@@ -1342,6 +1358,8 @@
 func (m *TestNestedExtension) Reset()         { *m = TestNestedExtension{} }
 func (m *TestNestedExtension) String() string { return proto.CompactTextString(m) }
 func (*TestNestedExtension) ProtoMessage()    {}
+
+// Deprecated: Use TestNestedExtension.ProtoReflect.Type instead.
 func (*TestNestedExtension) Descriptor() ([]byte, []int) {
 	return xxx_File_test_test_proto_rawdesc_gzipped, []int{7}
 }
@@ -1377,6 +1395,8 @@
 func (m *FooRequest) Reset()         { *m = FooRequest{} }
 func (m *FooRequest) String() string { return proto.CompactTextString(m) }
 func (*FooRequest) ProtoMessage()    {}
+
+// Deprecated: Use FooRequest.ProtoReflect.Type instead.
 func (*FooRequest) Descriptor() ([]byte, []int) {
 	return xxx_File_test_test_proto_rawdesc_gzipped, []int{8}
 }
@@ -1411,6 +1431,8 @@
 func (m *FooResponse) Reset()         { *m = FooResponse{} }
 func (m *FooResponse) String() string { return proto.CompactTextString(m) }
 func (*FooResponse) ProtoMessage()    {}
+
+// Deprecated: Use FooResponse.ProtoReflect.Type instead.
 func (*FooResponse) Descriptor() ([]byte, []int) {
 	return xxx_File_test_test_proto_rawdesc_gzipped, []int{9}
 }
@@ -1447,6 +1469,8 @@
 func (m *TestAllTypes_NestedMessage) Reset()         { *m = TestAllTypes_NestedMessage{} }
 func (m *TestAllTypes_NestedMessage) String() string { return proto.CompactTextString(m) }
 func (*TestAllTypes_NestedMessage) ProtoMessage()    {}
+
+// Deprecated: Use TestAllTypes_NestedMessage.ProtoReflect.Type instead.
 func (*TestAllTypes_NestedMessage) Descriptor() ([]byte, []int) {
 	return xxx_File_test_test_proto_rawdesc_gzipped, []int{0, 0}
 }
@@ -1496,6 +1520,8 @@
 func (m *TestAllTypes_OptionalGroup) Reset()         { *m = TestAllTypes_OptionalGroup{} }
 func (m *TestAllTypes_OptionalGroup) String() string { return proto.CompactTextString(m) }
 func (*TestAllTypes_OptionalGroup) ProtoMessage()    {}
+
+// Deprecated: Use TestAllTypes_OptionalGroup.ProtoReflect.Type instead.
 func (*TestAllTypes_OptionalGroup) Descriptor() ([]byte, []int) {
 	return xxx_File_test_test_proto_rawdesc_gzipped, []int{0, 1}
 }
@@ -1538,6 +1564,8 @@
 func (m *TestAllTypes_RepeatedGroup) Reset()         { *m = TestAllTypes_RepeatedGroup{} }
 func (m *TestAllTypes_RepeatedGroup) String() string { return proto.CompactTextString(m) }
 func (*TestAllTypes_RepeatedGroup) ProtoMessage()    {}
+
+// Deprecated: Use TestAllTypes_RepeatedGroup.ProtoReflect.Type instead.
 func (*TestAllTypes_RepeatedGroup) Descriptor() ([]byte, []int) {
 	return xxx_File_test_test_proto_rawdesc_gzipped, []int{0, 2}
 }
@@ -2812,7 +2840,7 @@
 	0x73, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x50, 0x01, 0x58, 0x02,
 }
 
-var xxx_File_test_test_proto_rawdesc_gzipped = protoapi.CompressGZIP(xxx_File_test_test_proto_rawdesc)
+var xxx_File_test_test_proto_rawdesc_gzipped = protoimpl.X.CompressGZIP(xxx_File_test_test_proto_rawdesc)
 
 const _ = protoimpl.EnforceVersion(protoimpl.Version - 0)
 
diff --git a/internal/testprotos/test/test_import.pb.go b/internal/testprotos/test/test_import.pb.go
index af4bfd6..2b7ad1f 100644
--- a/internal/testprotos/test/test_import.pb.go
+++ b/internal/testprotos/test/test_import.pb.go
@@ -5,18 +5,11 @@
 
 import (
 	proto "github.com/golang/protobuf/proto"
-	protoapi "github.com/golang/protobuf/protoapi"
 	protoreflect "github.com/golang/protobuf/v2/reflect/protoreflect"
 	protoimpl "github.com/golang/protobuf/v2/runtime/protoimpl"
 	reflect "reflect"
 )
 
-// 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 ImportEnum int32
 
 const (
@@ -30,33 +23,35 @@
 	return protoreflect.EnumNumber(e)
 }
 
+// Deprecated: Use ImportEnum.Type.Values instead.
 var ImportEnum_name = map[int32]string{
 	0: "IMPORT_ZERO",
 }
 
+// Deprecated: Use ImportEnum.Type.Values instead.
 var ImportEnum_value = map[string]int32{
 	"IMPORT_ZERO": 0,
 }
 
 func (x ImportEnum) Enum() *ImportEnum {
-	p := new(ImportEnum)
-	*p = x
-	return p
+	return &x
 }
 
 func (x ImportEnum) String() string {
-	return proto.EnumName(ImportEnum_name, int32(x))
+	return protoimpl.X.EnumStringOf(x.Type(), protoreflect.EnumNumber(x))
 }
 
-func (x *ImportEnum) UnmarshalJSON(data []byte) error {
-	value, err := proto.UnmarshalJSONEnum(ImportEnum_value, data, "ImportEnum")
+// Deprecated: Do not use.
+func (x *ImportEnum) UnmarshalJSON(b []byte) error {
+	num, err := protoimpl.X.UnmarshalJSONEnum(x.Type(), b)
 	if err != nil {
 		return err
 	}
-	*x = ImportEnum(value)
+	*x = ImportEnum(num)
 	return nil
 }
 
+// Deprecated: Use ImportEnum.Type instead.
 func (ImportEnum) EnumDescriptor() ([]byte, []int) {
 	return xxx_File_test_test_import_proto_rawdesc_gzipped, []int{0}
 }
@@ -73,6 +68,8 @@
 func (m *ImportMessage) Reset()         { *m = ImportMessage{} }
 func (m *ImportMessage) String() string { return proto.CompactTextString(m) }
 func (*ImportMessage) ProtoMessage()    {}
+
+// Deprecated: Use ImportMessage.ProtoReflect.Type instead.
 func (*ImportMessage) Descriptor() ([]byte, []int) {
 	return xxx_File_test_test_import_proto_rawdesc_gzipped, []int{0}
 }
@@ -115,7 +112,7 @@
 	0x73, 0x2f, 0x74, 0x65, 0x73, 0x74,
 }
 
-var xxx_File_test_test_import_proto_rawdesc_gzipped = protoapi.CompressGZIP(xxx_File_test_test_import_proto_rawdesc)
+var xxx_File_test_test_import_proto_rawdesc_gzipped = protoimpl.X.CompressGZIP(xxx_File_test_test_import_proto_rawdesc)
 
 const _ = protoimpl.EnforceVersion(protoimpl.Version - 0)
 
diff --git a/internal/testprotos/test/test_public.pb.go b/internal/testprotos/test/test_public.pb.go
index 64303df..2be4785 100644
--- a/internal/testprotos/test/test_public.pb.go
+++ b/internal/testprotos/test/test_public.pb.go
@@ -5,18 +5,11 @@
 
 import (
 	proto "github.com/golang/protobuf/proto"
-	protoapi "github.com/golang/protobuf/protoapi"
 	protoreflect "github.com/golang/protobuf/v2/reflect/protoreflect"
 	protoimpl "github.com/golang/protobuf/v2/runtime/protoimpl"
 	reflect "reflect"
 )
 
-// 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 PublicImportMessage struct {
 	XXX_NoUnkeyedLiteral struct{} `json:"-"`
 	XXX_unrecognized     []byte   `json:"-"`
@@ -29,6 +22,8 @@
 func (m *PublicImportMessage) Reset()         { *m = PublicImportMessage{} }
 func (m *PublicImportMessage) String() string { return proto.CompactTextString(m) }
 func (*PublicImportMessage) ProtoMessage()    {}
+
+// Deprecated: Use PublicImportMessage.ProtoReflect.Type instead.
 func (*PublicImportMessage) Descriptor() ([]byte, []int) {
 	return xxx_File_test_test_public_proto_rawdesc_gzipped, []int{0}
 }
@@ -68,7 +63,7 @@
 	0x73, 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2f, 0x74, 0x65, 0x73, 0x74,
 }
 
-var xxx_File_test_test_public_proto_rawdesc_gzipped = protoapi.CompressGZIP(xxx_File_test_test_public_proto_rawdesc)
+var xxx_File_test_test_public_proto_rawdesc_gzipped = protoimpl.X.CompressGZIP(xxx_File_test_test_public_proto_rawdesc)
 
 const _ = protoimpl.EnforceVersion(protoimpl.Version - 0)
 
diff --git a/internal/testprotos/test/test_weak.pb.go b/internal/testprotos/test/test_weak.pb.go
index 7fa7296..54a9f44 100644
--- a/internal/testprotos/test/test_weak.pb.go
+++ b/internal/testprotos/test/test_weak.pb.go
@@ -5,18 +5,11 @@
 
 import (
 	proto "github.com/golang/protobuf/proto"
-	protoapi "github.com/golang/protobuf/protoapi"
 	protoreflect "github.com/golang/protobuf/v2/reflect/protoreflect"
 	protoimpl "github.com/golang/protobuf/v2/runtime/protoimpl"
 	reflect "reflect"
 )
 
-// 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 WeakImportMessage struct {
 	XXX_NoUnkeyedLiteral struct{} `json:"-"`
 	XXX_unrecognized     []byte   `json:"-"`
@@ -29,6 +22,8 @@
 func (m *WeakImportMessage) Reset()         { *m = WeakImportMessage{} }
 func (m *WeakImportMessage) String() string { return proto.CompactTextString(m) }
 func (*WeakImportMessage) ProtoMessage()    {}
+
+// Deprecated: Use WeakImportMessage.ProtoReflect.Type instead.
 func (*WeakImportMessage) Descriptor() ([]byte, []int) {
 	return xxx_File_test_test_weak_proto_rawdesc_gzipped, []int{0}
 }
@@ -68,7 +63,7 @@
 	0x6f, 0x74, 0x6f, 0x73, 0x2f, 0x74, 0x65, 0x73, 0x74,
 }
 
-var xxx_File_test_test_weak_proto_rawdesc_gzipped = protoapi.CompressGZIP(xxx_File_test_test_weak_proto_rawdesc)
+var xxx_File_test_test_weak_proto_rawdesc_gzipped = protoimpl.X.CompressGZIP(xxx_File_test_test_weak_proto_rawdesc)
 
 const _ = protoimpl.EnforceVersion(protoimpl.Version - 0)