<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/101777>101777</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
-Wthread-safety-analysis FR: automatic inference of annotations for local-scope lambdas
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
pkasting
</td>
</tr>
</table>
<pre>
With `-Wthread-safety-analysis`, accessing a guarded resource inside a lambda requires annotating the lambda similarly to any other function. See sample code at https://godbolt.org/z/7Pd1f3v5v; without the annotation on the lambda, compilation produces a warning.
This is problematic when the lambda comes from a macro. For example, in Chromium, we have an `assert()`-like macro called `CHECK()`, which currently preprocesses to an expression, but which we'd like to turn into a statement (in order to use C++20 `[[unlikely]]`, which can only appear on a statement). A simplified implementation might look akin to:
```
#define CHECK(cond) [&] { if (!(cond)) [[unlikely]] CheckFailure(); }()
```
Unfortunately, if the condition being `CHECK`ed is subject to thread safety analysis, this can produce cryptic compile failures. We could annotate this lambda with `no_thread_safety_analysis`, but that would hide any real thread-safety issues inside the condition. What we'd like is a partly- or fully-automatic way of making this work.
It seems like this may become intractable if the scope is "any lambda with any linkage", so perhaps if we say that only immediately-invoked lambdas are of concern, things are better? Then perhaps the requirements can be inferred either from the body of the function (what does it actually require?) or else from the invocation context (what resources are available?).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx0VV2P6jYQ_TXmZQTKOhuyPPCwsEWt-lK1t9rHq4k9IS6OndoO3PTXVxOH_VIrISDK-MzHOXOMMZqzI9qL6iCqlxWOqfNhP1wwJuPOq8braf9qUgdiW6xfUxcI9TpiS2lao0M7RRPFthDyCKgUxWjcGRDOIwZNGgJFPwZFYFw0mgDBYt9ohEB_jyZQBHTOJ-RkkDq6v46mNxaDnSB5QDeBTx0FaEenkvFuA38QQcR-sATKM3CCLqUhivJZyJOQp7PXjbdp48NZyNM_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