A Protobuf Reserved Range Is a Pair With an Open Top (and the Number You Wrote the Line For Slips Through)
When you kill a field, the habit is to write a reserved line so nobody reuses the number. I wanted to know what a JavaScript parser actually does with that line, so I fed reserved forms into protobufjs and, for the compiler side, into protoc 25.3, and compared. The parser does not keep the number you wrote. A single, reserved 7, becomes the pair [7, 7]. A range, reserved 10 to 15, becomes [10, 15]. reserved 20 to max becomes [20, 536870911], and that top is not infinity, it is 2 to the 29 minus 1, the largest number a tag varint can carry. A name, reserved 'oldname', stays a string. It all lands on its own reserved key beside the fields map, not inside it, so a tool that diffs only the fields never sees it.
Now the check, and where it opens up. The parser tests a field number against each stored pair and throws only if the number is at or above the low end and strictly below the high end. The lower end counts, the upper end does not. reserved 7 is [7, 7], open on top, so a field at 7 slips through; I put reserved 7 next to a field at 7 and it parsed clean. Same for reserved 7, 8 and a field at 8, each single is its own open pair. protoc rejects both of those with Field a uses reserved number. So the first hole: a single number, which is the exact thing you write when you retire one field, does nothing in the parser.
The range is the same rule and the hole lands on the number you were protecting. reserved 10 to 15 is [10, 15]. A field at 10 throws, id 10 is reserved in Type B. A field at 12 throws. A field at 15 parses with no error, because 15 is not strictly below 15. The range guards ten through fourteen and lets the fifth, the one you wrote the line for, walk in. protoc checks both ends, so it rejects the field at 15 too. The range you typed to reserve five numbers is, in the parser, reserving four.
Name reservations open a different way, on order. reserved 'a' before a field named a throws, name a is reserved in Type G. Flip the two lines, field first and the reservation after, and it parses clean. Move the reserved line from below the field to above it and the same file changes from accepted to rejected. protoc rejects both orders. So a parser that accepts your file tells you less about the names than it does about the numbers, and only in the one line order that happens to be the polite one.
Put the two together and the direction of the gap is one-way. Every file protoc accepts, the parser accepts. The parser also accepts three files protoc throws out: a single number re-used, the top of a range re-used, and a name re-used when the reservation sits after the field. So a protoc-clean file will always parse in a JavaScript tool, but a file that parses cleanly in a JavaScript tool is not proof protoc will take it. That is the shape of the trust you can put in each side.
None of it changes the wire. Reserved is schema-only. Bytes that arrive at a reserved number decode as an unknown field and are dropped, no error, which is the point of the line: a service still sending the dead field reads as if the field was never there. I decoded a=5 plus a two-byte string at the reserved number two and got {a: 5} back, the string gone, nothing to flag it. The reserved-fields post is the retirement procedure, deprecate first, reserve in the same commit, and this is what happens underneath that habit when the parser is not protoc. Paste the file into proto.datajutsu.com and the reserved lines parse on every tab, and the breaking-changes post is where reusing a number becomes a break on the wire.