Keep track of a reasonable 'unused' enum value for closed enum languages.
commit97dd5935e2e2f67caaace888963b6ab2529ce13c
authorAlfred Fuller <afuller@meta.com>
Mon, 7 Nov 2022 22:40:10 +0000 (7 14:40 -0800)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Mon, 7 Nov 2022 22:40:10 +0000 (7 14:40 -0800)
treeb8ef78074f852328079c7c54e84ce3aa34fc8765
parent083802713e86e4d4b84f1888c4155f7e010a2edb
Keep track of a reasonable 'unused' enum value for closed enum languages.

Summary:
For use in closed-enum languages that need any unused value to represent 'Unrecognized'. The actual value doesn't matter, because it is not representative of that value, but rather that the real value can only be accessed via a function that returns the raw i32 value. For example:
```
enum MyOption {
  Option1 = 1,
}
```

might be handle as follows:
```
switch (val.option()) {
  case Option1:
    ...

  case MyOption.Unspecified:  // == 0, added by Thrift v1 automatically.
    throw new IllegalArgumentException("option must be specified")
  case MyOption.Unrecognized: // == ?, added by codegen.
    throw new NotImplementedException("unrecognized option: " + val.optionAsInt()); // The real value.
}

```

The actual implementation is (over) engineered to give the smallest unused value equal to or greater than 113 (a byte-sized unlikely prime number).
Also made `find by value` O(lnN) instead of O(N).

Reviewed By: robertroeser

Differential Revision: D40969276

fbshipit-source-id: f2ca3c75496c462a0e52d21852db68fa12ae73b3
third-party/thrift/src/thrift/compiler/ast/t_enum.cc
third-party/thrift/src/thrift/compiler/ast/t_enum.h
third-party/thrift/src/thrift/compiler/ast/t_enum_test.cc [new file with mode: 0644]