[clang] [clang] [SemaCXX] Disallow deducing "this" on operator `new` and `delete` (PR #82251)
Rajveer Singh Bharadwaj via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 19 06:06:00 PST 2024
https://github.com/Rajveer100 created https://github.com/llvm/llvm-project/pull/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).
>From 3ad4e85144739e16be13a4d5641b24d1a7e5b00b Mon Sep 17 00:00:00 2001
From: Rajveer <rajveer.developer at icloud.com>
Date: Mon, 19 Feb 2024 19:29:48 +0530
Subject: [PATCH] [clang] [SemaCXX] Disallow deducing "this" on operator `new`
and `delete`
Resolves Issue #82249
---
clang/lib/Sema/SemaDeclCXX.cpp | 4 +++-
clang/test/SemaCXX/cxx2b-deducing-this.cpp | 4 ++++
2 files changed, 7 insertions(+), 1 deletion(-)
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}}
More information about the cfe-commits
mailing list