diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go index 1b8b332f..a1254053 100644 --- a/cmd/protoc-gen-go/internal_gengo/main.go +++ b/cmd/protoc-gen-go/internal_gengo/main.go @@ -253,7 +253,9 @@ func genEnum(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, enum // Enum method. if enum.Desc.Syntax() != protoreflect.Proto3 { g.P("func (x ", enum.GoIdent, ") Enum() *", enum.GoIdent, " {") - g.P("return &x") + g.P("p := new(", enum.GoIdent, ")") + g.P("*p = x") + g.P("return p") g.P("}") g.P() } diff --git a/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go b/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go index b5e3701c..97c1814b 100644 --- a/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go +++ b/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go @@ -30,7 +30,9 @@ var AnnotationsTestEnum_value = map[string]int32{ } func (x AnnotationsTestEnum) Enum() *AnnotationsTestEnum { - return &x + p := new(AnnotationsTestEnum) + *p = x + return p } func (x AnnotationsTestEnum) String() string { diff --git a/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go.meta b/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go.meta index 2db1effb..6f002300 100644 --- a/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go.meta +++ b/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go.meta @@ -1 +1 @@ -annotation:{path:5 path:0 source_file:"annotations/annotations.proto" begin:461 end:480} annotation:{path:5 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:497 end:544} annotation:{path:4 path:0 source_file:"annotations/annotations.proto" begin:1702 end:1724} annotation:{path:4 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:1735 end:1755} annotation:{path:4 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:2736 end:2759} \ No newline at end of file +annotation:{path:5 path:0 source_file:"annotations/annotations.proto" begin:461 end:480} annotation:{path:5 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:497 end:544} annotation:{path:4 path:0 source_file:"annotations/annotations.proto" begin:1740 end:1762} annotation:{path:4 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:1773 end:1793} annotation:{path:4 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:2774 end:2797} \ No newline at end of file diff --git a/cmd/protoc-gen-go/testdata/extensions/ext/ext.pb.go b/cmd/protoc-gen-go/testdata/extensions/ext/ext.pb.go index 22674803..0799cc9f 100644 --- a/cmd/protoc-gen-go/testdata/extensions/ext/ext.pb.go +++ b/cmd/protoc-gen-go/testdata/extensions/ext/ext.pb.go @@ -32,7 +32,9 @@ var Enum_value = map[string]int32{ } func (x Enum) Enum() *Enum { - return &x + p := new(Enum) + *p = x + return p } func (x Enum) String() string { 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 cf4e72c0..304e7169 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 @@ -36,7 +36,9 @@ var E_value = map[string]int32{ } func (x E) Enum() *E { - return &x + p := new(E) + *p = x + return p } func (x E) String() string { @@ -83,7 +85,9 @@ var M_Subenum_value = map[string]int32{ } func (x M_Subenum) Enum() *M_Subenum { - return &x + p := new(M_Subenum) + *p = x + return p } func (x M_Subenum) String() string { @@ -130,7 +134,9 @@ var M_Submessage_Submessage_Subenum_value = map[string]int32{ } func (x M_Submessage_Submessage_Subenum) Enum() *M_Submessage_Submessage_Subenum { - return &x + p := new(M_Submessage_Submessage_Subenum) + *p = x + return p } func (x M_Submessage_Submessage_Subenum) String() string { diff --git a/cmd/protoc-gen-go/testdata/nopackage/nopackage.pb.go b/cmd/protoc-gen-go/testdata/nopackage/nopackage.pb.go index 75f0eacf..f20c1fb9 100644 --- a/cmd/protoc-gen-go/testdata/nopackage/nopackage.pb.go +++ b/cmd/protoc-gen-go/testdata/nopackage/nopackage.pb.go @@ -30,7 +30,9 @@ var Enum_value = map[string]int32{ } func (x Enum) Enum() *Enum { - return &x + p := new(Enum) + *p = x + return p } func (x Enum) String() string { diff --git a/cmd/protoc-gen-go/testdata/proto2/enum.pb.go b/cmd/protoc-gen-go/testdata/proto2/enum.pb.go index eb0e87b5..71a52f50 100644 --- a/cmd/protoc-gen-go/testdata/proto2/enum.pb.go +++ b/cmd/protoc-gen-go/testdata/proto2/enum.pb.go @@ -36,7 +36,9 @@ var EnumType1_value = map[string]int32{ } func (x EnumType1) Enum() *EnumType1 { - return &x + p := new(EnumType1) + *p = x + return p } func (x EnumType1) String() string { @@ -86,7 +88,9 @@ var EnumType2_value = map[string]int32{ } func (x EnumType2) Enum() *EnumType2 { - return &x + p := new(EnumType2) + *p = x + return p } func (x EnumType2) String() string { @@ -135,7 +139,9 @@ var EnumContainerMessage1_NestedEnumType1A_value = map[string]int32{ } func (x EnumContainerMessage1_NestedEnumType1A) Enum() *EnumContainerMessage1_NestedEnumType1A { - return &x + p := new(EnumContainerMessage1_NestedEnumType1A) + *p = x + return p } func (x EnumContainerMessage1_NestedEnumType1A) String() string { @@ -182,7 +188,9 @@ var EnumContainerMessage1_NestedEnumType1B_value = map[string]int32{ } func (x EnumContainerMessage1_NestedEnumType1B) Enum() *EnumContainerMessage1_NestedEnumType1B { - return &x + p := new(EnumContainerMessage1_NestedEnumType1B) + *p = x + return p } func (x EnumContainerMessage1_NestedEnumType1B) String() string { @@ -231,7 +239,9 @@ var EnumContainerMessage1_EnumContainerMessage2_NestedEnumType2A_value = map[str } func (x EnumContainerMessage1_EnumContainerMessage2_NestedEnumType2A) Enum() *EnumContainerMessage1_EnumContainerMessage2_NestedEnumType2A { - return &x + p := new(EnumContainerMessage1_EnumContainerMessage2_NestedEnumType2A) + *p = x + return p } func (x EnumContainerMessage1_EnumContainerMessage2_NestedEnumType2A) String() string { @@ -278,7 +288,9 @@ var EnumContainerMessage1_EnumContainerMessage2_NestedEnumType2B_value = map[str } func (x EnumContainerMessage1_EnumContainerMessage2_NestedEnumType2B) Enum() *EnumContainerMessage1_EnumContainerMessage2_NestedEnumType2B { - return &x + p := new(EnumContainerMessage1_EnumContainerMessage2_NestedEnumType2B) + *p = x + return p } func (x EnumContainerMessage1_EnumContainerMessage2_NestedEnumType2B) String() string { diff --git a/cmd/protoc-gen-go/testdata/proto2/fields.pb.go b/cmd/protoc-gen-go/testdata/proto2/fields.pb.go index 21345181..41dd12e9 100644 --- a/cmd/protoc-gen-go/testdata/proto2/fields.pb.go +++ b/cmd/protoc-gen-go/testdata/proto2/fields.pb.go @@ -34,7 +34,9 @@ var FieldTestMessage_Enum_value = map[string]int32{ } func (x FieldTestMessage_Enum) Enum() *FieldTestMessage_Enum { - return &x + p := new(FieldTestMessage_Enum) + *p = x + return p } func (x FieldTestMessage_Enum) String() string { diff --git a/encoding/testprotos/pb2/test.pb.go b/encoding/testprotos/pb2/test.pb.go index a9d11314..2e43716b 100644 --- a/encoding/testprotos/pb2/test.pb.go +++ b/encoding/testprotos/pb2/test.pb.go @@ -37,7 +37,9 @@ var Enum_value = map[string]int32{ } func (x Enum) Enum() *Enum { - return &x + p := new(Enum) + *p = x + return p } func (x Enum) String() string { @@ -90,7 +92,9 @@ var Enums_NestedEnum_value = map[string]int32{ } func (x Enums_NestedEnum) Enum() *Enums_NestedEnum { - return &x + p := new(Enums_NestedEnum) + *p = x + return p } func (x Enums_NestedEnum) String() string { diff --git a/internal/testprotos/test/test.pb.go b/internal/testprotos/test/test.pb.go index 0ac6b8e8..43b2bacf 100644 --- a/internal/testprotos/test/test.pb.go +++ b/internal/testprotos/test/test.pb.go @@ -36,7 +36,9 @@ var ForeignEnum_value = map[string]int32{ } func (x ForeignEnum) Enum() *ForeignEnum { - return &x + p := new(ForeignEnum) + *p = x + return p } func (x ForeignEnum) String() string { @@ -83,7 +85,9 @@ var TestReservedEnumFields_value = map[string]int32{ } func (x TestReservedEnumFields) Enum() *TestReservedEnumFields { - return &x + p := new(TestReservedEnumFields) + *p = x + return p } func (x TestReservedEnumFields) String() string { @@ -139,7 +143,9 @@ var TestAllTypes_NestedEnum_value = map[string]int32{ } func (x TestAllTypes_NestedEnum) Enum() *TestAllTypes_NestedEnum { - return &x + p := new(TestAllTypes_NestedEnum) + *p = x + return p } func (x TestAllTypes_NestedEnum) String() string { @@ -185,7 +191,9 @@ var TestDeprecatedMessage_DeprecatedEnum_value = map[string]int32{ } func (x TestDeprecatedMessage_DeprecatedEnum) Enum() *TestDeprecatedMessage_DeprecatedEnum { - return &x + p := new(TestDeprecatedMessage_DeprecatedEnum) + *p = x + return p } func (x TestDeprecatedMessage_DeprecatedEnum) String() string { diff --git a/internal/testprotos/test/test_import.pb.go b/internal/testprotos/test/test_import.pb.go index 6a5fc076..52af12df 100644 --- a/internal/testprotos/test/test_import.pb.go +++ b/internal/testprotos/test/test_import.pb.go @@ -30,7 +30,9 @@ var ImportEnum_value = map[string]int32{ } func (x ImportEnum) Enum() *ImportEnum { - return &x + p := new(ImportEnum) + *p = x + return p } func (x ImportEnum) String() string { diff --git a/reflect/protoregistry/testprotos/test.pb.go b/reflect/protoregistry/testprotos/test.pb.go index 44d7e4a1..8d082cd5 100644 --- a/reflect/protoregistry/testprotos/test.pb.go +++ b/reflect/protoregistry/testprotos/test.pb.go @@ -30,7 +30,9 @@ var Enum1_value = map[string]int32{ } func (x Enum1) Enum() *Enum1 { - return &x + p := new(Enum1) + *p = x + return p } func (x Enum1) String() string { @@ -77,7 +79,9 @@ var Enum2_value = map[string]int32{ } func (x Enum2) Enum() *Enum2 { - return &x + p := new(Enum2) + *p = x + return p } func (x Enum2) String() string { @@ -124,7 +128,9 @@ var Enum3_value = map[string]int32{ } func (x Enum3) Enum() *Enum3 { - return &x + p := new(Enum3) + *p = x + return p } func (x Enum3) String() string { diff --git a/types/descriptor/descriptor.pb.go b/types/descriptor/descriptor.pb.go index 5e436a36..3c38f61d 100644 --- a/types/descriptor/descriptor.pb.go +++ b/types/descriptor/descriptor.pb.go @@ -93,7 +93,9 @@ var FieldDescriptorProto_Type_value = map[string]int32{ } func (x FieldDescriptorProto_Type) Enum() *FieldDescriptorProto_Type { - return &x + p := new(FieldDescriptorProto_Type) + *p = x + return p } func (x FieldDescriptorProto_Type) String() string { @@ -147,7 +149,9 @@ var FieldDescriptorProto_Label_value = map[string]int32{ } func (x FieldDescriptorProto_Label) Enum() *FieldDescriptorProto_Label { - return &x + p := new(FieldDescriptorProto_Label) + *p = x + return p } func (x FieldDescriptorProto_Label) String() string { @@ -202,7 +206,9 @@ var FileOptions_OptimizeMode_value = map[string]int32{ } func (x FileOptions_OptimizeMode) Enum() *FileOptions_OptimizeMode { - return &x + p := new(FileOptions_OptimizeMode) + *p = x + return p } func (x FileOptions_OptimizeMode) String() string { @@ -256,7 +262,9 @@ var FieldOptions_CType_value = map[string]int32{ } func (x FieldOptions_CType) Enum() *FieldOptions_CType { - return &x + p := new(FieldOptions_CType) + *p = x + return p } func (x FieldOptions_CType) String() string { @@ -312,7 +320,9 @@ var FieldOptions_JSType_value = map[string]int32{ } func (x FieldOptions_JSType) Enum() *FieldOptions_JSType { - return &x + p := new(FieldOptions_JSType) + *p = x + return p } func (x FieldOptions_JSType) String() string { @@ -368,7 +378,9 @@ var MethodOptions_IdempotencyLevel_value = map[string]int32{ } func (x MethodOptions_IdempotencyLevel) Enum() *MethodOptions_IdempotencyLevel { - return &x + p := new(MethodOptions_IdempotencyLevel) + *p = x + return p } func (x MethodOptions_IdempotencyLevel) String() string {