[clang] [Clang][Sema] Don't delay the access check when computing implicit deletion (PR #210254)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 16 23:12:43 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Akash Agrawal (akashagrwl)

<details>
<summary>Changes</summary>



Sema::isMemberAccessibleForDeletion treats AR_delayed as unreachable, but CheckAccess returns AR_delayed whenever it runs inside an enclosing delayed-diagnostics scope. That happens when deletion checking runs synchronously while parsing a later declaration -- e.g. while explaining why a defaulted operator<=> is deleted for an expression in that declaration's initializer. The caller cannot consume a delayed diagnostic, so letting CheckAccess delay always hits
llvm_unreachable("cannot delay =delete computation") and crashes.

Force an immediate answer by wrapping the CheckAccess call in DelayedDiagnostics.pushUndelayed()/popUndelayed() via llvm::scope_exit, mirroring the existing Sema::CheckEnableIf pattern in SemaOverload.cpp.

Co-authored - Claude-Sonnet

---
Full diff: https://github.com/llvm/llvm-project/pull/210254.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaAccess.cpp (+11) 
- (modified) clang/test/SemaCXX/cxx20-default-compare.cpp (+18) 


``````````diff
diff --git a/clang/lib/Sema/SemaAccess.cpp b/clang/lib/Sema/SemaAccess.cpp
index 17415b4185eff..f2e19488a6ee2 100644
--- a/clang/lib/Sema/SemaAccess.cpp
+++ b/clang/lib/Sema/SemaAccess.cpp
@@ -21,6 +21,7 @@
 #include "clang/Sema/DelayedDiagnostic.h"
 #include "clang/Sema/Initialization.h"
 #include "clang/Sema/Lookup.h"
+#include "llvm/ADT/ScopeExit.h"
 
 using namespace clang;
 using namespace sema;
@@ -1612,6 +1613,16 @@ bool Sema::isMemberAccessibleForDeletion(CXXRecordDecl *NamingClass,
   // Suppress diagnostics.
   Entity.setDiag(Diag);
 
+  // Deletion checking can run while we are inside an enclosing
+  // delayed-diagnostics scope (e.g. when parsing a later declaration whose
+  // initializer requires explaining why a defaulted comparison operator is
+  // deleted). CheckAccess would then return AR_delayed, but the result must be
+  // known immediately here. Force an undelayed check, mirroring CheckEnableIf.
+  llvm::scope_exit UndelayDiags(
+      [&, CurrentState(DelayedDiagnostics.pushUndelayed())] {
+        DelayedDiagnostics.popUndelayed(CurrentState);
+      });
+
   switch (CheckAccess(*this, Loc, Entity)) {
   case AR_accessible: return true;
   case AR_inaccessible: return false;
diff --git a/clang/test/SemaCXX/cxx20-default-compare.cpp b/clang/test/SemaCXX/cxx20-default-compare.cpp
index 3e4673c31e489..cb2886608d6f6 100644
--- a/clang/test/SemaCXX/cxx20-default-compare.cpp
+++ b/clang/test/SemaCXX/cxx20-default-compare.cpp
@@ -65,3 +65,21 @@ struct MutCheck : MutOnly {
   // bool operator==(this MutCheck, MutCheck) = default;
 };
 }
+
+namespace delayed_deletion_check {
+// Explaining why a defaulted comparison operator is deleted can run while we
+// are parsing a later declaration, i.e. inside an enclosing delayed-diagnostics
+// scope. The access check for the deleted-ness computation must produce an
+// immediate answer rather than being delayed. Previously this crashed.
+struct HasPrivateSpaceship {
+private:
+  std::strong_ordering operator<=>(const HasPrivateSpaceship &) const; // expected-note 2 {{declared private here}}
+};
+
+struct S {
+  HasPrivateSpaceship member; // expected-note 2 {{because it would invoke a private 'operator<=>' member of 'delayed_deletion_check::HasPrivateSpaceship' to compare member 'member'}}
+  auto operator<=>(const S &) const = default; // expected-warning {{explicitly defaulted three-way comparison operator is implicitly deleted}} expected-note {{replace 'default' with 'delete'}} expected-note {{explicitly defaulted function was implicitly deleted here}}
+};
+
+bool b = (S{} < S{}); // expected-error {{object of type 'S' cannot be compared because its 'operator<=>' is implicitly deleted}}
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/210254


More information about the cfe-commits mailing list