Add compatibility markers to grpc generated code.
This asserts at build time that the generated code is compatible with
the grpc package that it is linked to. It is analogous to the similar
constant that asserts compatibility with regular proto generated code.
diff --git a/protoc-gen-go/internal/grpc/grpc.go b/protoc-gen-go/internal/grpc/grpc.go
index f33f701..a11e07b 100644
--- a/protoc-gen-go/internal/grpc/grpc.go
+++ b/protoc-gen-go/internal/grpc/grpc.go
@@ -44,6 +44,12 @@
"github.com/golang/protobuf/protoc-gen-go/generator"
)
+// generatedCodeVersion indicates a version of the generated code.
+// It is incremented whenever an incompatibility between the generated code and
+// the grpc package is introduced; the generated code references
+// a constant, grpc.SupportPackageIsVersionN (where N is generatedCodeVersion).
+const generatedCodeVersion = 1
+
// Paths for packages used by code generated in this file,
// relative to the import_prefix of the generator.Generator.
const (
@@ -101,10 +107,18 @@
if len(file.FileDescriptorProto.Service) == 0 {
return
}
+
g.P("// Reference imports to suppress errors if they are not otherwise used.")
g.P("var _ ", contextPkg, ".Context")
g.P("var _ ", grpcPkg, ".ClientConn")
g.P()
+
+ // Assert version compatibility.
+ g.P("// This is a compile-time assertion to ensure that this generated file")
+ g.P("// is compatible with the grpc package it is being compiled against.")
+ g.P("const _ = ", grpcPkg, ".SupportPackageIsVersion", generatedCodeVersion)
+ g.P()
+
for i, service := range file.FileDescriptorProto.Service {
g.generateService(file, service, i)
}