[clang] [Clang][Sema] Use correct DeclContext when checking 'this' (PR #163243)
Krystian Stasiowski via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 14 13:45:48 PDT 2025
https://github.com/sdkrystian updated https://github.com/llvm/llvm-project/pull/163243
>From c64c550605f9102c952f12b4594f7399d204ae0d Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski <sdkrystian at gmail.com>
Date: Mon, 13 Oct 2025 14:28:59 -0400
Subject: [PATCH 1/4] [Clang][Sema] Use correct DeclContext when checking
'this'
---
clang/lib/Sema/SemaExprCXX.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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;
>From bd083b21aa7e4ab3ecdc3f0daf2173a1f7f9d957 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski <sdkrystian at gmail.com>
Date: Mon, 13 Oct 2025 15:03:12 -0400
Subject: [PATCH 2/4] [FOLD] add tests
---
.../CXX/expr/expr.prim/expr.prim.this/p4.cpp | 65 +++++++++++++++++++
1 file changed, 65 insertions(+)
create mode 100644 clang/test/CXX/expr/expr.prim/expr.prim.this/p4.cpp
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
>From f9c6583840256a34c5c1c543a98a34bc93845fe0 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski <sdkrystian at gmail.com>
Date: Tue, 14 Oct 2025 13:01:00 -0400
Subject: [PATCH 3/4] fix tests
---
clang/test/CXX/expr/expr.prim/expr.prim.this/p4.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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
index 85e34112895a5..5d44bac4929a7 100644
--- a/clang/test/CXX/expr/expr.prim/expr.prim.this/p4.cpp
+++ b/clang/test/CXX/expr/expr.prim/expr.prim.this/p4.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -Wno-unused-value -verify %s
namespace N0 {
struct A {
>From d824f5fc0bc545b48ab06ffbac0bd71d7f1f0333 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski <sdkrystian at gmail.com>
Date: Tue, 14 Oct 2025 16:45:18 -0400
Subject: [PATCH 4/4] [FOLD] fix whitespace
---
clang/test/CXX/expr/expr.prim/expr.prim.this/p4.cpp | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
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
index 5d44bac4929a7..adba5deeef40f 100644
--- a/clang/test/CXX/expr/expr.prim/expr.prim.this/p4.cpp
+++ b/clang/test/CXX/expr/expr.prim/expr.prim.this/p4.cpp
@@ -2,17 +2,17 @@
namespace N0 {
struct A {
- static void f() {
- []() -> decltype(this) { }; // expected-error{{invalid use of 'this' outside of a non-static member function}}
- }
+ 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}}
- }
+ static void f() {
+ []() noexcept(decltype(this)()) { }; // expected-error{{invalid use of 'this' outside of a non-static member function}}
+ }
};
} // namespace N1
More information about the cfe-commits
mailing list