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>
diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go
index 1b8b332..a125405 100644
--- a/cmd/protoc-gen-go/internal_gengo/main.go
+++ b/cmd/protoc-gen-go/internal_gengo/main.go
@@ -253,7 +253,9 @@
// 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 b5e3701..97c1814 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 @@
}
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 2db1eff..6f00230 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 2267480..0799cc9 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 @@
}
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 cf4e72c..304e716 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 @@
}
func (x E) Enum() *E {
- return &x
+ p := new(E)
+ *p = x
+ return p
}
func (x E) String() string {
@@ -83,7 +85,9 @@
}
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 @@
}
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 75f0eac..f20c1fb 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 @@
}
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 eb0e87b..71a52f5 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 @@
}
func (x EnumType1) Enum() *EnumType1 {
- return &x
+ p := new(EnumType1)
+ *p = x
+ return p
}
func (x EnumType1) String() string {
@@ -86,7 +88,9 @@
}
func (x EnumType2) Enum() *EnumType2 {
- return &x
+ p := new(EnumType2)
+ *p = x
+ return p
}
func (x EnumType2) String() string {
@@ -135,7 +139,9 @@
}
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 @@
}
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 @@
}
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 @@
}
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 2134518..41dd12e 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 @@
}
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 a9d1131..2e43716 100644
--- a/encoding/testprotos/pb2/test.pb.go
+++ b/encoding/testprotos/pb2/test.pb.go
@@ -37,7 +37,9 @@
}
func (x Enum) Enum() *Enum {
- return &x
+ p := new(Enum)
+ *p = x
+ return p
}
func (x Enum) String() string {
@@ -90,7 +92,9 @@
}
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 0ac6b8e..43b2bac 100644
--- a/internal/testprotos/test/test.pb.go
+++ b/internal/testprotos/test/test.pb.go
@@ -36,7 +36,9 @@
}
func (x ForeignEnum) Enum() *ForeignEnum {
- return &x
+ p := new(ForeignEnum)
+ *p = x
+ return p
}
func (x ForeignEnum) String() string {
@@ -83,7 +85,9 @@
}
func (x TestReservedEnumFields) Enum() *TestReservedEnumFields {
- return &x
+ p := new(TestReservedEnumFields)
+ *p = x
+ return p
}
func (x TestReservedEnumFields) String() string {
@@ -139,7 +143,9 @@
}
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 @@
}
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 6a5fc07..52af12d 100644
--- a/internal/testprotos/test/test_import.pb.go
+++ b/internal/testprotos/test/test_import.pb.go
@@ -30,7 +30,9 @@
}
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 44d7e4a..8d082cd 100644
--- a/reflect/protoregistry/testprotos/test.pb.go
+++ b/reflect/protoregistry/testprotos/test.pb.go
@@ -30,7 +30,9 @@
}
func (x Enum1) Enum() *Enum1 {
- return &x
+ p := new(Enum1)
+ *p = x
+ return p
}
func (x Enum1) String() string {
@@ -77,7 +79,9 @@
}
func (x Enum2) Enum() *Enum2 {
- return &x
+ p := new(Enum2)
+ *p = x
+ return p
}
func (x Enum2) String() string {
@@ -124,7 +128,9 @@
}
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 5e436a3..3c38f61 100644
--- a/types/descriptor/descriptor.pb.go
+++ b/types/descriptor/descriptor.pb.go
@@ -93,7 +93,9 @@
}
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 @@
}
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 @@
}
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 @@
}
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 @@
}
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 @@
}
func (x MethodOptions_IdempotencyLevel) Enum() *MethodOptions_IdempotencyLevel {
- return &x
+ p := new(MethodOptions_IdempotencyLevel)
+ *p = x
+ return p
}
func (x MethodOptions_IdempotencyLevel) String() string {