-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[clang] Fix overload resolution ranking of inherited constructors #132830
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
e458a6b
bd15fc4
9eb3b12
5a1f4c1
531eba3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ | |
#include "llvm/ADT/ScopeExit.h" | ||
#include "llvm/ADT/SmallPtrSet.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/Support/Casting.h" | ||
#include <algorithm> | ||
#include <cassert> | ||
#include <cstddef> | ||
|
@@ -10724,15 +10725,14 @@ bool clang::isBetterOverloadCandidate( | |
// -- F1 is a constructor for a class D, F2 is a constructor for a base | ||
// class B of D, and for all arguments the corresponding parameters of | ||
// F1 and F2 have the same type. | ||
// FIXME: Implement the "all parameters have the same type" check. | ||
bool Cand1IsInherited = | ||
isa_and_nonnull<ConstructorUsingShadowDecl>(Cand1.FoundDecl.getDecl()); | ||
bool Cand2IsInherited = | ||
isa_and_nonnull<ConstructorUsingShadowDecl>(Cand2.FoundDecl.getDecl()); | ||
if (Cand1IsInherited != Cand2IsInherited) | ||
return Cand2IsInherited; | ||
else if (Cand1IsInherited) { | ||
assert(Cand2IsInherited); | ||
if (isa_and_nonnull<CXXConstructorDecl>(Cand1.Function) && | ||
isa_and_nonnull<CXXConstructorDecl>(Cand2.Function) && | ||
llvm::equal(Cand1.Function->parameters().take_front(NumArgs), | ||
Cand2.Function->parameters().take_front(NumArgs), | ||
[&](ParmVarDecl *P1, ParmVarDecl *P2) { | ||
return S.Context.hasSameUnqualifiedType(P1->getType(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have a test case for the basic situation:
I believe that case will work with your patch, but it would be interesting to swap There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to ignore cv-qualifiers here because their deletion, unlike other parameter transformations, only affects the function's parameter-type-list but not the parameter declaration itself. I added a comment. |
||
P2->getType()); | ||
})) { | ||
auto *Cand1Class = cast<CXXRecordDecl>(Cand1.Function->getDeclContext()); | ||
auto *Cand2Class = cast<CXXRecordDecl>(Cand2.Function->getDeclContext()); | ||
if (Cand1Class->isDerivedFrom(Cand2Class)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,6 +169,20 @@ B b; | |
// since-cxx11-error@-1 {{call to implicitly-deleted default constructor of 'B'}} | ||
// since-cxx11-note@#cwg2273-B {{default constructor of 'B' is implicitly deleted because base class 'A' has a deleted default constructor}} | ||
// since-cxx11-note@#cwg2273-A {{'A' has been explicitly marked deleted here}} | ||
|
||
Endilll marked this conversation as resolved.
Show resolved
Hide resolved
|
||
struct X { | ||
X(float); // since-cxx11-note {{candidate inherited constructor}} | ||
X(void*, int = 0) = delete; | ||
}; | ||
|
||
struct Y : X { | ||
using X::X; // since-cxx11-note {{constructor from base class 'X' inherited here}} | ||
Y(double); // since-cxx11-note {{candidate constructor}} | ||
Y(void* const, long = 1); | ||
}; | ||
|
||
Y y = 1; // since-cxx11-error {{conversion from 'int' to 'Y' is ambiguous}} | ||
Endilll marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Y z = nullptr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The wording says "same type", but here you have two constructors, where their first parameter differs in cv-qualification. I suspect this case falls under the usual "exact match is better than qualification conversion", and doesn't need this tie-breaker, so this case is not related to CWG2273 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Formally speaking, what matters here is that adjusted parameters types form parameter-type-list, which overload resolution seems to be concerned with (https://eel.is/c++draft/over#match.general-1, even if I don't really trust this wording). But you are correct. Then, if Also, I think it would be worth leaving a comment that default arguments do not contribute to the set of arguments that [over.match.best] considers. (Instead, parameters which have default arguments are discarded per https://eel.is/c++draft/over#match.viable-2.3.sentence-2) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added tests for array/function parameters. The wording related to default arguments is already quoted elsewhere in the code, I'm not sure there's a need to repeat it. (Or did you mean it should be mentioned in the test case?) |
||
#endif | ||
} // namespace cwg2273 | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don;t think you need to add this include.