Procházet zdrojové kódy

cmd/protoc-gen-go: revert Enum simplication

Go1.12 on GoLLVM with LLVM 9.0.0 has a bug where the simpler but
equivalent expression results in memory corruption.
Revert to the more complex expression to avoid breaking GoLLVM and
it is not worth waiting for a GoLLVM fix.

Change-Id: I8b42965ca9bd333deb8693021e7b22f966e13521
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/171463
Reviewed-by: Damien Neil <dneil@google.com>
Joe Tsai před 6 roky
rodič
revize
09b5b46567

+ 3 - 1
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()
 	}

+ 3 - 1
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 {

+ 1 - 1
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}
+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}

+ 3 - 1
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 {

+ 9 - 3
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 {

+ 3 - 1
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 {

+ 18 - 6
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 {

+ 3 - 1
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 {

+ 6 - 2
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 {

+ 12 - 4
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 {

+ 3 - 1
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 {

+ 9 - 3
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 {

+ 18 - 6
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 {