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

Equality operator returns false for an equal empty union #457

Open
jordanLatta155 opened this issue Nov 6, 2023 · 3 comments
Open

Equality operator returns false for an equal empty union #457

jordanLatta155 opened this issue Nov 6, 2023 · 3 comments

Comments

@jordanLatta155
Copy link

I have an IDL union with an enum discriminator with one of the enum cases being empty. The generated C++ code's == operator will return false when comparing two "empty" unions , even if their discriminator is the same.

example:
IDL:

enum AllocationState {
    Free,
    Allocated,
    Offline
};

union AllocationStateUnion switch (AllocationState) {
    case Allocated:
    case Offline:
        string reason;
};

C++:

bool operator==(const AllocationStateUnion& _other) const
{
  if(_d() != _other._d()) return false;
    switch(_d()) {
      case AllocationState::Allocated:
      case AllocationState::Offline:
        return reason() == _other.reason();
  }
  return false;
}

The last return false in the generated C++ is the branched reached when both _d() are equal to AllocationState::Free.

@eboasson
Copy link
Contributor

eboasson commented Nov 6, 2023

Interesting ... makes me wonder if it shouldn't simply end in return true; instead. @reicheratwork am I barking up the right tree?

@reicheratwork
Copy link
Contributor

Let me have a look at this

reicheratwork added a commit to reicheratwork/cyclonedds-cxx that referenced this issue Nov 6, 2023
This fixes the issue of comparison operators not returning true for
unions with default cases not associated with a type for said default
case
Also added unittests for union comparison operators

Signed-off-by: Martijn Reicher <[email protected]>
@reicheratwork
Copy link
Contributor

Also found some other issues that were exposed by creating a unittest that used unions with default cases, fixing those as well :)

@jordanLatta155 , can I count on you to verify my fix and review my PR when I create it?

reicheratwork added a commit to reicheratwork/cyclonedds-cxx that referenced this issue Nov 7, 2023
This fixes the issue of comparison operators not returning true for
unions with default cases not associated with a type for said default
case
Also added unittests for union comparison operators

Signed-off-by: Martijn Reicher <[email protected]>
reicheratwork added a commit to reicheratwork/cyclonedds-cxx that referenced this issue Nov 7, 2023
This fixes the issue of comparison operators not returning true for
unions with default cases not associated with a type for said default
case
Also added unittests for union comparison operators

Signed-off-by: Martijn Reicher <[email protected]>
eboasson pushed a commit that referenced this issue Nov 9, 2023
This fixes the issue of comparison operators not returning true for
unions with default cases not associated with a type for said default
case
Also added unittests for union comparison operators

Signed-off-by: Martijn Reicher <[email protected]>
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