Skip to content

Commit

Permalink
[Concepts] Fix incorrect DeclContext for transformed RequiresExprBody…
Browse files Browse the repository at this point in the history
…Decl

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)
  • Loading branch information
saarraz authored and zmodem committed Mar 19, 2020
1 parent 5401d39 commit a36a14b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/Sema/TreeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -11303,7 +11303,7 @@ TreeTransform<Derived>::TransformRequiresExpr(RequiresExpr *E) {
SemaRef, Sema::ExpressionEvaluationContext::Unevaluated);

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

Sema::ContextRAII SavedContext(getSema(), Body, /*NewThisContext*/false);
Expand Down
13 changes: 13 additions & 0 deletions clang/test/SemaTemplate/instantiate-requires-expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,19 @@ namespace expr_requirement {
struct r3 {};

using r3i = r3<int, unsigned int>; // expected-error{{constraints not satisfied for class template 'r3' [with Ts = <int, unsigned int>]}}

template<typename T>
struct r4 {
constexpr int foo() {
if constexpr (requires { this->invalid(); })
return 1;
else
return 0;
}

constexpr void invalid() requires false { }
};
static_assert(r4<int>{}.foo() == 0);
}

namespace nested_requirement {
Expand Down

0 comments on commit a36a14b

Please sign in to comment.