Commit a36a14b7 authored by Saar Raz's avatar Saar Raz Committed by Hans Wennborg
Browse files

[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 9769e1ee)
parent 5401d393
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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);
+13 −0
Original line number Diff line number Diff line
@@ -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 {