[clang] [clang] Avoid invalid recovery cast for non-pointer Class redefinitions (PR #213590)
Gauarv Chaudhary via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 2 21:50:35 PDT 2026
https://github.com/ANAMASGARD created https://github.com/llvm/llvm-project/pull/213590
## Summary
Clang's Objective-C++ member lookup recovery can substitute a user-defined redefinition for builtin `id` or `Class` and construct a `CK_BitCast`.
When the redefinition is a non-pointer type, this creates an invalid AST state by casting an Objective-C object pointer to a record type. The resulting AST violates `CastExpr::CastConsistency()` and triggers an assertion failure.
This change rejects non-pointer redefinition types before constructing the recovery cast. C pointer and Objective-C object pointer redefinitions continue to use the existing recovery behavior.
Fixes #213066
## Testing
- Added an assertions-enabled regression test:
`clang/test/SemaObjCXX/gh213066.mm`
- Confirmed the unpatched compiler reproduces the assertion.
- Confirmed the patched compiler emits normal diagnostics without crashing.
- Focused lit tests pass: `gh213066.mm` and `pr32725.mm`.
- `git diff --check` passes.
- The full `check-clang` suite was not run locally to avoid excessive resource
usage.
## AI Assistance
AI assistance was used to inspect the relevant Clang code paths, validate the root cause, develop the narrow fix, draft the regression test, and help run and interpret focused verification. The final patch and test results were
reviewed against the LLVM source and local build output.
>From 0237bc2c44324d3ae4f02e8c8992b96c6d1e2bbd Mon Sep 17 00:00:00 2001
From: Gaurav Chaudhary <chaudharygaurav2004 at gmail.com>
Date: Mon, 3 Aug 2026 10:17:56 +0530
Subject: [PATCH] [clang] Avoid invalid recovery cast for non-pointer Class
redefinitions
ShouldTryAgainWithRedefinitionType() can attempt to recover builtin Objective-C id/
Class member access using a user-written non-pointer redefinition. This constructs a
CK_BitCast between incompatible type categories and violates CastExpr's AST invariants.
Reject non-pointer redefinition types before attempting the recovery cast, while preserving
recovery for C and Objective-C object pointers.
Fixes #213066
Signed-off-by: Gaurav Chaudhary <chaudharygaurav2004 at gmail.com>
---
clang/lib/Sema/SemaExprMember.cpp | 5 +++++
clang/test/SemaObjCXX/gh213066.mm | 9 +++++++++
2 files changed, 14 insertions(+)
create mode 100644 clang/test/SemaObjCXX/gh213066.mm
diff --git a/clang/lib/Sema/SemaExprMember.cpp b/clang/lib/Sema/SemaExprMember.cpp
index 62ecc52c0374b..f42b94c216660 100644
--- a/clang/lib/Sema/SemaExprMember.cpp
+++ b/clang/lib/Sema/SemaExprMember.cpp
@@ -1188,6 +1188,11 @@ static bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) {
return false;
}
+ // Only retry with pointer redefinition types, since a non-pointer
+ // destination would produce an invalid CK_BitCast.
+ if (!redef->isAnyPointerType())
+ return false;
+
// Do the substitution as long as the redefinition type isn't just a
// possibly-qualified pointer to builtin-id or builtin-Class again.
opty = redef->getAs<ObjCObjectPointerType>();
diff --git a/clang/test/SemaObjCXX/gh213066.mm b/clang/test/SemaObjCXX/gh213066.mm
new file mode 100644
index 0000000000000..58123a3640a4a
--- /dev/null
+++ b/clang/test/SemaObjCXX/gh213066.mm
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple x86_64-apple-macosx -fsyntax-only -verify -x objective-c++ %s
+// REQUIRES: asserts
+
+typedef struct {} Class;
+
+void test() {
+ Class c;
+ c.className; // expected-error {{member reference base type 'Class' is not a structure or union}}
+}
More information about the cfe-commits
mailing list