[clang] [Clang][RFC] Bypass TAD during overload resolution if a perfect match exists (PR #133426)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 3 06:54:24 PDT 2025
https://github.com/cor3ntin updated https://github.com/llvm/llvm-project/pull/133426
>From f71efb36518ea09f1d6daf2b87ffffa246516bf6 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Thu, 27 Mar 2025 16:25:07 +0100
Subject: [PATCH 01/18] [Clang][WIP][RFC] Bypass TAD during overload resolution
if a perfect match exists
This implements the same overload resolution behavior as GCC,
as described in https://wg21.link/p3606 (section 1-2, not 3)
If during overload resolution, there is a non-template candidate
that would be always be picked - because each of the argument
is a perfect match (ie the source and target types are the same),
we do not perform deduction for any template candidate
that might exists.
The goal is to be able to merge #122423 without being too disruptive.
This change means that the selection of the best viable candidate and
template argument deduction become interleaved.
To avoid rewriting half of Clang we store in `OverloadCandidateSet`
enough information to be able to deduce template candidates from
`OverloadCandidateSet::BestViableFunction`. Which means
the lifetime of any object used by template argument must outlive
a call to `Add*Template*Candidate`.
This two phase resolution is not performed for some initialization
as there are cases where template candidate are better match
in these cases per the standard. It's also bypassed for code completion.
The change has a nice impact on compile times
https://llvm-compile-time-tracker.com/compare.php?from=719b029c16eeb1035da522fd641dfcc4cee6be74&to=bf7041045c9408490c395230047c5461de72fc39&stat=instructions%3Au
Fixes #62096
Fixes #74581
---
clang/include/clang/Sema/Overload.h | 130 +++++++++++++-
clang/include/clang/Sema/Sema.h | 25 +++
clang/lib/Sema/SemaCodeComplete.cpp | 6 +-
clang/lib/Sema/SemaInit.cpp | 11 +-
clang/lib/Sema/SemaOverload.cpp | 263 +++++++++++++++++++++++++---
5 files changed, 395 insertions(+), 40 deletions(-)
diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h
index 6e08762dcc6d7..2cc7e1809e26c 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -38,6 +38,7 @@
#include <cstddef>
#include <cstdint>
#include <utility>
+#include <variant>
namespace clang {
@@ -743,6 +744,12 @@ class Sema;
Standard.setAllToTypes(T);
}
+ bool isPerfect(const ASTContext &C) const {
+ return (isStandard() && Standard.isIdentityConversion() &&
+ C.hasSameType(Standard.getFromType(), Standard.getToType(2))) ||
+ getKind() == StaticObjectArgumentConversion;
+ }
+
// True iff this is a conversion sequence from an initializer list to an
// array or std::initializer.
bool hasInitializerListContainerType() const {
@@ -979,6 +986,18 @@ class Sema;
return false;
}
+ bool isPerfectMatch(const ASTContext &Ctx) const {
+ if (!Viable)
+ return false;
+ for (auto &C : Conversions) {
+ if (!C.isInitialized())
+ return false;
+ if (!C.isPerfect(Ctx))
+ return false;
+ }
+ return true;
+ }
+
bool TryToFixBadConversion(unsigned Idx, Sema &S) {
bool CanFix = Fix.tryToFixConversion(
Conversions[Idx].Bad.FromExpr,
@@ -1015,6 +1034,61 @@ class Sema;
RewriteKind(CRK_None) {}
};
+ struct NonDeducedConversionTemplateOverloadCandidate {
+ FunctionTemplateDecl *FunctionTemplate;
+ DeclAccessPair FoundDecl;
+ CXXRecordDecl *ActingContext;
+ Expr *From;
+ QualType ToType;
+
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned AllowObjCConversionOnExplicit : 1;
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned AllowExplicit : 1;
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned AllowResultConversion : 1;
+ };
+
+ struct NonDeducedMethodTemplateOverloadCandidate {
+ FunctionTemplateDecl *FunctionTemplate;
+ DeclAccessPair FoundDecl;
+ ArrayRef<Expr *> Args;
+ CXXRecordDecl *ActingContext;
+ Expr::Classification ObjectClassification;
+ QualType ObjectType;
+
+ OverloadCandidateParamOrder PO;
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned SuppressUserConversions : 1;
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned PartialOverloading : 1;
+ };
+
+ struct NonDeducedFunctionTemplateOverloadCandidate {
+ FunctionTemplateDecl *FunctionTemplate;
+ DeclAccessPair FoundDecl;
+ ArrayRef<Expr *> Args;
+
+ CallExpr::ADLCallKind IsADLCandidate;
+ OverloadCandidateParamOrder PO;
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned SuppressUserConversions : 1;
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned PartialOverloading : 1;
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned AllowExplicit : 1;
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned AggregateCandidateDeduction : 1;
+ };
+
+ using NonDeducedTemplateOverloadCandidate =
+ std::variant<NonDeducedConversionTemplateOverloadCandidate,
+ NonDeducedMethodTemplateOverloadCandidate,
+ NonDeducedFunctionTemplateOverloadCandidate>;
+
+ static_assert(
+ std::is_trivially_destructible_v<NonDeducedTemplateOverloadCandidate>);
+
/// OverloadCandidateSet - A set of overload candidates, used in C++
/// overload resolution (C++ 13.3).
class OverloadCandidateSet {
@@ -1043,6 +1117,8 @@ class Sema;
/// C++ [over.match.call.general]
/// Resolve a call through the address of an overload set.
CSK_AddressOfOverloadSet,
+
+ CSK_CodeCompletion,
};
/// Information about operator rewrites to consider when adding operator
@@ -1116,6 +1192,7 @@ class Sema;
private:
SmallVector<OverloadCandidate, 16> Candidates;
llvm::SmallPtrSet<uintptr_t, 16> Functions;
+ SmallVector<NonDeducedTemplateOverloadCandidate, 8> NonDeducedCandidates;
// Allocator for ConversionSequenceLists. We store the first few of these
// inline to avoid allocation for small sets.
@@ -1126,7 +1203,7 @@ class Sema;
OperatorRewriteInfo RewriteInfo;
constexpr static unsigned NumInlineBytes =
- 24 * sizeof(ImplicitConversionSequence);
+ 32 * sizeof(ImplicitConversionSequence);
unsigned NumInlineBytesUsed = 0;
alignas(void *) char InlineSpace[NumInlineBytes];
@@ -1144,8 +1221,8 @@ class Sema;
// It's simpler if this doesn't need to consider alignment.
static_assert(alignof(T) == alignof(void *),
"Only works for pointer-aligned types.");
- static_assert(std::is_trivial<T>::value ||
- std::is_same<ImplicitConversionSequence, T>::value,
+ static_assert(std::is_trivially_destructible_v<T> ||
+ (std::is_same_v<ImplicitConversionSequence, T>),
"Add destruction logic to OverloadCandidateSet::clear().");
unsigned NBytes = sizeof(T) * N;
@@ -1199,8 +1276,12 @@ class Sema;
iterator begin() { return Candidates.begin(); }
iterator end() { return Candidates.end(); }
- size_t size() const { return Candidates.size(); }
- bool empty() const { return Candidates.empty(); }
+ size_t size() const {
+ return Candidates.size() + NonDeducedCandidates.size();
+ }
+ bool empty() const {
+ return Candidates.empty() && NonDeducedCandidates.empty();
+ }
/// Allocate storage for conversion sequences for NumConversions
/// conversions.
@@ -1216,6 +1297,19 @@ class Sema;
return ConversionSequenceList(Conversions, NumConversions);
}
+ llvm::MutableArrayRef<Expr *> getPersistentArgsArray(unsigned N) {
+ Expr **Exprs = slabAllocate<Expr *>(N);
+ return llvm::MutableArrayRef<Expr *>(Exprs, N);
+ }
+
+ template <typename... T>
+ llvm::MutableArrayRef<Expr *> getPersistentArgsArray(T *...Exprs) {
+ llvm::MutableArrayRef<Expr *> Arr =
+ getPersistentArgsArray(sizeof...(Exprs));
+ llvm::copy(std::initializer_list<Expr *>{Exprs...}, Arr.data());
+ return Arr;
+ }
+
/// Add a new candidate with NumConversions conversion sequence slots
/// to the overload set.
OverloadCandidate &addCandidate(unsigned NumConversions = 0,
@@ -1231,10 +1325,36 @@ class Sema;
return C;
}
+ void AddNonDeducedTemplateCandidate(
+ FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
+ ArrayRef<Expr *> Args, bool SuppressUserConversions,
+ bool PartialOverloading, bool AllowExplicit,
+ CallExpr::ADLCallKind IsADLCandidate, OverloadCandidateParamOrder PO,
+ bool AggregateCandidateDeduction);
+
+ void AddNonDeducedMethodTemplateCandidate(
+ FunctionTemplateDecl *MethodTmpl, DeclAccessPair FoundDecl,
+ CXXRecordDecl *ActingContext, QualType ObjectType,
+ Expr::Classification ObjectClassification, ArrayRef<Expr *> Args,
+ bool SuppressUserConversions, bool PartialOverloading,
+ OverloadCandidateParamOrder PO);
+
+ void AddNonDeducedConversionTemplateCandidate(
+ FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
+ CXXRecordDecl *ActingContext, Expr *From, QualType ToType,
+ bool AllowObjCConversionOnExplicit, bool AllowExplicit,
+ bool AllowResultConversion);
+
+ void InjectNonDeducedTemplateCandidates(Sema &S);
+
/// Find the best viable function on this overload set, if it exists.
OverloadingResult BestViableFunction(Sema &S, SourceLocation Loc,
OverloadCandidateSet::iterator& Best);
+ OverloadingResult
+ BestViableFunctionImpl(Sema &S, SourceLocation Loc,
+ OverloadCandidateSet::iterator &Best);
+
SmallVector<OverloadCandidate *, 32> CompleteCandidates(
Sema &S, OverloadCandidateDisplayKind OCD, ArrayRef<Expr *> Args,
SourceLocation OpLoc = SourceLocation(),
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index a62562bb134f5..791617e92616d 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -60,6 +60,7 @@
#include "clang/Sema/DeclSpec.h"
#include "clang/Sema/ExternalSemaSource.h"
#include "clang/Sema/IdentifierResolver.h"
+#include "clang/Sema/Overload.h"
#include "clang/Sema/Ownership.h"
#include "clang/Sema/ParsedAttr.h"
#include "clang/Sema/Redeclaration.h"
@@ -10344,9 +10345,26 @@ class Sema final : public SemaBase {
OverloadCandidateSet &CandidateSet, bool SuppressUserConversions = false,
bool PartialOverloading = false, OverloadCandidateParamOrder PO = {});
+ void AddMethodTemplateCandidateImmediately(
+ OverloadCandidateSet &CandidateSet, FunctionTemplateDecl *MethodTmpl,
+ DeclAccessPair FoundDecl, CXXRecordDecl *ActingContext,
+ TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ObjectType,
+ Expr::Classification ObjectClassification, ArrayRef<Expr *> Args,
+ bool SuppressUserConversions, bool PartialOverloading,
+ OverloadCandidateParamOrder PO);
+
/// Add a C++ function template specialization as a candidate
/// in the candidate set, using template argument deduction to produce
/// an appropriate function template specialization.
+
+ void AddTemplateOverloadCandidateImmediately(
+ OverloadCandidateSet &CandidateSet,
+ FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
+ TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args,
+ bool SuppressUserConversions, bool PartialOverloading, bool AllowExplicit,
+ ADLCallKind IsADLCandidate, OverloadCandidateParamOrder PO,
+ bool AggregateCandidateDeduction);
+
void AddTemplateOverloadCandidate(
FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args,
@@ -10391,6 +10409,13 @@ class Sema final : public SemaBase {
OverloadCandidateSet &CandidateSet, bool AllowObjCConversionOnExplicit,
bool AllowExplicit, bool AllowResultConversion = true);
+ void AddTemplateConversionCandidateImmediately(
+ OverloadCandidateSet &CandidateSet,
+ FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
+ CXXRecordDecl *ActingContext, Expr *From, QualType ToType,
+ bool AllowObjCConversionOnExplicit, bool AllowExplicit,
+ bool AllowResultConversion);
+
/// AddSurrogateCandidate - Adds a "surrogate" candidate function that
/// converts the given @c Object to a function pointer via the
/// conversion function @c Conversion, and then attempts to call it
diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp
index 44a49a6e3148e..170b9c942bfd7 100644
--- a/clang/lib/Sema/SemaCodeComplete.cpp
+++ b/clang/lib/Sema/SemaCodeComplete.cpp
@@ -6365,7 +6365,8 @@ SemaCodeCompletion::ProduceCallSignatureHelp(Expr *Fn, ArrayRef<Expr *> Args,
Expr *NakedFn = Fn->IgnoreParenCasts();
// Build an overload candidate set based on the functions we find.
SourceLocation Loc = Fn->getExprLoc();
- OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal);
+ OverloadCandidateSet CandidateSet(Loc,
+ OverloadCandidateSet::CSK_CodeCompletion);
if (auto ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn)) {
SemaRef.AddOverloadedCallCandidates(ULE, ArgsWithoutDependentTypes,
@@ -6568,7 +6569,8 @@ QualType SemaCodeCompletion::ProduceConstructorSignatureHelp(
// FIXME: Provide support for variadic template constructors.
if (CRD) {
- OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal);
+ OverloadCandidateSet CandidateSet(Loc,
+ OverloadCandidateSet::CSK_CodeCompletion);
for (NamedDecl *C : SemaRef.LookupConstructors(CRD)) {
if (auto *FD = dyn_cast<FunctionDecl>(C)) {
// FIXME: we can't yet provide correct signature help for initializer
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 9814c3f456f0d..f947b29e16881 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -10043,12 +10043,15 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer(
// When [...] the constructor [...] is a candidate by
// - [over.match.copy] (in all cases)
if (TD) {
- SmallVector<Expr *, 8> TmpInits;
- for (Expr *E : Inits)
+ MutableArrayRef<Expr *> TmpInits =
+ Candidates.getPersistentArgsArray(Inits.size());
+ for (auto [I, E] : llvm::enumerate(Inits)) {
if (auto *DI = dyn_cast<DesignatedInitExpr>(E))
- TmpInits.push_back(DI->getInit());
+ TmpInits[I] = DI->getInit();
else
- TmpInits.push_back(E);
+ TmpInits[I] = E;
+ }
+
AddTemplateOverloadCandidate(
TD, FoundDecl, /*ExplicitArgs=*/nullptr, TmpInits, Candidates,
/*SuppressUserConversions=*/false,
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 1802f8f4e1f91..5b41d153e6e72 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -31,6 +31,7 @@
#include "clang/Sema/Lookup.h"
#include "clang/Sema/Overload.h"
#include "clang/Sema/SemaCUDA.h"
+#include "clang/Sema/SemaCodeCompletion.h"
#include "clang/Sema/SemaObjC.h"
#include "clang/Sema/Template.h"
#include "clang/Sema/TemplateDeduction.h"
@@ -45,6 +46,7 @@
#include <cstddef>
#include <cstdlib>
#include <optional>
+#include <variant>
using namespace clang;
using namespace sema;
@@ -7797,6 +7799,28 @@ void Sema::AddMethodTemplateCandidate(
if (!CandidateSet.isNewCandidate(MethodTmpl, PO))
return;
+ if (CandidateSet.getKind() == OverloadCandidateSet::CSK_CodeCompletion ||
+ ExplicitTemplateArgs) {
+ AddMethodTemplateCandidateImmediately(
+ CandidateSet, MethodTmpl, FoundDecl, ActingContext,
+ ExplicitTemplateArgs, ObjectType, ObjectClassification, Args,
+ SuppressUserConversions, PartialOverloading, PO);
+ return;
+ }
+
+ CandidateSet.AddNonDeducedMethodTemplateCandidate(
+ MethodTmpl, FoundDecl, ActingContext, ObjectType, ObjectClassification,
+ Args, SuppressUserConversions, PartialOverloading, PO);
+}
+
+void Sema::AddMethodTemplateCandidateImmediately(
+ OverloadCandidateSet &CandidateSet, FunctionTemplateDecl *MethodTmpl,
+ DeclAccessPair FoundDecl, CXXRecordDecl *ActingContext,
+ TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ObjectType,
+ Expr::Classification ObjectClassification, ArrayRef<Expr *> Args,
+ bool SuppressUserConversions, bool PartialOverloading,
+ OverloadCandidateParamOrder PO) {
+
// C++ [over.match.funcs]p7:
// In each case where a candidate is a function template, candidate
// function template specializations are generated using template argument
@@ -7826,7 +7850,7 @@ void Sema::AddMethodTemplateCandidate(
Candidate.Function = MethodTmpl->getTemplatedDecl();
Candidate.Viable = false;
Candidate.RewriteKind =
- CandidateSet.getRewriteInfo().getRewriteKind(Candidate.Function, PO);
+ CandidateSet.getRewriteInfo().getRewriteKind(Candidate.Function, PO);
Candidate.IsSurrogate = false;
Candidate.IgnoreObjectArgument =
cast<CXXMethodDecl>(Candidate.Function)->isStatic() ||
@@ -7836,8 +7860,8 @@ void Sema::AddMethodTemplateCandidate(
Candidate.FailureKind = ovl_fail_bad_conversion;
else {
Candidate.FailureKind = ovl_fail_bad_deduction;
- Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result,
- Info);
+ Candidate.DeductionFailure =
+ MakeDeductionFailureInfo(Context, Result, Info);
}
return;
}
@@ -7868,6 +7892,28 @@ void Sema::AddTemplateOverloadCandidate(
if (!CandidateSet.isNewCandidate(FunctionTemplate, PO))
return;
+ if (CandidateSet.getKind() == OverloadCandidateSet::CSK_CodeCompletion ||
+ ExplicitTemplateArgs) {
+ AddTemplateOverloadCandidateImmediately(
+ CandidateSet, FunctionTemplate, FoundDecl, ExplicitTemplateArgs, Args,
+ SuppressUserConversions, PartialOverloading, AllowExplicit,
+ IsADLCandidate, PO, AggregateCandidateDeduction);
+ return;
+ }
+
+ CandidateSet.AddNonDeducedTemplateCandidate(
+ FunctionTemplate, FoundDecl, Args, SuppressUserConversions,
+ PartialOverloading, AllowExplicit, IsADLCandidate, PO,
+ AggregateCandidateDeduction);
+}
+
+void Sema::AddTemplateOverloadCandidateImmediately(
+ OverloadCandidateSet &CandidateSet, FunctionTemplateDecl *FunctionTemplate,
+ DeclAccessPair FoundDecl, TemplateArgumentListInfo *ExplicitTemplateArgs,
+ ArrayRef<Expr *> Args, bool SuppressUserConversions,
+ bool PartialOverloading, bool AllowExplicit, ADLCallKind IsADLCandidate,
+ OverloadCandidateParamOrder PO, bool AggregateCandidateDeduction) {
+
// If the function template has a non-dependent explicit specification,
// exclude it now if appropriate; we are not permitted to perform deduction
// and substitution in this case.
@@ -7911,7 +7957,7 @@ void Sema::AddTemplateOverloadCandidate(
Candidate.Function = FunctionTemplate->getTemplatedDecl();
Candidate.Viable = false;
Candidate.RewriteKind =
- CandidateSet.getRewriteInfo().getRewriteKind(Candidate.Function, PO);
+ CandidateSet.getRewriteInfo().getRewriteKind(Candidate.Function, PO);
Candidate.IsSurrogate = false;
Candidate.IsADLCandidate = llvm::to_underlying(IsADLCandidate);
// Ignore the object argument if there is one, since we don't have an object
@@ -7924,8 +7970,8 @@ void Sema::AddTemplateOverloadCandidate(
Candidate.FailureKind = ovl_fail_bad_conversion;
else {
Candidate.FailureKind = ovl_fail_bad_deduction;
- Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result,
- Info);
+ Candidate.DeductionFailure =
+ MakeDeductionFailureInfo(Context, Result, Info);
}
return;
}
@@ -8267,6 +8313,25 @@ void Sema::AddTemplateConversionCandidate(
if (!CandidateSet.isNewCandidate(FunctionTemplate))
return;
+ if (CandidateSet.getKind() == OverloadCandidateSet::CSK_CodeCompletion) {
+ AddTemplateConversionCandidateImmediately(
+ CandidateSet, FunctionTemplate, FoundDecl, ActingDC, From, ToType,
+ AllowObjCConversionOnExplicit, AllowExplicit, AllowResultConversion);
+
+ return;
+ }
+
+ CandidateSet.AddNonDeducedConversionTemplateCandidate(
+ FunctionTemplate, FoundDecl, ActingDC, From, ToType,
+ AllowObjCConversionOnExplicit, AllowExplicit, AllowResultConversion);
+}
+
+void Sema::AddTemplateConversionCandidateImmediately(
+ OverloadCandidateSet &CandidateSet, FunctionTemplateDecl *FunctionTemplate,
+ DeclAccessPair FoundDecl, CXXRecordDecl *ActingContext, Expr *From,
+ QualType ToType, bool AllowObjCConversionOnExplicit, bool AllowExplicit,
+ bool AllowResultConversion) {
+
// If the function template has a non-dependent explicit specification,
// exclude it now if appropriate; we are not permitted to perform deduction
// and substitution in this case.
@@ -8294,15 +8359,15 @@ void Sema::AddTemplateConversionCandidate(
Candidate.Viable = false;
Candidate.FailureKind = ovl_fail_bad_deduction;
Candidate.ExplicitCallArguments = 1;
- Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result,
- Info);
+ Candidate.DeductionFailure =
+ MakeDeductionFailureInfo(Context, Result, Info);
return;
}
// Add the conversion function template specialization produced by
// template argument deduction as a candidate.
assert(Specialization && "Missing function template specialization?");
- AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType,
+ AddConversionCandidate(Specialization, FoundDecl, ActingContext, From, ToType,
CandidateSet, AllowObjCConversionOnExplicit,
AllowExplicit, AllowResultConversion,
Info.hasStrictPackMatch());
@@ -8441,6 +8506,13 @@ void Sema::AddNonMemberOperatorCandidates(
NamedDecl *D = F.getDecl()->getUnderlyingDecl();
ArrayRef<Expr *> FunctionArgs = Args;
+ auto ReversedArgs = [&, Arr = ArrayRef<Expr *>{}]() mutable {
+ if (Arr.empty())
+ Arr = CandidateSet.getPersistentArgsArray(FunctionArgs[1],
+ FunctionArgs[0]);
+ return Arr;
+ };
+
FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
FunctionDecl *FD =
FunTmpl ? FunTmpl->getTemplatedDecl() : cast<FunctionDecl>(D);
@@ -8455,18 +8527,18 @@ void Sema::AddNonMemberOperatorCandidates(
if (FunTmpl) {
AddTemplateOverloadCandidate(FunTmpl, F.getPair(), ExplicitTemplateArgs,
FunctionArgs, CandidateSet);
- if (CandidateSet.getRewriteInfo().shouldAddReversed(*this, Args, FD))
- AddTemplateOverloadCandidate(
- FunTmpl, F.getPair(), ExplicitTemplateArgs,
- {FunctionArgs[1], FunctionArgs[0]}, CandidateSet, false, false,
- true, ADLCallKind::NotADL, OverloadCandidateParamOrder::Reversed);
+ if (CandidateSet.getRewriteInfo().shouldAddReversed(*this, Args, FD)) {
+ AddTemplateOverloadCandidate(FunTmpl, F.getPair(), ExplicitTemplateArgs,
+ ReversedArgs(), CandidateSet, false, false,
+ true, ADLCallKind::NotADL,
+ OverloadCandidateParamOrder::Reversed);
+ }
} else {
if (ExplicitTemplateArgs)
continue;
AddOverloadCandidate(FD, F.getPair(), FunctionArgs, CandidateSet);
if (CandidateSet.getRewriteInfo().shouldAddReversed(*this, Args, FD))
- AddOverloadCandidate(FD, F.getPair(),
- {FunctionArgs[1], FunctionArgs[0]}, CandidateSet,
+ AddOverloadCandidate(FD, F.getPair(), ReversedArgs(), CandidateSet,
false, false, true, false, ADLCallKind::NotADL, {},
OverloadCandidateParamOrder::Reversed);
}
@@ -10191,6 +10263,12 @@ Sema::AddArgumentDependentLookupCandidates(DeclarationName Name,
// FIXME: Pass in the explicit template arguments?
ArgumentDependentLookup(Name, Loc, Args, Fns);
+ auto ReversedArgs = [&, Arr = ArrayRef<Expr *>{}]() mutable {
+ if (Arr.empty())
+ Arr = CandidateSet.getPersistentArgsArray(Args[1], Args[0]);
+ return Arr;
+ };
+
// Erase all of the candidates we already knew about.
for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(),
CandEnd = CandidateSet.end();
@@ -10217,7 +10295,7 @@ Sema::AddArgumentDependentLookupCandidates(DeclarationName Name,
/*AllowExplicitConversion=*/false, ADLCallKind::UsesADL);
if (CandidateSet.getRewriteInfo().shouldAddReversed(*this, Args, FD)) {
AddOverloadCandidate(
- FD, FoundDecl, {Args[1], Args[0]}, CandidateSet,
+ FD, FoundDecl, ReversedArgs(), CandidateSet,
/*SuppressUserConversions=*/false, PartialOverloading,
/*AllowExplicit=*/true, /*AllowExplicitConversion=*/false,
ADLCallKind::UsesADL, {}, OverloadCandidateParamOrder::Reversed);
@@ -10231,8 +10309,8 @@ Sema::AddArgumentDependentLookupCandidates(DeclarationName Name,
if (CandidateSet.getRewriteInfo().shouldAddReversed(
*this, Args, FTD->getTemplatedDecl())) {
AddTemplateOverloadCandidate(
- FTD, FoundDecl, ExplicitTemplateArgs, {Args[1], Args[0]},
- CandidateSet, /*SuppressUserConversions=*/false, PartialOverloading,
+ FTD, FoundDecl, ExplicitTemplateArgs, ReversedArgs(), CandidateSet,
+ /*SuppressUserConversions=*/false, PartialOverloading,
/*AllowExplicit=*/true, ADLCallKind::UsesADL,
OverloadCandidateParamOrder::Reversed);
}
@@ -10905,6 +10983,93 @@ bool OverloadCandidate::NotValidBecauseConstraintExprHasError() const {
->Satisfaction.ContainsErrors;
}
+void OverloadCandidateSet::AddNonDeducedTemplateCandidate(
+ FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
+ ArrayRef<Expr *> Args, bool SuppressUserConversions,
+ bool PartialOverloading, bool AllowExplicit,
+ CallExpr::ADLCallKind IsADLCandidate, OverloadCandidateParamOrder PO,
+ bool AggregateCandidateDeduction) {
+ NonDeducedFunctionTemplateOverloadCandidate C{FunctionTemplate,
+ FoundDecl,
+ Args,
+ IsADLCandidate,
+ PO,
+ SuppressUserConversions,
+ PartialOverloading,
+ AllowExplicit,
+ AggregateCandidateDeduction};
+ NonDeducedCandidates.emplace_back(std::move(C));
+}
+
+void OverloadCandidateSet::AddNonDeducedMethodTemplateCandidate(
+ FunctionTemplateDecl *MethodTmpl, DeclAccessPair FoundDecl,
+ CXXRecordDecl *ActingContext, QualType ObjectType,
+ Expr::Classification ObjectClassification, ArrayRef<Expr *> Args,
+ bool SuppressUserConversions, bool PartialOverloading,
+ OverloadCandidateParamOrder PO) {
+ NonDeducedMethodTemplateOverloadCandidate C{
+ MethodTmpl, FoundDecl, Args, ActingContext,
+ ObjectClassification, ObjectType, PO, SuppressUserConversions,
+ PartialOverloading};
+ NonDeducedCandidates.emplace_back(std::move(C));
+}
+
+void OverloadCandidateSet::AddNonDeducedConversionTemplateCandidate(
+ FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
+ CXXRecordDecl *ActingContext, Expr *From, QualType ToType,
+ bool AllowObjCConversionOnExplicit, bool AllowExplicit,
+ bool AllowResultConversion) {
+
+ NonDeducedConversionTemplateOverloadCandidate C{
+ FunctionTemplate, FoundDecl,
+ ActingContext, From,
+ ToType, AllowObjCConversionOnExplicit,
+ AllowExplicit, AllowResultConversion};
+
+ NonDeducedCandidates.emplace_back(std::move(C));
+}
+
+static void
+AddTemplateOverloadCandidate(Sema &S, OverloadCandidateSet &CandidateSet,
+ NonDeducedMethodTemplateOverloadCandidate &&C) {
+
+ S.AddMethodTemplateCandidateImmediately(
+ CandidateSet, C.FunctionTemplate, C.FoundDecl, C.ActingContext,
+ /*ExplicitTemplateArgs=*/nullptr, C.ObjectType, C.ObjectClassification,
+ C.Args, C.SuppressUserConversions, C.PartialOverloading, C.PO);
+}
+
+static void
+AddTemplateOverloadCandidate(Sema &S, OverloadCandidateSet &CandidateSet,
+ NonDeducedFunctionTemplateOverloadCandidate &&C) {
+ S.AddTemplateOverloadCandidateImmediately(
+ CandidateSet, C.FunctionTemplate, C.FoundDecl,
+ /*ExplicitTemplateArgs=*/nullptr, C.Args, C.SuppressUserConversions,
+ C.PartialOverloading, C.AllowExplicit, C.IsADLCandidate, C.PO,
+ C.AggregateCandidateDeduction);
+}
+
+static void AddTemplateOverloadCandidate(
+ Sema &S, OverloadCandidateSet &CandidateSet,
+ NonDeducedConversionTemplateOverloadCandidate &&C) {
+ return S.AddTemplateConversionCandidateImmediately(
+ CandidateSet, C.FunctionTemplate, C.FoundDecl, C.ActingContext, C.From,
+ C.ToType, C.AllowObjCConversionOnExplicit, C.AllowExplicit,
+ C.AllowResultConversion);
+}
+
+void OverloadCandidateSet::InjectNonDeducedTemplateCandidates(Sema &S) {
+ Candidates.reserve(Candidates.size() + NonDeducedCandidates.size());
+ for (auto &&Elem : NonDeducedCandidates) {
+ std::visit(
+ [&](auto &&Cand) {
+ AddTemplateOverloadCandidate(S, *this, std::move(Cand));
+ },
+ Elem);
+ }
+ NonDeducedCandidates.clear();
+}
+
/// Computes the best viable function (C++ 13.3.3)
/// within an overload candidate set.
///
@@ -10918,7 +11083,44 @@ bool OverloadCandidate::NotValidBecauseConstraintExprHasError() const {
OverloadingResult
OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc,
iterator &Best) {
+
+ bool TwoPhaseResolution =
+ !NonDeducedCandidates.empty() && Kind != CSK_CodeCompletion &&
+ Kind != CSK_InitByUserDefinedConversion && Kind != CSK_InitByConstructor;
+
+ if (TwoPhaseResolution) {
+ Best = end();
+ for (auto It = begin(); It != end(); ++It) {
+ if (It->isPerfectMatch(S.getASTContext())) {
+ if (Best == end()) {
+ Best = It;
+ } else {
+ Best = end();
+ break;
+ }
+ }
+ }
+ if (Best != end()) {
+ Best->Best = true;
+ if (Best->Function && Best->Function->isDeleted())
+ return OR_Deleted;
+ if (auto *M = dyn_cast_or_null<CXXMethodDecl>(Best->Function);
+ Kind == CSK_AddressOfOverloadSet && M &&
+ M->isImplicitObjectMemberFunction()) {
+ return OR_No_Viable_Function;
+ }
+ return OR_Success;
+ }
+ }
+ InjectNonDeducedTemplateCandidates(S);
+ return BestViableFunctionImpl(S, Loc, Best);
+}
+
+OverloadingResult OverloadCandidateSet::BestViableFunctionImpl(
+ Sema &S, SourceLocation Loc, OverloadCandidateSet::iterator &Best) {
+
llvm::SmallVector<OverloadCandidate *, 16> Candidates;
+ Candidates.reserve(this->Candidates.size());
std::transform(begin(), end(), std::back_inserter(Candidates),
[](OverloadCandidate &Cand) { return &Cand; });
@@ -10953,7 +11155,6 @@ OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc,
}
}
- // Find the best viable function.
Best = end();
for (auto *Cand : Candidates) {
Cand->Best = false;
@@ -10975,9 +11176,8 @@ OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc,
if (Best == end())
return OR_No_Viable_Function;
+ llvm::SmallVector<OverloadCandidate *, 4> PendingBest;
llvm::SmallVector<const NamedDecl *, 4> EquivalentCands;
-
- llvm::SmallVector<OverloadCandidate*, 4> PendingBest;
PendingBest.push_back(&*Best);
Best->Best = true;
@@ -10999,8 +11199,6 @@ OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc,
}
}
}
-
- // If we found more than one best candidate, this is ambiguous.
if (Best == end())
return OR_Ambiguous;
@@ -11014,10 +11212,9 @@ OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc,
return OR_No_Viable_Function;
}
- if (!EquivalentCands.empty())
+ if (NonDeducedCandidates.empty() && !EquivalentCands.empty())
S.diagnoseEquivalentInternalLinkageDeclarations(Loc, Best->Function,
EquivalentCands);
-
return OR_Success;
}
@@ -12714,6 +12911,9 @@ SmallVector<OverloadCandidate *, 32> OverloadCandidateSet::CompleteCandidates(
Sema &S, OverloadCandidateDisplayKind OCD, ArrayRef<Expr *> Args,
SourceLocation OpLoc,
llvm::function_ref<bool(OverloadCandidate &)> Filter) {
+
+ InjectNonDeducedTemplateCandidates(S);
+
// Sort the candidates by viability and position. Sorting directly would
// be prohibitive, so we make a set of pointers and sort those.
SmallVector<OverloadCandidate*, 32> Cands;
@@ -14689,18 +14889,23 @@ void Sema::LookupOverloadedBinOp(OverloadCandidateSet &CandidateSet,
// rewritten candidates using these functions if necessary.
AddNonMemberOperatorCandidates(Fns, Args, CandidateSet);
+ auto ReversedArgs = [&, Arr = ArrayRef<Expr *>{}]() mutable {
+ if (Arr.empty())
+ Arr = CandidateSet.getPersistentArgsArray(Args[1], Args[0]);
+ return Arr;
+ };
+
// Add operator candidates that are member functions.
AddMemberOperatorCandidates(Op, OpLoc, Args, CandidateSet);
if (CandidateSet.getRewriteInfo().allowsReversed(Op))
- AddMemberOperatorCandidates(Op, OpLoc, {Args[1], Args[0]}, CandidateSet,
+ AddMemberOperatorCandidates(Op, OpLoc, ReversedArgs(), CandidateSet,
OverloadCandidateParamOrder::Reversed);
// In C++20, also add any rewritten member candidates.
if (ExtraOp) {
AddMemberOperatorCandidates(ExtraOp, OpLoc, Args, CandidateSet);
if (CandidateSet.getRewriteInfo().allowsReversed(ExtraOp))
- AddMemberOperatorCandidates(ExtraOp, OpLoc, {Args[1], Args[0]},
- CandidateSet,
+ AddMemberOperatorCandidates(ExtraOp, OpLoc, ReversedArgs(), CandidateSet,
OverloadCandidateParamOrder::Reversed);
}
>From 7b64e719b2dc4ede36bbe6c4b27eea79ab08dcd6 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Sat, 29 Mar 2025 12:34:55 +0100
Subject: [PATCH 02/18] avoid comparing types
---
clang/include/clang/Sema/Overload.h | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h
index 2cc7e1809e26c..4fb6bfe0aa55b 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -745,8 +745,9 @@ class Sema;
}
bool isPerfect(const ASTContext &C) const {
- return (isStandard() && Standard.isIdentityConversion() &&
- C.hasSameType(Standard.getFromType(), Standard.getToType(2))) ||
+ return (isStandard() && Standard.isIdentityConversion()
+ && !Standard.DirectBinding
+ ) ||
getKind() == StaticObjectArgumentConversion;
}
@@ -989,7 +990,7 @@ class Sema;
bool isPerfectMatch(const ASTContext &Ctx) const {
if (!Viable)
return false;
- for (auto &C : Conversions) {
+ for (const auto &C : Conversions) {
if (!C.isInitialized())
return false;
if (!C.isPerfect(Ctx))
>From f8f74743726ff9b375dbc6f64f87cc4bfbbe7fc5 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Sat, 29 Mar 2025 13:57:47 +0100
Subject: [PATCH 03/18] Fix logic
---
clang/include/clang/Sema/Overload.h | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h
index 4fb6bfe0aa55b..4bd94ce7bff94 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -745,10 +745,9 @@ class Sema;
}
bool isPerfect(const ASTContext &C) const {
- return (isStandard() && Standard.isIdentityConversion()
- && !Standard.DirectBinding
- ) ||
- getKind() == StaticObjectArgumentConversion;
+ return isStandard() && Standard.isIdentityConversion() &&
+ (!Standard.ReferenceBinding || C.hasSameType(Standard.getFromType(), Standard.getToType(2)))
+ ;
}
// True iff this is a conversion sequence from an initializer list to an
>From ad3e7600432d14804f89d1cf8d8e8a7716849c64 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Sat, 29 Mar 2025 15:19:03 +0100
Subject: [PATCH 04/18] optimize
---
clang/lib/Sema/SemaOverload.cpp | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 5b41d153e6e72..86dd27113e390 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -11112,7 +11112,10 @@ OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc,
return OR_Success;
}
}
- InjectNonDeducedTemplateCandidates(S);
+
+ if(!NonDeducedCandidates.empty())
+ InjectNonDeducedTemplateCandidates(S);
+
return BestViableFunctionImpl(S, Loc, Best);
}
>From 149ec947f65e9a75bfbebb9439559be9eaa81aa4 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Sat, 29 Mar 2025 15:26:52 +0100
Subject: [PATCH 05/18] optimize again
---
clang/lib/Sema/SemaOverload.cpp | 14 +++++---------
1 file changed, 5 insertions(+), 9 deletions(-)
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 86dd27113e390..4e9b18135e123 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -8506,13 +8506,6 @@ void Sema::AddNonMemberOperatorCandidates(
NamedDecl *D = F.getDecl()->getUnderlyingDecl();
ArrayRef<Expr *> FunctionArgs = Args;
- auto ReversedArgs = [&, Arr = ArrayRef<Expr *>{}]() mutable {
- if (Arr.empty())
- Arr = CandidateSet.getPersistentArgsArray(FunctionArgs[1],
- FunctionArgs[0]);
- return Arr;
- };
-
FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
FunctionDecl *FD =
FunTmpl ? FunTmpl->getTemplatedDecl() : cast<FunctionDecl>(D);
@@ -8528,8 +8521,10 @@ void Sema::AddNonMemberOperatorCandidates(
AddTemplateOverloadCandidate(FunTmpl, F.getPair(), ExplicitTemplateArgs,
FunctionArgs, CandidateSet);
if (CandidateSet.getRewriteInfo().shouldAddReversed(*this, Args, FD)) {
+ ArrayRef<Expr *> Reversed = CandidateSet.getPersistentArgsArray(FunctionArgs[1],
+ FunctionArgs[0]);
AddTemplateOverloadCandidate(FunTmpl, F.getPair(), ExplicitTemplateArgs,
- ReversedArgs(), CandidateSet, false, false,
+ Reversed, CandidateSet, false, false,
true, ADLCallKind::NotADL,
OverloadCandidateParamOrder::Reversed);
}
@@ -8538,7 +8533,8 @@ void Sema::AddNonMemberOperatorCandidates(
continue;
AddOverloadCandidate(FD, F.getPair(), FunctionArgs, CandidateSet);
if (CandidateSet.getRewriteInfo().shouldAddReversed(*this, Args, FD))
- AddOverloadCandidate(FD, F.getPair(), ReversedArgs(), CandidateSet,
+ AddOverloadCandidate(FD, F.getPair(), {FunctionArgs[1],
+ FunctionArgs[0]}, CandidateSet,
false, false, true, false, ADLCallKind::NotADL, {},
OverloadCandidateParamOrder::Reversed);
}
>From 3634702a1e474490a28627c068241e020a386e9a Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Mon, 31 Mar 2025 16:34:02 +0200
Subject: [PATCH 06/18] Cleanups, fix tests
---
clang/include/clang/Sema/Overload.h | 67 ++-
clang/include/clang/Sema/Sema.h | 23 -
clang/lib/Sema/SemaInit.cpp | 4 +
clang/lib/Sema/SemaOverload.cpp | 475 ++++++++++--------
.../constrant-satisfaction-conversions.cpp | 8 +-
.../SemaCXX/implicit-member-functions.cpp | 21 +-
.../instantiate-function-params.cpp | 7 +-
.../Templight/templight-empty-entries-fix.cpp | 34 +-
8 files changed, 336 insertions(+), 303 deletions(-)
diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h
index 4bd94ce7bff94..63339a917c3a4 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -408,6 +408,11 @@ class Sema;
Third == ICK_Identity;
}
+ bool isPerfect(const ASTContext &C) const {
+ return isIdentityConversion() &&
+ (!ReferenceBinding || C.hasSameType(getFromType(), getToType(2)));
+ }
+
ImplicitConversionRank getRank() const;
NarrowingKind
getNarrowingKind(ASTContext &Context, const Expr *Converted,
@@ -745,9 +750,7 @@ class Sema;
}
bool isPerfect(const ASTContext &C) const {
- return isStandard() && Standard.isIdentityConversion() &&
- (!Standard.ReferenceBinding || C.hasSameType(Standard.getFromType(), Standard.getToType(2)))
- ;
+ return isStandard() && Standard.isPerfect(C);
}
// True iff this is a conversion sequence from an initializer list to an
@@ -995,6 +998,8 @@ class Sema;
if (!C.isPerfect(Ctx))
return false;
}
+ if (isa_and_nonnull<CXXConversionDecl>(Function))
+ return FinalConversion.isPerfect(Ctx);
return true;
}
@@ -1034,7 +1039,7 @@ class Sema;
RewriteKind(CRK_None) {}
};
- struct NonDeducedConversionTemplateOverloadCandidate {
+ struct DeferredConversionTemplateOverloadCandidate {
FunctionTemplateDecl *FunctionTemplate;
DeclAccessPair FoundDecl;
CXXRecordDecl *ActingContext;
@@ -1049,7 +1054,7 @@ class Sema;
unsigned AllowResultConversion : 1;
};
- struct NonDeducedMethodTemplateOverloadCandidate {
+ struct DeferredMethodTemplateOverloadCandidate {
FunctionTemplateDecl *FunctionTemplate;
DeclAccessPair FoundDecl;
ArrayRef<Expr *> Args;
@@ -1064,7 +1069,7 @@ class Sema;
unsigned PartialOverloading : 1;
};
- struct NonDeducedFunctionTemplateOverloadCandidate {
+ struct DeferredFunctionTemplateOverloadCandidate {
FunctionTemplateDecl *FunctionTemplate;
DeclAccessPair FoundDecl;
ArrayRef<Expr *> Args;
@@ -1081,13 +1086,13 @@ class Sema;
unsigned AggregateCandidateDeduction : 1;
};
- using NonDeducedTemplateOverloadCandidate =
- std::variant<NonDeducedConversionTemplateOverloadCandidate,
- NonDeducedMethodTemplateOverloadCandidate,
- NonDeducedFunctionTemplateOverloadCandidate>;
+ using DeferredTemplateOverloadCandidate =
+ std::variant<DeferredConversionTemplateOverloadCandidate,
+ DeferredMethodTemplateOverloadCandidate,
+ DeferredFunctionTemplateOverloadCandidate>;
static_assert(
- std::is_trivially_destructible_v<NonDeducedTemplateOverloadCandidate>);
+ std::is_trivially_destructible_v<DeferredTemplateOverloadCandidate>);
/// OverloadCandidateSet - A set of overload candidates, used in C++
/// overload resolution (C++ 13.3).
@@ -1192,7 +1197,7 @@ class Sema;
private:
SmallVector<OverloadCandidate, 16> Candidates;
llvm::SmallPtrSet<uintptr_t, 16> Functions;
- SmallVector<NonDeducedTemplateOverloadCandidate, 8> NonDeducedCandidates;
+ SmallVector<DeferredTemplateOverloadCandidate, 8> DeferredCandidates;
// Allocator for ConversionSequenceLists. We store the first few of these
// inline to avoid allocation for small sets.
@@ -1204,6 +1209,7 @@ class Sema;
constexpr static unsigned NumInlineBytes =
32 * sizeof(ImplicitConversionSequence);
+
unsigned NumInlineBytesUsed = 0;
alignas(void *) char InlineSpace[NumInlineBytes];
@@ -1214,8 +1220,6 @@ class Sema;
/// from the slab allocator.
/// FIXME: It would probably be nice to have a SmallBumpPtrAllocator
/// instead.
- /// FIXME: Now that this only allocates ImplicitConversionSequences, do we
- /// want to un-generalize this?
template <typename T>
T *slabAllocate(unsigned N) {
// It's simpler if this doesn't need to consider alignment.
@@ -1253,6 +1257,9 @@ class Sema;
/// Whether diagnostics should be deferred.
bool shouldDeferDiags(Sema &S, ArrayRef<Expr *> Args, SourceLocation OpLoc);
+ // Whether the resolution of template candidates should be defered
+ bool shouldDeferTemplateArgumentDeduction(const LangOptions &Opts) const;
+
/// Determine when this overload candidate will be new to the
/// overload set.
bool isNewCandidate(Decl *F, OverloadCandidateParamOrder PO =
@@ -1277,10 +1284,10 @@ class Sema;
iterator end() { return Candidates.end(); }
size_t size() const {
- return Candidates.size() + NonDeducedCandidates.size();
+ return Candidates.size() + DeferredCandidates.size();
}
bool empty() const {
- return Candidates.empty() && NonDeducedCandidates.empty();
+ return Candidates.empty() && DeferredCandidates.empty();
}
/// Allocate storage for conversion sequences for NumConversions
@@ -1325,21 +1332,21 @@ class Sema;
return C;
}
- void AddNonDeducedTemplateCandidate(
+ void AddDeferredTemplateCandidate(
FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
ArrayRef<Expr *> Args, bool SuppressUserConversions,
bool PartialOverloading, bool AllowExplicit,
CallExpr::ADLCallKind IsADLCandidate, OverloadCandidateParamOrder PO,
bool AggregateCandidateDeduction);
- void AddNonDeducedMethodTemplateCandidate(
+ void AddDeferredMethodTemplateCandidate(
FunctionTemplateDecl *MethodTmpl, DeclAccessPair FoundDecl,
CXXRecordDecl *ActingContext, QualType ObjectType,
Expr::Classification ObjectClassification, ArrayRef<Expr *> Args,
bool SuppressUserConversions, bool PartialOverloading,
OverloadCandidateParamOrder PO);
- void AddNonDeducedConversionTemplateCandidate(
+ void AddDeferredConversionTemplateCandidate(
FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
CXXRecordDecl *ActingContext, Expr *From, QualType ToType,
bool AllowObjCConversionOnExplicit, bool AllowExplicit,
@@ -1351,10 +1358,6 @@ class Sema;
OverloadingResult BestViableFunction(Sema &S, SourceLocation Loc,
OverloadCandidateSet::iterator& Best);
- OverloadingResult
- BestViableFunctionImpl(Sema &S, SourceLocation Loc,
- OverloadCandidateSet::iterator &Best);
-
SmallVector<OverloadCandidate *, 32> CompleteCandidates(
Sema &S, OverloadCandidateDisplayKind OCD, ArrayRef<Expr *> Args,
SourceLocation OpLoc = SourceLocation(),
@@ -1383,6 +1386,14 @@ class Sema;
DestAS = AS;
}
+ private:
+ OverloadingResult ResultForBestCandidate(const iterator &Best);
+ void CudaExcludeWrongSideCandidates(Sema &S);
+ OverloadingResult
+ BestViableFunctionImpl(Sema &S, SourceLocation Loc,
+ OverloadCandidateSet::iterator &Best);
+ void PerfectViableFunction(Sema &S, SourceLocation Loc,
+ OverloadCandidateSet::iterator &Best);
};
bool isBetterOverloadCandidate(Sema &S, const OverloadCandidate &Cand1,
@@ -1431,6 +1442,16 @@ class Sema;
// parameter.
bool shouldEnforceArgLimit(bool PartialOverloading, FunctionDecl *Function);
+ inline bool OverloadCandidateSet::shouldDeferTemplateArgumentDeduction(
+ const LangOptions &Opts) const {
+ return
+ // For user defined conversion we need to check against different
+ // combination of CV qualifiers and look at any expicit specifier, so
+ // always deduce template candidate.
+ Kind != CSK_InitByUserDefinedConversion && Kind != CSK_CodeCompletion &&
+ Opts.CPlusPlus && (!Opts.CUDA || Opts.GPUExcludeWrongSideOverloads);
+ }
+
} // namespace clang
#endif // LLVM_CLANG_SEMA_OVERLOAD_H
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 791617e92616d..9008c1af35885 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -10345,26 +10345,10 @@ class Sema final : public SemaBase {
OverloadCandidateSet &CandidateSet, bool SuppressUserConversions = false,
bool PartialOverloading = false, OverloadCandidateParamOrder PO = {});
- void AddMethodTemplateCandidateImmediately(
- OverloadCandidateSet &CandidateSet, FunctionTemplateDecl *MethodTmpl,
- DeclAccessPair FoundDecl, CXXRecordDecl *ActingContext,
- TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ObjectType,
- Expr::Classification ObjectClassification, ArrayRef<Expr *> Args,
- bool SuppressUserConversions, bool PartialOverloading,
- OverloadCandidateParamOrder PO);
-
/// Add a C++ function template specialization as a candidate
/// in the candidate set, using template argument deduction to produce
/// an appropriate function template specialization.
- void AddTemplateOverloadCandidateImmediately(
- OverloadCandidateSet &CandidateSet,
- FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
- TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args,
- bool SuppressUserConversions, bool PartialOverloading, bool AllowExplicit,
- ADLCallKind IsADLCandidate, OverloadCandidateParamOrder PO,
- bool AggregateCandidateDeduction);
-
void AddTemplateOverloadCandidate(
FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args,
@@ -10409,13 +10393,6 @@ class Sema final : public SemaBase {
OverloadCandidateSet &CandidateSet, bool AllowObjCConversionOnExplicit,
bool AllowExplicit, bool AllowResultConversion = true);
- void AddTemplateConversionCandidateImmediately(
- OverloadCandidateSet &CandidateSet,
- FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
- CXXRecordDecl *ActingContext, Expr *From, QualType ToType,
- bool AllowObjCConversionOnExplicit, bool AllowExplicit,
- bool AllowResultConversion);
-
/// AddSurrogateCandidate - Adds a "surrogate" candidate function that
/// converts the given @c Object to a function pointer via the
/// conversion function @c Conversion, and then attempts to call it
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index f947b29e16881..a8ab43776a6de 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -10043,8 +10043,12 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer(
// When [...] the constructor [...] is a candidate by
// - [over.match.copy] (in all cases)
if (TD) {
+
+ // As template candidates are not deduced immediately,
+ // persist the arry in the overload set.
MutableArrayRef<Expr *> TmpInits =
Candidates.getPersistentArgsArray(Inits.size());
+
for (auto [I, E] : llvm::enumerate(Inits)) {
if (auto *DI = dyn_cast<DesignatedInitExpr>(E))
TmpInits[I] = DI->getInit();
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 4e9b18135e123..521f3efc646f1 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -7789,35 +7789,12 @@ void Sema::AddMethodCandidate(
}
}
-void Sema::AddMethodTemplateCandidate(
+static void AddMethodTemplateCandidateImmediately(
+ Sema &S, OverloadCandidateSet &CandidateSet,
FunctionTemplateDecl *MethodTmpl, DeclAccessPair FoundDecl,
CXXRecordDecl *ActingContext,
TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ObjectType,
Expr::Classification ObjectClassification, ArrayRef<Expr *> Args,
- OverloadCandidateSet &CandidateSet, bool SuppressUserConversions,
- bool PartialOverloading, OverloadCandidateParamOrder PO) {
- if (!CandidateSet.isNewCandidate(MethodTmpl, PO))
- return;
-
- if (CandidateSet.getKind() == OverloadCandidateSet::CSK_CodeCompletion ||
- ExplicitTemplateArgs) {
- AddMethodTemplateCandidateImmediately(
- CandidateSet, MethodTmpl, FoundDecl, ActingContext,
- ExplicitTemplateArgs, ObjectType, ObjectClassification, Args,
- SuppressUserConversions, PartialOverloading, PO);
- return;
- }
-
- CandidateSet.AddNonDeducedMethodTemplateCandidate(
- MethodTmpl, FoundDecl, ActingContext, ObjectType, ObjectClassification,
- Args, SuppressUserConversions, PartialOverloading, PO);
-}
-
-void Sema::AddMethodTemplateCandidateImmediately(
- OverloadCandidateSet &CandidateSet, FunctionTemplateDecl *MethodTmpl,
- DeclAccessPair FoundDecl, CXXRecordDecl *ActingContext,
- TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ObjectType,
- Expr::Classification ObjectClassification, ArrayRef<Expr *> Args,
bool SuppressUserConversions, bool PartialOverloading,
OverloadCandidateParamOrder PO) {
@@ -7833,12 +7810,12 @@ void Sema::AddMethodTemplateCandidateImmediately(
TemplateDeductionInfo Info(CandidateSet.getLocation());
FunctionDecl *Specialization = nullptr;
ConversionSequenceList Conversions;
- if (TemplateDeductionResult Result = DeduceTemplateArguments(
+ if (TemplateDeductionResult Result = S.DeduceTemplateArguments(
MethodTmpl, ExplicitTemplateArgs, Args, Specialization, Info,
PartialOverloading, /*AggregateDeductionCandidate=*/false,
/*PartialOrdering=*/false, ObjectType, ObjectClassification,
[&](ArrayRef<QualType> ParamTypes) {
- return CheckNonDependentConversions(
+ return S.CheckNonDependentConversions(
MethodTmpl, ParamTypes, Args, CandidateSet, Conversions,
SuppressUserConversions, ActingContext, ObjectType,
ObjectClassification, PO);
@@ -7861,7 +7838,7 @@ void Sema::AddMethodTemplateCandidateImmediately(
else {
Candidate.FailureKind = ovl_fail_bad_deduction;
Candidate.DeductionFailure =
- MakeDeductionFailureInfo(Context, Result, Info);
+ MakeDeductionFailureInfo(S.Context, Result, Info);
}
return;
}
@@ -7871,48 +7848,49 @@ void Sema::AddMethodTemplateCandidateImmediately(
assert(Specialization && "Missing member function template specialization?");
assert(isa<CXXMethodDecl>(Specialization) &&
"Specialization is not a member function?");
- AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl,
- ActingContext, ObjectType, ObjectClassification, Args,
- CandidateSet, SuppressUserConversions, PartialOverloading,
- Conversions, PO, Info.hasStrictPackMatch());
+ S.AddMethodCandidate(
+ cast<CXXMethodDecl>(Specialization), FoundDecl, ActingContext, ObjectType,
+ ObjectClassification, Args, CandidateSet, SuppressUserConversions,
+ PartialOverloading, Conversions, PO, Info.hasStrictPackMatch());
}
-/// Determine whether a given function template has a simple explicit specifier
-/// or a non-value-dependent explicit-specification that evaluates to true.
-static bool isNonDependentlyExplicit(FunctionTemplateDecl *FTD) {
- return ExplicitSpecifier::getFromDecl(FTD->getTemplatedDecl()).isExplicit();
-}
-
-void Sema::AddTemplateOverloadCandidate(
- FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
- TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args,
+void Sema::AddMethodTemplateCandidate(
+ FunctionTemplateDecl *MethodTmpl, DeclAccessPair FoundDecl,
+ CXXRecordDecl *ActingContext,
+ TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ObjectType,
+ Expr::Classification ObjectClassification, ArrayRef<Expr *> Args,
OverloadCandidateSet &CandidateSet, bool SuppressUserConversions,
- bool PartialOverloading, bool AllowExplicit, ADLCallKind IsADLCandidate,
- OverloadCandidateParamOrder PO, bool AggregateCandidateDeduction) {
- if (!CandidateSet.isNewCandidate(FunctionTemplate, PO))
+ bool PartialOverloading, OverloadCandidateParamOrder PO) {
+ if (!CandidateSet.isNewCandidate(MethodTmpl, PO))
return;
- if (CandidateSet.getKind() == OverloadCandidateSet::CSK_CodeCompletion ||
- ExplicitTemplateArgs) {
- AddTemplateOverloadCandidateImmediately(
- CandidateSet, FunctionTemplate, FoundDecl, ExplicitTemplateArgs, Args,
- SuppressUserConversions, PartialOverloading, AllowExplicit,
- IsADLCandidate, PO, AggregateCandidateDeduction);
+ if (ExplicitTemplateArgs ||
+ !CandidateSet.shouldDeferTemplateArgumentDeduction(getLangOpts())) {
+ AddMethodTemplateCandidateImmediately(
+ *this, CandidateSet, MethodTmpl, FoundDecl, ActingContext,
+ ExplicitTemplateArgs, ObjectType, ObjectClassification, Args,
+ SuppressUserConversions, PartialOverloading, PO);
return;
}
- CandidateSet.AddNonDeducedTemplateCandidate(
- FunctionTemplate, FoundDecl, Args, SuppressUserConversions,
- PartialOverloading, AllowExplicit, IsADLCandidate, PO,
- AggregateCandidateDeduction);
+ CandidateSet.AddDeferredMethodTemplateCandidate(
+ MethodTmpl, FoundDecl, ActingContext, ObjectType, ObjectClassification,
+ Args, SuppressUserConversions, PartialOverloading, PO);
}
-void Sema::AddTemplateOverloadCandidateImmediately(
- OverloadCandidateSet &CandidateSet, FunctionTemplateDecl *FunctionTemplate,
- DeclAccessPair FoundDecl, TemplateArgumentListInfo *ExplicitTemplateArgs,
- ArrayRef<Expr *> Args, bool SuppressUserConversions,
- bool PartialOverloading, bool AllowExplicit, ADLCallKind IsADLCandidate,
- OverloadCandidateParamOrder PO, bool AggregateCandidateDeduction) {
+/// Determine whether a given function template has a simple explicit specifier
+/// or a non-value-dependent explicit-specification that evaluates to true.
+static bool isNonDependentlyExplicit(FunctionTemplateDecl *FTD) {
+ return ExplicitSpecifier::getFromDecl(FTD->getTemplatedDecl()).isExplicit();
+}
+
+static void AddTemplateOverloadCandidateImmediately(
+ Sema &S, OverloadCandidateSet &CandidateSet,
+ FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
+ TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args,
+ bool SuppressUserConversions, bool PartialOverloading, bool AllowExplicit,
+ Sema::ADLCallKind IsADLCandidate, OverloadCandidateParamOrder PO,
+ bool AggregateCandidateDeduction) {
// If the function template has a non-dependent explicit specification,
// exclude it now if appropriate; we are not permitted to perform deduction
@@ -7939,14 +7917,14 @@ void Sema::AddTemplateOverloadCandidateImmediately(
FunctionTemplate->getTemplateDepth());
FunctionDecl *Specialization = nullptr;
ConversionSequenceList Conversions;
- if (TemplateDeductionResult Result = DeduceTemplateArguments(
+ if (TemplateDeductionResult Result = S.DeduceTemplateArguments(
FunctionTemplate, ExplicitTemplateArgs, Args, Specialization, Info,
PartialOverloading, AggregateCandidateDeduction,
/*PartialOrdering=*/false,
/*ObjectType=*/QualType(),
/*ObjectClassification=*/Expr::Classification(),
[&](ArrayRef<QualType> ParamTypes) {
- return CheckNonDependentConversions(
+ return S.CheckNonDependentConversions(
FunctionTemplate, ParamTypes, Args, CandidateSet, Conversions,
SuppressUserConversions, nullptr, QualType(), {}, PO);
});
@@ -7971,7 +7949,7 @@ void Sema::AddTemplateOverloadCandidateImmediately(
else {
Candidate.FailureKind = ovl_fail_bad_deduction;
Candidate.DeductionFailure =
- MakeDeductionFailureInfo(Context, Result, Info);
+ MakeDeductionFailureInfo(S.Context, Result, Info);
}
return;
}
@@ -7979,7 +7957,7 @@ void Sema::AddTemplateOverloadCandidateImmediately(
// Add the function template specialization produced by template argument
// deduction as a candidate.
assert(Specialization && "Missing function template specialization?");
- AddOverloadCandidate(
+ S.AddOverloadCandidate(
Specialization, FoundDecl, Args, CandidateSet, SuppressUserConversions,
PartialOverloading, AllowExplicit,
/*AllowExplicitConversions=*/false, IsADLCandidate, Conversions, PO,
@@ -7987,6 +7965,30 @@ void Sema::AddTemplateOverloadCandidateImmediately(
Info.hasStrictPackMatch());
}
+void Sema::AddTemplateOverloadCandidate(
+ FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
+ TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args,
+ OverloadCandidateSet &CandidateSet, bool SuppressUserConversions,
+ bool PartialOverloading, bool AllowExplicit, ADLCallKind IsADLCandidate,
+ OverloadCandidateParamOrder PO, bool AggregateCandidateDeduction) {
+ if (!CandidateSet.isNewCandidate(FunctionTemplate, PO))
+ return;
+
+ if (ExplicitTemplateArgs ||
+ !CandidateSet.shouldDeferTemplateArgumentDeduction(getLangOpts())) {
+ AddTemplateOverloadCandidateImmediately(
+ *this, CandidateSet, FunctionTemplate, FoundDecl, ExplicitTemplateArgs,
+ Args, SuppressUserConversions, PartialOverloading, AllowExplicit,
+ IsADLCandidate, PO, AggregateCandidateDeduction);
+ return;
+ }
+
+ CandidateSet.AddDeferredTemplateCandidate(
+ FunctionTemplate, FoundDecl, Args, SuppressUserConversions,
+ PartialOverloading, AllowExplicit, IsADLCandidate, PO,
+ AggregateCandidateDeduction);
+}
+
bool Sema::CheckNonDependentConversions(
FunctionTemplateDecl *FunctionTemplate, ArrayRef<QualType> ParamTypes,
ArrayRef<Expr *> Args, OverloadCandidateSet &CandidateSet,
@@ -8302,34 +8304,11 @@ void Sema::AddConversionCandidate(
}
}
-void Sema::AddTemplateConversionCandidate(
+static void AddTemplateConversionCandidateImmediately(
+ Sema &S, OverloadCandidateSet &CandidateSet,
FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
- CXXRecordDecl *ActingDC, Expr *From, QualType ToType,
- OverloadCandidateSet &CandidateSet, bool AllowObjCConversionOnExplicit,
- bool AllowExplicit, bool AllowResultConversion) {
- assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) &&
- "Only conversion function templates permitted here");
-
- if (!CandidateSet.isNewCandidate(FunctionTemplate))
- return;
-
- if (CandidateSet.getKind() == OverloadCandidateSet::CSK_CodeCompletion) {
- AddTemplateConversionCandidateImmediately(
- CandidateSet, FunctionTemplate, FoundDecl, ActingDC, From, ToType,
- AllowObjCConversionOnExplicit, AllowExplicit, AllowResultConversion);
-
- return;
- }
-
- CandidateSet.AddNonDeducedConversionTemplateCandidate(
- FunctionTemplate, FoundDecl, ActingDC, From, ToType,
- AllowObjCConversionOnExplicit, AllowExplicit, AllowResultConversion);
-}
-
-void Sema::AddTemplateConversionCandidateImmediately(
- OverloadCandidateSet &CandidateSet, FunctionTemplateDecl *FunctionTemplate,
- DeclAccessPair FoundDecl, CXXRecordDecl *ActingContext, Expr *From,
- QualType ToType, bool AllowObjCConversionOnExplicit, bool AllowExplicit,
+ CXXRecordDecl *ActingContext, Expr *From, QualType ToType,
+ bool AllowObjCConversionOnExplicit, bool AllowExplicit,
bool AllowResultConversion) {
// If the function template has a non-dependent explicit specification,
@@ -8345,11 +8324,11 @@ void Sema::AddTemplateConversionCandidateImmediately(
}
QualType ObjectType = From->getType();
- Expr::Classification ObjectClassification = From->Classify(getASTContext());
+ Expr::Classification ObjectClassification = From->Classify(S.Context);
TemplateDeductionInfo Info(CandidateSet.getLocation());
CXXConversionDecl *Specialization = nullptr;
- if (TemplateDeductionResult Result = DeduceTemplateArguments(
+ if (TemplateDeductionResult Result = S.DeduceTemplateArguments(
FunctionTemplate, ObjectType, ObjectClassification, ToType,
Specialization, Info);
Result != TemplateDeductionResult::Success) {
@@ -8360,17 +8339,42 @@ void Sema::AddTemplateConversionCandidateImmediately(
Candidate.FailureKind = ovl_fail_bad_deduction;
Candidate.ExplicitCallArguments = 1;
Candidate.DeductionFailure =
- MakeDeductionFailureInfo(Context, Result, Info);
+ MakeDeductionFailureInfo(S.Context, Result, Info);
return;
}
// Add the conversion function template specialization produced by
// template argument deduction as a candidate.
assert(Specialization && "Missing function template specialization?");
- AddConversionCandidate(Specialization, FoundDecl, ActingContext, From, ToType,
- CandidateSet, AllowObjCConversionOnExplicit,
- AllowExplicit, AllowResultConversion,
- Info.hasStrictPackMatch());
+ S.AddConversionCandidate(Specialization, FoundDecl, ActingContext, From,
+ ToType, CandidateSet, AllowObjCConversionOnExplicit,
+ AllowExplicit, AllowResultConversion,
+ Info.hasStrictPackMatch());
+}
+
+void Sema::AddTemplateConversionCandidate(
+ FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
+ CXXRecordDecl *ActingDC, Expr *From, QualType ToType,
+ OverloadCandidateSet &CandidateSet, bool AllowObjCConversionOnExplicit,
+ bool AllowExplicit, bool AllowResultConversion) {
+ assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) &&
+ "Only conversion function templates permitted here");
+
+ if (!CandidateSet.isNewCandidate(FunctionTemplate))
+ return;
+
+ if (!CandidateSet.shouldDeferTemplateArgumentDeduction(getLangOpts())) {
+ AddTemplateConversionCandidateImmediately(
+ *this, CandidateSet, FunctionTemplate, FoundDecl, ActingDC, From,
+ ToType, AllowObjCConversionOnExplicit, AllowExplicit,
+ AllowResultConversion);
+
+ return;
+ }
+
+ CandidateSet.AddDeferredConversionTemplateCandidate(
+ FunctionTemplate, FoundDecl, ActingDC, From, ToType,
+ AllowObjCConversionOnExplicit, AllowExplicit, AllowResultConversion);
}
void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion,
@@ -8521,11 +8525,14 @@ void Sema::AddNonMemberOperatorCandidates(
AddTemplateOverloadCandidate(FunTmpl, F.getPair(), ExplicitTemplateArgs,
FunctionArgs, CandidateSet);
if (CandidateSet.getRewriteInfo().shouldAddReversed(*this, Args, FD)) {
- ArrayRef<Expr *> Reversed = CandidateSet.getPersistentArgsArray(FunctionArgs[1],
- FunctionArgs[0]);
+
+ // As template candidates are not deduced immediately,
+ // persist the arry in the overload set.
+ ArrayRef<Expr *> Reversed = CandidateSet.getPersistentArgsArray(
+ FunctionArgs[1], FunctionArgs[0]);
AddTemplateOverloadCandidate(FunTmpl, F.getPair(), ExplicitTemplateArgs,
- Reversed, CandidateSet, false, false,
- true, ADLCallKind::NotADL,
+ Reversed, CandidateSet, false, false, true,
+ ADLCallKind::NotADL,
OverloadCandidateParamOrder::Reversed);
}
} else {
@@ -8533,8 +8540,8 @@ void Sema::AddNonMemberOperatorCandidates(
continue;
AddOverloadCandidate(FD, F.getPair(), FunctionArgs, CandidateSet);
if (CandidateSet.getRewriteInfo().shouldAddReversed(*this, Args, FD))
- AddOverloadCandidate(FD, F.getPair(), {FunctionArgs[1],
- FunctionArgs[0]}, CandidateSet,
+ AddOverloadCandidate(FD, F.getPair(),
+ {FunctionArgs[1], FunctionArgs[0]}, CandidateSet,
false, false, true, false, ADLCallKind::NotADL, {},
OverloadCandidateParamOrder::Reversed);
}
@@ -10259,11 +10266,7 @@ Sema::AddArgumentDependentLookupCandidates(DeclarationName Name,
// FIXME: Pass in the explicit template arguments?
ArgumentDependentLookup(Name, Loc, Args, Fns);
- auto ReversedArgs = [&, Arr = ArrayRef<Expr *>{}]() mutable {
- if (Arr.empty())
- Arr = CandidateSet.getPersistentArgsArray(Args[1], Args[0]);
- return Arr;
- };
+ ArrayRef<Expr *> ReversedArgs;
// Erase all of the candidates we already knew about.
for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(),
@@ -10291,7 +10294,7 @@ Sema::AddArgumentDependentLookupCandidates(DeclarationName Name,
/*AllowExplicitConversion=*/false, ADLCallKind::UsesADL);
if (CandidateSet.getRewriteInfo().shouldAddReversed(*this, Args, FD)) {
AddOverloadCandidate(
- FD, FoundDecl, ReversedArgs(), CandidateSet,
+ FD, FoundDecl, {Args[1], Args[0]}, CandidateSet,
/*SuppressUserConversions=*/false, PartialOverloading,
/*AllowExplicit=*/true, /*AllowExplicitConversion=*/false,
ADLCallKind::UsesADL, {}, OverloadCandidateParamOrder::Reversed);
@@ -10304,8 +10307,14 @@ Sema::AddArgumentDependentLookupCandidates(DeclarationName Name,
/*AllowExplicit=*/true, ADLCallKind::UsesADL);
if (CandidateSet.getRewriteInfo().shouldAddReversed(
*this, Args, FTD->getTemplatedDecl())) {
+
+ // As template candidates are not deduced immediately,
+ // persist the arry in the overload set.
+ if (ReversedArgs.empty())
+ ReversedArgs = CandidateSet.getPersistentArgsArray(Args[1], Args[0]);
+
AddTemplateOverloadCandidate(
- FTD, FoundDecl, ExplicitTemplateArgs, ReversedArgs(), CandidateSet,
+ FTD, FoundDecl, ExplicitTemplateArgs, ReversedArgs, CandidateSet,
/*SuppressUserConversions=*/false, PartialOverloading,
/*AllowExplicit=*/true, ADLCallKind::UsesADL,
OverloadCandidateParamOrder::Reversed);
@@ -10979,91 +10988,143 @@ bool OverloadCandidate::NotValidBecauseConstraintExprHasError() const {
->Satisfaction.ContainsErrors;
}
-void OverloadCandidateSet::AddNonDeducedTemplateCandidate(
+void OverloadCandidateSet::AddDeferredTemplateCandidate(
FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
ArrayRef<Expr *> Args, bool SuppressUserConversions,
bool PartialOverloading, bool AllowExplicit,
CallExpr::ADLCallKind IsADLCandidate, OverloadCandidateParamOrder PO,
bool AggregateCandidateDeduction) {
- NonDeducedFunctionTemplateOverloadCandidate C{FunctionTemplate,
- FoundDecl,
- Args,
- IsADLCandidate,
- PO,
- SuppressUserConversions,
- PartialOverloading,
- AllowExplicit,
- AggregateCandidateDeduction};
- NonDeducedCandidates.emplace_back(std::move(C));
+ DeferredFunctionTemplateOverloadCandidate C{FunctionTemplate,
+ FoundDecl,
+ Args,
+ IsADLCandidate,
+ PO,
+ SuppressUserConversions,
+ PartialOverloading,
+ AllowExplicit,
+ AggregateCandidateDeduction};
+ DeferredCandidates.emplace_back(std::move(C));
}
-void OverloadCandidateSet::AddNonDeducedMethodTemplateCandidate(
+void OverloadCandidateSet::AddDeferredMethodTemplateCandidate(
FunctionTemplateDecl *MethodTmpl, DeclAccessPair FoundDecl,
CXXRecordDecl *ActingContext, QualType ObjectType,
Expr::Classification ObjectClassification, ArrayRef<Expr *> Args,
bool SuppressUserConversions, bool PartialOverloading,
OverloadCandidateParamOrder PO) {
- NonDeducedMethodTemplateOverloadCandidate C{
+ DeferredMethodTemplateOverloadCandidate C{
MethodTmpl, FoundDecl, Args, ActingContext,
ObjectClassification, ObjectType, PO, SuppressUserConversions,
PartialOverloading};
- NonDeducedCandidates.emplace_back(std::move(C));
+ DeferredCandidates.emplace_back(std::move(C));
}
-void OverloadCandidateSet::AddNonDeducedConversionTemplateCandidate(
+void OverloadCandidateSet::AddDeferredConversionTemplateCandidate(
FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
CXXRecordDecl *ActingContext, Expr *From, QualType ToType,
bool AllowObjCConversionOnExplicit, bool AllowExplicit,
bool AllowResultConversion) {
- NonDeducedConversionTemplateOverloadCandidate C{
+ DeferredConversionTemplateOverloadCandidate C{
FunctionTemplate, FoundDecl,
ActingContext, From,
ToType, AllowObjCConversionOnExplicit,
AllowExplicit, AllowResultConversion};
- NonDeducedCandidates.emplace_back(std::move(C));
+ DeferredCandidates.emplace_back(std::move(C));
}
static void
AddTemplateOverloadCandidate(Sema &S, OverloadCandidateSet &CandidateSet,
- NonDeducedMethodTemplateOverloadCandidate &&C) {
+ DeferredMethodTemplateOverloadCandidate &&C) {
- S.AddMethodTemplateCandidateImmediately(
- CandidateSet, C.FunctionTemplate, C.FoundDecl, C.ActingContext,
+ AddMethodTemplateCandidateImmediately(
+ S, CandidateSet, C.FunctionTemplate, C.FoundDecl, C.ActingContext,
/*ExplicitTemplateArgs=*/nullptr, C.ObjectType, C.ObjectClassification,
C.Args, C.SuppressUserConversions, C.PartialOverloading, C.PO);
}
static void
AddTemplateOverloadCandidate(Sema &S, OverloadCandidateSet &CandidateSet,
- NonDeducedFunctionTemplateOverloadCandidate &&C) {
- S.AddTemplateOverloadCandidateImmediately(
- CandidateSet, C.FunctionTemplate, C.FoundDecl,
+ DeferredFunctionTemplateOverloadCandidate &&C) {
+ AddTemplateOverloadCandidateImmediately(
+ S, CandidateSet, C.FunctionTemplate, C.FoundDecl,
/*ExplicitTemplateArgs=*/nullptr, C.Args, C.SuppressUserConversions,
C.PartialOverloading, C.AllowExplicit, C.IsADLCandidate, C.PO,
C.AggregateCandidateDeduction);
}
-static void AddTemplateOverloadCandidate(
- Sema &S, OverloadCandidateSet &CandidateSet,
- NonDeducedConversionTemplateOverloadCandidate &&C) {
- return S.AddTemplateConversionCandidateImmediately(
- CandidateSet, C.FunctionTemplate, C.FoundDecl, C.ActingContext, C.From,
+static void
+AddTemplateOverloadCandidate(Sema &S, OverloadCandidateSet &CandidateSet,
+ DeferredConversionTemplateOverloadCandidate &&C) {
+ return AddTemplateConversionCandidateImmediately(
+ S, CandidateSet, C.FunctionTemplate, C.FoundDecl, C.ActingContext, C.From,
C.ToType, C.AllowObjCConversionOnExplicit, C.AllowExplicit,
C.AllowResultConversion);
}
void OverloadCandidateSet::InjectNonDeducedTemplateCandidates(Sema &S) {
- Candidates.reserve(Candidates.size() + NonDeducedCandidates.size());
- for (auto &&Elem : NonDeducedCandidates) {
+ Candidates.reserve(Candidates.size() + DeferredCandidates.size());
+ for (auto &&Elem : DeferredCandidates) {
std::visit(
[&](auto &&Cand) {
AddTemplateOverloadCandidate(S, *this, std::move(Cand));
},
Elem);
}
- NonDeducedCandidates.clear();
+ DeferredCandidates.clear();
+}
+
+OverloadingResult
+OverloadCandidateSet::ResultForBestCandidate(const iterator &Best) {
+ Best->Best = true;
+ if (Best->Function && Best->Function->isDeleted())
+ return OR_Deleted;
+ if (auto *M = dyn_cast_or_null<CXXMethodDecl>(Best->Function);
+ Kind == CSK_AddressOfOverloadSet && M &&
+ M->isImplicitObjectMemberFunction()) {
+ return OR_No_Viable_Function;
+ }
+ return OR_Success;
+}
+
+void OverloadCandidateSet::CudaExcludeWrongSideCandidates(Sema &S) {
+ // [CUDA] HD->H or HD->D calls are technically not allowed by CUDA but
+ // are accepted by both clang and NVCC. However, during a particular
+ // compilation mode only one call variant is viable. We need to
+ // exclude non-viable overload candidates from consideration based
+ // only on their host/device attributes. Specifically, if one
+ // candidate call is WrongSide and the other is SameSide, we ignore
+ // the WrongSide candidate.
+ // We only need to remove wrong-sided candidates here if
+ // -fgpu-exclude-wrong-side-overloads is off. When
+ // -fgpu-exclude-wrong-side-overloads is on, all candidates are compared
+ // uniformly in isBetterOverloadCandidate.
+ if (!S.getLangOpts().CUDA || S.getLangOpts().GPUExcludeWrongSideOverloads)
+ return;
+ const FunctionDecl *Caller = S.getCurFunctionDecl(/*AllowLambda=*/true);
+
+ bool ContainsSameSideCandidate =
+ llvm::any_of(Candidates, [&](const OverloadCandidate &Cand) {
+ // Check viable function only.
+ return Cand.Viable && Cand.Function &&
+ S.CUDA().IdentifyPreference(Caller, Cand.Function) ==
+ SemaCUDA::CFP_SameSide;
+ });
+ if (!ContainsSameSideCandidate)
+ return;
+
+ auto IsWrongSideCandidate = [&](const OverloadCandidate &Cand) {
+ // Check viable function only to avoid unnecessary data copying/moving.
+ return Cand.Viable && Cand.Function &&
+ S.CUDA().IdentifyPreference(Caller, Cand.Function) ==
+ SemaCUDA::CFP_WrongSide;
+ };
+
+ for (auto &Cand : Candidates) {
+ if (IsWrongSideCandidate(Cand))
+ Cand.Viable = false;
+ }
}
/// Computes the best viable function (C++ 13.3.3)
@@ -11080,41 +11141,56 @@ OverloadingResult
OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc,
iterator &Best) {
- bool TwoPhaseResolution =
- !NonDeducedCandidates.empty() && Kind != CSK_CodeCompletion &&
- Kind != CSK_InitByUserDefinedConversion && Kind != CSK_InitByConstructor;
+ assert(shouldDeferTemplateArgumentDeduction(S.getLangOpts()) ||
+ DeferredCandidates.empty() &&
+ "Unexpected deferred template candidate");
+
+ if (S.getLangOpts().CUDA)
+ CudaExcludeWrongSideCandidates(S);
+
+ bool TwoPhaseResolution = !DeferredCandidates.empty();
if (TwoPhaseResolution) {
- Best = end();
- for (auto It = begin(); It != end(); ++It) {
- if (It->isPerfectMatch(S.getASTContext())) {
- if (Best == end()) {
- Best = It;
- } else {
- Best = end();
- break;
- }
- }
- }
- if (Best != end()) {
- Best->Best = true;
- if (Best->Function && Best->Function->isDeleted())
- return OR_Deleted;
- if (auto *M = dyn_cast_or_null<CXXMethodDecl>(Best->Function);
- Kind == CSK_AddressOfOverloadSet && M &&
- M->isImplicitObjectMemberFunction()) {
- return OR_No_Viable_Function;
- }
- return OR_Success;
- }
- }
- if(!NonDeducedCandidates.empty())
+ PerfectViableFunction(S, Loc, Best);
+ if (Best != end())
+ return ResultForBestCandidate(Best);
+
InjectNonDeducedTemplateCandidates(S);
+ if (S.getLangOpts().CUDA)
+ CudaExcludeWrongSideCandidates(S);
+ }
+
return BestViableFunctionImpl(S, Loc, Best);
}
+void OverloadCandidateSet::PerfectViableFunction(
+ Sema &S, SourceLocation Loc, OverloadCandidateSet::iterator &Best) {
+ Best = end();
+ for (auto It = begin(); It != end(); ++It) {
+ if (It->isPerfectMatch(S.getASTContext())) {
+ if (Best == end()) {
+ Best = It;
+ } else {
+ if (Best->Function && It->Function) {
+ FunctionDecl *D =
+ S.getMoreConstrainedFunction(Best->Function, It->Function);
+ if (D == nullptr) {
+ Best = end();
+ break;
+ }
+ if (D == It->Function)
+ Best = It;
+ continue;
+ }
+ Best = end();
+ break;
+ }
+ }
+ }
+}
+
OverloadingResult OverloadCandidateSet::BestViableFunctionImpl(
Sema &S, SourceLocation Loc, OverloadCandidateSet::iterator &Best) {
@@ -11123,37 +11199,6 @@ OverloadingResult OverloadCandidateSet::BestViableFunctionImpl(
std::transform(begin(), end(), std::back_inserter(Candidates),
[](OverloadCandidate &Cand) { return &Cand; });
- // [CUDA] HD->H or HD->D calls are technically not allowed by CUDA but
- // are accepted by both clang and NVCC. However, during a particular
- // compilation mode only one call variant is viable. We need to
- // exclude non-viable overload candidates from consideration based
- // only on their host/device attributes. Specifically, if one
- // candidate call is WrongSide and the other is SameSide, we ignore
- // the WrongSide candidate.
- // We only need to remove wrong-sided candidates here if
- // -fgpu-exclude-wrong-side-overloads is off. When
- // -fgpu-exclude-wrong-side-overloads is on, all candidates are compared
- // uniformly in isBetterOverloadCandidate.
- if (S.getLangOpts().CUDA && !S.getLangOpts().GPUExcludeWrongSideOverloads) {
- const FunctionDecl *Caller = S.getCurFunctionDecl(/*AllowLambda=*/true);
- bool ContainsSameSideCandidate =
- llvm::any_of(Candidates, [&](OverloadCandidate *Cand) {
- // Check viable function only.
- return Cand->Viable && Cand->Function &&
- S.CUDA().IdentifyPreference(Caller, Cand->Function) ==
- SemaCUDA::CFP_SameSide;
- });
- if (ContainsSameSideCandidate) {
- auto IsWrongSideCandidate = [&](OverloadCandidate *Cand) {
- // Check viable function only to avoid unnecessary data copying/moving.
- return Cand->Viable && Cand->Function &&
- S.CUDA().IdentifyPreference(Caller, Cand->Function) ==
- SemaCUDA::CFP_WrongSide;
- };
- llvm::erase_if(Candidates, IsWrongSideCandidate);
- }
- }
-
Best = end();
for (auto *Cand : Candidates) {
Cand->Best = false;
@@ -11198,23 +11243,16 @@ OverloadingResult OverloadCandidateSet::BestViableFunctionImpl(
}
}
}
+
if (Best == end())
return OR_Ambiguous;
- // Best is the best viable function.
- if (Best->Function && Best->Function->isDeleted())
- return OR_Deleted;
+ OverloadingResult R = ResultForBestCandidate(Best);
- if (auto *M = dyn_cast_or_null<CXXMethodDecl>(Best->Function);
- Kind == CSK_AddressOfOverloadSet && M &&
- M->isImplicitObjectMemberFunction()) {
- return OR_No_Viable_Function;
- }
-
- if (NonDeducedCandidates.empty() && !EquivalentCands.empty())
+ if (DeferredCandidates.empty() && !EquivalentCands.empty())
S.diagnoseEquivalentInternalLinkageDeclarations(Loc, Best->Function,
EquivalentCands);
- return OR_Success;
+ return R;
}
namespace {
@@ -14888,23 +14926,24 @@ void Sema::LookupOverloadedBinOp(OverloadCandidateSet &CandidateSet,
// rewritten candidates using these functions if necessary.
AddNonMemberOperatorCandidates(Fns, Args, CandidateSet);
- auto ReversedArgs = [&, Arr = ArrayRef<Expr *>{}]() mutable {
- if (Arr.empty())
- Arr = CandidateSet.getPersistentArgsArray(Args[1], Args[0]);
- return Arr;
- };
+ // As template candidates are not deduced immediately,
+ // persist the arry in the overload set.
+ ArrayRef<Expr *> ReversedArgs;
+ if (CandidateSet.getRewriteInfo().allowsReversed(Op) ||
+ CandidateSet.getRewriteInfo().allowsReversed(ExtraOp))
+ ReversedArgs = CandidateSet.getPersistentArgsArray(Args[1], Args[0]);
// Add operator candidates that are member functions.
AddMemberOperatorCandidates(Op, OpLoc, Args, CandidateSet);
if (CandidateSet.getRewriteInfo().allowsReversed(Op))
- AddMemberOperatorCandidates(Op, OpLoc, ReversedArgs(), CandidateSet,
+ AddMemberOperatorCandidates(Op, OpLoc, ReversedArgs, CandidateSet,
OverloadCandidateParamOrder::Reversed);
// In C++20, also add any rewritten member candidates.
if (ExtraOp) {
AddMemberOperatorCandidates(ExtraOp, OpLoc, Args, CandidateSet);
if (CandidateSet.getRewriteInfo().allowsReversed(ExtraOp))
- AddMemberOperatorCandidates(ExtraOp, OpLoc, ReversedArgs(), CandidateSet,
+ AddMemberOperatorCandidates(ExtraOp, OpLoc, ReversedArgs, CandidateSet,
OverloadCandidateParamOrder::Reversed);
}
diff --git a/clang/test/CXX/temp/temp.constr/temp.constr.atomic/constrant-satisfaction-conversions.cpp b/clang/test/CXX/temp/temp.constr/temp.constr.atomic/constrant-satisfaction-conversions.cpp
index ba8e2dc372e98..083e743818121 100644
--- a/clang/test/CXX/temp/temp.constr/temp.constr.atomic/constrant-satisfaction-conversions.cpp
+++ b/clang/test/CXX/temp/temp.constr/temp.constr.atomic/constrant-satisfaction-conversions.cpp
@@ -14,7 +14,7 @@ template<typename T> struct S {
// expected-note@#FINST{{in instantiation of function template specialization}}
template<typename T> requires (S<T>{})
void f(T);
-void f(int);
+void f(long);
// Ensure this applies to operator && as well.
// expected-error at +3{{atomic constraint must be of type 'bool' (found 'S<int>')}}
@@ -22,7 +22,7 @@ void f(int);
// expected-note@#F2INST{{in instantiation of function template specialization}}
template<typename T> requires (S<T>{} && true)
void f2(T);
-void f2(int);
+void f2(long);
template<typename T> requires requires {
requires S<T>{};
@@ -36,12 +36,12 @@ template<typename T> requires requires {
//
}
void f3(T);
-void f3(int);
+void f3(long);
// Doesn't diagnose, since this is no longer a compound requirement.
template<typename T> requires (bool(1 && 2))
void f4(T);
-void f4(int);
+void f4(long);
void g() {
f(0); // #FINST
diff --git a/clang/test/SemaCXX/implicit-member-functions.cpp b/clang/test/SemaCXX/implicit-member-functions.cpp
index 1554b1af5d59a..8350eac5b88a0 100644
--- a/clang/test/SemaCXX/implicit-member-functions.cpp
+++ b/clang/test/SemaCXX/implicit-member-functions.cpp
@@ -54,31 +54,24 @@ namespace PR7594 {
namespace Recursion {
template<typename T> struct InvokeCopyConstructor {
static const T &get();
- typedef decltype(T(get())) type; // expected-error {{no matching conver}}
+ typedef decltype(T(get())) type;
};
struct B;
struct A {
- // expected-note at -1 {{while substituting deduced template arguments}}
typedef B type;
template<typename T,
typename = typename InvokeCopyConstructor<typename T::type>::type>
- // expected-note at -1 {{in instantiation of template class}}
A(const T &);
- // expected-note at -1 {{in instantiation of default argument}}
};
- struct B { // expected-note {{while declaring the implicit copy constructor for 'B'}}
- // expected-note at -1 {{candidate constructor (the implicit move }}
- B(); // expected-note {{candidate constructor not viable}}
+ struct B {
+ B();
A a;
};
// Triggering the declaration of B's copy constructor causes overload
- // resolution to occur for A's copying constructor, which instantiates
- // InvokeCopyConstructor<B>, which triggers the declaration of B's copy
- // constructor. Notionally, this happens when we get to the end of the
- // definition of 'struct B', so there is no declared copy constructor yet.
- //
- // This behavior is g++-compatible, but isn't exactly right; the class is
- // supposed to be incomplete when we implicitly declare its special members.
+ // resolution to occur for A's copying constructor, which picks
+ // the implicit copy constructor of A.
+ // Because that copy constructor is always a perfect match the template
+ // candidate is not instantiated.
B b = B();
diff --git a/clang/test/SemaTemplate/instantiate-function-params.cpp b/clang/test/SemaTemplate/instantiate-function-params.cpp
index 7dd5595de58a3..eb2a7c5d4e8d6 100644
--- a/clang/test/SemaTemplate/instantiate-function-params.cpp
+++ b/clang/test/SemaTemplate/instantiate-function-params.cpp
@@ -6,13 +6,12 @@ template<typename T1> struct if_ {
typedef if_c< static_cast<bool>(T1::value)> almost_type_; // expected-note 7{{in instantiation}}
};
template <class Model, void (Model::*)()> struct wrap_constraints { };
-template <class Model>
+template <class Model>
inline char has_constraints_(Model* , // expected-note 3{{candidate template ignored}}
- wrap_constraints<Model,&Model::constraints>* = 0); // expected-note 4{{in instantiation}}
-
+ wrap_constraints<Model,&Model::constraints>* = 0);
template <class Model> struct not_satisfied {
static const bool value = sizeof( has_constraints_((Model*)0) == 1); // expected-error 3{{no matching function}} \
- // expected-note 4{{while substituting deduced template arguments into function template 'has_constraints_' [with }}
+ // expected-note 4{{in instantiation}}
};
template <class ModelFn> struct requirement_;
template <void(*)()> struct instantiate {
diff --git a/clang/test/Templight/templight-empty-entries-fix.cpp b/clang/test/Templight/templight-empty-entries-fix.cpp
index d13b748068efe..9f4590cec2333 100644
--- a/clang/test/Templight/templight-empty-entries-fix.cpp
+++ b/clang/test/Templight/templight-empty-entries-fix.cpp
@@ -1,6 +1,6 @@
// RUN: %clang_cc1 -templight-dump -Wno-unused-value %s 2>&1 | FileCheck %s
-void a() {
+void a(long) {
[] {};
}
@@ -17,14 +17,14 @@ void a() {
// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:4:3'$}}
// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:4:3'$}}
-template <int = 0> void a() { a(); }
+template <int = 0> void a(long) { a(0); }
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+a$}}
// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+Begin$}}
// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:20:25'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:20:31'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:20:35'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+unnamed template non-type parameter 0 of a$}}
// CHECK: {{^kind:[ ]+DefaultTemplateArgumentInstantiation$}}
@@ -42,29 +42,29 @@ template <int = 0> void a() { a(); }
// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+End$}}
// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:20:25'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:20:31'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:20:35'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+'a<0>'$}}
// CHECK: {{^kind:[ ]+TemplateInstantiation$}}
// CHECK: {{^event:[ ]+Begin$}}
// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:20:25'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:20:31'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:20:35'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+'a<0>'$}}
// CHECK: {{^kind:[ ]+TemplateInstantiation$}}
// CHECK: {{^event:[ ]+End$}}
// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:20:25'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:20:31'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:20:35'$}}
template <int> struct b { typedef int c; };
-template <bool d = true, class = typename b<d>::c> void a() { a(); }
+template <bool d = true, class = typename b<d>::c> void a(long) { a(0); }
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+a$}}
// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+Begin$}}
// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:60:57'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:63'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:67'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+d$}}
// CHECK: {{^kind:[ ]+DefaultTemplateArgumentInstantiation$}}
@@ -130,25 +130,25 @@ template <bool d = true, class = typename b<d>::c> void a() { a(); }
// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+End$}}
// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:60:57'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:63'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:67'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+'a<true, int>'$}}
// CHECK: {{^kind:[ ]+TemplateInstantiation$}}
// CHECK: {{^event:[ ]+Begin$}}
// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:60:57'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:63'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:67'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+'a<true, int>'$}}
// CHECK: {{^kind:[ ]+TemplateInstantiation$}}
// CHECK: {{^event:[ ]+End$}}
// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:60:57'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:63'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:67'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+a$}}
// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+Begin$}}
// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:20:25'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:63'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:67'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+unnamed template non-type parameter 0 of a$}}
// CHECK: {{^kind:[ ]+DefaultTemplateArgumentInstantiation$}}
@@ -166,7 +166,7 @@ template <bool d = true, class = typename b<d>::c> void a() { a(); }
// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+End$}}
// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:20:25'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:63'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:67'$}}
template <bool = true> void d(int = 0) { d(); }
@@ -175,25 +175,25 @@ template <bool = true> void d(int = 0) { d(); }
// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+Begin$}}
// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:60:57'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:63'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:67'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+a$}}
// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+End$}}
// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:60:57'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:63'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:67'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+a$}}
// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+Begin$}}
// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:20:25'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:63'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:67'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+a$}}
// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+End$}}
// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:20:25'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:63'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:67'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+d$}}
// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
>From dc59ceb359307c9d2f7ca0091c5ae73079c0f82f Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Mon, 31 Mar 2025 17:06:02 +0200
Subject: [PATCH 07/18] Fix templight test
---
.../Templight/templight-empty-entries-fix.cpp | 100 +++++++-----------
1 file changed, 38 insertions(+), 62 deletions(-)
diff --git a/clang/test/Templight/templight-empty-entries-fix.cpp b/clang/test/Templight/templight-empty-entries-fix.cpp
index 9f4590cec2333..7f34b10134929 100644
--- a/clang/test/Templight/templight-empty-entries-fix.cpp
+++ b/clang/test/Templight/templight-empty-entries-fix.cpp
@@ -170,30 +170,6 @@ template <bool d = true, class = typename b<d>::c> void a(long) { a(0); }
template <bool = true> void d(int = 0) { d(); }
-// CHECK-LABEL: {{^---$}}
-// CHECK: {{^name:[ ]+a$}}
-// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
-// CHECK: {{^event:[ ]+Begin$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:60:57'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:67'$}}
-// CHECK-LABEL: {{^---$}}
-// CHECK: {{^name:[ ]+a$}}
-// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
-// CHECK: {{^event:[ ]+End$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:60:57'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:67'$}}
-// CHECK-LABEL: {{^---$}}
-// CHECK: {{^name:[ ]+a$}}
-// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
-// CHECK: {{^event:[ ]+Begin$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:20:25'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:67'$}}
-// CHECK-LABEL: {{^---$}}
-// CHECK: {{^name:[ ]+a$}}
-// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
-// CHECK: {{^event:[ ]+End$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:20:25'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:60:67'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+d$}}
// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
@@ -249,41 +225,41 @@ void e() {
}
// CHECK-LABEL: {{^---$}}
-// CHECK: {{^name:[ ]+'\(unnamed struct at .*templight-empty-entries-fix.cpp:247:3\)'$}}
+// CHECK: {{^name:[ ]+'\(unnamed struct at .*templight-empty-entries-fix.cpp:223:3\)'$}}
// CHECK: {{^kind:[ ]+Memoization$}}
// CHECK: {{^event:[ ]+Begin$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:247:3'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:248:5'$}}
+// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:223:3'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:224:5'$}}
// CHECK-LABEL: {{^---$}}
-// CHECK: {{^name:[ ]+'\(unnamed struct at .*templight-empty-entries-fix.cpp:247:3\)'$}}
+// CHECK: {{^name:[ ]+'\(unnamed struct at .*templight-empty-entries-fix.cpp:223:3\)'$}}
// CHECK: {{^kind:[ ]+Memoization$}}
// CHECK: {{^event:[ ]+End$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:247:3'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:248:5'$}}
+// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:223:3'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:224:5'$}}
// CHECK-LABEL: {{^---$}}
-// CHECK: {{^name:[ ]+'\(unnamed struct at .*templight-empty-entries-fix.cpp:247:3\)'$}}
+// CHECK: {{^name:[ ]+'\(unnamed struct at .*templight-empty-entries-fix.cpp:223:3\)'$}}
// CHECK: {{^kind:[ ]+Memoization$}}
// CHECK: {{^event:[ ]+Begin$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:247:3'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:248:5'$}}
+// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:223:3'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:224:5'$}}
// CHECK-LABEL: {{^---$}}
-// CHECK: {{^name:[ ]+'\(unnamed struct at .*templight-empty-entries-fix.cpp:247:3\)'$}}
+// CHECK: {{^name:[ ]+'\(unnamed struct at .*templight-empty-entries-fix.cpp:223:3\)'$}}
// CHECK: {{^kind:[ ]+Memoization$}}
// CHECK: {{^event:[ ]+End$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:247:3'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:248:5'$}}
+// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:223:3'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:224:5'$}}
// CHECK-LABEL: {{^---$}}
-// CHECK: {{^name:[ ]+'\(unnamed struct at .*templight-empty-entries-fix.cpp:247:3\)'$}}
+// CHECK: {{^name:[ ]+'\(unnamed struct at .*templight-empty-entries-fix.cpp:223:3\)'$}}
// CHECK: {{^kind:[ ]+Memoization$}}
// CHECK: {{^event:[ ]+Begin$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:247:3'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:247:3'$}}
+// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:223:3'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:223:3'$}}
// CHECK-LABEL: {{^---$}}
-// CHECK: {{^name:[ ]+'\(unnamed struct at .*templight-empty-entries-fix.cpp:247:3\)'$}}
+// CHECK: {{^name:[ ]+'\(unnamed struct at .*templight-empty-entries-fix.cpp:223:3\)'$}}
// CHECK: {{^kind:[ ]+Memoization$}}
// CHECK: {{^event:[ ]+End$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:247:3'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:247:3'$}}
+// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:223:3'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:223:3'$}}
template <template<typename> class>
@@ -299,71 +275,71 @@ void foo() {
// CHECK: {{^name:[ ]+d$}}
// CHECK: {{^kind:[ ]+ExplicitTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+Begin$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:290:6'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:295:3'$}}
+// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:266:6'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:271:3'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+unnamed template template parameter 0 of d$}}
// CHECK: {{^kind:[ ]+PriorTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+Begin$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:289:35'$}}
+// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:265:35'$}}
// CHECK: {{^poi:[ ]+''$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+unnamed template template parameter 0 of d$}}
// CHECK: {{^kind:[ ]+PriorTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+End$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:289:35'$}}
+// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:265:35'$}}
// CHECK: {{^poi:[ ]+''$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+unnamed template template parameter 0 of d$}}
// CHECK: {{^kind:[ ]+PartialOrderingTTP$}}
// CHECK: {{^event:[ ]+Begin$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:289:35'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:295:5'$}}
+// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:265:35'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:271:5'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+unnamed template template parameter 0 of d$}}
// CHECK: {{^kind:[ ]+PartialOrderingTTP$}}
// CHECK: {{^event:[ ]+End$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:289:35'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:295:5'$}}
+// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:265:35'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:271:5'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+d$}}
// CHECK: {{^kind:[ ]+ExplicitTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+End$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:290:6'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:295:3'$}}
+// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:266:6'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:271:3'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+d$}}
// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+Begin$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:290:6'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:295:3'$}}
+// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:266:6'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:271:3'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+d$}}
// CHECK: {{^kind:[ ]+DeducedTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+End$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:290:6'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:295:3'$}}
+// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:266:6'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:271:3'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+'d<C>'$}}
// CHECK: {{^kind:[ ]+TemplateInstantiation$}}
// CHECK: {{^event:[ ]+Begin$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:290:6'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:295:3'$}}
+// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:266:6'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:271:3'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+'d<C>'$}}
// CHECK: {{^kind:[ ]+TemplateInstantiation$}}
// CHECK: {{^event:[ ]+End$}}
-// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:290:6'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:295:3'$}}
+// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:266:6'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:271:3'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+d$}}
// CHECK: {{^kind:[ ]+ExplicitTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+Begin$}}
// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:171:29'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:295:3'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:271:3'$}}
// CHECK-LABEL: {{^---$}}
// CHECK: {{^name:[ ]+d$}}
// CHECK: {{^kind:[ ]+ExplicitTemplateArgumentSubstitution$}}
// CHECK: {{^event:[ ]+End$}}
// CHECK: {{^orig:[ ]+'.*templight-empty-entries-fix.cpp:171:29'$}}
-// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:295:3'$}}
+// CHECK: {{^poi:[ ]+'.*templight-empty-entries-fix.cpp:271:3'$}}
>From 3fd24e03b97a22050a1c91956a12c09b7327e580 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Mon, 31 Mar 2025 17:09:41 +0200
Subject: [PATCH 08/18] format
---
clang/lib/Sema/SemaOverload.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 521f3efc646f1..6fc24c0cd44ec 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -11137,9 +11137,9 @@ void OverloadCandidateSet::CudaExcludeWrongSideCandidates(Sema &S) {
/// function, \p Best points to the candidate function found.
///
/// \returns The result of overload resolution.
-OverloadingResult
-OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc,
- iterator &Best) {
+OverloadingResult OverloadCandidateSet::BestViableFunction(Sema &S,
+ SourceLocation Loc,
+ iterator &Best) {
assert(shouldDeferTemplateArgumentDeduction(S.getLangOpts()) ||
DeferredCandidates.empty() &&
>From 7694e81328346d474701d6109c1e919fd8dbf4c4 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Mon, 31 Mar 2025 17:30:10 +0200
Subject: [PATCH 09/18] do not skip templates when initializing by constructors
as that might include a conversion
---
clang/include/clang/Sema/Overload.h | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h
index 63339a917c3a4..547845c01db11 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -1448,7 +1448,9 @@ class Sema;
// For user defined conversion we need to check against different
// combination of CV qualifiers and look at any expicit specifier, so
// always deduce template candidate.
- Kind != CSK_InitByUserDefinedConversion && Kind != CSK_CodeCompletion &&
+ Kind != CSK_InitByUserDefinedConversion
+ && Kind != CSK_InitByConstructor
+ && Kind != CSK_CodeCompletion &&
Opts.CPlusPlus && (!Opts.CUDA || Opts.GPUExcludeWrongSideOverloads);
}
>From 4014940940a03d495a7e2419ff2b0e4e004addeb Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Mon, 31 Mar 2025 17:53:23 +0200
Subject: [PATCH 10/18] move and document the special handling of implicit
object member function in non-member calls
---
clang/include/clang/Sema/Overload.h | 5 ++---
clang/lib/Sema/SemaOverload.cpp | 26 +++++++++++++++++---------
2 files changed, 19 insertions(+), 12 deletions(-)
diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h
index 547845c01db11..88b0e49b99ca7 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -1448,9 +1448,8 @@ class Sema;
// For user defined conversion we need to check against different
// combination of CV qualifiers and look at any expicit specifier, so
// always deduce template candidate.
- Kind != CSK_InitByUserDefinedConversion
- && Kind != CSK_InitByConstructor
- && Kind != CSK_CodeCompletion &&
+ Kind != CSK_InitByUserDefinedConversion &&
+ Kind != CSK_InitByConstructor && Kind != CSK_CodeCompletion &&
Opts.CPlusPlus && (!Opts.CUDA || Opts.GPUExcludeWrongSideOverloads);
}
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 6fc24c0cd44ec..b050d08849297 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -11080,11 +11080,6 @@ OverloadCandidateSet::ResultForBestCandidate(const iterator &Best) {
Best->Best = true;
if (Best->Function && Best->Function->isDeleted())
return OR_Deleted;
- if (auto *M = dyn_cast_or_null<CXXMethodDecl>(Best->Function);
- Kind == CSK_AddressOfOverloadSet && M &&
- M->isImplicitObjectMemberFunction()) {
- return OR_No_Viable_Function;
- }
return OR_Success;
}
@@ -14572,10 +14567,12 @@ ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn,
Expr *ExecConfig,
bool AllowTypoCorrection,
bool CalleesAddressIsTaken) {
- OverloadCandidateSet CandidateSet(
- Fn->getExprLoc(), CalleesAddressIsTaken
- ? OverloadCandidateSet::CSK_AddressOfOverloadSet
- : OverloadCandidateSet::CSK_Normal);
+
+ OverloadCandidateSet::CandidateSetKind CSK =
+ CalleesAddressIsTaken ? OverloadCandidateSet::CSK_AddressOfOverloadSet
+ : OverloadCandidateSet::CSK_Normal;
+
+ OverloadCandidateSet CandidateSet(Fn->getExprLoc(), CSK);
ExprResult result;
if (buildOverloadedCallSet(S, Fn, ULE, Args, LParenLoc, &CandidateSet,
@@ -14591,6 +14588,17 @@ ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn,
OverloadingResult OverloadResult =
CandidateSet.BestViableFunction(*this, Fn->getBeginLoc(), Best);
+ // [C++23][over.call.func]
+ // if overload resolution selects a non-static member function,
+ // the call is ill-formed;
+ if (CSK == OverloadCandidateSet::CSK_AddressOfOverloadSet &&
+ Best != CandidateSet.end()) {
+ if (auto *M = dyn_cast_or_null<CXXMethodDecl>(Best->Function);
+ M && M->isImplicitObjectMemberFunction()) {
+ OverloadResult = OR_No_Viable_Function;
+ }
+ }
+
// Model the case with a call to a templated function whose definition
// encloses the call and whose return type contains a placeholder type as if
// the UnresolvedLookupExpr was type-dependent.
>From b57e9c1818a100eda68f6a6f4c051bf3f59b53e7 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Tue, 1 Apr 2025 00:25:01 +0200
Subject: [PATCH 11/18] Fix typos and cuda tests
---
clang/include/clang/Sema/Overload.h | 16 +++++++++---
clang/lib/Sema/SemaInit.cpp | 2 +-
clang/lib/Sema/SemaOverload.cpp | 38 +++++++++++++----------------
3 files changed, 30 insertions(+), 26 deletions(-)
diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h
index 88b0e49b99ca7..d2f58864c0399 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -1388,7 +1388,8 @@ class Sema;
private:
OverloadingResult ResultForBestCandidate(const iterator &Best);
- void CudaExcludeWrongSideCandidates(Sema &S);
+ void CudaExcludeWrongSideCandidates(
+ Sema &S, SmallVectorImpl<OverloadCandidate *> &Candidates);
OverloadingResult
BestViableFunctionImpl(Sema &S, SourceLocation Loc,
OverloadCandidateSet::iterator &Best);
@@ -1447,10 +1448,17 @@ class Sema;
return
// For user defined conversion we need to check against different
// combination of CV qualifiers and look at any expicit specifier, so
- // always deduce template candidate.
+ // always deduce template candidates.
Kind != CSK_InitByUserDefinedConversion &&
- Kind != CSK_InitByConstructor && Kind != CSK_CodeCompletion &&
- Opts.CPlusPlus && (!Opts.CUDA || Opts.GPUExcludeWrongSideOverloads);
+ Kind != CSK_InitByConstructor
+ // When doing code completion, we want to see all the
+ // viable candidates.
+ && Kind != CSK_CodeCompletion &&
+ Opts.CPlusPlus
+ // When -fgpu-exclude-wrong-side-overloads, CUDA needs
+ // to exclude templates from the overload sets after they
+ // have been instantiated. See CudaExcludeWrongSideCandidates.
+ && (!Opts.CUDA || !Opts.GPUExcludeWrongSideOverloads);
}
} // namespace clang
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index a8ab43776a6de..ca503246a01be 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -10045,7 +10045,7 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer(
if (TD) {
// As template candidates are not deduced immediately,
- // persist the arry in the overload set.
+ // persist the array in the overload set.
MutableArrayRef<Expr *> TmpInits =
Candidates.getPersistentArgsArray(Inits.size());
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index b050d08849297..2b3d9b2d74937 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -8527,7 +8527,7 @@ void Sema::AddNonMemberOperatorCandidates(
if (CandidateSet.getRewriteInfo().shouldAddReversed(*this, Args, FD)) {
// As template candidates are not deduced immediately,
- // persist the arry in the overload set.
+ // persist the array in the overload set.
ArrayRef<Expr *> Reversed = CandidateSet.getPersistentArgsArray(
FunctionArgs[1], FunctionArgs[0]);
AddTemplateOverloadCandidate(FunTmpl, F.getPair(), ExplicitTemplateArgs,
@@ -10309,7 +10309,7 @@ Sema::AddArgumentDependentLookupCandidates(DeclarationName Name,
*this, Args, FTD->getTemplatedDecl())) {
// As template candidates are not deduced immediately,
- // persist the arry in the overload set.
+ // persist the array in the overload set.
if (ReversedArgs.empty())
ReversedArgs = CandidateSet.getPersistentArgsArray(Args[1], Args[0]);
@@ -11083,7 +11083,8 @@ OverloadCandidateSet::ResultForBestCandidate(const iterator &Best) {
return OR_Success;
}
-void OverloadCandidateSet::CudaExcludeWrongSideCandidates(Sema &S) {
+void OverloadCandidateSet::CudaExcludeWrongSideCandidates(
+ Sema &S, SmallVectorImpl<OverloadCandidate *> &Candidates) {
// [CUDA] HD->H or HD->D calls are technically not allowed by CUDA but
// are accepted by both clang and NVCC. However, during a particular
// compilation mode only one call variant is viable. We need to
@@ -11100,26 +11101,23 @@ void OverloadCandidateSet::CudaExcludeWrongSideCandidates(Sema &S) {
const FunctionDecl *Caller = S.getCurFunctionDecl(/*AllowLambda=*/true);
bool ContainsSameSideCandidate =
- llvm::any_of(Candidates, [&](const OverloadCandidate &Cand) {
+ llvm::any_of(Candidates, [&](const OverloadCandidate *Cand) {
// Check viable function only.
- return Cand.Viable && Cand.Function &&
- S.CUDA().IdentifyPreference(Caller, Cand.Function) ==
+ return Cand->Viable && Cand->Function &&
+ S.CUDA().IdentifyPreference(Caller, Cand->Function) ==
SemaCUDA::CFP_SameSide;
});
+
if (!ContainsSameSideCandidate)
return;
- auto IsWrongSideCandidate = [&](const OverloadCandidate &Cand) {
+ auto IsWrongSideCandidate = [&](const OverloadCandidate *Cand) {
// Check viable function only to avoid unnecessary data copying/moving.
- return Cand.Viable && Cand.Function &&
- S.CUDA().IdentifyPreference(Caller, Cand.Function) ==
+ return Cand->Viable && Cand->Function &&
+ S.CUDA().IdentifyPreference(Caller, Cand->Function) ==
SemaCUDA::CFP_WrongSide;
};
-
- for (auto &Cand : Candidates) {
- if (IsWrongSideCandidate(Cand))
- Cand.Viable = false;
- }
+ llvm::erase_if(Candidates, IsWrongSideCandidate);
}
/// Computes the best viable function (C++ 13.3.3)
@@ -11140,9 +11138,6 @@ OverloadingResult OverloadCandidateSet::BestViableFunction(Sema &S,
DeferredCandidates.empty() &&
"Unexpected deferred template candidate");
- if (S.getLangOpts().CUDA)
- CudaExcludeWrongSideCandidates(S);
-
bool TwoPhaseResolution = !DeferredCandidates.empty();
if (TwoPhaseResolution) {
@@ -11152,9 +11147,6 @@ OverloadingResult OverloadCandidateSet::BestViableFunction(Sema &S,
return ResultForBestCandidate(Best);
InjectNonDeducedTemplateCandidates(S);
-
- if (S.getLangOpts().CUDA)
- CudaExcludeWrongSideCandidates(S);
}
return BestViableFunctionImpl(S, Loc, Best);
@@ -11162,6 +11154,7 @@ OverloadingResult OverloadCandidateSet::BestViableFunction(Sema &S,
void OverloadCandidateSet::PerfectViableFunction(
Sema &S, SourceLocation Loc, OverloadCandidateSet::iterator &Best) {
+
Best = end();
for (auto It = begin(); It != end(); ++It) {
if (It->isPerfectMatch(S.getASTContext())) {
@@ -11194,6 +11187,9 @@ OverloadingResult OverloadCandidateSet::BestViableFunctionImpl(
std::transform(begin(), end(), std::back_inserter(Candidates),
[](OverloadCandidate &Cand) { return &Cand; });
+ if (S.getLangOpts().CUDA)
+ CudaExcludeWrongSideCandidates(S, Candidates);
+
Best = end();
for (auto *Cand : Candidates) {
Cand->Best = false;
@@ -14935,7 +14931,7 @@ void Sema::LookupOverloadedBinOp(OverloadCandidateSet &CandidateSet,
AddNonMemberOperatorCandidates(Fns, Args, CandidateSet);
// As template candidates are not deduced immediately,
- // persist the arry in the overload set.
+ // persist the array in the overload set.
ArrayRef<Expr *> ReversedArgs;
if (CandidateSet.getRewriteInfo().allowsReversed(Op) ||
CandidateSet.getRewriteInfo().allowsReversed(ExtraOp))
>From 09df91a69fc9684e216b2b5b1e5025db69b019f8 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Tue, 1 Apr 2025 00:28:05 +0200
Subject: [PATCH 12/18] remove redundant check
---
clang/include/clang/Sema/Overload.h | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h
index d2f58864c0399..43114b97b445b 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -1453,8 +1453,7 @@ class Sema;
Kind != CSK_InitByConstructor
// When doing code completion, we want to see all the
// viable candidates.
- && Kind != CSK_CodeCompletion &&
- Opts.CPlusPlus
+ && Kind != CSK_CodeCompletion
// When -fgpu-exclude-wrong-side-overloads, CUDA needs
// to exclude templates from the overload sets after they
// have been instantiated. See CudaExcludeWrongSideCandidates.
>From d7c248261d94eb67c53adf2c30b1875b0a1b8a80 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Tue, 1 Apr 2025 10:05:06 +0200
Subject: [PATCH 13/18] More code simplification
---
clang/lib/Sema/SemaOverload.cpp | 30 +++++++++++++++---------------
1 file changed, 15 insertions(+), 15 deletions(-)
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 2b3d9b2d74937..67f360f2b916c 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -11157,25 +11157,25 @@ void OverloadCandidateSet::PerfectViableFunction(
Best = end();
for (auto It = begin(); It != end(); ++It) {
- if (It->isPerfectMatch(S.getASTContext())) {
- if (Best == end()) {
- Best = It;
- } else {
- if (Best->Function && It->Function) {
- FunctionDecl *D =
- S.getMoreConstrainedFunction(Best->Function, It->Function);
- if (D == nullptr) {
- Best = end();
- break;
- }
- if (D == It->Function)
- Best = It;
- continue;
- }
+ if (!It->isPerfectMatch(S.getASTContext()))
+ continue;
+ if (Best == end()) {
+ Best = It;
+ continue;
+ }
+ if (Best->Function && It->Function) {
+ FunctionDecl *D =
+ S.getMoreConstrainedFunction(Best->Function, It->Function);
+ if (D == nullptr) {
Best = end();
break;
}
+ if (D == It->Function)
+ Best = It;
+ continue;
}
+ Best = end();
+ break;
}
}
>From 384939ea08d5286a573d2e1df35889855ee6addc Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Tue, 1 Apr 2025 11:00:05 +0200
Subject: [PATCH 14/18] fix cuda, add comments
---
clang/include/clang/Sema/Overload.h | 17 ++++++++++++-----
clang/lib/Sema/SemaOverload.cpp | 1 -
clang/test/SemaCUDA/function-overload.cu | 3 ---
3 files changed, 12 insertions(+), 9 deletions(-)
diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h
index 43114b97b445b..de1f27844530a 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -1199,7 +1199,8 @@ class Sema;
llvm::SmallPtrSet<uintptr_t, 16> Functions;
SmallVector<DeferredTemplateOverloadCandidate, 8> DeferredCandidates;
- // Allocator for ConversionSequenceLists. We store the first few of these
+ // Allocator for ConversionSequenceLists and deferred candidate args.
+ // We store the first few of these
// inline to avoid allocation for small sets.
llvm::BumpPtrAllocator SlabAllocator;
@@ -1207,6 +1208,8 @@ class Sema;
CandidateSetKind Kind;
OperatorRewriteInfo RewriteInfo;
+ /// Small storage size for ImplicitConversionSequences
+ /// and the persisted arguments of deferred candidates.
constexpr static unsigned NumInlineBytes =
32 * sizeof(ImplicitConversionSequence);
@@ -1304,6 +1307,11 @@ class Sema;
return ConversionSequenceList(Conversions, NumConversions);
}
+ /// Provide storage for any Expr* arg that must be preserved
+ /// until deferred template candidates are deduced.
+ /// Typically this should be used for reversed operator arguments
+ /// and any time the argument array is transformed while adding
+ /// a template candidate.
llvm::MutableArrayRef<Expr *> getPersistentArgsArray(unsigned N) {
Expr **Exprs = slabAllocate<Expr *>(N);
return llvm::MutableArrayRef<Expr *>(Exprs, N);
@@ -1454,10 +1462,9 @@ class Sema;
// When doing code completion, we want to see all the
// viable candidates.
&& Kind != CSK_CodeCompletion
- // When -fgpu-exclude-wrong-side-overloads, CUDA needs
- // to exclude templates from the overload sets after they
- // have been instantiated. See CudaExcludeWrongSideCandidates.
- && (!Opts.CUDA || !Opts.GPUExcludeWrongSideOverloads);
+ // CUDA may prefer template candidates even when a non-candidate
+ // is a perfect match
+ && !Opts.CUDA;
}
} // namespace clang
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 67f360f2b916c..207b8b73e9cfc 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -31,7 +31,6 @@
#include "clang/Sema/Lookup.h"
#include "clang/Sema/Overload.h"
#include "clang/Sema/SemaCUDA.h"
-#include "clang/Sema/SemaCodeCompletion.h"
#include "clang/Sema/SemaObjC.h"
#include "clang/Sema/Template.h"
#include "clang/Sema/TemplateDeduction.h"
diff --git a/clang/test/SemaCUDA/function-overload.cu b/clang/test/SemaCUDA/function-overload.cu
index 4710c81763adf..3d05839af7528 100644
--- a/clang/test/SemaCUDA/function-overload.cu
+++ b/clang/test/SemaCUDA/function-overload.cu
@@ -1,6 +1,3 @@
-// REQUIRES: x86-registered-target
-// REQUIRES: nvptx-registered-target
-
// RUN: %clang_cc1 -std=c++14 -triple x86_64-unknown-linux-gnu -fsyntax-only \
// RUN: -verify=host,hostdefer,devdefer,expected %s
// RUN: %clang_cc1 -std=c++14 -triple nvptx64-nvidia-cuda -fsyntax-only \
>From 49ec99b43044866df9b125de066ed4833c9981ed Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Tue, 1 Apr 2025 15:15:14 +0200
Subject: [PATCH 15/18] add tests
---
...overload-resolution-deferred-templates.cpp | 145 ++++++++++++++++++
1 file changed, 145 insertions(+)
create mode 100644 clang/test/SemaCXX/overload-resolution-deferred-templates.cpp
diff --git a/clang/test/SemaCXX/overload-resolution-deferred-templates.cpp b/clang/test/SemaCXX/overload-resolution-deferred-templates.cpp
new file mode 100644
index 0000000000000..d0a7a57adcf13
--- /dev/null
+++ b/clang/test/SemaCXX/overload-resolution-deferred-templates.cpp
@@ -0,0 +1,145 @@
+// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -verify -std=c++11 %s
+// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -verify -std=c++20 %s
+// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -verify -std=c++2c %s
+
+template <typename T>
+struct Invalid { static_assert(false, "instantiated Invalid"); }; // #err-invalid
+
+template <typename T>
+int f(T a, Invalid<T> = {}); // #note-f
+
+// sanity check
+int e1 = f(0);
+//expected-error@#err-invalid {{static assertion failed: instantiated Invalid}}
+//expected-note at -2 {{in instantiation of default function argument expression for 'f<int>' required here}}
+//expected-note@#note-f {{in instantiation of template class 'Invalid<int>' requested here}}
+//expected-note@#note-f {{passing argument to parameter here}}
+
+int f(int);
+int ok1 = f(0);
+int e4 = f((const int&)(ok1));
+
+int f(int, int = 0);
+int ok2 = f(0, 0);
+
+int e2 = f(0L);
+//expected-error@#err-invalid {{static assertion failed: instantiated Invalid}}
+//expected-note at -2 {{in instantiation of default function argument expression for 'f<long>' required here}}
+//expected-note@#note-f {{in instantiation of template class 'Invalid<long>' requested here}}
+//expected-note@#note-f {{passing argument to parameter here}}
+
+int f(long);
+int ok3 = f(0L);
+
+template <typename T>
+struct Invalid2 { static_assert(false, "instantiated Invalid2"); }; // #err-qualifiers
+
+template <typename T>
+int ref(T a, Invalid2<T> = {}); // expected-note 2{{here}}
+int ref(int&);
+int ref1 = ref(ok3);
+int ref2 = ref((const int&)ok3); // expected-note {{here}}
+//expected-error@#err-qualifiers {{static assertion failed: instantiated Invalid2}}
+
+
+template <typename T>
+int f_alias(T a, Invalid<T> = {});
+using Alias = int;
+int f_alias(Alias);
+int ok4 = f_alias(0);
+
+#if __cplusplus >= 202002
+
+struct Copyable {
+ template <typename T>
+ requires __is_constructible(Copyable, T)
+ explicit Copyable(T op) noexcept; // #1
+ Copyable(const Copyable&) noexcept = default; // #2
+};
+static_assert(__is_constructible(Copyable, const Copyable&));
+
+struct ImplicitlyCopyable {
+ template <typename T>
+ requires __is_constructible(ImplicitlyCopyable, T)
+ explicit ImplicitlyCopyable(T op) = delete; // #1
+};
+static_assert(__is_constructible(ImplicitlyCopyable, const ImplicitlyCopyable&));
+
+
+struct Movable {
+ template <typename T>
+ requires __is_constructible(Movable, T) // #err-self-constraint-1
+ explicit Movable(T op) noexcept; // #1
+ Movable(Movable&&) noexcept = default; // #2
+};
+static_assert(__is_constructible(Movable, Movable&&));
+static_assert(__is_constructible(Movable, const Movable&));
+// expected-error at -1 {{static assertion failed due to requirement '__is_constructible(Movable, const Movable &)'}}
+
+static_assert(__is_constructible(Movable, int));
+// expected-error at -1{{static assertion failed due to requirement '__is_constructible(Movable, int)'}} \
+// expected-note at -1 2{{}}
+// expected-error@#err-self-constraint-1{{satisfaction of constraint '__is_constructible(Movable, T)' depends on itself}}
+// expected-note@#err-self-constraint-1 4{{}}
+
+template <typename T>
+struct Members {
+ constexpr auto f(auto) {
+ static_assert(false, "");
+ }
+ constexpr auto f(int) { return 1; }
+ constexpr auto f(int) requires true { return 2; }
+
+ constexpr auto g(auto) {
+ static_assert(false, "instantiated member"); //#err-qualified-member
+ return 0;
+ }
+ constexpr auto g(int) & { return 1; }
+
+ static constexpr auto s(auto) {
+ static_assert(false, "");
+ }
+ static constexpr auto s(int) {
+ return 1;
+ }
+ static constexpr auto s(int) requires true {
+ return 2;
+ }
+};
+
+static_assert(Members<int[1]>{}.f(0) == 2);
+static_assert(Members<int[2]>{}.g(0) == 0);
+// expected-error@#err-qualified-member {{static assertion failed: instantiated member}} \
+// expected-note at -1{{in instantiation of function template specialization 'Members<int[2]>::g<int>' }}
+Members<int[3]> m1;
+static_assert(m1.g(0) == 1);
+static_assert(Members<int[3]>{}.s(0) == 2);
+
+
+
+namespace GH62096 {
+template <typename T>
+struct Oops {
+ static_assert(sizeof(T) == 0); // #GH62096-err
+ static constexpr bool value = true;
+};
+
+template <class OPERATOR>
+concept Operator = Oops<OPERATOR>::value; // #GH62096-note1
+
+template <Operator OP> void f(OP op); // // #GH62096-note2
+void f(int);
+
+void g(int n) { f(n); } // OK
+void h(short n) { f(n); }
+// expected-error@#GH62096-err {{static assertion failed due to requirement 'sizeof(short) == 0'}} \
+// expected-note at -1{{in instantiation of function template specialization}} \
+// expected-note at -1{{while checking constraint satisfaction for template}}
+// expected-note@#GH62096-note1{{in instantiation}}
+// expected-note@#GH62096-note1{{while substituting template arguments into constraint expression here}}
+// expected-note@#GH62096-note2{{while substituting template arguments into constraint expression here}}
+// expected-note@#GH62096-note2{{while checking the satisfaction of concept}}
+// expected-note@#GH62096-err {{expression evaluates}}
+}
+
+#endif
>From 3be7712f005f46e504f0c837d1fc74da4f98b7c6 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Tue, 1 Apr 2025 19:30:30 +0200
Subject: [PATCH 16/18] * Fix handling of explicit specifiers * Prefer a
constructor template over a non-template conversion function
---
clang/include/clang/Sema/Overload.h | 13 ++++++----
clang/lib/Sema/SemaOverload.cpp | 26 +++++++++++++++++--
clang/lib/Sema/SemaTemplateDeduction.cpp | 4 +--
...overload-resolution-deferred-templates.cpp | 12 +++++++++
4 files changed, 46 insertions(+), 9 deletions(-)
diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h
index de1f27844530a..98fcd07111537 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -1199,6 +1199,9 @@ class Sema;
llvm::SmallPtrSet<uintptr_t, 16> Functions;
SmallVector<DeferredTemplateOverloadCandidate, 8> DeferredCandidates;
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned HasDeferredTemplateConstructors : 1;
+
// Allocator for ConversionSequenceLists and deferred candidate args.
// We store the first few of these
// inline to avoid allocation for small sets.
@@ -1248,7 +1251,8 @@ class Sema;
public:
OverloadCandidateSet(SourceLocation Loc, CandidateSetKind CSK,
OperatorRewriteInfo RewriteInfo = {})
- : Loc(Loc), Kind(CSK), RewriteInfo(RewriteInfo) {}
+ : HasDeferredTemplateConstructors(false), Loc(Loc), Kind(CSK),
+ RewriteInfo(RewriteInfo) {}
OverloadCandidateSet(const OverloadCandidateSet &) = delete;
OverloadCandidateSet &operator=(const OverloadCandidateSet &) = delete;
~OverloadCandidateSet() { destroyCandidates(); }
@@ -1260,7 +1264,7 @@ class Sema;
/// Whether diagnostics should be deferred.
bool shouldDeferDiags(Sema &S, ArrayRef<Expr *> Args, SourceLocation OpLoc);
- // Whether the resolution of template candidates should be defered
+ // Whether the resolution of template candidates should be deferred
bool shouldDeferTemplateArgumentDeduction(const LangOptions &Opts) const;
/// Determine when this overload candidate will be new to the
@@ -1455,10 +1459,9 @@ class Sema;
const LangOptions &Opts) const {
return
// For user defined conversion we need to check against different
- // combination of CV qualifiers and look at any expicit specifier, so
+ // combination of CV qualifiers and look at any explicit specifier, so
// always deduce template candidates.
- Kind != CSK_InitByUserDefinedConversion &&
- Kind != CSK_InitByConstructor
+ Kind != CSK_InitByUserDefinedConversion
// When doing code completion, we want to see all the
// viable candidates.
&& Kind != CSK_CodeCompletion
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 207b8b73e9cfc..fa086c16c4bf0 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -1124,6 +1124,7 @@ void OverloadCandidateSet::clear(CandidateSetKind CSK) {
Candidates.clear();
Functions.clear();
Kind = CSK;
+ HasDeferredTemplateConstructors = false;
}
namespace {
@@ -7883,6 +7884,11 @@ static bool isNonDependentlyExplicit(FunctionTemplateDecl *FTD) {
return ExplicitSpecifier::getFromDecl(FTD->getTemplatedDecl()).isExplicit();
}
+static bool hasDependentExplicit(FunctionTemplateDecl *FTD) {
+ return ExplicitSpecifier::getFromDecl(FTD->getTemplatedDecl()).getKind() ==
+ ExplicitSpecKind::Unresolved;
+}
+
static void AddTemplateOverloadCandidateImmediately(
Sema &S, OverloadCandidateSet &CandidateSet,
FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
@@ -7974,7 +7980,10 @@ void Sema::AddTemplateOverloadCandidate(
return;
if (ExplicitTemplateArgs ||
- !CandidateSet.shouldDeferTemplateArgumentDeduction(getLangOpts())) {
+ !CandidateSet.shouldDeferTemplateArgumentDeduction(getLangOpts()) ||
+ (isa<CXXConstructorDecl>(FunctionTemplate->getTemplatedDecl()) &&
+ hasDependentExplicit(FunctionTemplate))) {
+
AddTemplateOverloadCandidateImmediately(
*this, CandidateSet, FunctionTemplate, FoundDecl, ExplicitTemplateArgs,
Args, SuppressUserConversions, PartialOverloading, AllowExplicit,
@@ -8362,7 +8371,9 @@ void Sema::AddTemplateConversionCandidate(
if (!CandidateSet.isNewCandidate(FunctionTemplate))
return;
- if (!CandidateSet.shouldDeferTemplateArgumentDeduction(getLangOpts())) {
+ if (!CandidateSet.shouldDeferTemplateArgumentDeduction(getLangOpts()) ||
+ hasDependentExplicit(FunctionTemplate)) {
+
AddTemplateConversionCandidateImmediately(
*this, CandidateSet, FunctionTemplate, FoundDecl, ActingDC, From,
ToType, AllowObjCConversionOnExplicit, AllowExplicit,
@@ -11003,6 +11014,8 @@ void OverloadCandidateSet::AddDeferredTemplateCandidate(
AllowExplicit,
AggregateCandidateDeduction};
DeferredCandidates.emplace_back(std::move(C));
+ HasDeferredTemplateConstructors |=
+ isa<CXXConstructorDecl>(FunctionTemplate->getTemplatedDecl());
}
void OverloadCandidateSet::AddDeferredMethodTemplateCandidate(
@@ -11158,6 +11171,15 @@ void OverloadCandidateSet::PerfectViableFunction(
for (auto It = begin(); It != end(); ++It) {
if (!It->isPerfectMatch(S.getASTContext()))
continue;
+ // We found a suitable conversion function
+ // but if there is a template constructor in the target class
+ // we might prefer that instead.
+ if (HasDeferredTemplateConstructors &&
+ isa_and_nonnull<CXXConversionDecl>(It->Function)) {
+ Best = end();
+ break;
+ }
+
if (Best == end()) {
Best = It;
continue;
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index 9969f1762fe36..860d043090815 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -6214,9 +6214,9 @@ FunctionDecl *Sema::getMoreConstrainedFunction(FunctionDecl *FD1,
assert(!FD1->getDescribedTemplate() && !FD2->getDescribedTemplate() &&
"not for function templates");
assert(!FD1->isFunctionTemplateSpecialization() ||
- isa<CXXConversionDecl>(FD1));
+ (isa<CXXConversionDecl, CXXConstructorDecl>(FD1)));
assert(!FD2->isFunctionTemplateSpecialization() ||
- isa<CXXConversionDecl>(FD2));
+ (isa<CXXConversionDecl, CXXConstructorDecl>(FD2)));
FunctionDecl *F1 = FD1;
if (FunctionDecl *P = FD1->getTemplateInstantiationPattern(false))
diff --git a/clang/test/SemaCXX/overload-resolution-deferred-templates.cpp b/clang/test/SemaCXX/overload-resolution-deferred-templates.cpp
index d0a7a57adcf13..e6fe155cc4d15 100644
--- a/clang/test/SemaCXX/overload-resolution-deferred-templates.cpp
+++ b/clang/test/SemaCXX/overload-resolution-deferred-templates.cpp
@@ -116,6 +116,18 @@ static_assert(m1.g(0) == 1);
static_assert(Members<int[3]>{}.s(0) == 2);
+namespace ConstructorInit{
+struct S {
+ template <typename T>
+ S(T&&) {}
+};
+struct Test {
+ operator S() = delete;
+};
+
+static_assert(__is_constructible(S, Test));
+}
+
namespace GH62096 {
template <typename T>
>From da7fcd939eb26a35b046b94c314d236f71a04736 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Thu, 3 Apr 2025 09:55:20 +0200
Subject: [PATCH 17/18] slab allocate template candidates
---
clang/include/clang/Sema/Overload.h | 94 +++++++++++++----------
clang/lib/Sema/SemaOverload.cpp | 111 +++++++++++++++++++---------
2 files changed, 130 insertions(+), 75 deletions(-)
diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h
index 98fcd07111537..631d105280c56 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -38,7 +38,6 @@
#include <cstddef>
#include <cstdint>
#include <utility>
-#include <variant>
namespace clang {
@@ -1039,60 +1038,61 @@ class Sema;
RewriteKind(CRK_None) {}
};
- struct DeferredConversionTemplateOverloadCandidate {
- FunctionTemplateDecl *FunctionTemplate;
- DeclAccessPair FoundDecl;
- CXXRecordDecl *ActingContext;
- Expr *From;
- QualType ToType;
+ struct DeferredTemplateOverloadCandidate {
+ DeferredTemplateOverloadCandidate *Next = nullptr;
+ enum Kind { Function, Method, Conversion };
+ LLVM_PREFERRED_TYPE(Kind)
+ unsigned Kind : 2;
LLVM_PREFERRED_TYPE(bool)
unsigned AllowObjCConversionOnExplicit : 1;
LLVM_PREFERRED_TYPE(bool)
+ unsigned AllowResultConversion : 1;
+ LLVM_PREFERRED_TYPE(bool)
unsigned AllowExplicit : 1;
LLVM_PREFERRED_TYPE(bool)
- unsigned AllowResultConversion : 1;
+ unsigned SuppressUserConversions : 1;
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned PartialOverloading : 1;
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned AggregateCandidateDeduction : 1;
};
- struct DeferredMethodTemplateOverloadCandidate {
+ struct DeferredFunctionTemplateOverloadCandidate
+ : public DeferredTemplateOverloadCandidate {
FunctionTemplateDecl *FunctionTemplate;
DeclAccessPair FoundDecl;
ArrayRef<Expr *> Args;
- CXXRecordDecl *ActingContext;
- Expr::Classification ObjectClassification;
- QualType ObjectType;
-
+ CallExpr::ADLCallKind IsADLCandidate;
OverloadCandidateParamOrder PO;
- LLVM_PREFERRED_TYPE(bool)
- unsigned SuppressUserConversions : 1;
- LLVM_PREFERRED_TYPE(bool)
- unsigned PartialOverloading : 1;
};
+ static_assert(std::is_trivially_destructible_v<
+ DeferredFunctionTemplateOverloadCandidate>);
- struct DeferredFunctionTemplateOverloadCandidate {
+ struct DeferredMethodTemplateOverloadCandidate
+ : public DeferredTemplateOverloadCandidate {
FunctionTemplateDecl *FunctionTemplate;
DeclAccessPair FoundDecl;
ArrayRef<Expr *> Args;
-
- CallExpr::ADLCallKind IsADLCandidate;
+ CXXRecordDecl *ActingContext;
+ Expr::Classification ObjectClassification;
+ QualType ObjectType;
OverloadCandidateParamOrder PO;
- LLVM_PREFERRED_TYPE(bool)
- unsigned SuppressUserConversions : 1;
- LLVM_PREFERRED_TYPE(bool)
- unsigned PartialOverloading : 1;
- LLVM_PREFERRED_TYPE(bool)
- unsigned AllowExplicit : 1;
- LLVM_PREFERRED_TYPE(bool)
- unsigned AggregateCandidateDeduction : 1;
};
+ static_assert(std::is_trivially_destructible_v<
+ DeferredMethodTemplateOverloadCandidate>);
- using DeferredTemplateOverloadCandidate =
- std::variant<DeferredConversionTemplateOverloadCandidate,
- DeferredMethodTemplateOverloadCandidate,
- DeferredFunctionTemplateOverloadCandidate>;
+ struct DeferredConversionTemplateOverloadCandidate
+ : public DeferredTemplateOverloadCandidate {
+ FunctionTemplateDecl *FunctionTemplate;
+ DeclAccessPair FoundDecl;
+ CXXRecordDecl *ActingContext;
+ Expr *From;
+ QualType ToType;
+ };
- static_assert(
- std::is_trivially_destructible_v<DeferredTemplateOverloadCandidate>);
+ static_assert(std::is_trivially_destructible_v<
+ DeferredConversionTemplateOverloadCandidate>);
/// OverloadCandidateSet - A set of overload candidates, used in C++
/// overload resolution (C++ 13.3).
@@ -1197,8 +1197,9 @@ class Sema;
private:
SmallVector<OverloadCandidate, 16> Candidates;
llvm::SmallPtrSet<uintptr_t, 16> Functions;
- SmallVector<DeferredTemplateOverloadCandidate, 8> DeferredCandidates;
+ DeferredTemplateOverloadCandidate *FirstDeferredCandidate;
+ unsigned DeferredCandidatesCount : 8 * sizeof(unsigned) - 1;
LLVM_PREFERRED_TYPE(bool)
unsigned HasDeferredTemplateConstructors : 1;
@@ -1246,12 +1247,27 @@ class Sema;
return reinterpret_cast<T *>(FreeSpaceStart);
}
+ template <typename T> T *allocateDeferredCandidate() {
+ T *C = slabAllocate<T>(1);
+ if (!FirstDeferredCandidate)
+ FirstDeferredCandidate = C;
+ else {
+ auto *F = FirstDeferredCandidate;
+ while (F->Next)
+ F = F->Next;
+ F->Next = C;
+ }
+ DeferredCandidatesCount++;
+ return C;
+ }
+
void destroyCandidates();
public:
OverloadCandidateSet(SourceLocation Loc, CandidateSetKind CSK,
OperatorRewriteInfo RewriteInfo = {})
- : HasDeferredTemplateConstructors(false), Loc(Loc), Kind(CSK),
+ : FirstDeferredCandidate(nullptr), DeferredCandidatesCount(0),
+ HasDeferredTemplateConstructors(false), Loc(Loc), Kind(CSK),
RewriteInfo(RewriteInfo) {}
OverloadCandidateSet(const OverloadCandidateSet &) = delete;
OverloadCandidateSet &operator=(const OverloadCandidateSet &) = delete;
@@ -1290,11 +1306,9 @@ class Sema;
iterator begin() { return Candidates.begin(); }
iterator end() { return Candidates.end(); }
- size_t size() const {
- return Candidates.size() + DeferredCandidates.size();
- }
+ size_t size() const { return Candidates.size() + DeferredCandidatesCount; }
bool empty() const {
- return Candidates.empty() && DeferredCandidates.empty();
+ return Candidates.empty() && DeferredCandidatesCount == 0;
}
/// Allocate storage for conversion sequences for NumConversions
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index fa086c16c4bf0..0905f52b0bccf 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -1124,6 +1124,8 @@ void OverloadCandidateSet::clear(CandidateSetKind CSK) {
Candidates.clear();
Functions.clear();
Kind = CSK;
+ FirstDeferredCandidate = nullptr;
+ DeferredCandidatesCount = 0;
HasDeferredTemplateConstructors = false;
}
@@ -11004,16 +11006,20 @@ void OverloadCandidateSet::AddDeferredTemplateCandidate(
bool PartialOverloading, bool AllowExplicit,
CallExpr::ADLCallKind IsADLCandidate, OverloadCandidateParamOrder PO,
bool AggregateCandidateDeduction) {
- DeferredFunctionTemplateOverloadCandidate C{FunctionTemplate,
- FoundDecl,
- Args,
- IsADLCandidate,
- PO,
- SuppressUserConversions,
- PartialOverloading,
- AllowExplicit,
- AggregateCandidateDeduction};
- DeferredCandidates.emplace_back(std::move(C));
+
+ auto *C =
+ allocateDeferredCandidate<DeferredFunctionTemplateOverloadCandidate>();
+
+ C = new (C) DeferredFunctionTemplateOverloadCandidate{
+ {nullptr, DeferredFunctionTemplateOverloadCandidate::Function,
+ /*AllowObjCConversionOnExplicit=*/false,
+ /*AllowResultConversion=*/false, AllowExplicit, SuppressUserConversions,
+ PartialOverloading, AggregateCandidateDeduction},
+ FunctionTemplate,
+ FoundDecl,
+ Args,
+ IsADLCandidate,
+ PO};
HasDeferredTemplateConstructors |=
isa<CXXConstructorDecl>(FunctionTemplate->getTemplatedDecl());
}
@@ -11024,11 +11030,23 @@ void OverloadCandidateSet::AddDeferredMethodTemplateCandidate(
Expr::Classification ObjectClassification, ArrayRef<Expr *> Args,
bool SuppressUserConversions, bool PartialOverloading,
OverloadCandidateParamOrder PO) {
- DeferredMethodTemplateOverloadCandidate C{
- MethodTmpl, FoundDecl, Args, ActingContext,
- ObjectClassification, ObjectType, PO, SuppressUserConversions,
- PartialOverloading};
- DeferredCandidates.emplace_back(std::move(C));
+
+ auto *C =
+ allocateDeferredCandidate<DeferredMethodTemplateOverloadCandidate>();
+
+ C = new (C) DeferredMethodTemplateOverloadCandidate{
+ {nullptr, DeferredFunctionTemplateOverloadCandidate::Method,
+ /*AllowObjCConversionOnExplicit=*/false,
+ /*AllowResultConversion=*/false,
+ /*AllowExplicit=*/false, SuppressUserConversions, PartialOverloading,
+ /*AggregateCandidateDeduction=*/false},
+ MethodTmpl,
+ FoundDecl,
+ Args,
+ ActingContext,
+ ObjectClassification,
+ ObjectType,
+ PO};
}
void OverloadCandidateSet::AddDeferredConversionTemplateCandidate(
@@ -11037,18 +11055,26 @@ void OverloadCandidateSet::AddDeferredConversionTemplateCandidate(
bool AllowObjCConversionOnExplicit, bool AllowExplicit,
bool AllowResultConversion) {
- DeferredConversionTemplateOverloadCandidate C{
- FunctionTemplate, FoundDecl,
- ActingContext, From,
- ToType, AllowObjCConversionOnExplicit,
- AllowExplicit, AllowResultConversion};
+ auto *C =
+ allocateDeferredCandidate<DeferredConversionTemplateOverloadCandidate>();
- DeferredCandidates.emplace_back(std::move(C));
+ C = new (C) DeferredConversionTemplateOverloadCandidate{
+ {nullptr, DeferredFunctionTemplateOverloadCandidate::Conversion,
+ AllowObjCConversionOnExplicit, AllowResultConversion,
+ /*AllowExplicit=*/false,
+ /*SuppressUserConversions=*/false,
+ /*PartialOverloading*/ false,
+ /*AggregateCandidateDeduction=*/false},
+ FunctionTemplate,
+ FoundDecl,
+ ActingContext,
+ From,
+ ToType};
}
static void
AddTemplateOverloadCandidate(Sema &S, OverloadCandidateSet &CandidateSet,
- DeferredMethodTemplateOverloadCandidate &&C) {
+ DeferredMethodTemplateOverloadCandidate &C) {
AddMethodTemplateCandidateImmediately(
S, CandidateSet, C.FunctionTemplate, C.FoundDecl, C.ActingContext,
@@ -11058,7 +11084,7 @@ AddTemplateOverloadCandidate(Sema &S, OverloadCandidateSet &CandidateSet,
static void
AddTemplateOverloadCandidate(Sema &S, OverloadCandidateSet &CandidateSet,
- DeferredFunctionTemplateOverloadCandidate &&C) {
+ DeferredFunctionTemplateOverloadCandidate &C) {
AddTemplateOverloadCandidateImmediately(
S, CandidateSet, C.FunctionTemplate, C.FoundDecl,
/*ExplicitTemplateArgs=*/nullptr, C.Args, C.SuppressUserConversions,
@@ -11068,7 +11094,7 @@ AddTemplateOverloadCandidate(Sema &S, OverloadCandidateSet &CandidateSet,
static void
AddTemplateOverloadCandidate(Sema &S, OverloadCandidateSet &CandidateSet,
- DeferredConversionTemplateOverloadCandidate &&C) {
+ DeferredConversionTemplateOverloadCandidate &C) {
return AddTemplateConversionCandidateImmediately(
S, CandidateSet, C.FunctionTemplate, C.FoundDecl, C.ActingContext, C.From,
C.ToType, C.AllowObjCConversionOnExplicit, C.AllowExplicit,
@@ -11076,15 +11102,30 @@ AddTemplateOverloadCandidate(Sema &S, OverloadCandidateSet &CandidateSet,
}
void OverloadCandidateSet::InjectNonDeducedTemplateCandidates(Sema &S) {
- Candidates.reserve(Candidates.size() + DeferredCandidates.size());
- for (auto &&Elem : DeferredCandidates) {
- std::visit(
- [&](auto &&Cand) {
- AddTemplateOverloadCandidate(S, *this, std::move(Cand));
- },
- Elem);
+ Candidates.reserve(Candidates.size() + DeferredCandidatesCount);
+ DeferredTemplateOverloadCandidate *Cand = FirstDeferredCandidate;
+ while (Cand) {
+ switch (Cand->Kind) {
+ case DeferredTemplateOverloadCandidate::Function:
+ AddTemplateOverloadCandidate(
+ S, *this,
+ *static_cast<DeferredFunctionTemplateOverloadCandidate *>(Cand));
+ break;
+ case DeferredTemplateOverloadCandidate::Method:
+ AddTemplateOverloadCandidate(
+ S, *this,
+ *static_cast<DeferredMethodTemplateOverloadCandidate *>(Cand));
+ break;
+ case DeferredTemplateOverloadCandidate::Conversion:
+ AddTemplateOverloadCandidate(
+ S, *this,
+ *static_cast<DeferredConversionTemplateOverloadCandidate *>(Cand));
+ break;
+ }
+ Cand = Cand->Next;
}
- DeferredCandidates.clear();
+ FirstDeferredCandidate = nullptr;
+ DeferredCandidatesCount = 0;
}
OverloadingResult
@@ -11147,10 +11188,10 @@ OverloadingResult OverloadCandidateSet::BestViableFunction(Sema &S,
iterator &Best) {
assert(shouldDeferTemplateArgumentDeduction(S.getLangOpts()) ||
- DeferredCandidates.empty() &&
+ DeferredCandidatesCount == 0 &&
"Unexpected deferred template candidate");
- bool TwoPhaseResolution = !DeferredCandidates.empty();
+ bool TwoPhaseResolution = DeferredCandidatesCount != 0;
if (TwoPhaseResolution) {
@@ -11261,7 +11302,7 @@ OverloadingResult OverloadCandidateSet::BestViableFunctionImpl(
OverloadingResult R = ResultForBestCandidate(Best);
- if (DeferredCandidates.empty() && !EquivalentCands.empty())
+ if (!EquivalentCands.empty())
S.diagnoseEquivalentInternalLinkageDeclarations(Loc, Best->Function,
EquivalentCands);
return R;
>From 8170b860bd4b70917005796c05a9be013a95abb2 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Thu, 3 Apr 2025 15:53:36 +0200
Subject: [PATCH 18/18] fix rvalue to lvalue binding
---
clang/include/clang/Sema/Overload.h | 11 ++++++++--
clang/lib/Sema/SemaOverload.cpp | 2 ++
...overload-resolution-deferred-templates.cpp | 21 +++++++++++++++++++
3 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h
index 631d105280c56..edd0e4742675a 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -408,8 +408,15 @@ class Sema;
}
bool isPerfect(const ASTContext &C) const {
- return isIdentityConversion() &&
- (!ReferenceBinding || C.hasSameType(getFromType(), getToType(2)));
+ if(!isIdentityConversion())
+ return false;
+ if(!ReferenceBinding)
+ return true;
+ if(!C.hasSameType(getFromType(), getToType(2)))
+ return false;
+ if(BindsToRvalue && IsLvalueReference)
+ return false;
+ return true;
}
ImplicitConversionRank getRank() const;
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 0905f52b0bccf..a7bd2157cef7d 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -11031,6 +11031,8 @@ void OverloadCandidateSet::AddDeferredMethodTemplateCandidate(
bool SuppressUserConversions, bool PartialOverloading,
OverloadCandidateParamOrder PO) {
+ assert(!isa<CXXConstructorDecl>(MethodTmpl->getTemplatedDecl()));
+
auto *C =
allocateDeferredCandidate<DeferredMethodTemplateOverloadCandidate>();
diff --git a/clang/test/SemaCXX/overload-resolution-deferred-templates.cpp b/clang/test/SemaCXX/overload-resolution-deferred-templates.cpp
index e6fe155cc4d15..04b7847cb9b82 100644
--- a/clang/test/SemaCXX/overload-resolution-deferred-templates.cpp
+++ b/clang/test/SemaCXX/overload-resolution-deferred-templates.cpp
@@ -128,6 +128,27 @@ struct Test {
static_assert(__is_constructible(S, Test));
}
+namespace RefBinding {
+
+template <typename> struct remove_reference;
+template <typename _Tp> struct remove_reference<_Tp &> {
+ using type = _Tp;
+};
+template <typename _Tp> remove_reference<_Tp>::type move(_Tp &&);
+template <typename _Head> struct _Head_base {
+ _Head_base(_Head &__h) : _M_head_impl(__h) {}
+ template <typename _UHead> _Head_base(_UHead &&);
+ _Head _M_head_impl;
+};
+
+template <typename _Elements> void forward_as_tuple(_Elements &&) {
+ _Head_base<_Elements &&>(_Elements{});
+}
+struct StringRef {
+ void operator[](const StringRef __k) { forward_as_tuple((move)(__k)); }
+};
+
+}
namespace GH62096 {
template <typename T>
More information about the cfe-commits
mailing list