ariesdevil opened a new pull request, #3094: URL: https://github.com/apache/fory/pull/3094
<!-- **Thanks for contributing to Apache Fory™.** **If this is your first time opening a PR on fory, you can refer to [CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md).** Contribution Checklist - The **Apache Fory™** community has requirements on the naming of pr titles. You can also find instructions in [CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md). - Apache Fory™ has a strong focus on performance. If the PR you submit will have an impact on performance, please benchmark it first and provide the benchmark result here. --> ## Why? When computing struct version hash (fingerprint) for cross-language compatibility, Java and C++ treat enum fields as `nullable=true` by default (since Java enums are reference types that can be null). However, Rust's proc-macro cannot determine at compile time whether a field type is an enum, causing fingerprint mismatch between Rust and Java/C++. For example, a struct like: ```rust struct Message { state: MyEnum, // Java: nullable=true, Rust (before): nullable=false name: String, } ``` Would produce different version hashes in Rust vs Java/C++, breaking cross-language schema compatibility. ## What does this PR do? This PR aligns Rust's struct fingerprint computation with Java/C++ behavior: 1. **Add runtime enum detection for fingerprint computation** - Create `gen_struct_version_hash_ts()` that generates code to compute fingerprint at runtime - For user-defined types (TypeId::UNKNOWN at macro time), call `fory_static_type_id()` to detect enums - For known primitive types, use compile-time constants to avoid unnecessary trait bounds 2. **Add helper function `is_enum_type_id()`** - Checks if TypeId is ENUM, NAMED_ENUM, or UNION (Union-compatible Rust enums) 3. **Fix Union-compatible enum handling** - Add `xlang` parameter to `fory_actual_type_id()` for proper TypeId selection - Fix enum variant meta registration to handle Union-compatible enums - Fix `field_need_write_type_info()` to handle UNION TypeId Now Rust computes the same fingerprint as Java/C++. ## Related issues <!-- Is there any related issue? If this PR closes them you say say fix/closes: - #xxxx0 - #xxxx1 - Fixes #xxxx2 --> ## Does this PR introduce any user-facing change? <!-- If any user-facing interface changes, please [open an issue](https://github.com/apache/fory/issues/new/choose) describing the need to do so and update the document if necessary. Delete section if not applicable. --> - [ ] Does this PR introduce any public API change? - [x] Does this PR introduce any binary protocol compatibility change? **Note**: This changes the struct version hash computation for structs containing enum fields. The new hash will match Java/C++, but may differ from previous Rust versions. This is intentional for cross-language compatibility. ## Benchmark ## Others It's really hard to determine this bug :sob:... <!-- When the PR has an impact on performance (if you don't know whether the PR will have an impact on performance, you can submit the PR first, and if it will have impact on performance, the code reviewer will explain it), be sure to attach a benchmark data here. Delete section if not applicable. --> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
