cmd/protoc-gen-go: generate Enum method even on proto3

A proto2 message can reference a proto3 enum. It is currently cumbersome
to set proto3 enums in proto2 messages. Add the Enum method in all situations
to support this situation. It also removes yet another point of divergence
between proto2 and proto3.

We could consider removing this method if Go ever gets generics,
but that hypothetical future is long ways away.

Change-Id: Ib83bc87e46b49f3271b90bacb2893bb8e278e4f2
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177257
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 6a48126..397467a 100644
--- a/cmd/protoc-gen-go/internal_gengo/main.go
+++ b/cmd/protoc-gen-go/internal_gengo/main.go
@@ -266,14 +266,17 @@
 	}
 
 	// Enum method.
-	if enum.Desc.Syntax() != protoreflect.Proto3 {
-		g.P("func (x ", enum.GoIdent, ") Enum() *", enum.GoIdent, " {")
-		g.P("p := new(", enum.GoIdent, ")")
-		g.P("*p = x")
-		g.P("return p")
-		g.P("}")
-		g.P()
-	}
+	//
+	// NOTE: A pointer value is needed to represent presence in proto2.
+	// Since a proto2 message can reference a proto3 enum, it is useful to
+	// always generate this method (even on proto3 enums) to support that case.
+	g.P("func (x ", enum.GoIdent, ") Enum() *", enum.GoIdent, " {")
+	g.P("p := new(", enum.GoIdent, ")")
+	g.P("*p = x")
+	g.P("return p")
+	g.P("}")
+	g.P()
+
 	// String method.
 	g.P("func (x ", enum.GoIdent, ") String() string {")
 	g.P("return ", protoimplPackage.Ident("X"), ".EnumStringOf(x.Descriptor(), ", protoreflectPackage.Ident("EnumNumber"), "(x))")