[clang] [Clang] Deleting an incomplete enum type is not an error (PR #118455)

via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 3 21:15:59 PST 2024


https://github.com/cor3ntin updated https://github.com/llvm/llvm-project/pull/118455

>From 8b89f6a56e0921b13c8638c9768ec99945825840 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Tue, 3 Dec 2024 10:29:23 +0100
Subject: [PATCH 1/2] [Clang] Deleting an incomplete enum type is not an error

The changes introduced in #97733 accidentally prevented
to delete an incomplete enum (the validity of which
has been confirmed by CWG2925

Fixes #99278
---
 clang/docs/ReleaseNotes.rst       | 1 +
 clang/lib/Sema/SemaExprCXX.cpp    | 3 ++-
 clang/test/SemaCXX/new-delete.cpp | 8 ++++++++
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 01c7899e36c932..e442516c225f74 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -766,6 +766,7 @@ Bug Fixes to C++ Support
 - Fixed an assertion failure caused by mangled names with invalid identifiers. (#GH112205)
 - Fixed an incorrect lambda scope of generic lambdas that caused Clang to crash when computing potential lambda
   captures at the end of a full expression. (#GH115931)
+- Clang no longer rejects deleting a pointer of incomplete enumeration type. (#GH99278)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index d85819b21c8265..20947ca4e35b17 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -3748,7 +3748,8 @@ Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
       // FIXME: This can result in errors if the definition was imported from a
       // module but is hidden.
       if (!RequireCompleteType(StartLoc, Pointee,
-                               LangOpts.CPlusPlus26
+                               Pointee->isStructureOrClassType() &&
+                                       LangOpts.CPlusPlus26
                                    ? diag::err_delete_incomplete
                                    : diag::warn_delete_incomplete,
                                Ex.get())) {
diff --git a/clang/test/SemaCXX/new-delete.cpp b/clang/test/SemaCXX/new-delete.cpp
index 595bdc689d694b..18b26e7f0f08a1 100644
--- a/clang/test/SemaCXX/new-delete.cpp
+++ b/clang/test/SemaCXX/new-delete.cpp
@@ -540,6 +540,14 @@ namespace PR10504 {
   void f(A *x) { delete x; } // expected-warning {{delete called on 'PR10504::A' that is abstract but has non-virtual destructor}}
 }
 
+#if __cplusplus >= 201103L
+enum GH99278_1 { // expected-note {{definition of 'GH99278_1' is not complete until the closing '}'}}
+    zero = decltype(delete static_cast<GH99278_1*>(nullptr), 0){}
+    // expected-warning at -1 {{deleting pointer to incomplete type}}
+    // expected-warning at -2 {{expression with side effects has no effect in an unevaluated context}}
+};
+#endif
+
 struct PlacementArg {};
 inline void *operator new[](size_t, const PlacementArg &) throw () {
   return 0;

>From eeaa30618c73febdf413dbec6deb8792c576a30d Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Wed, 4 Dec 2024 06:08:38 +0100
Subject: [PATCH 2/2] remove warning

---
 clang/lib/Sema/SemaExprCXX.cpp    | 6 +++---
 clang/test/SemaCXX/new-delete.cpp | 5 ++---
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 20947ca4e35b17..d524337f7f79b5 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -3747,9 +3747,9 @@ Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
     } else if (!Pointee->isDependentType()) {
       // FIXME: This can result in errors if the definition was imported from a
       // module but is hidden.
-      if (!RequireCompleteType(StartLoc, Pointee,
-                               Pointee->isStructureOrClassType() &&
-                                       LangOpts.CPlusPlus26
+      if (!Pointee->isStructureOrClassType() ||
+          !RequireCompleteType(StartLoc, Pointee,
+                               LangOpts.CPlusPlus26
                                    ? diag::err_delete_incomplete
                                    : diag::warn_delete_incomplete,
                                Ex.get())) {
diff --git a/clang/test/SemaCXX/new-delete.cpp b/clang/test/SemaCXX/new-delete.cpp
index 18b26e7f0f08a1..98b168d9df1f37 100644
--- a/clang/test/SemaCXX/new-delete.cpp
+++ b/clang/test/SemaCXX/new-delete.cpp
@@ -541,10 +541,9 @@ namespace PR10504 {
 }
 
 #if __cplusplus >= 201103L
-enum GH99278_1 { // expected-note {{definition of 'GH99278_1' is not complete until the closing '}'}}
+enum GH99278_1 {
     zero = decltype(delete static_cast<GH99278_1*>(nullptr), 0){}
-    // expected-warning at -1 {{deleting pointer to incomplete type}}
-    // expected-warning at -2 {{expression with side effects has no effect in an unevaluated context}}
+    // expected-warning at -1 {{expression with side effects has no effect in an unevaluated context}}
 };
 #endif
 



More information about the cfe-commits mailing list