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

Unnamed bit field is being compared in defaulted equality operator #61335

Closed
foonathan opened this issue Mar 10, 2023 · 7 comments
Closed

Unnamed bit field is being compared in defaulted equality operator #61335

foonathan opened this issue Mar 10, 2023 · 7 comments
Assignees
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party

Comments

@foonathan
Copy link

#include <cassert>
#include <cstring>

struct foo
{
    int member : 8;
    int : 24;

    bool operator==(const foo&) const = default;
};

int main()
{
    // Create two objects with different object representations.
    foo a, b;
    std::memset(&a, 0xFF, sizeof(foo));
    std::memset(&b, 0x99, sizeof(foo));

    // Make all members identical to give them the same value representation.
    a.member = 0;
    b.member = 0;
    // This does not pass?!
    assert(a == b);
}

Demo: https://godbolt.org/z/75ee78Gss

Unnamed bit fields aren't members, so they shouldn't be compared.

@llvmbot
Copy link
Member

llvmbot commented Mar 10, 2023

@llvm/issue-subscribers-clang-codegen

@shafik
Copy link
Collaborator

shafik commented Mar 11, 2023

This does look like a bug, let me think a bit more before confirming.

@shafik
Copy link
Collaborator

shafik commented Mar 11, 2023

I agree this is a bug, we should not be including this in the comparison. I was wondering if because class.bit p2 says:

... Unnamed bit-fields are not members and cannot be initialized ...

That perhaps clang is assuming some sort of zero init and therefore since it could not change then it would be fair to include them in the comparison but looking at the code I don't see this.

My first take on fixing this would be to modify DefaultedComparisonVisitor member function visitSubobjects to include this check while iterating over the fields:

if (Field->isUnnamedBitfield())
        continue;

My initial test seems to indicate this does the job but I may be missing something. CC @zygoloid @AaronBallman

@AaronBallman AaronBallman added the confirmed Verified by a second party label Mar 13, 2023
@AaronBallman
Copy link
Collaborator

Based on my reading of:

https://eel.is/c++draft/class.compare.default#5.sentence-1
https://eel.is/c++draft/class.bit#2.sentence-2

I believe you're correct on the approach for the fix, as well.

@shafik
Copy link
Collaborator

shafik commented Mar 17, 2023

We have another bug that looks like it is fixed by my proposed fix for this, so I am going to run check-clang for this and if it passes I will put a PR together since this looks like a serious bug.

@shafik
Copy link
Collaborator

shafik commented Mar 17, 2023

@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed clang:codegen labels Mar 17, 2023
@llvmbot
Copy link
Member

llvmbot commented Mar 17, 2023

@llvm/issue-subscribers-clang-frontend

@shafik shafik closed this as completed in 6d0fab4 Apr 14, 2023
flemairen6 pushed a commit to Xilinx/llvm-project that referenced this issue May 10, 2023
…o compare unnamed bit-fields

If we look at class.bit p2 it tells us that that unnamed bit-fields are not
members and class.compare.default p5 tells us that we should only compare
non-static data members of the class.

This fixes: llvm#61335 and llvm#61417

Differential Revision: https://reviews.llvm.org/D146329
veselypeta pushed a commit to veselypeta/cherillvm that referenced this issue Aug 21, 2024
…o compare unnamed bit-fields

If we look at class.bit p2 it tells us that that unnamed bit-fields are not
members and class.compare.default p5 tells us that we should only compare
non-static data members of the class.

This fixes: llvm/llvm-project#61335 and llvm/llvm-project#61417

Differential Revision: https://reviews.llvm.org/D146329
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party
Projects
None yet
Development

No branches or pull requests

5 participants