Skip to content

Commit f7c2e5f

Browse files
authored
[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).
1 parent 91ebd01 commit f7c2e5f

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ Bug Fixes to C++ Support
279279
Fixes (`#68490 <https://github.com/llvm/llvm-project/issues/68490>`_)
280280
- Fix a crash when trying to call a varargs function that also has an explicit object parameter.
281281
Fixes (`#80971 ICE when explicit object parameter be a function parameter pack`)
282+
- Reject explicit object parameters on `new` and `delete` operators.
283+
Fixes (`#82249 <https://github.com/llvm/llvm-project/issues/82249>` _)
282284
- Fixed a bug where abbreviated function templates would append their invented template parameters to
283285
an empty template parameter lists.
284286
- Clang now classifies aggregate initialization in C++17 and newer as constant

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11395,7 +11395,9 @@ void Sema::CheckExplicitObjectMemberFunction(Declarator &D,
1139511395
<< ExplicitObjectParam->getSourceRange();
1139611396
}
1139711397

11398-
if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static) {
11398+
if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static ||
11399+
(D.getContext() == clang::DeclaratorContext::Member &&
11400+
D.isStaticMember())) {
1139911401
Diag(ExplicitObjectParam->getBeginLoc(),
1140011402
diag::err_explicit_object_parameter_nonmember)
1140111403
<< D.getSourceRange() << /*static=*/0 << IsLambda;

clang/test/SemaCXX/cxx2b-deducing-this.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ struct S {
1616
static void f(this auto); // expected-error{{an explicit object parameter cannot appear in a static function}}
1717
virtual void f(this S); // expected-error{{an explicit object parameter cannot appear in a virtual function}}
1818

19+
// new and delete are implicitly static
20+
void *operator new(this unsigned long); // expected-error{{an explicit object parameter cannot appear in a static function}}
21+
void operator delete(this void*); // expected-error{{an explicit object parameter cannot appear in a static function}}
22+
1923
void g(this auto) const; // expected-error{{explicit object member function cannot have 'const' qualifier}}
2024
void h(this auto) &; // expected-error{{explicit object member function cannot have '&' qualifier}}
2125
void i(this auto) &&; // expected-error{{explicit object member function cannot have '&&' qualifier}}

0 commit comments

Comments
 (0)