[clang] [Clang] Skip invalid fields when synthesizing defaulted comparisons (PR #221570)
Akash Manna via cfe-commits
cfe-commits at lists.llvm.org
Sun Sep 6 06:31:02 PDT 2026
https://github.com/akash-manna-sky created https://github.com/llvm/llvm-project/pull/221570
Fixes #194605
`CheckFieldDecl` already rejects `int [[clang::address_space(1)]] i;` and marks the field invalid, but `DefaultedComparisonVisitor` still visited it when synthesizing the defaulted `operator==`. `BuildFieldReferenceExpr` then tripped the `!MemberQuals.hasAddressSpace()` assertion. Both `__is_trivially_equality_comparable(S)` and a plain `a == b` reach that path.
`visitSubobjects` now skips invalid fields, the same way `SpecialMemberVisitor` and the copy-assignment synthesis already do. That covers the analyzer and the synthesizer together, and the assertion stays in place since it still holds for valid code.
>From 5a94c6aa71cc44a905991bc364e6599dc4de0132 Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Sun, 6 Sep 2026 18:58:51 +0530
Subject: [PATCH] [Clang] Skip invalid fields when synthesizing defaulted
comparisons
A field whose type carries an address space is already rejected by
CheckFieldDecl and marked invalid, but DefaultedComparisonVisitor
still visited it when building a defaulted operator== or operator<=>.
BuildFieldReferenceExpr then asserted because a member type is never
supposed to carry an address space qualifier.
Skip invalid fields in visitSubobjects, matching what the other
defaulted-member visitors already do.
Fixes #194605
---
clang/docs/ReleaseNotes.md | 4 ++++
clang/lib/Sema/SemaDeclCXX.cpp | 3 +++
clang/test/SemaCXX/cxx20-default-compare.cpp | 11 +++++++++++
3 files changed, 18 insertions(+)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 042d7112dbe7d..7f7d9e4813b91 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -628,6 +628,10 @@ features cannot lower the translation-unit ABI level;
- Fixed a crash when a coroutine keyword appeared inside a mem-initializer on a
function that is not a constructor. (#GH194298)
+- Fixed an assertion when a defaulted comparison operator was synthesized for a
+ class with an invalid non-static data member, such as one qualified with an
+ address space. (#GH194605)
+
#### Bug Fixes to AST Handling
- Fixed a non-deterministic ordering of unused local typedefs that made
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 4457ec58902d0..28beb44af987b 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -8203,6 +8203,9 @@ class DefaultedComparisonVisitor {
// Unnamed bit-fields are not members ...
if (Field->isUnnamedBitField())
continue;
+ // Skip invalid fields; they have already been diagnosed.
+ if (Field->isInvalidDecl())
+ continue;
// Recursively expand anonymous structs.
if (Field->isAnonymousStructOrUnion()) {
if (visitSubobjects(Results, Field->getType()->getAsCXXRecordDecl(),
diff --git a/clang/test/SemaCXX/cxx20-default-compare.cpp b/clang/test/SemaCXX/cxx20-default-compare.cpp
index c569e9d866970..fd5b703c566d1 100644
--- a/clang/test/SemaCXX/cxx20-default-compare.cpp
+++ b/clang/test/SemaCXX/cxx20-default-compare.cpp
@@ -79,3 +79,14 @@ struct S {
bool b = (S{} < S{}); // expected-error {{object of type 'S' cannot be compared because its 'operator<=>' is implicitly deleted}}
}
+
+namespace GH194605 {
+struct S {
+ int [[clang::address_space(1)]] i; // expected-error {{field may not be qualified with an address space}}
+ bool operator==(const S &) const = default;
+};
+
+static_assert(!__is_trivially_equality_comparable(S));
+
+bool f(const S &a, const S &b) { return a == b; }
+}
More information about the cfe-commits
mailing list