Skip to content

Conversation

sdkrystian
Copy link
Member

As mentioned in #163089, clang crashes for the following inputs:

struct S {
    static void f() {
        auto x = []() -> decltype(this) {};
    }
};
struct S {
    static void f() {
        auto x = []() noexcept(decltype(this)()) {};
    }
};

The crash happens because we currently pass CurContext to isLambdaCallWithExplicitObjectParameter in Sema::CheckCXXThisType when we should be passing DC (returned from getFunctionLevelDeclContext).

Fixes #163089.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 13, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 13, 2025

@llvm/pr-subscribers-clang

Author: Krystian Stasiowski (sdkrystian)

Changes

As mentioned in #163089, clang crashes for the following inputs:

struct S {
    static void f() {
        auto x = []() -> decltype(this) {};
    }
};
struct S {
    static void f() {
        auto x = []() noexcept(decltype(this)()) {};
    }
};

The crash happens because we currently pass CurContext to isLambdaCallWithExplicitObjectParameter in Sema::CheckCXXThisType when we should be passing DC (returned from getFunctionLevelDeclContext).

Fixes #163089.


Full diff: https://github.com/llvm/llvm-project/pull/163243.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaExprCXX.cpp (+1-1)
  • (added) clang/test/CXX/expr/expr.prim/expr.prim.this/p4.cpp (+65)
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 0fe242dce45e9..9fa5e6ecdee2c 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -1426,7 +1426,7 @@ bool Sema::CheckCXXThisType(SourceLocation Loc, QualType Type) {
   const auto *Method = dyn_cast<CXXMethodDecl>(DC);
   if (Method && Method->isExplicitObjectMemberFunction()) {
     Diag(Loc, diag::err_invalid_this_use) << 1;
-  } else if (Method && isLambdaCallWithExplicitObjectParameter(CurContext)) {
+  } else if (Method && isLambdaCallWithExplicitObjectParameter(DC)) {
     Diag(Loc, diag::err_invalid_this_use) << 1;
   } else {
     Diag(Loc, diag::err_invalid_this_use) << 0;
diff --git a/clang/test/CXX/expr/expr.prim/expr.prim.this/p4.cpp b/clang/test/CXX/expr/expr.prim/expr.prim.this/p4.cpp
new file mode 100644
index 0000000000000..85e34112895a5
--- /dev/null
+++ b/clang/test/CXX/expr/expr.prim/expr.prim.this/p4.cpp
@@ -0,0 +1,65 @@
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
+
+namespace N0 {
+  struct A {
+      static void f() {
+        []() -> decltype(this) { }; // expected-error{{invalid use of 'this' outside of a non-static member function}}
+      }
+  };
+} // namespace N0
+
+namespace N1 {
+  struct A {
+      static void f() {
+        []() noexcept(decltype(this)()) { }; // expected-error{{invalid use of 'this' outside of a non-static member function}}
+      }
+  };
+} // namespace N1
+
+namespace N2 {
+  struct A {
+    static void f() {
+      [](this auto&&) -> decltype(this) { }; // expected-error{{invalid use of 'this' outside of a non-static member function}}
+    }
+  };
+} // namespace N2
+
+namespace N3 {
+  struct A {
+    static void f() {
+      [](this auto&&) noexcept(decltype(this)()) { }; // expected-error{{invalid use of 'this' outside of a non-static member function}}
+    }
+  };
+} // namespace N3
+
+namespace N4 {
+  struct A {
+    void f() {
+      []() -> decltype(this) { };
+    }
+  };
+} // namespace N4
+
+namespace N5 {
+  struct A {
+    void f() {
+      []() noexcept(decltype(this)()) { }; // expected-error{{conversion from 'decltype(this)' (aka 'N5::A *') to 'bool' is not allowed in a converted constant expression}}
+    }
+  };
+} // namespace N5
+
+namespace N6 {
+  struct A {
+    void f() {
+      [](this auto&&) -> decltype(this) { };
+    }
+  };
+} // namespace N6
+
+namespace N7 {
+  struct A {
+    void f() {
+      [](this auto&&) noexcept(decltype(this)()) { }; // expected-error{{conversion from 'decltype(this)' (aka 'N7::A *') to 'bool' is not allowed in a converted constant expression}}
+    }
+  };
+} // namespace N7

@shafik shafik requested review from cor3ntin and erichkeane October 13, 2025 23:59
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but I want @cor3ntin to look to

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Clang] [Sema] Crash when this is used in the return type (or noexcept clause) of a lambda in a static member function

5 participants