Skip to content

Commit a36a14b

Browse files
saarrazzmodem
authored andcommitted
[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 #45162). Assign the current context as the DeclContext of the transformed decl. (cherry picked from commit 9769e1e)
1 parent 5401d39 commit a36a14b

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
@@ -11303,7 +11303,7 @@ TreeTransform<Derived>::TransformRequiresExpr(RequiresExpr *E) {
1130311303
SemaRef, Sema::ExpressionEvaluationContext::Unevaluated);
1130411304

1130511305
RequiresExprBodyDecl *Body = RequiresExprBodyDecl::Create(
11306-
getSema().Context, E->getBody()->getDeclContext(),
11306+
getSema().Context, getSema().CurContext,
1130711307
E->getBody()->getBeginLoc());
1130811308

1130911309
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)