cmd/protoc-gen-go, runtime/protoimpl: enforce minimum and maximum versions
Generate the needed infrastructure to ensure that we can statically
enforce minimum and maximum versions. This enables us to have a policy
when we release v2 where it fails to build for:
* new generated code with really old runtimes
* new runtimes with really old generated code
Change-Id: Ib699ad62c06dff8f9285806394a741c18db00288
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/178546
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 397467a..a17af06 100644
--- a/cmd/protoc-gen-go/internal_gengo/main.go
+++ b/cmd/protoc-gen-go/internal_gengo/main.go
@@ -20,15 +20,11 @@
"google.golang.org/protobuf/internal/encoding/tag"
"google.golang.org/protobuf/internal/fieldnum"
"google.golang.org/protobuf/reflect/protoreflect"
+ "google.golang.org/protobuf/runtime/protoimpl"
"google.golang.org/protobuf/types/descriptorpb"
)
-// minimumVersion is minimum version of the v2 proto package that is required.
-// This is incremented every time the generated code relies on some property
-// in the proto package that was introduced in a later version.
-const minimumVersion = 0
-
const (
// generateEnumMapVars specifies whether to generate enum maps,
// which provide a bi-directional mapping between enum numbers and names.
@@ -110,7 +106,12 @@
g.P()
// Emit a static check that enforces a minimum version of the proto package.
- g.P("const _ = ", protoimplPackage.Ident("EnforceVersion"), "(", protoimplPackage.Ident("Version"), " - ", minimumVersion, ")")
+ g.P("const (")
+ g.P("// Verify that runtime/protoimpl is sufficiently up-to-date.")
+ g.P("_ = ", protoimplPackage.Ident("EnforceVersion"), "(", protoimplPackage.Ident("MaxVersion"), " - ", protoimpl.Version, ")")
+ g.P("// Verify that this generated code is sufficiently up-to-date.")
+ g.P("_ = ", protoimplPackage.Ident("EnforceVersion"), "(", protoimpl.Version, " - ", protoimplPackage.Ident("MinVersion"), ")")
+ g.P(")")
g.P()
for i, imps := 0, f.Desc.Imports(); i < imps.Len(); i++ {