[clang] [Clang] Fix stack overflow with recursive lambdas in templates (PR #180325)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 9 06:33:37 PST 2026


erichkeane wrote:

> ## Summary
> 
> Fixes #180319
> 
> Recursive lambdas in templates could cause Clang to crash with a stack overflow before hitting the template instantiation depth limit. This happened because `LambdaExpressionSubstitution` doesn't count toward the depth limit (by design for SFINAE per [temp.deduct]p9).
> 
> This PR wraps the `TransformLambdaBody` call in `runWithSufficientStackSpace` to handle deeply nested recursive lambdas gracefully:
> 
>     * If threads are enabled, it runs on a new 8MB stack when stack is nearly exhausted
> 
>     * If threads are disabled, it emits a warning and continues
> 
> 
> ## Test plan
> 
>     * Added TEST=4 to `clang/test/SemaTemplate/stack-exhaustion.cpp` with the reproduction case from the issue
> 
>     * The test uses `complexify<200>` which would previously crash but now handles gracefully

What does the first bit have to do at all with the rest?  This commit message is pretty nonsensical, please write it yourself.  

As far as the patch: It makes no sense if you look at the bug's stack trace.  I don't see 'TransformLambaBody' ANYWHERE there.  Everything in the stack trace is about constant evaluation.  It SEEMS to me that the lambda transformation solving this is just lucky/happens by resetting it and letting the expression evaluation 'work'.

The real solution to this bug is likely to figure out a way to get the constant expression evaluator to do a better job at managing stack/tailcalling/something else.

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


More information about the cfe-commits mailing list