Skip to content

Commit d256511

Browse files
authored
Merge pull request #47 from zyn0217/fix-access-checking-after-normalization
Fix SemaTemplate/concepts-lambda.cpp
2 parents 6f462d8 + c37199b commit d256511

File tree

6 files changed

+58
-23
lines changed

6 files changed

+58
-23
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13134,6 +13134,8 @@ class Sema final : public SemaBase {
1313413134
/// Whether we're substituting into constraints.
1313513135
bool InConstraintSubstitution;
1313613136

13137+
bool InParameterMappingSubstitution;
13138+
1313713139
/// The point of instantiation or synthesis within the source code.
1313813140
SourceLocation PointOfInstantiation;
1313913141

@@ -13873,11 +13875,17 @@ class Sema final : public SemaBase {
1387313875
}
1387413876

1387513877
/// Determine whether we are currently performing constraint substitution.
13878+
// FIXME: Rename it
1387613879
bool inConstraintSubstitution() const {
1387713880
return !CodeSynthesisContexts.empty() &&
1387813881
CodeSynthesisContexts.back().InConstraintSubstitution;
1387913882
}
1388013883

13884+
bool inParameterMappingSubstitution() const {
13885+
return !CodeSynthesisContexts.empty() &&
13886+
CodeSynthesisContexts.back().InParameterMappingSubstitution;
13887+
}
13888+
1388113889
using EntityPrinter = llvm::function_ref<void(llvm::raw_ostream &)>;
1388213890

1388313891
/// \brief create a Requirement::SubstitutionDiagnostic with only a

clang/include/clang/Sema/SemaConcept.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct NormalizedConstraint {
6565
struct AtomicBits {
6666
LLVM_PREFERRED_TYPE(ConstraintKind)
6767
unsigned Kind : 5;
68-
unsigned : 1;
68+
unsigned Placeholder : 1;
6969
unsigned PackSubstitutionIndex : 26;
7070
llvm::SmallBitVector Indexes;
7171
TemplateArgumentLoc *Args;
@@ -114,6 +114,7 @@ struct NormalizedConstraint {
114114
const NamedDecl *ConstraintDecl,
115115
UnsignedOrNone PackIndex)
116116
: Atomic{llvm::to_underlying(ConstraintKind::Atomic),
117+
/*Placeholder=*/0,
117118
PackIndex.toInternalRepresentation(),
118119
/*Indexes=*/{},
119120
/*Args=*/nullptr,
@@ -134,7 +135,8 @@ struct NormalizedConstraint {
134135
NormalizedConstraint *SubConstraint,
135136
UnsignedOrNone PackIndex)
136137
: ConceptId{{llvm::to_underlying(ConstraintKind::ConceptId),
137-
PackIndex.toInternalRepresentation(), /*Indexes=*/{},
138+
/*Placeholder=*/0, PackIndex.toInternalRepresentation(),
139+
/*Indexes=*/{},
138140
/*Args=*/nullptr, ConceptId, ConstraintDecl},
139141
SubConstraint} {}
140142

@@ -237,7 +239,8 @@ struct NormalizedConstraint {
237239
fromAssociatedConstraints(Sema &S, const NamedDecl *D,
238240
ArrayRef<AssociatedConstraint> ACs);
239241
static NormalizedConstraint *fromConstraintExpr(Sema &S, const NamedDecl *D,
240-
const Expr *E);
242+
const Expr *E,
243+
UnsignedOrNone SubstIndex);
241244
};
242245

243246
class CompoundConstraint : public NormalizedConstraint {

clang/include/clang/Sema/Template.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ enum class TemplateSubstitutionKind : char {
261261
// ==
262262
// AssociatedDecl) &&
263263
// "Trying to change incorrect declaration?");
264+
TemplateArgumentLists.back().AssociatedDeclAndFinal.setPointer(AssociatedDecl);
264265
TemplateArgumentLists.back().Args = Args;
265266
}
266267

clang/lib/Sema/SemaConcept.cpp

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ SubstitutionInTemplateArguments(
368368

369369
TemplateArgumentListInfo SubstArgs;
370370
if (Constraint.hasParameterMapping()) {
371+
Sema::ArgPackSubstIndexRAII SubstIndex(
372+
S, Constraint.getPackSubstitutionIndex());
371373
if (S.SubstTemplateArgumentsInParameterMapping(
372374
Constraint.getParameterMapping(), MLTAL, SubstArgs) ||
373375
Trap.hasErrorOccurred())
@@ -590,6 +592,8 @@ static bool calculateConstraintSatisfaction(
590592
return Ok;
591593

592594
Sema::SFINAETrap Trap(S);
595+
Sema::ArgPackSubstIndexRAII SubstIndex(
596+
S, Constraint.getPackSubstitutionIndex());
593597

594598
const ASTTemplateArgumentListInfo *Ori =
595599
Constraint.getConceptId()->getTemplateArgsAsWritten();
@@ -604,7 +608,8 @@ static bool calculateConstraintSatisfaction(
604608
Ori->NumTemplateArgs, TransArgs))
605609
return Ok;
606610

607-
if (S.SubstTemplateArguments(TransArgs.arguments(), MLTAL, OutArgs) ||
611+
if (S.SubstTemplateArguments(TransArgs.arguments(), *SubstitutedArgs,
612+
OutArgs) ||
608613
Trap.hasErrorOccurred())
609614
return Ok;
610615

@@ -1659,19 +1664,23 @@ static bool substituteParameterMappings(Sema &S, NormalizedConstraint &N,
16591664
/*RelativeToPrimary=*/true,
16601665
/*Pattern=*/nullptr,
16611666
/*ForConstraintInstantiation=*/true);
1662-
1667+
// Don't build Subst* nodes to model lambda expressions.
1668+
// The transform of Subst* is oblivious to the lambda type.
1669+
MLTAL.setKind(TemplateSubstitutionKind::Rewrite);
16631670
return substituteParameterMappings(S, N, MLTAL,
16641671
CSE->getTemplateArgsAsWritten());
16651672
}
16661673

16671674
NormalizedConstraint *NormalizedConstraint::fromAssociatedConstraints(
16681675
Sema &S, const NamedDecl *D, ArrayRef<AssociatedConstraint> ACs) {
16691676
assert(ACs.size() != 0);
1670-
auto Conjunction = fromConstraintExpr(S, D, ACs[0].ConstraintExpr);
1677+
auto *Conjunction =
1678+
fromConstraintExpr(S, D, ACs[0].ConstraintExpr, ACs[0].ArgPackSubstIndex);
16711679
if (!Conjunction)
16721680
return nullptr;
16731681
for (unsigned I = 1; I < ACs.size(); ++I) {
1674-
auto Next = fromConstraintExpr(S, D, ACs[I].ConstraintExpr);
1682+
auto *Next = fromConstraintExpr(S, D, ACs[I].ConstraintExpr,
1683+
ACs[I].ArgPackSubstIndex);
16751684
if (!Next)
16761685
return nullptr;
16771686
Conjunction = CompoundConstraint::CreateConjunction(S.getASTContext(),
@@ -1680,9 +1689,8 @@ NormalizedConstraint *NormalizedConstraint::fromAssociatedConstraints(
16801689
return Conjunction;
16811690
}
16821691

1683-
NormalizedConstraint *
1684-
NormalizedConstraint::fromConstraintExpr(Sema &S, const NamedDecl *D,
1685-
const Expr *E) {
1692+
NormalizedConstraint *NormalizedConstraint::fromConstraintExpr(
1693+
Sema &S, const NamedDecl *D, const Expr *E, UnsignedOrNone SubstIndex) {
16861694
assert(E != nullptr);
16871695

16881696
// C++ [temp.constr.normal]p1.1
@@ -1703,10 +1711,10 @@ NormalizedConstraint::fromConstraintExpr(Sema &S, const NamedDecl *D,
17031711
// See http://cplusplus.github.io/concepts-ts/ts-active.html#28
17041712

17051713
if (LogicalBinOp BO = E) {
1706-
auto LHS = fromConstraintExpr(S, D, BO.getLHS());
1714+
auto *LHS = fromConstraintExpr(S, D, BO.getLHS(), SubstIndex);
17071715
if (!LHS)
17081716
return nullptr;
1709-
auto RHS = fromConstraintExpr(S, D, BO.getRHS());
1717+
auto *RHS = fromConstraintExpr(S, D, BO.getRHS(), SubstIndex);
17101718
if (!RHS)
17111719
return nullptr;
17121720

@@ -1734,7 +1742,7 @@ NormalizedConstraint::fromConstraintExpr(Sema &S, const NamedDecl *D,
17341742
ConceptDecl *CD = CSE->getNamedConcept();
17351743
SubNF = NormalizedConstraint::fromAssociatedConstraints(
17361744
S, CSE->getNamedConcept(),
1737-
AssociatedConstraint(CD->getConstraintExpr()));
1745+
AssociatedConstraint(CD->getConstraintExpr(), SubstIndex));
17381746

17391747
if (!SubNF)
17401748
return nullptr;
@@ -1744,7 +1752,7 @@ NormalizedConstraint::fromConstraintExpr(Sema &S, const NamedDecl *D,
17441752

17451753
return ConceptIdConstraint::Create(S.getASTContext(),
17461754
CSE->getConceptReference(), SubNF, D,
1747-
S.ArgPackSubstIndex);
1755+
SubstIndex);
17481756

17491757
} else if (auto *FE = dyn_cast<const CXXFoldExpr>(E);
17501758
FE && S.getLangOpts().CPlusPlus26 &&
@@ -1759,8 +1767,8 @@ NormalizedConstraint::fromConstraintExpr(Sema &S, const NamedDecl *D,
17591767
: FoldExpandedConstraint::FoldOperatorKind::Or;
17601768

17611769
if (FE->getInit()) {
1762-
auto LHS = fromConstraintExpr(S, D, FE->getLHS());
1763-
auto RHS = fromConstraintExpr(S, D, FE->getRHS());
1770+
auto *LHS = fromConstraintExpr(S, D, FE->getLHS(), SubstIndex);
1771+
auto *RHS = fromConstraintExpr(S, D, FE->getRHS(), SubstIndex);
17641772
if (!LHS || !RHS)
17651773
return nullptr;
17661774

@@ -1777,13 +1785,13 @@ NormalizedConstraint::fromConstraintExpr(Sema &S, const NamedDecl *D,
17771785
: CCK_Disjunction),
17781786
RHS);
17791787
}
1780-
auto Sub = fromConstraintExpr(S, D, FE->getPattern());
1788+
auto *Sub = fromConstraintExpr(S, D, FE->getPattern(), SubstIndex);
17811789
if (!Sub)
17821790
return nullptr;
17831791
return FoldExpandedConstraint::Create(S.getASTContext(), FE->getPattern(),
17841792
Kind, Sub);
17851793
}
1786-
return AtomicConstraint::Create(S.getASTContext(), E, D, S.ArgPackSubstIndex);
1794+
return AtomicConstraint::Create(S.getASTContext(), E, D, SubstIndex);
17871795
}
17881796

17891797
bool FoldExpandedConstraint::AreCompatibleForSubsumption(

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,9 @@ static ExprResult formImmediatelyDeclaredConstraint(
11651165
ExprResult ImmediatelyDeclaredConstraint = S.CheckConceptTemplateId(
11661166
SS, /*TemplateKWLoc=*/SourceLocation(), NameInfo,
11671167
/*FoundDecl=*/FoundDecl ? FoundDecl : NamedConcept, NamedConcept,
1168-
&ConstraintArgs);
1168+
&ConstraintArgs,
1169+
/*DoCheckConstraintSatisfaction=*/
1170+
!S.inParameterMappingSubstitution());
11691171
if (ImmediatelyDeclaredConstraint.isInvalid() || !EllipsisLoc.isValid())
11701172
return ImmediatelyDeclaredConstraint;
11711173

@@ -4742,7 +4744,7 @@ ExprResult Sema::CheckConceptTemplateId(
47424744
*TemplateArgs, CTAI.CanonicalConverted);
47434745
MultiLevelTemplateArgumentList MLTAL(NamedConcept, CTAI.CanonicalConverted,
47444746
/*Final=*/false);
4745-
auto *CL = ConceptReference::Create(
4747+
auto *CL = ConceptReference::Create(
47464748
Context,
47474749
SS.isSet() ? SS.getWithLocInContext(Context) : NestedNameSpecifierLoc{},
47484750
TemplateKWLoc, ConceptNameInfo, FoundDecl, NamedConcept,

clang/lib/Sema/SemaTemplateInstantiate.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,10 +623,15 @@ Sema::InstantiatingTemplate::InstantiatingTemplate(
623623
Inst.DeductionInfo = DeductionInfo;
624624
Inst.InstantiationRange = InstantiationRange;
625625
Inst.InConstraintSubstitution =
626-
Inst.Kind == CodeSynthesisContext::ConstraintSubstitution;
627-
if (!SemaRef.CodeSynthesisContexts.empty())
626+
Inst.Kind == CodeSynthesisContext::ConstraintsCheck;
627+
Inst.InParameterMappingSubstitution =
628+
Inst.Kind == CodeSynthesisContext::ParameterMappingSubstitution;
629+
if (!SemaRef.CodeSynthesisContexts.empty()) {
628630
Inst.InConstraintSubstitution |=
629631
SemaRef.CodeSynthesisContexts.back().InConstraintSubstitution;
632+
Inst.InParameterMappingSubstitution |=
633+
SemaRef.CodeSynthesisContexts.back().InParameterMappingSubstitution;
634+
}
630635

631636
SemaRef.pushCodeSynthesisContext(Inst);
632637

@@ -2225,6 +2230,14 @@ TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
22252230
// We're rewriting the template parameter as a reference to another
22262231
// template parameter.
22272232
Arg = getTemplateArgumentPackPatternForRewrite(Arg);
2233+
if (Arg.getKind() != TemplateArgument::Expression) {
2234+
assert(SemaRef.inParameterMappingSubstitution());
2235+
// FIXME: SourceLocation()?
2236+
ExprResult E = SemaRef.BuildExpressionFromNonTypeTemplateArgument(Arg, SourceLocation());
2237+
if (E.isInvalid())
2238+
return E;
2239+
Arg = TemplateArgument(E.get(), /*IsCanonical=*/false);
2240+
}
22282241
assert(Arg.getKind() == TemplateArgument::Expression &&
22292242
"unexpected nontype template argument kind in template rewrite");
22302243
// FIXME: This can lead to the same subexpression appearing multiple times
@@ -3249,7 +3262,7 @@ bool Sema::SubstTypeConstraint(
32493262
const ASTTemplateArgumentListInfo *TemplArgInfo =
32503263
TC->getTemplateArgsAsWritten();
32513264

3252-
if (!EvaluateConstraints) {
3265+
if (!EvaluateConstraints && !inParameterMappingSubstitution()) {
32533266
UnsignedOrNone Index = TC->getArgPackSubstIndex();
32543267
if (!Index)
32553268
Index = SemaRef.ArgPackSubstIndex;

0 commit comments

Comments
 (0)