[PATCH] D126160: [Concepts] Add an error for unsupported P0848 (destructor overloading) code

Roy Jacobson via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun May 22 04:43:29 PDT 2022


royjacobson updated this revision to Diff 431229.
royjacobson added a comment.

A working version.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126160/new/

https://reviews.llvm.org/D126160

Files:
  clang/include/clang/Basic/DiagnosticASTKinds.td
  clang/lib/AST/DeclCXX.cpp
  clang/test/CXX/over/over.match/over.match.viable/p3.cpp


Index: clang/test/CXX/over/over.match/over.match.viable/p3.cpp
===================================================================
--- clang/test/CXX/over/over.match/over.match.viable/p3.cpp
+++ clang/test/CXX/over/over.match/over.match.viable/p3.cpp
@@ -44,24 +44,26 @@
 
 template<typename T>
 struct S {
+  // expected-error at -1 7{{overloading destructors is not supported}}
   void foo(int) requires false;
   void foo(A) requires true;
   S(A) requires false;
   S(double) requires true;
   ~S() requires false;
   // expected-note at -1 2{{because 'false' evaluated to false}}
+  // expected-note at -2 7{{destructor declared here}}
   ~S() requires true;
+  // expected-note at -1 7{{destructor declared here}}
   operator int() requires true;
   operator int() requires false;
 };
 
 void bar() {
+  // Note - we have a hard error in this case until P0848 is implemented.
   WrapsStatics<int>::foo(A{});
   S<int>{1.}.foo(A{});
   // expected-error at -1{{invalid reference to function '~S': constraints not satisfied}}
-  // Note - this behavior w.r.t. constrained dtors is a consequence of current
-  // wording, which does not invoke overload resolution when a dtor is called.
-  // P0848 is set to address this issue.
+
   S<int> s = 1;
   // expected-error at -1{{invalid reference to function '~S': constraints not satisfied}}
   int a = s;
Index: clang/lib/AST/DeclCXX.cpp
===================================================================
--- clang/lib/AST/DeclCXX.cpp
+++ clang/lib/AST/DeclCXX.cpp
@@ -29,6 +29,7 @@
 #include "clang/AST/TypeLoc.h"
 #include "clang/AST/UnresolvedSet.h"
 #include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/DiagnosticAST.h"
 #include "clang/Basic/IdentifierTable.h"
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/LangOptions.h"
@@ -1895,6 +1896,33 @@
 
   DeclContext::lookup_result R = lookup(Name);
 
+  // We don't support C++20 constrained destructors yet, and we are unlikely to
+  // do so in the near future. Until we do, we check here for multiple
+  // declarations and report an error. We have to handle correctly the case of
+  // merged declarations in modules and the case of invalid templated
+  // destructors so this is a bit involved.
+  // We also don't emit errors on OpenCL code because OpenCL desturctors can
+  // overload on address space (https://reviews.llvm.org/D64569).
+  if (!(R.empty() || R.isSingleResult()) && !Context.getLangOpts().OpenCL) {
+    bool hasNonTrivialOverloads = false;
+    auto firstDecl = R.front();
+    for (const auto &decl : R) {
+      if (!decl->isTemplateDecl() && !firstDecl->isTemplateDecl() &&
+          !Context.isSameEntity(decl, firstDecl)) {
+        hasNonTrivialOverloads = true;
+      }
+    }
+    if (hasNonTrivialOverloads) {
+      Context.getDiagnostics().Report(
+          getLocation(), diag::err_unsupported_destructor_overloading);
+      for (const auto &decl : R) {
+        Context.getDiagnostics().Report(
+            decl->getLocation(),
+            diag::note_unsupported_destructor_overloading_declaration);
+      }
+    }
+  }
+
   return R.empty() ? nullptr : dyn_cast<CXXDestructorDecl>(R.front());
 }
 
Index: clang/include/clang/Basic/DiagnosticASTKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticASTKinds.td
+++ clang/include/clang/Basic/DiagnosticASTKinds.td
@@ -597,4 +597,11 @@
 def warn_unaligned_access : Warning<
   "field %1 within %0 is less aligned than %2 and is usually due to %0 being "
   "packed, which can lead to unaligned accesses">, InGroup<UnalignedAccess>, DefaultIgnore;
+
+def err_unsupported_destructor_overloading : Error<
+  "overloading destructors is not supported yet in this version. Consult "
+  "'https://github.com/llvm/llvm-project/issues/45614' for updates">;
+def note_unsupported_destructor_overloading_declaration : Note<
+  "destructor declared here.">;
+
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126160.431229.patch
Type: text/x-patch
Size: 3908 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220522/dc1ed930/attachment.bin>


More information about the cfe-commits mailing list