Skip to content

Commit aba2075

Browse files
Add #[non_exhaustive] on the oneof-case enum
The enum for the oneof accessor (which holds a both which case was set and the value of that field as a tagged-union) was already marked non_exhaustive, but the Case enum (which only signals which field is set) was not. There are known tradeoffs to this choice: in other languages we are generally comfortable treating the oneof case enum as closed and letting people write exhaustive switches on the case enum if they want to. The main reason to do this in Rust is because people may use protobuf messages as their exposed API on a Rust library: adding a new field to a preexisting message should not demand a semver major version bump for that library. With messages, enums, non-oneof fields, and enum-values the gencode is already designed such that adding to it would not be a breaking change, but only for "fields in oneofs" adding a new field to the message would result in a semver breaking change and require a major version bump on that Rust crate API due unless we make the case enum non_exhaustive. PiperOrigin-RevId: 754905268
1 parent 9e9ecf6 commit aba2075

File tree

1 file changed

+1
-0
lines changed
  • src/google/protobuf/compiler/rust

1 file changed

+1
-0
lines changed

src/google/protobuf/compiler/rust/oneof.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ void GenerateOneofDefinition(Context& ctx, const OneofDescriptor& oneof) {
190190
R"rs(
191191
#[repr(C)]
192192
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
193+
#[non_exhaustive]
193194
#[allow(dead_code)]
194195
pub enum $case_enum_name$ {
195196
$cases$

0 commit comments

Comments
 (0)