Skip to content

Commit d056cc7

Browse files
habermancopybara-github
authored andcommitted
Document field number limits, including for MessageSet extensions.
Regular field numbers are limited to 2^29 - 1 because they share a tag with the wire type, but MessageSet extension numbers are encoded as a separate varint, so they are instead limited by the descriptor representation: the largest number an extension range can contain is 2^31 - 2. Add a Field Numbers section to go/proto-limits, and note in the proto2 and editions language guides that `max` is 2^31 - 2 for MessageSet messages. PiperOrigin-RevId: 1002463091
1 parent ffc09fe commit d056cc7

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

‎content/programming-guides/editions.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,10 @@ message Foo {
13191319
}
13201320
```
13211321

1322-
`max` is 2<sup>29</sup> - 1, or 536,870,911.
1322+
`max` is 2<sup>29</sup> - 1, or 536,870,911. For a message that uses
1323+
`message_set_wire_format` (MessageSet, which is deprecated), `max` is
1324+
2<sup>31</sup> - 2, or 2,147,483,646; see
1325+
[Proto Limits](/programming-guides/proto-limits#field-numbers).
13231326

13241327
### Choosing Extension Numbers {#choosing}
13251328

‎content/programming-guides/proto-limits.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,29 @@ To test this limitation, create a proto message with more than the upper bound
3232
number of fields and compile using a Java proto rule. The limit comes from JVM
3333
specs.
3434

35+
## Field Numbers {#field-numbers}
36+
37+
Regular fields can use numbers from 1 to 2<sup>29</sup> - 1 (536,870,911),
38+
because the field number shares a varint-encoded 32-bit tag with the 3-bit wire
39+
type. For more on this, see the
40+
[Encoding topic](/programming-guides/encoding#structure).
41+
42+
### MessageSet
43+
44+
Extensions of a message that uses `message_set_wire_format` (MessageSet) do not
45+
have this limit, because their number is not packed into a tag: it is encoded as
46+
a separate varint (`type_id`), so the wire format would allow numbers up to
47+
2<sup>32</sup> - 1. Two limits of the descriptor representation apply instead:
48+
49+
1. `FieldDescriptorProto.number` is an `int32`, so a number can be at most
50+
2<sup>31</sup> - 1 (`INT32_MAX`).
51+
2. `DescriptorProto.ExtensionRange.end` is an `int32` and is exclusive, so the
52+
largest number that any extension range can contain is 2<sup>31</sup> - 2
53+
(`INT32_MAX - 1`). `extensions 4 to max` on a MessageSet message resolves to
54+
this, and an explicit `extensions 4 to 2147483647` is rejected.
55+
56+
MessageSet is deprecated: do not use `message_set_wire_format` for new messages.
57+
3558
## Number of Values in an Enum {#enum}
3659

3760
The lowest limit is ~1700 values, in Java

‎content/programming-guides/proto2.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,10 @@ message Foo {
13611361
}
13621362
```
13631363

1364-
`max` is 2<sup>29</sup> - 1, or 536,870,911.
1364+
`max` is 2<sup>29</sup> - 1, or 536,870,911. For a message that uses
1365+
`message_set_wire_format` (MessageSet, which is deprecated), `max` is
1366+
2<sup>31</sup> - 2, or 2,147,483,646; see
1367+
[Proto Limits](/programming-guides/proto-limits#field-numbers).
13651368

13661369
### Choosing Extension Numbers {#choosing}
13671370

0 commit comments

Comments
 (0)