Skip to content

[clang] [SemaCXX] Disallow deducing "this" on operator new and delete #82251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2024

Conversation

Rajveer100
Copy link
Member

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

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 19, 2024
@llvmbot
Copy link
Member

llvmbot commented Feb 19, 2024

@llvm/pr-subscribers-clang

Author: Rajveer Singh Bharadwaj (Rajveer100)

Changes

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 --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}}

@cor3ntin
Copy link
Contributor

This change needs a release note.
Please add an entry to clang/docs/ReleaseNotes.rst in the section the most adapted to the change, and referencing any Github issue this change fixes. Thanks!

Besides that, I think the change makes sense, thanks!

@Rajveer100 Rajveer100 force-pushed the branch-for-issue-82249 branch from 3ad4e85 to 8fd5e1b Compare February 19, 2024 18:48
Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo nit

@Rajveer100 Rajveer100 force-pushed the branch-for-issue-82249 branch from 8fd5e1b to 05dbfac Compare February 20, 2024 05:30
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for submitting a fix.

@cor3ntin
Copy link
Contributor

@Rajveer100 you need us to merge that for you?

@Rajveer100 Rajveer100 force-pushed the branch-for-issue-82249 branch from 3ccd24a to 05dbfac Compare February 21, 2024 08:27
@Rajveer100
Copy link
Member Author

Rajveer100 commented Feb 21, 2024

Considering the PR sounds good to the reviewers, yes it would be great to have it merged.

@cor3ntin cor3ntin merged commit f7c2e5f into llvm:main Feb 21, 2024
@cor3ntin
Copy link
Contributor

Thanks for working on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants