Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions clang/lib/Sema/SemaConcept.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ class ConstraintSatisfactionChecker {
// XXX: It is SLOW! Use it very carefully.
std::optional<MultiLevelTemplateArgumentList> SubstitutionInTemplateArguments(
const NormalizedConstraintWithParamMapping &Constraint,
MultiLevelTemplateArgumentList MLTAL,
const MultiLevelTemplateArgumentList &MLTAL,
llvm::SmallVector<TemplateArgument> &SubstitutedOuterMost);

ExprResult EvaluateSlow(const AtomicConstraint &Constraint,
Expand Down Expand Up @@ -564,12 +564,17 @@ ExprResult ConstraintSatisfactionChecker::EvaluateAtomicConstraint(
std::optional<MultiLevelTemplateArgumentList>
ConstraintSatisfactionChecker::SubstitutionInTemplateArguments(
const NormalizedConstraintWithParamMapping &Constraint,
MultiLevelTemplateArgumentList MLTAL,
llvm::SmallVector<TemplateArgument> &SubstitutedOuterMost) {
const MultiLevelTemplateArgumentList &MLTAL,
llvm::SmallVector<TemplateArgument> &SubstitutedOutermost) {

if (!Constraint.hasParameterMapping())
return std::move(MLTAL);

// The mapping is empty, meaning no template arguments are needed for
// evaluation.
if (Constraint.getParameterMapping().empty())
return MultiLevelTemplateArgumentList();

TemplateDeductionInfo Info(Constraint.getBeginLoc());
Sema::InstantiatingTemplate Inst(
S, Constraint.getBeginLoc(),
Expand Down Expand Up @@ -607,27 +612,27 @@ ConstraintSatisfactionChecker::SubstitutionInTemplateArguments(
// The empty MLTAL situation should only occur when evaluating non-dependent
// constraints.
if (MLTAL.getNumSubstitutedLevels())
SubstitutedOuterMost =
SubstitutedOutermost =
llvm::to_vector_of<TemplateArgument>(MLTAL.getOutermost());
unsigned Offset = 0;
for (unsigned I = 0, MappedIndex = 0; I < Used.size(); I++) {
TemplateArgument Arg;
if (Used[I])
Arg = S.Context.getCanonicalTemplateArgument(
CTAI.SugaredConverted[MappedIndex++]);
if (I < SubstitutedOuterMost.size()) {
SubstitutedOuterMost[I] = Arg;
if (I < SubstitutedOutermost.size()) {
SubstitutedOutermost[I] = Arg;
Offset = I + 1;
} else {
SubstitutedOuterMost.push_back(Arg);
Offset = SubstitutedOuterMost.size();
SubstitutedOutermost.push_back(Arg);
Offset = SubstitutedOutermost.size();
}
}
if (Offset < SubstitutedOuterMost.size())
SubstitutedOuterMost.erase(SubstitutedOuterMost.begin() + Offset);
if (Offset < SubstitutedOutermost.size())
SubstitutedOutermost.erase(SubstitutedOutermost.begin() + Offset);

MultiLevelTemplateArgumentList SubstitutedTemplateArgs;
SubstitutedTemplateArgs.addOuterTemplateArguments(TD, SubstitutedOuterMost,
SubstitutedTemplateArgs.addOuterTemplateArguments(TD, SubstitutedOutermost,
/*Final=*/false);
return std::move(SubstitutedTemplateArgs);
}
Expand All @@ -636,9 +641,9 @@ ExprResult ConstraintSatisfactionChecker::EvaluateSlow(
const AtomicConstraint &Constraint,
const MultiLevelTemplateArgumentList &MLTAL) {

llvm::SmallVector<TemplateArgument> SubstitutedOuterMost;
llvm::SmallVector<TemplateArgument> SubstitutedOutermost;
std::optional<MultiLevelTemplateArgumentList> SubstitutedArgs =
SubstitutionInTemplateArguments(Constraint, MLTAL, SubstitutedOuterMost);
SubstitutionInTemplateArguments(Constraint, MLTAL, SubstitutedOutermost);
if (!SubstitutedArgs) {
Satisfaction.IsSatisfied = false;
return ExprEmpty();
Expand Down Expand Up @@ -786,13 +791,13 @@ ExprResult ConstraintSatisfactionChecker::EvaluateSlow(
FoldExpandedConstraint::FoldOperatorKind::And;
unsigned EffectiveDetailEndIndex = Satisfaction.Details.size();

llvm::SmallVector<TemplateArgument> SubstitutedOuterMost;
llvm::SmallVector<TemplateArgument> SubstitutedOutermost;
// FIXME: Is PackSubstitutionIndex correct?
llvm::SaveAndRestore _(PackSubstitutionIndex, S.ArgPackSubstIndex);
std::optional<MultiLevelTemplateArgumentList> SubstitutedArgs =
SubstitutionInTemplateArguments(
static_cast<const NormalizedConstraintWithParamMapping &>(Constraint),
MLTAL, SubstitutedOuterMost);
MLTAL, SubstitutedOutermost);
if (!SubstitutedArgs) {
Satisfaction.IsSatisfied = false;
return ExprError();
Expand Down Expand Up @@ -880,9 +885,9 @@ ExprResult ConstraintSatisfactionChecker::EvaluateSlow(
const MultiLevelTemplateArgumentList &MLTAL, unsigned Size) {
const ConceptReference *ConceptId = Constraint.getConceptId();

llvm::SmallVector<TemplateArgument> SubstitutedOuterMost;
llvm::SmallVector<TemplateArgument> SubstitutedOutermost;
std::optional<MultiLevelTemplateArgumentList> SubstitutedArgs =
SubstitutionInTemplateArguments(Constraint, MLTAL, SubstitutedOuterMost);
SubstitutionInTemplateArguments(Constraint, MLTAL, SubstitutedOutermost);

if (!SubstitutedArgs) {
Satisfaction.IsSatisfied = false;
Expand Down Expand Up @@ -2017,8 +2022,13 @@ void SubstituteParameterMappings::buildParameterMapping(
SemaRef.MarkUsedTemplateParameters(Args->arguments(),
/*Depth=*/0, OccurringIndices);
}
unsigned Size = OccurringIndices.count();
// It's OK when Size is 0. We build an empty parameter mapping when the atomic
// constraint is independent of any template parameters, so we can distinguish
// it from cases where no mapping exists at all, e.g. when there are only
// atomic constraints.
TemplateArgumentLoc *TempArgs =
new (SemaRef.Context) TemplateArgumentLoc[OccurringIndices.count()];
new (SemaRef.Context) TemplateArgumentLoc[Size];
llvm::SmallVector<NamedDecl *> UsedParams;
for (unsigned I = 0, J = 0, C = TemplateParams->size(); I != C; ++I) {
SourceLocation Loc = ArgsAsWritten->NumTemplateArgs > I
Expand All @@ -2039,7 +2049,6 @@ void SubstituteParameterMappings::buildParameterMapping(
TemplateParams->getLAngleLoc(), UsedParams,
/*RAngleLoc=*/SourceLocation(),
/*RequiresClause=*/nullptr);
unsigned Size = OccurringIndices.count();
N.updateParameterMapping(
std::move(OccurringIndices), std::move(OccurringIndicesForSubsumption),
MutableArrayRef<TemplateArgumentLoc>{TempArgs, Size}, UsedList);
Expand All @@ -2050,6 +2059,11 @@ bool SubstituteParameterMappings::substitute(
if (!N.hasParameterMapping())
buildParameterMapping(N);

// Don't bother into substituting if we have determined the mapping maps to
// nothing.
if (N.getParameterMapping().empty())
return false;

SourceLocation InstLocBegin, InstLocEnd;
llvm::ArrayRef Arguments = ArgsAsWritten->arguments();
if (Arguments.empty()) {
Expand Down
18 changes: 18 additions & 0 deletions clang/test/SemaTemplate/concepts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,24 @@ void main() { Feeder<int>{}.feed<Cat<int>>(); }

}

namespace case9 {

template <typename>
concept a = requires { requires true; };
template <typename T>
concept b = a<typename T::EntitySpec>;
template <typename T>
concept c = requires { b<T>; };
template <typename T>
requires c<T>
struct s;
template <typename> constexpr bool f() { return true; }
template <typename T> constexpr bool d = f<T>();
struct s2;
static_assert(d<s<s2>>);

}

}

namespace GH162125 {
Expand Down