Skip to content

Commit fc43618

Browse files
committed
[Clang][SemaCXX] Preserve qualifiers in derived-to-base cast in defaulted comparison operators
1 parent d45de80 commit fc43618

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ Bug Fixes to C++ Support
216216
- Clang now preserves the unexpanded flag in a lambda transform used for pack expansion. (#GH56852), (#GH85667),
217217
(#GH99877).
218218
- Fixed a bug when diagnosing ambiguous explicit specializations of constrained member functions.
219+
- Fixed a bug where defaulted comparison operators would remove ``const`` from base classes. (#GH102588)
219220

220221
Bug Fixes to AST Handling
221222
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8414,10 +8414,12 @@ class DefaultedComparisonSynthesizer
84148414
if (Obj.first.isInvalid() || Obj.second.isInvalid())
84158415
return {ExprError(), ExprError()};
84168416
CXXCastPath Path = {Base};
8417-
return {S.ImpCastExprToType(Obj.first.get(), Base->getType(),
8418-
CK_DerivedToBase, VK_LValue, &Path),
8419-
S.ImpCastExprToType(Obj.second.get(), Base->getType(),
8420-
CK_DerivedToBase, VK_LValue, &Path)};
8417+
const auto CastToBase = [&](Expr *E) {
8418+
QualType ToType = S.Context.getQualifiedType(
8419+
Base->getType(), E->getType().getQualifiers());
8420+
return S.ImpCastExprToType(E, ToType, CK_DerivedToBase, VK_LValue, &Path);
8421+
};
8422+
return {CastToBase(Obj.first.get()), CastToBase(Obj.second.get())};
84218423
}
84228424

84238425
ExprPair getField(FieldDecl *Field) {

clang/test/SemaCXX/cxx20-default-compare.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// RUN: %clang_cc1 %s -std=c++23 -verify -Wfloat-equal
22

3+
#include "Inputs/std-compare.h"
4+
35
struct Foo {
46
float val;
57
bool operator==(const Foo &) const;
@@ -15,3 +17,51 @@ bool operator==(const Foo &, const Foo &) = default; // expected-warning {{comp
1517

1618
// Declare the defaulted comparison function as a non-member function. Arguments are passed by value.
1719
bool operator==(Foo, Foo) = default; // expected-warning {{comparing floating point with == or != is unsafe}} expected-note {{in defaulted equality comparison operator for 'Foo' first required here}}
20+
21+
namespace GH102588 {
22+
struct A {
23+
int i = 0;
24+
constexpr operator int() const { return i; }
25+
constexpr operator int&() { return ++i; }
26+
};
27+
28+
struct B : A {
29+
bool operator==(const B &) const = default;
30+
};
31+
32+
constexpr bool f() {
33+
B x;
34+
return x == x;
35+
}
36+
37+
static_assert(f());
38+
39+
struct ConstOnly {
40+
std::strong_ordering operator<=>(const ConstOnly&) const;
41+
std::strong_ordering operator<=>(ConstOnly&) = delete;
42+
friend bool operator==(const ConstOnly&, const ConstOnly&);
43+
friend bool operator==(ConstOnly&, ConstOnly&) = delete;
44+
};
45+
46+
struct MutOnly {
47+
std::strong_ordering operator<=>(const MutOnly&) const = delete;;
48+
std::strong_ordering operator<=>(MutOnly&);
49+
friend bool operator==(const MutOnly&, const MutOnly&) = delete;;
50+
friend bool operator==(MutOnly&, MutOnly&);
51+
};
52+
53+
struct ConstCheck : ConstOnly {
54+
friend std::strong_ordering operator<=>(const ConstCheck&, const ConstCheck&) = default;
55+
std::strong_ordering operator<=>(ConstCheck const& __restrict) const __restrict = default;
56+
friend bool operator==(const ConstCheck&, const ConstCheck&) = default;
57+
bool operator==(this const ConstCheck&, const ConstCheck&) = default;
58+
};
59+
60+
// FIXME: Non-reference explicit object parameter are rejected
61+
struct MutCheck : MutOnly {
62+
friend bool operator==(MutCheck, MutCheck) = default;
63+
// std::strong_ordering operator<=>(this MutCheck, MutCheck) = default;
64+
friend std::strong_ordering operator<=>(MutCheck, MutCheck) = default;
65+
// bool operator==(this MutCheck, MutCheck) = default;
66+
};
67+
}

0 commit comments

Comments
 (0)