diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -97,6 +97,11 @@ - The builtin function __builtin_dump_struct would crash clang when the target struct contains a bitfield. It now correctly handles bitfields. This fixes Issue `Issue 54462 `_. +- Overload resolution for constrained function templates could use the partial + order of constraints to select an overload, even if the parameter types of + the functions were different. It now diagnoses this case correctly as an + ambiguous call and an error. Fixes + `Issue 53640 `_. Improvements to Clang's diagnostics ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -3558,7 +3558,8 @@ QualType& ConvertedType); bool FunctionParamTypesAreEqual(const FunctionProtoType *OldType, const FunctionProtoType *NewType, - unsigned *ArgPos = nullptr); + unsigned *ArgPos = nullptr, + bool Reversed = false); void HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, QualType FromType, QualType ToType); @@ -8727,7 +8728,8 @@ FunctionTemplateDecl *getMoreSpecializedTemplate( FunctionTemplateDecl *FT1, FunctionTemplateDecl *FT2, SourceLocation Loc, TemplatePartialOrderingContext TPOC, unsigned NumCallArguments1, - unsigned NumCallArguments2, bool Reversed = false); + unsigned NumCallArguments2, bool Reversed = false, + bool AllowOrderingByConstraints = true); UnresolvedSetIterator getMostSpecialized(UnresolvedSetIterator SBegin, UnresolvedSetIterator SEnd, TemplateSpecCandidateSet &FailedCandidates, diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -2952,24 +2952,30 @@ } /// FunctionParamTypesAreEqual - This routine checks two function proto types -/// for equality of their argument types. Caller has already checked that -/// they have same number of arguments. If the parameters are different, +/// for equality of their parameter types. Caller has already checked that +/// they have same number of parameters. If the parameters are different, /// ArgPos will have the parameter index of the first different parameter. +/// If `Reversed` is true, the parameters of `NewType` will be compared in +/// reverse order. That's useful if one of the functions is being used as a C++20 +/// synthesized operator overload with a reversed parameter order. bool Sema::FunctionParamTypesAreEqual(const FunctionProtoType *OldType, const FunctionProtoType *NewType, - unsigned *ArgPos) { - for (FunctionProtoType::param_type_iterator O = OldType->param_type_begin(), - N = NewType->param_type_begin(), - E = OldType->param_type_end(); - O && (O != E); ++O, ++N) { + unsigned *ArgPos, bool Reversed) { + assert(OldType->getNumParams() == NewType->getNumParams() && + "Can't compare parameters of functions with different number of " + "parameters!"); + for (size_t I = 0; I < OldType->getNumParams(); I++) { + // Reverse iterate over the parameters of `OldType` if `Reversed` is true. + size_t J = Reversed ? (OldType->getNumParams() - I - 1) : I; + // Ignore address spaces in pointee type. This is to disallow overloading // on __ptr32/__ptr64 address spaces. - QualType Old = Context.removePtrSizeAddrSpace(O->getUnqualifiedType()); - QualType New = Context.removePtrSizeAddrSpace(N->getUnqualifiedType()); + QualType Old = Context.removePtrSizeAddrSpace(OldType->getParamType(I).getUnqualifiedType()); + QualType New = Context.removePtrSizeAddrSpace(NewType->getParamType(J).getUnqualifiedType()); if (!Context.hasSameType(Old, New)) { if (ArgPos) - *ArgPos = O - OldType->param_type_begin(); + *ArgPos = I; return false; } } @@ -9811,6 +9817,26 @@ if (Cand1IsSpecialization != Cand2IsSpecialization) return Cand2IsSpecialization; + // We're allowed to use constraints partial ordering only if the functions + // have the same parameters: + // [temp.func.order]p6.2.2 [...] or if the function parameters that + // positionally correspond between the two templates are not of the same type, + // neither template is more specialized than the other. + // [over.match.best]p2.6 + // F1 and F2 are non-template functions with the same parameter-type-lists, + // and F1 is more constrained than F2 [...] + bool CanCompareConstraints = false; + if (Cand1.Function && Cand2.Function && Cand1.Function->hasPrototype() && + Cand2.Function->hasPrototype()) { + auto *PT1 = cast(Cand1.Function->getFunctionType()); + auto *PT2 = cast(Cand2.Function->getFunctionType()); + if (PT1->getNumParams() == PT2->getNumParams() && + PT1->isVariadic() == PT2->isVariadic() && + S.FunctionParamTypesAreEqual(PT1, PT2, nullptr, + Cand1.isReversed() ^ Cand2.isReversed())) + CanCompareConstraints = true; + } + // -- F1 and F2 are function template specializations, and the function // template for F1 is more specialized than the template for F2 // according to the partial ordering rules described in 14.5.5.2, or, @@ -9822,34 +9848,27 @@ isa(Cand1.Function) ? TPOC_Conversion : TPOC_Call, Cand1.ExplicitCallArguments, Cand2.ExplicitCallArguments, - Cand1.isReversed() ^ Cand2.isReversed())) + Cand1.isReversed() ^ Cand2.isReversed(), CanCompareConstraints)) return BetterTemplate == Cand1.Function->getPrimaryTemplate(); } // -— F1 and F2 are non-template functions with the same // parameter-type-lists, and F1 is more constrained than F2 [...], - if (Cand1.Function && Cand2.Function && !Cand1IsSpecialization && - !Cand2IsSpecialization && Cand1.Function->hasPrototype() && - Cand2.Function->hasPrototype()) { - auto *PT1 = cast(Cand1.Function->getFunctionType()); - auto *PT2 = cast(Cand2.Function->getFunctionType()); - if (PT1->getNumParams() == PT2->getNumParams() && - PT1->isVariadic() == PT2->isVariadic() && - S.FunctionParamTypesAreEqual(PT1, PT2)) { - Expr *RC1 = Cand1.Function->getTrailingRequiresClause(); - Expr *RC2 = Cand2.Function->getTrailingRequiresClause(); - if (RC1 && RC2) { - bool AtLeastAsConstrained1, AtLeastAsConstrained2; - if (S.IsAtLeastAsConstrained(Cand1.Function, {RC1}, Cand2.Function, - {RC2}, AtLeastAsConstrained1) || - S.IsAtLeastAsConstrained(Cand2.Function, {RC2}, Cand1.Function, - {RC1}, AtLeastAsConstrained2)) - return false; - if (AtLeastAsConstrained1 != AtLeastAsConstrained2) - return AtLeastAsConstrained1; - } else if (RC1 || RC2) { - return RC1 != nullptr; - } + if (!Cand1IsSpecialization && !Cand2IsSpecialization && + CanCompareConstraints) { + Expr *RC1 = Cand1.Function->getTrailingRequiresClause(); + Expr *RC2 = Cand2.Function->getTrailingRequiresClause(); + if (RC1 && RC2) { + bool AtLeastAsConstrained1, AtLeastAsConstrained2; + if (S.IsAtLeastAsConstrained(Cand1.Function, {RC1}, Cand2.Function, {RC2}, + AtLeastAsConstrained1) || + S.IsAtLeastAsConstrained(Cand2.Function, {RC2}, Cand1.Function, {RC1}, + AtLeastAsConstrained2)) + return false; + if (AtLeastAsConstrained1 != AtLeastAsConstrained2) + return AtLeastAsConstrained1; + } else if (RC1 || RC2) { + return RC1 != nullptr; } } diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp --- a/clang/lib/Sema/SemaTemplateDeduction.cpp +++ b/clang/lib/Sema/SemaTemplateDeduction.cpp @@ -5143,18 +5143,20 @@ /// candidate with a reversed parameter order. In this case, the corresponding /// P/A pairs between FT1 and FT2 are reversed. /// +/// \param AllowOrderingByConstraints If \c is false, don't check whether one +/// of the templates is more constrained than the other. Default is true. +/// /// \returns the more specialized function template. If neither /// template is more specialized, returns NULL. -FunctionTemplateDecl * -Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, - FunctionTemplateDecl *FT2, - SourceLocation Loc, - TemplatePartialOrderingContext TPOC, - unsigned NumCallArguments1, - unsigned NumCallArguments2, - bool Reversed) { - - auto JudgeByConstraints = [&] () -> FunctionTemplateDecl * { +FunctionTemplateDecl *Sema::getMoreSpecializedTemplate( + FunctionTemplateDecl *FT1, FunctionTemplateDecl *FT2, SourceLocation Loc, + TemplatePartialOrderingContext TPOC, unsigned NumCallArguments1, + unsigned NumCallArguments2, bool Reversed, + bool AllowOrderingByConstraints) { + + auto JudgeByConstraints = [&]() -> FunctionTemplateDecl * { + if (!AllowOrderingByConstraints) + return nullptr; llvm::SmallVector AC1, AC2; FT1->getAssociatedConstraints(AC1); FT2->getAssociatedConstraints(AC2); diff --git a/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p6.cpp b/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p6.cpp new file mode 100644 --- /dev/null +++ b/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p6.cpp @@ -0,0 +1,49 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s + +struct A; +struct B; + +template constexpr bool True = true; +template concept C = True; + +void f(C auto &, auto &) = delete; +template void f(Q &, C auto &); + +void g(struct A *ap, struct B *bp) { + f(*ap, *bp); +} + +template struct X {}; + +template bool operator==(X, V) = delete; +template bool operator==(T, X); + +bool h() { + return X{} == 0; +} + +namespace PR53640 { + +template +concept C = true; + +template +void f(T t) {} // expected-note {{candidate function [with T = int]}} + +template +void f(const T &t) {} // expected-note {{candidate function [with T = int]}} + +int g() { + f(0); // expected-error {{call to 'f' is ambiguous}} +} + +struct S { + template explicit S(T) noexcept requires C {} // expected-note {{candidate constructor}} + template explicit S(const T &) noexcept {} // expected-note {{candidate constructor}} +}; + +int h() { + S s(4); // expected-error-re {{call to constructor of {{.*}} is ambiguous}} +} + +}