cmd/protoc-gen-go: reference proto.ProtoPackageIsVersionX
Include the compile-time assertion that the generated file is compatible
with the proto package it is being compiled against.
Change-Id: Iefa27ee3a99a6669a93303b6674b0033794ba0be
Reviewed-on: https://go-review.googlesource.com/134995
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/cmd/protoc-gen-go/main.go b/cmd/protoc-gen-go/main.go
index 1f24a1a..dc4ded3 100644
--- a/cmd/protoc-gen-go/main.go
+++ b/cmd/protoc-gen-go/main.go
@@ -22,6 +22,12 @@
"google.golang.org/proto/reflect/protoreflect"
)
+// generatedCodeVersion indicates a version of the generated code.
+// It is incremented whenever an incompatibility between the generated code and
+// proto package is introduced; the generated code references
+// a constant, proto.ProtoPackageIsVersionN (where N is generatedCodeVersion).
+const generatedCodeVersion = 2
+
const protoPackage = "github.com/golang/protobuf/proto"
func main() {
@@ -74,6 +80,15 @@
g.P()
g.P("package ", f.GoPackageName)
g.P()
+ g.P("// This is a compile-time assertion to ensure that this generated file")
+ g.P("// is compatible with the proto package it is being compiled against.")
+ g.P("// A compilation error at this line likely means your copy of the")
+ g.P("// proto package needs to be updated.")
+ g.P("const _ = ", protogen.GoIdent{
+ GoImportPath: protoPackage,
+ GoName: fmt.Sprintf("ProtoPackageIsVersion%d", generatedCodeVersion),
+ }, "// please upgrade the proto package")
+ g.P()
for _, enum := range f.Enums {
genEnum(gen, g, f, enum)