<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/145776>145776</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Clang] Immediate-escalating expression attributed to wrong lambda
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
katzdm
</td>
</tr>
</table>
<pre>
The following should be well-formed, but [clang diagnoses](https://godbolt.org/z/57xfWbE71):
```cpp
void runtime_only() { }
consteval void comptime_only() {}
void fn() {
[]() {
runtime_only();
[](){ (void) comptime_only; }();
}();
}
```
The _id-expression_ that names `comptime_only` is [immediate-escalating](https://eel.is/c++draft/expr.const#25.1). Clang currently attributes that expression to the outer lambda, despite its occurrence within the inner lambda. It seems to me that this stems from the `ExpressionEvaluationContext` corresponding to the inner lambda [being popped](https://github.com/llvm/llvm-project/blob/0c359d747daa97197d17d2c48d7964d49f7f1b98/clang/lib/Sema/SemaLambda.cpp#L2173) _after_ the `FunctionScopeInfo` corresponding to the same has [already been popped](https://github.com/llvm/llvm-project/blob/0c359d747daa97197d17d2c48d7964d49f7f1b98/clang/lib/Sema/SemaLambda.cpp#L1978). Naively re-sequencing these operations appears to cause other problems (e.g., failing assertions related to ODR-uses in `ActOnFinishFunctionBody`).
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzMVE1v4zYQ_TX0ZWBBoixROugQJzEQYNEFugV6DPgxkthSpEpSTry_vqBkZ5NteuitgABC5Hy8efNmeAh6sIgdqY6ketjxJY7Od3_y-F1NO-HUpfttROidMe5F2wHC6BajQCC8oDH73vkJFaH3IJYIpDpKw-0ASvPBuoCBVA-ENmOMcyDlHaEnQk-DU8KZmDk_EHr6TuipYq_97-KRFYS2ySxfvzrfPjnPJL87O63ALzbqCZ-dNRdCG0JbIOwIhD2Q_E46GyKeuYHVVrpp_sR4s70F7O27l_wOAGBj4uP1P_OS8m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