1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
|
# Prototiller Requirements for Edition Zero
**Authors:** [@mkruskal-google](https://github.com/mkruskal-google)
**Approved:** 2023-07-07
## Background
[Edition Zero Features](edition-zero-features.md) lays out the design for our
first edition, which will unify proto2 and proto3 via features. In order to
migrate internal Google repositories (and aid OSS migrations), we will be
leveraging Prototiller to upgrade from legacy syntax to the new editions model.
We will also be using Prototiller for every edition bump, but that's out of
scope for this document (more general requirements are laid out in
[Prototiller Requirements for Editions](prototiller-reqs-for-editions.md)).
## Overview
The way the edition zero features were derived, there will always exist a no-op
transformation from proto2/proto3. However, the details of this transformation
can be fairly complex and depend on a lot of different factors.
A temporary script has been created as a placeholder, which manages to get
fairly good coverage of these rules. Notably, it can't handle groups inside
extensions or oneofs. This, along with its golden tests, can serve as a useful
benchmark for Prototiller.
### Feature Optimization
One important piece of Prototiller that will ease the friction of the Edition
Zero large-scale change is a feature optimization phase. If certain features
aren't necessary to make the upgrade a no-op, we shouldn't add them and should
instead rely on the edition defaults for future changes. Similarly, we should
try to minimize the total size of the change by collapsing a feature
specification to a higher level (e.g. file-level defaults of a field feature).
## Frontend Feature Transformations
This section details all of our frontend features and how to transform from
proto2/proto3 to edition zero.
### Field Presence
The `field_presence` feature defaults to `EXPLICIT`, which matches proto2/proto3
optional behavior. The `LEGACY_REQUIRED` value corresponds to proto2 required
fields, and `IMPLICIT` corresponds to non-optional proto3 fields. In order to
minimize changes, file-level defaults should be utilized.
Example transformations:
<table>
<tr>
<td>
<pre>syntax = "proto2";
message Foo {
optional string bar = 1;
required string baz = 2;
}</pre>
</td>
<td>
<pre>edition = "2023";
message Foo {
string bar = 1;
string baz = 2 [
features.field_presence = LEGACY_REQUIRED];
}</pre>
</td>
</tr>
</table>
<table>
<tr>
<td>
<pre>syntax = "proto3";
message Foo {
optional string bar = 1;
string baz = 2;
string bam = 3;
}</pre>
</td>
<td>
<pre>edition = "2023";
features.field_presence = IMPLICIT;
message Foo {
string bar = 1 [features.field_presence = EXPLICIT];
string baz = 2;
string bam = 3;
}</pre>
</td>
</tr>
</table>
<table>
<tr>
<td>
<pre>syntax = "proto3";
message Foo {
optional string bar = 1;
optional string baz = 2;
}</pre>
</td>
<td>
<pre>edition = "2023";
message Foo {
string bar = 1;
string baz = 2;
}</pre>
</td>
</tr>
</table>
### Enum Type
The `enum_type` feature defaults to `OPEN`, which matches proto3 behavior. The
`CLOSED` value corresponds to typical proto2 behavior. In order to minimize
changes, file-level defaults should be utilized.
Example transformations:
<table>
<tr>
<td>
<pre>syntax = "proto2";
enum Foo {
VALUE1 = 0;
VALUE2 = 1;
}</pre>
</td>
<td>
<pre>edition = "2023";
features.enum_type = CLOSED;
enum Foo {
VALUE1 = 0;
VALUE2 = 1;
}</pre>
</td>
</tr>
</table>
<table>
<tr>
<td>
<pre>syntax = "proto3";
enum Foo {
VALUE1 = 0;
VALUE2 = 1;
}</pre>
</td>
<td>
<pre>edition = "2023";
enum Foo {
VALUE1 = 0;
VALUE2 = 1;
}</pre>
</td>
</tr>
</table>
### Repeated Field Encoding
The `repeated_field_encoding` feature defaults to `PACKED`, which matches proto3
behavior. The `EXPANDED` value corresponds to the default proto2 behavior. Both
proto2 and proto3 can have the default behavior overridden by using the `packed`
field option. All of these should be replaced in the migration to edition zero.
Minimization of changes will be a little more complicated here, since there
could exist files where the majority of repeated fields have been overridden.
Example transformations:
<table>
<tr>
<td>
<pre>syntax = "proto2";
message Foo {
repeated int32 bar = 1;
repeated int32 baz = 2 [packed = true];
repeated int32 bam = 3;
}</pre>
</td>
<td>
<pre>edition = "2023";
features.repeated_field_encoding = EXPANDED;
message Foo {
repeated int32 bar = 1;
repeated int32 baz = 2 [
features.repeated_field_encoding = PACKED];
repeated int32 bar = 3;
}</pre>
</td>
</tr>
</table>
<table>
<tr>
<td>
<pre>syntax = "proto3";
message Foo {
repeated int32 bar = 1;
repeated int32 baz = 2 [packed = false];
}</pre>
</td>
<td>
<pre>edition = "2023";
message Foo {
repeated int32 bar = 2;
repeated int32 baz = 2 [
features.repeated_field_encoding = EXPANDED];
}</pre>
</td>
</tr>
</table>
<table>
<tr>
<td>
<pre>syntax = "proto2";
message Foo {
repeated int32 x = 1 [packed = true];
// Strings are never packed.
repeated string z = 1;
repeated string w = 2;
}</pre>
</td>
<td>
<pre>edition = "2023";
message Foo {
repeated int32 x = 1;
repeated string z = 1;
repeated string w = 2;
}</pre>
</td>
</tr>
</table>
### Message Encoding
The `message_encoding` feature is designed to replace the proto2-only `group`
syntax (with value `DELIMITED`), with a default that will always be
`LENGTH_PREFIXED`. This is a somewhat awkward transformation in the general
case, since we allow group definitions anywhere fields exist even if message
definitions can't. The basic transformation is to create a new message type in
the nearest enclosing scope with the same name as the field, and lowercase the
field and give it that type.
Example transformations:
<table>
<tr>
<td>
<pre>syntax = "proto2";
message Foo {
optional group Bar = 1 {
optional int32 x = 1;
}
optional Bar baz = 2;
}</pre>
</td>
<td>
<pre>edition = "2023";
message Foo {
message Bar {
int32 x = 1;
}
Bar bar = 1 [features.message_encoding = DELIMITED];
Bar baz = 2;
}</pre>
</td>
</tr>
</table>
<table>
<tr>
<td>
<pre>syntax = "proto2";
message Foo {
oneof foo {
group Bar = 1 {
optional int32 x = 1;
}
}
}</pre>
</td>
<td>
<pre>edition = "2023";
message Foo {
message Bar {
int32 x = 1;
}
oneof foo {
Bar bar = 1 [
features.message_encoding = DELIMITED];
}
}</pre>
</td>
</tr>
</table>
### JSON Format
The `json_format` feature is a bit of an outlier, because (at least for edition
zero) it only affects the frontend build of the proto file. The `ALLOW` value
(proto3 behavior) enables all JSON mapping conflict checks on field names,
unless `deprecated_legacy_json_field_conflicts` is set. The `LEGACY_BEST_EFFORT`
value (proto2 behavior) disables these checks. The ideal minimal transformation
would be to switch to `ALLOW` in all cases except where
`deprecated_legacy_json_field_conflicts` is set or there exist JSON mapping
conflicts. In those cases we can fallback to `LEGACY_BEST_EFFORT`.
Alternatively, if it's difficult for Prototiller to handle, we could do a
followup large-scale change to remove all `LEGACY_BEST_EFFORT` instances that
pass build.
Example transformations:
<table>
<tr>
<td>
<pre>syntax = "proto2";
message Foo {
optional string bar = 1;
optional string baz = 2;
}</pre>
</td>
<td>
<pre>edition = "2023";
message Foo {
string bar = 1;
string baz = 2;
}</pre>
</td>
</tr>
</table>
<table>
<tr>
<td>
<pre>syntax = "proto3";
message Foo {
string bar = 1;
string baz = 2;
}</pre>
</td>
<td>
<pre>edition = "2023";
features.field_presence = IMPLICIT;
message Foo {
string bar = 1;
string baz = 2;
}</pre>
</td>
</tr>
</table>
<table>
<tr>
<td>
<pre>syntax = "proto2";
message Foo {
// Warning only
string bar = 1;
string bar_ = 2;
}</pre>
</td>
<td>
<pre>edition = "2023";
features.json_format = LEGACY_BEST_EFFORT;
message Foo {
string bar = 1;
string bar_ = 2;
}</pre>
</td>
</tr>
</table>
<table>
<tr>
<td>
<pre>syntax = "proto3";
message Foo {
option
deprecated_legacy_json_field_conflicts = true;
string bar = 1;
string baz = 2 [json_name = "bar"];
}</pre>
</td>
<td>
<pre>edition = "2023";
features.field_presence = IMPLICIT;
features.json_format = LEGACY_BEST_EFFORT;
message Foo {
string bar = 1;
string baz = 2;
}</pre>
</td>
</tr>
</table>
## Backend Feature Transformations
This section details our backend-specific features and how to transform from
proto2/proto3 to edition zero.
In order to limit bloat, it would be ideal if we could check whether or not a
proto file is ever used to generate code in the target language. If it's not,
there's no reason to add backend-specific features.
### Legacy Closed Enum
Java and C++ by default treat proto3 enums as closed if they're used in a proto2
message. The internal `cc_open_enum` field option can override this, but it has
**very** limited use and may not be worth considering. While the enum type
behavior should still be determined by Enum Type, we'll need to add this feature
to proto2 files using proto3 messages (the reverse is disallowed).
Example transformations:
<table>
<tr>
<td>
<pre>syntax = "proto2";
import "some_proto3_file.proto"
enum Proto2Enum {
BAR = 0;
}
message Foo {
optional Proto3Enum bar = 1;
optional Proto2Enum baz = 2;
}</pre>
</td>
<td>
<pre>edition = "2023";
import "third_party/protobuf/cpp_features.proto"
import "third_party/protobuf/java_features.proto"
import "some_proto3_file.proto"
features.enum_type = CLOSED;
message Foo {
Proto3Enum bar = 1 [
features.(pb.cpp).legacy_closed_enum = true,
features.(pb.java).legacy_closed_enum = true];
Proto2Enum baz = 2;
}</pre>
</td>
</tr>
</table>
### UTF8 Validation
This feature is pending approval of *Editions Zero Feature: utf8_validation*
(not available externally).
## Other Transformations
In addition to features, there are some other changes we've made for edition
zero.
### Reserved Identifier Syntax
With *Protobuf Change Proposal: Reserved Identifiers* (not available
externally), we've decided to switch from strings to identifiers for reserved
fields. This *should* be a trivial change, but if the proto file contains
strings that aren't valid identifiers there's some ambiguity. They're currently
ignored today, but they could be typos we wouldn't want to just blindly delete.
So instead, we'll leave behind a comment.
Example transformations:
<table>
<tr>
<td>
<pre>syntax = "proto2";
message Foo {
reserved "bar", "baz";
}</pre>
</td>
<td>
<pre>edition = "2023";
message Foo {
reserved bar, baz;
}</pre>
</td>
</tr>
</table>
<table>
<tr>
<td>
<pre>syntax = "proto2";
message Foo {
reserved "bar", "1";
}</pre>
</td>
<td>
<pre>edition = "2023";
message Foo {
reserved bar;
/*reserved "1";*/
}</pre>
</td>
</tr>
</table>
|