Skip to content

[Clang] Better handle overload of explicit object member functions #147498

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ Bug Fixes to C++ Support
- Fix a bug where private access specifier of overloaded function not respected. (#GH107629)
- Correctly handle allocations in the condition of a ``if constexpr``.(#GH120197) (#GH134820)
- Fixed a crash when handling invalid member using-declaration in C++20+ mode. (#GH63254)
- Fix the handling of overload sets mixing explicit and implicit member functions. (#GH99902)

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
22 changes: 15 additions & 7 deletions clang/lib/Sema/SemaOverload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1483,8 +1483,8 @@ static bool IsOverloadOrOverrideImpl(Sema &SemaRef, FunctionDecl *New,
if (BS.Ty != DS.Ty)
return false;

if (Base->isLValueReferenceType())
return D->isLValueReferenceType();
if (Base->isLValueReferenceType() || D->isLValueReferenceType())
return Base->isLValueReferenceType() == D->isLValueReferenceType();
return Base->isRValueReferenceType() == D->isRValueReferenceType();
};

Expand Down Expand Up @@ -1536,10 +1536,18 @@ static bool IsOverloadOrOverrideImpl(Sema &SemaRef, FunctionDecl *New,
!F->isExplicitObjectMemberFunction();
};

if (IsImplicitWithNoRefQual(Old) != IsImplicitWithNoRefQual(New) &&
CompareType(OldObjectType.getNonReferenceType(),
NewObjectType.getNonReferenceType()))
return true;
// If one of the two function has no ref qualifier and the other one
// has a rvalue reference object, we need to adjust their types
// so that both functions are rvalue-reference qualified.
if (!IsImplicitWithNoRefQual(Old) && IsImplicitWithNoRefQual(New) &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Toss a quick comment explaining what is happening here/why we are doing this, else this is much improved.

OldObjectType->isRValueReferenceType())
NewObjectType =
SemaRef.getASTContext().getRValueReferenceType(NewObjectType);
else if (!IsImplicitWithNoRefQual(New) && IsImplicitWithNoRefQual(Old) &&
NewObjectType->isRValueReferenceType())
OldObjectType =
SemaRef.getASTContext().getRValueReferenceType(OldObjectType);

return CompareType(OldObjectType, NewObjectType);
}(OldMethod, NewMethod);

Expand Down Expand Up @@ -5922,7 +5930,7 @@ static ImplicitConversionSequence TryObjectArgumentInitialization(
if (Method->isExplicitObjectMemberFunction()) {
if (ExplicitParameterType.isNull())
ExplicitParameterType = Method->getFunctionObjectParameterReferenceType();
OpaqueValueExpr TmpExpr(Loc, FromType.getNonReferenceType(),
OpaqueValueExpr TmpExpr(Loc, FromType,
ValueKindFromClassification(FromClassification));
ImplicitConversionSequence ICS = TryCopyInitialization(
S, &TmpExpr, ExplicitParameterType, SuppressUserConversion,
Expand Down
43 changes: 43 additions & 0 deletions clang/test/SemaCXX/cxx2b-deducing-this.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1168,3 +1168,46 @@ struct S {
bool g() { return f(); } // expected-error {{no viable conversion from returned value of type 'S' to function return type 'bool'}}
};
}

namespace GH99902 {
struct A {
constexpr int f() const { return 1; }
constexpr int g() const & { return 1; }
constexpr int h() const && { return 1; }

constexpr int i() { return 1; }
constexpr int j() & { return 1; }
constexpr int k() && { return 1; }

};

struct B : A {
using A::f;
using A::g;
using A::h;
using A::i;
using A::j;
using A::k;
constexpr int f(this A) { return 2; }
constexpr int g(this A) { return 2; }
constexpr int h(this A) { return 2; }
constexpr int i(this A) { return 2; }
constexpr int j(this A) { return 2; }
constexpr int k(this A) { return 2; }
};

constexpr B b;
static_assert(B{}.f() == 1);
static_assert(B{}.g() == 1);
static_assert(B{}.h() == 1);
static_assert(B{}.i() == 1);
static_assert(B{}.j() == 2);
static_assert(B{}.k() == 1);
static_assert(b.f() == 1);
static_assert(b.g() == 1);
static_assert(b.h() == 2);
static_assert(b.i() == 2);
static_assert(b.j() == 2);
static_assert(b.k() == 2);

}