[clang] [clang] [SemaCXX] Disallow deducing "this" on operator `new` and `delete` (PR #82251)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 19 06:06:28 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Rajveer Singh Bharadwaj (Rajveer100)
<details>
<summary>Changes</summary>
Resolves Issue #<!-- -->82249
As described in the issue, any deallocation function for a `class X` is a static member (even if not explicitly declared static).
---
Full diff: https://github.com/llvm/llvm-project/pull/82251.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaDeclCXX.cpp (+3-1)
- (modified) clang/test/SemaCXX/cxx2b-deducing-this.cpp (+4)
``````````diff
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index ab8a967b06a456..7a7b544d82b640 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -11391,7 +11391,9 @@ void Sema::CheckExplicitObjectMemberFunction(Declarator &D,
<< ExplicitObjectParam->getSourceRange();
}
- if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static) {
+ if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static ||
+ (D.getContext() == clang::DeclaratorContext::Member &&
+ D.isStaticMember())) {
Diag(ExplicitObjectParam->getBeginLoc(),
diag::err_explicit_object_parameter_nonmember)
<< D.getSourceRange() << /*static=*/0 << IsLambda;
diff --git a/clang/test/SemaCXX/cxx2b-deducing-this.cpp b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
index aab35828096a8e..530f8bf6af1b6b 100644
--- a/clang/test/SemaCXX/cxx2b-deducing-this.cpp
+++ b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
@@ -16,6 +16,10 @@ struct S {
static void f(this auto); // expected-error{{an explicit object parameter cannot appear in a static function}}
virtual void f(this S); // expected-error{{an explicit object parameter cannot appear in a virtual function}}
+ // new and delete are implicitly static
+ void *operator new(this unsigned long); // expected-error{{an explicit object parameter cannot appear in a static function}}
+ void operator delete(this void*); // expected-error{{an explicit object parameter cannot appear in a static function}}
+
void g(this auto) const; // expected-error{{explicit object member function cannot have 'const' qualifier}}
void h(this auto) &; // expected-error{{explicit object member function cannot have '&' qualifier}}
void i(this auto) &&; // expected-error{{explicit object member function cannot have '&&' qualifier}}
``````````
</details>
https://github.com/llvm/llvm-project/pull/82251
More information about the cfe-commits
mailing list