[clang] 1299c87 - fix gh185270 consteval crash (#185511)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 10 06:18:28 PDT 2026
Author: Serosh
Date: 2026-03-10T06:18:23-07:00
New Revision: 1299c87f48b9e580ac28a42ae2a6970130cea855
URL: https://github.com/llvm/llvm-project/commit/1299c87f48b9e580ac28a42ae2a6970130cea855
DIFF: https://github.com/llvm/llvm-project/commit/1299c87f48b9e580ac28a42ae2a6970130cea855.diff
LOG: fix gh185270 consteval crash (#185511)
replace `cast<VarDecl>` with `dyn_cast_or_null<VarDecl>` in
`HandleImmediateInvocations` to avoid the crash when
`ManglingContextDecl` is a `FunctionDecl` instead of a `VarDecl`.
fixes #185270
Added:
clang/test/SemaCXX/gh185270.cpp
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaExpr.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7482bb079d3a2..b57b77649fc80 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -341,6 +341,8 @@ Bug Fixes to C++ Support
when used inside decltype in the return type. (#GH180460)
- Fixed a crash when evaluating uninitialized GCC vector/ext_vector_type vectors in ``constexpr``. (#GH180044)
- Fixed a crash on ``typeid`` of incomplete local types during template instantiation. (#GH63242), (#GH176397)
+- Fixed a crash when an immediate-invoked ``consteval`` lambda is used as an invalid initializer. (#GH185270)
+
- Inherited constructors in ``dllexport`` classes are now exported for ABI-compatible cases, matching
MSVC behavior. Constructors with variadic arguments or callee-cleanup parameters are not yet supported
and produce a warning. (#GH162640)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 04b3b36aacf60..db6d93ce54791 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -18455,9 +18455,9 @@ HandleImmediateInvocations(Sema &SemaRef,
if (SemaRef.getLangOpts().CPlusPlus23 &&
Rec.ExprContext ==
Sema::ExpressionEvaluationContextRecord::EK_VariableInit) {
- auto *VD = cast<VarDecl>(Rec.ManglingContextDecl);
- if (VD->isUsableInConstantExpressions(SemaRef.Context) ||
- VD->hasConstantInitialization()) {
+ auto *VD = dyn_cast<VarDecl>(Rec.ManglingContextDecl);
+ if (VD && (VD->isUsableInConstantExpressions(SemaRef.Context) ||
+ VD->hasConstantInitialization())) {
// An expression or conversion is in an 'immediate function context' if it
// is potentially evaluated and either:
// [...]
diff --git a/clang/test/SemaCXX/gh185270.cpp b/clang/test/SemaCXX/gh185270.cpp
new file mode 100644
index 0000000000000..3f7bdaaca1f8b
--- /dev/null
+++ b/clang/test/SemaCXX/gh185270.cpp
@@ -0,0 +1,3 @@
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
+
+void foo()([] consteval -> int { return 0; }()); // expected-error {{illegal initializer (only variables can be initialized)}}
More information about the cfe-commits
mailing list