fix(compiler): support C++ equality codegen for std::any in collections and unions#3810
Open
BaldDemian wants to merge 1 commit into
Open
fix(compiler): support C++ equality codegen for std::any in collections and unions#3810BaldDemian wants to merge 1 commit into
BaldDemian wants to merge 1 commit into
Conversation
Collaborator
|
whether we should not generate equals method when message have any field for c++? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why?
Fory IDL
anymaps tostd::anyin generated C++ code.std::anydoes not defineoperator==, so generated equality code MUST NOT ask the C++ standard library to compare twostd::anydirectly.But the current C++ compiler still does this in some generated
operator==implementations, especially whenanyappeared inside standard-comparable containers or union alternatives.For example, generated code such as
values_ == other.values_forstd::vector<std::any>,by_name_ == other.by_name_forstd::unordered_map<std::string, std::any>, orvalue_ == other.value_forstd::variant<std::any, ...>causes the standardcontainer or variant equality operator to instantiate
std::any == std::any.This causes compilation error like:
The Rust compiler avoids the equivalent derived-equality problem by not deriving comparison traits for generated types that contain
any:fory/compiler/fory_compiler/generators/rust.py
Lines 1149 to 1158 in 9b2bcec
The C++ compiler already had an approximate comparison strategy for direct
std::anyfields instead of trying to compare the stored values:fory/compiler/fory_compiler/generators/cpp.py
Lines 689 to 700 in 9b2bcec
What does this PR do?
This PR extends existing approximate comparison approach to every generated equality path that may contain
std::any.The C++ compiler now recursively detects whether a field type contains
any;fields without
anykeep the existing direct comparison, while fields withanyuse generated approximate comparison.optionalfields compare presence first, then recursively compare the contained value when both sides are present.lists andarrays compare size first, then recursively compare elements in order.maps compare size first, then require every left key to exist in the right map and recursively compare the corresponding valuesunions compare the active variant index first, then dispatch to the same recursive comparison rule for the active alternative.Related issues
N/A.
AI Contribution Checklist
noDoes this PR introduce any user-facing change?
N/A.
Benchmark
N/A.