Skip to content

Commit 9769e1e

Browse files
committed
[Concepts] Fix incorrect DeclContext for transformed RequiresExprBodyDecl
We would assign the incorrect DeclContext when transforming the RequiresExprBodyDecl, causing incorrect handling of 'this' inside RequiresExprBodyDecls (bug llvm#45162). Assign the current context as the DeclContext of the transformed decl.
1 parent 40568fe commit 9769e1e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

clang/lib/Sema/TreeTransform.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11380,7 +11380,7 @@ TreeTransform<Derived>::TransformRequiresExpr(RequiresExpr *E) {
1138011380
SemaRef, Sema::ExpressionEvaluationContext::Unevaluated);
1138111381

1138211382
RequiresExprBodyDecl *Body = RequiresExprBodyDecl::Create(
11383-
getSema().Context, E->getBody()->getDeclContext(),
11383+
getSema().Context, getSema().CurContext,
1138411384
E->getBody()->getBeginLoc());
1138511385

1138611386
Sema::ContextRAII SavedContext(getSema(), Body, /*NewThisContext*/false);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,19 @@ namespace expr_requirement {
164164
struct r3 {};
165165

166166
using r3i = r3<int, unsigned int>; // expected-error{{constraints not satisfied for class template 'r3' [with Ts = <int, unsigned int>]}}
167+
168+
template<typename T>
169+
struct r4 {
170+
constexpr int foo() {
171+
if constexpr (requires { this->invalid(); })
172+
return 1;
173+
else
174+
return 0;
175+
}
176+
177+
constexpr void invalid() requires false { }
178+
};
179+
static_assert(r4<int>{}.foo() == 0);
167180
}
168181

169182
namespace nested_requirement {

0 commit comments

Comments
 (0)