[clang] [clang] Avoid invalid recovery cast for non-pointer Class redefinitions (PR #213590)

via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 2 21:51:39 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Gauarv Chaudhary (ANAMASGARD)

<details>
<summary>Changes</summary>

## 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.

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


2 Files Affected:

- (modified) clang/lib/Sema/SemaExprMember.cpp (+5) 
- (added) clang/test/SemaObjCXX/gh213066.mm (+9) 


``````````diff
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}}
+}

``````````

</details>


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


More information about the cfe-commits mailing list