Flatbuffers: [Rust] Too many enum values leads to compile error

Created on 27 Mar 2019  路  3Comments  路  Source: google/flatbuffers

I only tested this in Rust (since that was easiest for me), but I imagine this would cause issues in other languages as well.

If I define an enum in a FlatBuffer Schema file that has more fields that the underlying integral type can store, it leads to compiler errors in Rust (that is, flatc generates Rust code without error, but that generated code will not compile).

A quick test case:

  1. Create a new Rust project: $ cargo new fb-enum-test && cd fb-enum-test
  2. Add the flatbuffers crate to the project: $ echo 'flatbuffers = "0.5"' >> Cargo.toml
  3. Add this to the top of src/main.rs:
#[path = "./too_many_enums_generated.rs"]
mod too_many_enums_generated;            
  1. Create too_many_enums.fbs, which looks like this (not using the literal ... field, but filling in all 257 fields):
enum Foo: uint8 {
    Field0,
    Field1,
    ...
    Field255,
    Field256
}
  1. Compile the project with generated FlatBuffer code: $ flatc -r -o src too_many_enums.fbs && cargo build

This last step will yield the following Rust compilation error:

error[E0081]: discriminant value `0` already exists
   --> src/./too_many_enums_generated.rs:268:14
    |
12  |   Field0 = 0,
    |            - first use of `0`
...
268 |   Field256 = 256,
    |              ^^^ enum already has `0`

Platform details:

  • OS: macOS 10.14.4
  • flatc version:
$ flatc --version
flatc version 1.10.0 (Oct  5 2018 07:47:40)

Most helpful comment

This should be fixed by #5265.

All 3 comments

@tymcauley Looks like we need to update the schema parser -- this is upstream of the language ports. @aardappel ?

This should be fixed by #5265.

Sorry for losing track of this, you're right, this is now fixed, and results in a flatc error:

error: /path/to/too_many_enums.fbs:258: 13: error: enum value does not fit [0; 255]
$ flatc --version
flatc version 1.11.0

Thanks for the fix @vglavnyy!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Yooong-W picture Yooong-W  路  4Comments

dakom picture dakom  路  8Comments

lixin-wei picture lixin-wei  路  7Comments

barreyra picture barreyra  路  9Comments

vglavnyy picture vglavnyy  路  10Comments