<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/63192>63192</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
__is_trivially_equality_comparable gives wrong answer when inheritance is involved
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
AMP999
</td>
</tr>
</table>
<pre>
https://godbolt.org/z/ozWvKhddf
``` cpp
struct Alloc {
int a;
bool operator==(const Alloc&) const { return true; }
};
struct S : Alloc {
int b;
S(int a, int b) : Alloc(a), b(b) {}
bool operator==(const S&) const = default;
};
static_assert(__is_trivially_equality_comparable(S)); // oops!
int main() {
S a[] = { {1,1}, {1,2}, {1,3} };
S b[] = { {2,1}, {2,2}, {2,3} };
assert(a[0] == b[0]);
assert(a[1] == b[1]);
assert(a[2] == b[2]);
assert(!std::ranges::equal(a, b)); // oops!
}
```
The symptom is that `std::equal` and `std::ranges::equal` produce wrong answers for arrays of `S`.
The root cause is that Clang's (relatively new) builtin `__is_trivially_equality_comparable` incorrectly produces `true` for `S` when it should produce `false`. Even though all of `S`'s data members are trivial, the defaulted `S::operator==` isn't trivial because its base class `Alloc::operator==` isn't trivial. I think `QualType::isTriviallyEqualityComparableType` needs to be updated to check base classes as well as members.
Thanks to @Quuxplusone for finding this bug
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVctu4zYU_ZrrzcUYFGXrsfDCjmOgKAp0kABdGpR0LbGhSZUPp56vLyjJSZQ0mQEMyHycw3PugxTOyVYTbWC9g_V-IYLvjN1s__izLMtFZZrrpvO-d5BugR-AH1rTVEb5pbEt8MMP4Afz46_L713TnIDtgW0hY-MP674fpxARnbeh9rhVytQI-e51BVFqjwLS2VxljELTkxXeWEj38ceL2mg3kQDPgJc4zkC-Q0s-WI3eBoJ0h5DvX_ni4C3_pOYBId1-rql6p-kBeDFq5XfTBl6-UgA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