Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rust] Too many enum values leads to compile error #5267

Closed
tymcauley opened this issue Mar 27, 2019 · 3 comments
Closed

[Rust] Too many enum values leads to compile error #5267

tymcauley opened this issue Mar 27, 2019 · 3 comments

Comments

@tymcauley
Copy link
Contributor

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)
@rw
Copy link
Collaborator

rw commented Apr 27, 2019

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

@vglavnyy
Copy link
Contributor

This should be fixed by #5265.

@tymcauley
Copy link
Contributor Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants