Skip to content

Commit 4d33a8d

Browse files
committed
[Concepts] Add ExpressionEvaluationContexts to instantiation of constraints
Proper ExpressionEvaluationContext were not being entered when instantiating constraint expressions, which caused assertion failures in certain cases, including bug llvm#44614.
1 parent 9c2eb22 commit 4d33a8d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,8 @@ Decl *TemplateDeclInstantiator::VisitFunctionDecl(
18481848
// FIXME: Concepts: Do not substitute into constraint expressions
18491849
Expr *TrailingRequiresClause = D->getTrailingRequiresClause();
18501850
if (TrailingRequiresClause) {
1851+
EnterExpressionEvaluationContext ConstantEvaluated(
1852+
SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated);
18511853
ExprResult SubstRC = SemaRef.SubstExpr(TrailingRequiresClause,
18521854
TemplateArgs);
18531855
if (SubstRC.isInvalid())
@@ -2186,6 +2188,8 @@ Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(
21862188
// FIXME: Concepts: Do not substitute into constraint expressions
21872189
Expr *TrailingRequiresClause = D->getTrailingRequiresClause();
21882190
if (TrailingRequiresClause) {
2191+
EnterExpressionEvaluationContext ConstantEvaluated(
2192+
SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated);
21892193
ExprResult SubstRC = SemaRef.SubstExpr(TrailingRequiresClause,
21902194
TemplateArgs);
21912195
if (SubstRC.isInvalid())
@@ -2525,6 +2529,8 @@ Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
25252529
TemplateArgumentListInfo InstArgs;
25262530

25272531
if (TemplArgInfo) {
2532+
EnterExpressionEvaluationContext ConstantEvaluated(
2533+
SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated);
25282534
InstArgs.setLAngleLoc(TemplArgInfo->LAngleLoc);
25292535
InstArgs.setRAngleLoc(TemplArgInfo->RAngleLoc);
25302536
if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(),
@@ -3729,6 +3735,8 @@ TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
37293735
// checking satisfaction.
37303736
Expr *InstRequiresClause = nullptr;
37313737
if (Expr *E = L->getRequiresClause()) {
3738+
EnterExpressionEvaluationContext ConstantEvaluated(
3739+
SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated);
37323740
ExprResult Res = SemaRef.SubstExpr(E, TemplateArgs);
37333741
if (Res.isInvalid() || !Res.isUsable()) {
37343742
return nullptr;

clang/test/SemaTemplate/instantiate-requires-clause.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,15 @@ struct S {
3939
};
4040

4141
static_assert(S<void>::f(1));
42+
43+
constexpr auto value = 0;
44+
45+
template<typename T>
46+
struct S2 {
47+
template<typename = void> requires(value, true)
48+
static constexpr auto f() requires(value, true) {
49+
}
50+
};
51+
52+
static_assert((S2<int>::f(), true));
53+

0 commit comments

Comments
 (0)