[clang] f7c2e5f - [clang] [SemaCXX] Disallow deducing "this" on operator `new` and `delete` (#82251)

via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 21 06:30:53 PST 2024


Author: Rajveer Singh Bharadwaj
Date: 2024-02-21T15:30:49+01:00
New Revision: f7c2e5fa05d221a3dfc53744f353517407c2ffec

URL: https://github.com/llvm/llvm-project/commit/f7c2e5fa05d221a3dfc53744f353517407c2ffec
DIFF: https://github.com/llvm/llvm-project/commit/f7c2e5fa05d221a3dfc53744f353517407c2ffec.diff

LOG: [clang] [SemaCXX] Disallow deducing "this" on operator `new` and `delete` (#82251)

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).

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaDeclCXX.cpp
    clang/test/SemaCXX/cxx2b-deducing-this.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 5bca2c965c866b..c17298bc7bce5e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -279,6 +279,8 @@ Bug Fixes to C++ Support
   Fixes (`#68490 <https://github.com/llvm/llvm-project/issues/68490>`_)
 - Fix a crash when trying to call a varargs function that also has an explicit object parameter.
   Fixes (`#80971 ICE when explicit object parameter be a function parameter pack`)
+- Reject explicit object parameters on `new` and `delete` operators.
+  Fixes (`#82249 <https://github.com/llvm/llvm-project/issues/82249>` _)
 - Fixed a bug where abbreviated function templates would append their invented template parameters to
   an empty template parameter lists.
 - Clang now classifies aggregate initialization in C++17 and newer as constant

diff  --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 79263bc3ff671d..7c009d9c8ec093 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -11395,7 +11395,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 30131d6adc4db0..b8ddb9ad300034 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}}


        


More information about the cfe-commits mailing list