https://github.com/woruyu created https://github.com/llvm/llvm-project/pull/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 >From 9cb437e26e5cb015a238959670e799bf94ef0ea4 Mon Sep 17 00:00:00 2001 From: woruyu <[email protected]> Date: Fri, 20 Nov 2026 21:03:08 +0800 Subject: [PATCH] [Clang] Fix crash when substituting an invalid _Generic controlling type --- clang/docs/ReleaseNotes.md | 3 +++ clang/lib/Sema/TreeTransform.h | 2 +- clang/test/SemaCXX/generic-selection.cpp | 10 ++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index f17f79eafcb703..76812a8c8e75da 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -1273,6 +1273,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 2b6348b7ec8a6b..0a72bfe9f440b5 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -13842,7 +13842,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 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
