[clang] 3cb45f4 - [Clang] Fix crash when substituting an invalid _Generic controlling type (#244586)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 25 17:34:38 PST 2026


Author: woruyu
Date: 2026-11-26T09:34:32+08:00
New Revision: 3cb45f434dbec0bef83043f2be0ce282439155a3

URL: https://github.com/llvm/llvm-project/commit/3cb45f434dbec0bef83043f2be0ce282439155a3
DIFF: https://github.com/llvm/llvm-project/commit/3cb45f434dbec0bef83043f2be0ce282439155a3.diff

LOG: [Clang] Fix crash when substituting an invalid _Generic controlling type (#244586)

This fixes https://github.com/llvm/llvm-project/issues/242122. When
substitution of the controlling type fails, the controlling expression
is unset, so isInvalid() returns false. This lets Clang attempt to
rebuild the generic selection with a null operand. Use isUsable() to
detect when both the expression and type are absent and propagate the
substitution failure.

Assisted-by: OpenAI Codex

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.md
    clang/lib/Sema/TreeTransform.h
    clang/test/SemaCXX/generic-selection.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index e461d4159098fb..2bc6c49cd684bd 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1351,6 +1351,9 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 - Fixed bugs where Clang could apply an incorrectly high alignment to a vptr
   load within a virtual function thunk. (#GH242501)
 
+- Fixed a crash when template substitution produces an invalid controlling type
+  for `_Generic`. (#GH242122)
+
 #### Bug Fixes to AST Handling
 
 - Fixed a non-deterministic ordering of unused local typedefs that made

diff  --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 84a0e52fecfa53..d845cb4142d7c0 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -13846,7 +13846,7 @@ TreeTransform<Derived>::TransformGenericSelectionExpr(GenericSelectionExpr *E) {
   else
     ControllingType = getDerived().TransformType(E->getControllingType());
 
-  if (ControllingExpr.isInvalid() && !ControllingType)
+  if (!ControllingExpr.isUsable() && !ControllingType)
     return ExprError();
 
   SmallVector<Expr *, 4> AssocExprs;

diff  --git a/clang/test/SemaCXX/generic-selection.cpp b/clang/test/SemaCXX/generic-selection.cpp
index ed9d2b7d44be3f..8aab5d023aa3e9 100644
--- a/clang/test/SemaCXX/generic-selection.cpp
+++ b/clang/test/SemaCXX/generic-selection.cpp
@@ -90,3 +90,13 @@ void func(struct S s) {
   (void)_Generic(s, struct W : S { int b; } : 1); // expected-error {{expected '(' for function-style cast or type construction}}
 }
 } // namespace GH55562
+
+namespace GH242122 {
+template <typename T> struct S {
+  // T() is parsed as a function type returning T.
+  enum { id = _Generic(T(), int(): 1) }; // expected-error {{function cannot return function type 'void ()'}}
+};
+
+static_assert(S<int>::id == 1, "");
+template struct S<void()>; // expected-note {{in instantiation of template class 'GH242122::S<void ()>' requested here}}
+} // namespace GH242122


        


More information about the cfe-commits mailing list